1 | package eu.clarin.cmdi.virtualcollectionregistry.rest; |
---|
2 | |
---|
3 | import com.sun.jersey.api.core.InjectParam; |
---|
4 | import com.sun.jersey.api.core.ResourceContext; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
8 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollectionList; |
---|
9 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller; |
---|
10 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller.Format; |
---|
11 | import java.io.IOException; |
---|
12 | import java.io.InputStream; |
---|
13 | import java.io.OutputStream; |
---|
14 | import java.net.URI; |
---|
15 | import java.security.Principal; |
---|
16 | import javax.ws.rs.Consumes; |
---|
17 | import javax.ws.rs.DefaultValue; |
---|
18 | import javax.ws.rs.GET; |
---|
19 | import javax.ws.rs.POST; |
---|
20 | import javax.ws.rs.Path; |
---|
21 | import javax.ws.rs.PathParam; |
---|
22 | import javax.ws.rs.Produces; |
---|
23 | import javax.ws.rs.QueryParam; |
---|
24 | import javax.ws.rs.WebApplicationException; |
---|
25 | import javax.ws.rs.core.Context; |
---|
26 | import javax.ws.rs.core.HttpHeaders; |
---|
27 | import javax.ws.rs.core.MediaType; |
---|
28 | import javax.ws.rs.core.Response; |
---|
29 | import javax.ws.rs.core.SecurityContext; |
---|
30 | import javax.ws.rs.core.StreamingOutput; |
---|
31 | import javax.ws.rs.core.UriInfo; |
---|
32 | |
---|
33 | /** |
---|
34 | * REST resource representing the collection of virtual collections |
---|
35 | * |
---|
36 | * @author twagoo |
---|
37 | */ |
---|
38 | @Path("/virtualcollections") |
---|
39 | public class VirtualCollectionsResource { |
---|
40 | |
---|
41 | @Context |
---|
42 | private ResourceContext resourceContext; |
---|
43 | @InjectParam |
---|
44 | private VirtualCollectionRegistry registry; |
---|
45 | @InjectParam |
---|
46 | private VirtualCollectionMarshaller marshaller; |
---|
47 | @Context |
---|
48 | private SecurityContext security; |
---|
49 | @Context |
---|
50 | private UriInfo uriInfo; |
---|
51 | @Context |
---|
52 | private HttpHeaders headers; |
---|
53 | |
---|
54 | /** |
---|
55 | * All virtual collections will be retrieved; if a query expression is used, |
---|
56 | * only the virtual collections satisfying the query will be retrieved. |
---|
57 | * |
---|
58 | * @param query a Virtual Collection Query Language expression |
---|
59 | * @param offset |
---|
60 | * @param count |
---|
61 | * @return a serialised list of all public virtual collections. If no |
---|
62 | * virtual collection are found an empty list will be returned. |
---|
63 | * @throws VirtualCollectionRegistryException if the collections could not |
---|
64 | * be retrieved |
---|
65 | * @see VirtualCollectionRegistry#getVirtualCollections(java.lang.String, |
---|
66 | * int, int) |
---|
67 | */ |
---|
68 | @GET |
---|
69 | @Produces({MediaType.TEXT_XML, |
---|
70 | MediaType.APPLICATION_XML, |
---|
71 | MediaType.APPLICATION_JSON}) |
---|
72 | public Response getVirtualCollections(@QueryParam("q") String query, |
---|
73 | @DefaultValue("0") @QueryParam("offset") int offset, |
---|
74 | @DefaultValue("-1") @QueryParam("count") int count) |
---|
75 | throws VirtualCollectionRegistryException { |
---|
76 | final VirtualCollectionList vcs = registry.getVirtualCollections(query, |
---|
77 | (offset > 0) ? offset : 0, count); |
---|
78 | StreamingOutput writer = new StreamingOutput() { |
---|
79 | @Override |
---|
80 | public void write(OutputStream output) throws IOException, |
---|
81 | WebApplicationException { |
---|
82 | final Format format = RestUtils.getOutputFormat(headers.getAcceptableMediaTypes()); |
---|
83 | marshaller.marshal(output, format, vcs); |
---|
84 | output.close(); |
---|
85 | } |
---|
86 | }; |
---|
87 | return Response.ok(writer).build(); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * A virtual collection will be created based on the representation of the |
---|
92 | * virtual collection sent in the request body. |
---|
93 | * |
---|
94 | * @param input Depending on Content-Type header either a valid XML instance |
---|
95 | * or the JSON representation of a virtual collection conforming to the |
---|
96 | * above mentioned XML schema. The root element is expected to be |
---|
97 | * "VirtualCollection" |
---|
98 | * @return A response containing a {@link RestResponse}. If successful, the |
---|
99 | * result will contain the ID of the created virtual collection |
---|
100 | * |
---|
101 | * @throws VirtualCollectionRegistryException |
---|
102 | */ |
---|
103 | @POST |
---|
104 | @Consumes({MediaType.TEXT_XML, |
---|
105 | MediaType.APPLICATION_XML, |
---|
106 | MediaType.APPLICATION_JSON}) |
---|
107 | @Produces({MediaType.TEXT_XML, |
---|
108 | MediaType.APPLICATION_XML, |
---|
109 | MediaType.APPLICATION_JSON}) |
---|
110 | public Response createVirtualCollection(InputStream input) |
---|
111 | throws VirtualCollectionRegistryException { |
---|
112 | Principal principal = security.getUserPrincipal(); |
---|
113 | if (principal == null) { |
---|
114 | String path = uriInfo.getPath(); |
---|
115 | if (path.endsWith("/")) { |
---|
116 | /* |
---|
117 | * fix bad client request and remove tailing slash |
---|
118 | */ |
---|
119 | path = path.substring(0, path.length() - 1); |
---|
120 | URI uri = uriInfo.getBaseUriBuilder().path(path).build(); |
---|
121 | return Response.temporaryRedirect(uri).build(); |
---|
122 | } |
---|
123 | /* |
---|
124 | * should never happen, because servlet container should supply a |
---|
125 | * valid principal |
---|
126 | */ |
---|
127 | throw new AssertionError("principal == null"); |
---|
128 | } |
---|
129 | try { |
---|
130 | VirtualCollectionMarshaller.Format format = RestUtils.getInputFormat(headers); |
---|
131 | String encoding = RestUtils.getInputEncoding(headers); |
---|
132 | VirtualCollection vc |
---|
133 | = marshaller.unmarshal(input, format, encoding); |
---|
134 | long id = registry.createVirtualCollection(principal, vc); |
---|
135 | RestResponse response = new RestResponse(); |
---|
136 | response.setIsSuccess(true); |
---|
137 | response.setInfo("created"); |
---|
138 | response.setId(id); |
---|
139 | URI uri |
---|
140 | = uriInfo.getRequestUriBuilder().path(Long.toString(id)).build(); |
---|
141 | return Response.created(uri).entity(response).build(); |
---|
142 | } catch (IOException e) { |
---|
143 | throw new VirtualCollectionRegistryException("create", e); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | @Path("/{id}") |
---|
148 | public VirtualCollectionResource getVirtualCollection(@PathParam("id") long id) { |
---|
149 | final VirtualCollectionResource resource = resourceContext.getResource(VirtualCollectionResource.class); |
---|
150 | resource.setId(id); |
---|
151 | return resource; |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|