1 package com.freemindcafe.apache.cxf.jaxrs.sample8;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.HashMap;
8 import java.util.concurrent.ConcurrentHashMap;
9 import java.util.stream.Collectors;
11 import javax.ws.rs.Consumes;
12 import javax.ws.rs.DELETE;
13 import javax.ws.rs.GET;
14 import javax.ws.rs.POST;
15 import javax.ws.rs.PUT;
16 import javax.ws.rs.Path;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.Produces;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.ResponseBuilder;
28 private Map<Long, Ticket> tickets =
new ConcurrentHashMap<Long, Ticket>();
35 ticket1.setAssignee(
"nikhil");
36 ticket1.setDescription(
"desc");
37 ticket1.setFileName(
"ticket1.xml");
40 ticket2.setAssignee(
"subhash");
41 ticket2.setDescription(
"desc");
42 ticket2.setFileName(
"ticket2.xml");
44 tickets.put(1L, ticket1);
45 tickets.put(2L, ticket2);
53 @Produces(MediaType.APPLICATION_JSON)
54 public List<Ticket> getTickets(){
55 System.out.println(tickets);
56 return tickets.values().stream().collect(Collectors.toList());
61 @Produces(MediaType.APPLICATION_JSON)
62 public List<String> getAssignees(){
63 return tickets.values().stream().map(t->t.getAssignee()).collect(Collectors.toList());
68 public Ticket getTicket(@PathParam(
"ticketId") Long ticketId){
69 return tickets.get(ticketId);
74 @Consumes(MediaType.APPLICATION_JSON)
75 @Produces(MediaType.APPLICATION_JSON)
76 public Ticket putTicket(@PathParam(
"ticketId") Long ticketId,
Ticket ticket){
77 tickets.get(ticketId).setAssignee(ticket.getAssignee());
78 tickets.get(ticketId).setDescription(ticket.getDescription());
79 return tickets.get(ticketId);
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
87 ticket.setId(tickets.size()+1L);
88 tickets.put(ticket.getId(), ticket);
94 public Ticket deleteTicket(@PathParam(
"ticketId") Long ticketId){
95 Ticket ticket = tickets.get(ticketId);
96 tickets.remove(ticketId);
101 @Path(
"/{ticketId}/download")
102 public Response downloadContent(@PathParam(
"ticketId")
final Long ticketId)
throws IOException{
103 Ticket ticket = tickets.get(ticketId);
104 ResponseBuilder responseBuilder = Response.ok(this.getClass().getResourceAsStream(ticket.getFileName()));
105 responseBuilder.type(MediaType.APPLICATION_XML);
106 responseBuilder.header(
"Content-Disposition",
"attachment; filename=\""+ticket.getFileName()+
"\"");
107 return responseBuilder.build();