1 | package eu.clarin.cmdi.virtualcollectionregistry.rest; |
---|
2 | |
---|
3 | import com.sun.jersey.api.core.InjectParam; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollectionList; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller; |
---|
8 | import java.io.IOException; |
---|
9 | import java.io.OutputStream; |
---|
10 | import java.net.URI; |
---|
11 | import java.security.Principal; |
---|
12 | import javax.ws.rs.DefaultValue; |
---|
13 | import javax.ws.rs.GET; |
---|
14 | import javax.ws.rs.Path; |
---|
15 | import javax.ws.rs.Produces; |
---|
16 | import javax.ws.rs.QueryParam; |
---|
17 | import javax.ws.rs.WebApplicationException; |
---|
18 | import javax.ws.rs.core.Context; |
---|
19 | import javax.ws.rs.core.HttpHeaders; |
---|
20 | import javax.ws.rs.core.MediaType; |
---|
21 | import javax.ws.rs.core.Response; |
---|
22 | import javax.ws.rs.core.SecurityContext; |
---|
23 | import javax.ws.rs.core.StreamingOutput; |
---|
24 | import javax.ws.rs.core.UriInfo; |
---|
25 | |
---|
26 | /** |
---|
27 | * REST resource representing the collection of the user's virtual collections |
---|
28 | * (public or private) |
---|
29 | * |
---|
30 | * @author twagoo |
---|
31 | */ |
---|
32 | @Path("/my-virtualcollections") |
---|
33 | public class MyVirtualCollectionsResource { |
---|
34 | |
---|
35 | @InjectParam |
---|
36 | private VirtualCollectionRegistry registry; |
---|
37 | @InjectParam |
---|
38 | private VirtualCollectionMarshaller marshaller; |
---|
39 | @Context |
---|
40 | private SecurityContext security; |
---|
41 | @Context |
---|
42 | private UriInfo uriInfo; |
---|
43 | @Context |
---|
44 | private HttpHeaders headers; |
---|
45 | |
---|
46 | /** |
---|
47 | * All virtual collections owned by the authenticated user will be |
---|
48 | * retrieved; if a query expression is used, only the virtual collections |
---|
49 | * satisfying the query will be retrieved. |
---|
50 | * |
---|
51 | * @param query a Virtual Collection Query Language expression |
---|
52 | * @param offset |
---|
53 | * @param count |
---|
54 | * @return a serialised list of all virtual collections created by the |
---|
55 | * authenticated user |
---|
56 | * @throws VirtualCollectionRegistryException if the collections could not |
---|
57 | * be retrieved |
---|
58 | * @see |
---|
59 | * VirtualCollectionRegistry#getVirtualCollections(java.security.Principal, |
---|
60 | * java.lang.String, int, int) |
---|
61 | */ |
---|
62 | @GET |
---|
63 | @Produces({MediaType.TEXT_XML, |
---|
64 | MediaType.APPLICATION_XML, |
---|
65 | MediaType.APPLICATION_JSON}) |
---|
66 | public Response getMyVirtualCollections(@QueryParam("q") String query, |
---|
67 | @DefaultValue("0") @QueryParam("offset") int offset, |
---|
68 | @DefaultValue("-1") @QueryParam("count") int count) |
---|
69 | throws VirtualCollectionRegistryException { |
---|
70 | Principal principal = security.getUserPrincipal(); |
---|
71 | if (principal == null) { |
---|
72 | String path = uriInfo.getPath(); |
---|
73 | if (path.endsWith("/")) { |
---|
74 | /* |
---|
75 | * fix bad client request and remove tailing slash |
---|
76 | */ |
---|
77 | path = path.substring(0, path.length() - 1); |
---|
78 | URI uri = uriInfo.getBaseUriBuilder().path(path).build(); |
---|
79 | return Response.seeOther(uri).build(); |
---|
80 | } |
---|
81 | /* |
---|
82 | * should never happen, because servlet container should supply a |
---|
83 | * valid principal |
---|
84 | */ |
---|
85 | throw new AssertionError("principal == null"); |
---|
86 | } |
---|
87 | final VirtualCollectionList vcs = registry.getVirtualCollections( |
---|
88 | principal, query, (offset > 0) ? offset : 0, count); |
---|
89 | StreamingOutput writer = new StreamingOutput() { |
---|
90 | @Override |
---|
91 | public void write(OutputStream output) throws IOException, |
---|
92 | WebApplicationException { |
---|
93 | final VirtualCollectionMarshaller.Format format = RestUtils.getOutputFormat(headers.getAcceptableMediaTypes()); |
---|
94 | marshaller.marshal(output, format, vcs); |
---|
95 | output.close(); |
---|
96 | } |
---|
97 | }; |
---|
98 | return Response.ok(writer).build(); |
---|
99 | } |
---|
100 | } |
---|