View Javadoc

1   /**
2    * 
3    */
4   package eu.scape_project.watch.rest.resource;
5   
6   import java.util.Collection;
7   
8   import javax.ws.rs.GET;
9   import javax.ws.rs.POST;
10  import javax.ws.rs.Path;
11  import javax.ws.rs.PathParam;
12  import javax.ws.rs.QueryParam;
13  import javax.ws.rs.core.GenericEntity;
14  import javax.ws.rs.core.Response;
15  
16  import com.wordnik.swagger.core.ApiError;
17  import com.wordnik.swagger.core.ApiErrors;
18  import com.wordnik.swagger.core.ApiOperation;
19  import com.wordnik.swagger.core.ApiParam;
20  import com.wordnik.swagger.core.JavaHelp;
21  
22  import eu.scape_project.watch.dao.AsyncRequestDAO;
23  import eu.scape_project.watch.domain.AsyncRequest;
24  import eu.scape_project.watch.utils.exception.NotFoundException;
25  
26  /**
27   * REST API for {@link AsyncRequest} operations.
28   * 
29   * 
30   * @author Luis Faria <lfaria@keep.pt>
31   * 
32   */
33  public class AsyncRequestResource extends JavaHelp {
34  
35    /**
36     * Get an existing {@link AsyncRequest}.
37     * 
38     * @param requestId
39     *          The {@link AsyncRequest} id
40     * @return The {@link AsyncRequest} or throws {@link NotFoundException} if not
41     *         found.
42     */
43    @GET
44    @Path("/{id}")
45    @ApiOperation(value = "Find Request by Id", notes = "")
46    @ApiErrors(value = {@ApiError(code = NotFoundException.CODE, reason = "Request not found")})
47    public Response getAsyncRequestById(
48      @ApiParam(value = "Request Id", required = true) @PathParam("id") final String requestId) {
49  
50      final AsyncRequest request = AsyncRequestDAO.getInstance().findById(requestId);
51  
52      if (request != null) {
53        return Response.ok().entity(request).build();
54      } else {
55        throw new NotFoundException("Request not found: " + requestId);
56      }
57    }
58  
59    /**
60     * Create a new {@link AsyncRequest}.
61     * 
62     * @param request
63     *          The async request to save
64     * @return The created async request
65     */
66    @POST
67    @Path("/")
68    @ApiOperation(value = "Create Async Request", notes = "This can only be done by a logged user (TODO)")
69    public Response createAsyncRequest(@ApiParam(value = "Async Request", required = true) final AsyncRequest request) {
70      AsyncRequestDAO.getInstance().save(request);
71      return Response.ok().entity(request).build();
72    }
73  
74    /**
75     * List all {@link AsyncRequest} in KB.
76     * 
77     * @param start
78     *          The index of the first item to retrieve
79     * 
80     * @param max
81     *          The maximum number of items to retrieve
82     * 
83     * @return A list will all {@link AsyncRequest}.
84     */
85    @GET
86    @Path("/list")
87    @ApiOperation(value = "List all async requests", notes = "")
88    public Response listAsyncRequest(
89      @ApiParam(value = "Index of first item to retrieve", required = true) @QueryParam("start") final int start,
90      @ApiParam(value = "Maximum number of items to retrieve", required = true) @QueryParam("max") final int max) {
91      final Collection<AsyncRequest> list = AsyncRequestDAO.getInstance().list(start, max);
92      return Response.ok().entity(new GenericEntity<Collection<AsyncRequest>>(list) {
93      }).build();
94    }
95  
96  }