View Javadoc
1   package ejava.examples.ejbwar.customer.rs;
2   
3   import java.net.URI;
4   
5   import javax.inject.Inject;
6   import javax.ws.rs.Consumes;
7   import javax.ws.rs.DELETE;
8   import javax.ws.rs.DefaultValue;
9   import javax.ws.rs.GET;
10  import javax.ws.rs.POST;
11  import javax.ws.rs.Path;
12  import javax.ws.rs.PathParam;
13  import javax.ws.rs.Produces;
14  import javax.ws.rs.QueryParam;
15  import javax.ws.rs.core.Context;
16  import javax.ws.rs.core.MediaType;
17  import javax.ws.rs.core.Request;
18  import javax.ws.rs.core.Response;
19  import javax.ws.rs.core.UriBuilder;
20  import javax.ws.rs.core.UriInfo;
21  import javax.ws.rs.core.Response.ResponseBuilder;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import ejava.examples.ejbwar.customer.bo.Customer;
27  import ejava.examples.ejbwar.customer.bo.Customers;
28  import ejava.examples.ejbwar.customer.ejb.CustomerMgmtLocal;
29  
30  /**
31   * This class provides a JAX-RS resource for interfacing with customer
32   * methods.
33   */
34  @Path("customers")
35  public class CustomersResource {
36  	private static final Log log = LogFactory.getLog(CustomersResource.class);
37  	@Inject
38  	private CustomerMgmtLocal ejb;
39  	@Context
40  	private Request request;
41  	@Context
42  	private UriInfo uriInfo;
43  	
44  	@POST @Path("")
45  	@Consumes(MediaType.APPLICATION_XML)
46  	@Produces(MediaType.APPLICATION_XML)
47  	public Response addCustomer(Customer customer) {
48  		log.debug(String.format("%s %s", request.getMethod(), uriInfo.getAbsolutePath()));
49  		try {
50  			Customer c = ejb.addCustomer(customer);
51  			URI uri = UriBuilder.fromUri(uriInfo.getAbsolutePath())
52  					.path(CustomersResource.class, "getCustomer")
53  					.build(c.getId());
54  			return Response.created(uri)
55  					.entity(c)
56  					.build();
57  		} catch (Exception ex) {
58  			return serverError(log, "creating person", ex).build();
59  		}
60  	}
61  	
62  	@GET @Path("")
63  	@Produces(MediaType.APPLICATION_XML)
64  	public Response findCustomersByName(
65  			@QueryParam("firstName") String firstName,
66  			@QueryParam("lastName") String lastName,
67  			@QueryParam("offset") @DefaultValue("0") int offset,
68  			@QueryParam("limit") @DefaultValue("0") int limit) {
69  		log.debug(String.format("%s %s", request.getMethod(), uriInfo.getAbsolutePath()));
70  		try {
71  			Customers customers = ejb.findCustomersByName(firstName, lastName, offset, limit);
72  			return Response.ok(customers)
73  					.build();
74  		} catch (Exception ex) {
75  			return serverError(log, "finding person", ex).build();
76  		}
77  	}
78  	
79  	@GET @Path("{id}")
80  	@Produces(MediaType.APPLICATION_XML)
81  	public Response getCustomer(@PathParam("id") int id) {
82  		log.debug(String.format("%s %s", request.getMethod(), uriInfo.getAbsolutePath()));
83  		try {
84  			Customer customer = ejb.getCustomer(id);
85  			if (customer!=null) {
86  				return Response.ok(customer)
87  						.build();
88  			} else {
89  				return Response.status(Response.Status.NOT_FOUND)
90  						.entity(String.format("person %d not found", id))
91  						.type(MediaType.TEXT_PLAIN)
92  						.build();
93  			}
94  		} catch (Exception ex) {
95  			return serverError(log, "getting person", ex).build();
96  		}
97  	}
98  
99  	@DELETE @Path("{id}")
100 	@Produces(MediaType.APPLICATION_XML)
101 	public Response deleteCustomer(@PathParam("id") int id) {
102 		log.debug(String.format("%s %s", request.getMethod(), uriInfo.getAbsolutePath()));
103 		try {
104 			ejb.deleteCustomer(id);
105 			return Response.ok()
106 					.build();
107 		} catch (Exception ex) {
108 			return serverError(log, "deleting person", ex).build();
109 		}
110 	}
111 	
112 	public static ResponseBuilder serverError(Log log, String context, Exception ex) {
113 		String message = String.format("unexpected error %s: %s",context, ex.getLocalizedMessage());
114 		log.warn(message, ex);
115 		return Response.serverError()
116 				.entity(message)
117 				.type(MediaType.TEXT_PLAIN);
118 	}
119 	
120 }