View Javadoc
1   package info.ejava.examples.jaxrs.todos.rs;
2   
3   import javax.ejb.EJB;
4   import javax.ws.rs.GET;
5   import javax.ws.rs.Path;
6   import javax.ws.rs.Produces;
7   import javax.ws.rs.QueryParam;
8   import javax.ws.rs.core.MediaType;
9   import javax.ws.rs.core.Response;
10  import javax.ws.rs.core.Response.ResponseBuilder;
11  import javax.ws.rs.core.Response.Status;
12  
13  import info.ejava.examples.jaxrs.todos.ejb.InternalErrorException;
14  import info.ejava.examples.jaxrs.todos.ejb.InvalidRequestException;
15  
16  @Path("greetings")
17  public class GreetingsResource {
18      @EJB
19      private GreetingEJB greetingEJB;
20  
21      @GET
22      @Path("hi")
23      @Produces(MediaType.TEXT_PLAIN)
24      public Response sayHi() {
25          String entity = "hi";
26          ResponseBuilder rb = Response.ok(entity);            
27          return rb.build();
28      }
29      
30      @GET
31      @Path("greet")
32      @Produces(MediaType.TEXT_PLAIN)
33      public Response greet(@QueryParam("name") String name) {
34          ResponseBuilder rb=null;
35          try {
36              String entity = greetingEJB.greet(name);
37              rb = Response.ok(entity);
38          } catch (InvalidRequestException ex) {
39              rb = Response.status(Status.BAD_REQUEST)
40                      .entity(ex.getMessage());
41          } catch (InternalErrorException ex) {
42              rb = Response.status(Status.INTERNAL_SERVER_ERROR)
43                      .entity(ex.getMessage());
44          } catch (Exception ex) {
45              rb=Response.serverError()
46                  .entity(String.format("unexpected error greeting name[%s]", name));
47          }
48          return rb.build();
49      }
50  }