1 | package eu.clarin.cmdi.virtualcollectionregistry.rest; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.io.OutputStream; |
---|
6 | import java.net.URI; |
---|
7 | import java.security.Principal; |
---|
8 | import java.util.UUID; |
---|
9 | |
---|
10 | import javax.ws.rs.Consumes; |
---|
11 | import javax.ws.rs.DELETE; |
---|
12 | import javax.ws.rs.DefaultValue; |
---|
13 | import javax.ws.rs.GET; |
---|
14 | import javax.ws.rs.POST; |
---|
15 | import javax.ws.rs.PUT; |
---|
16 | import javax.ws.rs.Path; |
---|
17 | import javax.ws.rs.PathParam; |
---|
18 | import javax.ws.rs.Produces; |
---|
19 | import javax.ws.rs.QueryParam; |
---|
20 | import javax.ws.rs.WebApplicationException; |
---|
21 | import javax.ws.rs.core.Context; |
---|
22 | import javax.ws.rs.core.HttpHeaders; |
---|
23 | import javax.ws.rs.core.MediaType; |
---|
24 | import javax.ws.rs.core.Response; |
---|
25 | import javax.ws.rs.core.SecurityContext; |
---|
26 | import javax.ws.rs.core.StreamingOutput; |
---|
27 | import javax.ws.rs.core.UriInfo; |
---|
28 | |
---|
29 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry; |
---|
30 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
31 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMarshaller.Format; |
---|
32 | import eu.clarin.cmdi.virtualcollectionregistry.model.ClarinVirtualCollection; |
---|
33 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
34 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollectionList; |
---|
35 | |
---|
36 | @Path("/") |
---|
37 | public class VirtualCollectionRegistryRestService { |
---|
38 | private final VirtualCollectionRegistry registry = |
---|
39 | VirtualCollectionRegistry.instance(); |
---|
40 | @Context |
---|
41 | SecurityContext security; |
---|
42 | @Context |
---|
43 | UriInfo uriInfo; |
---|
44 | @Context |
---|
45 | HttpHeaders headers; |
---|
46 | |
---|
47 | @POST |
---|
48 | @Path("/virtualcollection") |
---|
49 | @Consumes({ MediaType.TEXT_XML, |
---|
50 | MediaType.TEXT_XML, |
---|
51 | MediaType.APPLICATION_JSON }) |
---|
52 | @Produces({ MediaType.TEXT_XML, |
---|
53 | MediaType.APPLICATION_XML, |
---|
54 | MediaType.APPLICATION_JSON }) |
---|
55 | public Response createVirtualCollection(InputStream input) |
---|
56 | throws VirtualCollectionRegistryException { |
---|
57 | Principal principal = security.getUserPrincipal(); |
---|
58 | if (principal == null) { |
---|
59 | String path = uriInfo.getPath(); |
---|
60 | if (path.endsWith("/")) { |
---|
61 | /* |
---|
62 | * fix bad client request and remove tailing slash |
---|
63 | */ |
---|
64 | path = path.substring(0, path.length() - 1); |
---|
65 | URI uri = uriInfo.getBaseUriBuilder().path(path).build(); |
---|
66 | return Response.temporaryRedirect(uri).build(); |
---|
67 | } |
---|
68 | /* |
---|
69 | * should never happen, because servlet container should |
---|
70 | * supply a valid principal |
---|
71 | */ |
---|
72 | throw new AssertionError("prinicial == null"); |
---|
73 | } |
---|
74 | try { |
---|
75 | Format format = getInputFormat(); |
---|
76 | String encoding = getInputEncoding(); |
---|
77 | VirtualCollection vc = registry.getMarshaller() |
---|
78 | .unmarshal(input, format, encoding); |
---|
79 | long id = registry.createVirtualCollection(principal, vc); |
---|
80 | RestResponse response = new RestResponse(); |
---|
81 | response.setIsSuccess(true); |
---|
82 | response.setInfo("created"); |
---|
83 | response.setId(id); |
---|
84 | URI uri = uriInfo.getRequestUriBuilder().path(Long.toString(id)) |
---|
85 | .build(); |
---|
86 | return Response.created(uri).entity(response).build(); |
---|
87 | } catch (IOException e) { |
---|
88 | throw new VirtualCollectionRegistryException("create", e); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | @GET |
---|
93 | @Path("/virtualcollection/{id}") |
---|
94 | @Produces({ MediaType.TEXT_XML, |
---|
95 | MediaType.APPLICATION_XML, |
---|
96 | MediaType.APPLICATION_JSON }) |
---|
97 | public Response getVirtualCollection(@PathParam("id") long id) |
---|
98 | throws VirtualCollectionRegistryException { |
---|
99 | final VirtualCollection vc = registry.retrieveVirtualCollection(id); |
---|
100 | StreamingOutput writer = new StreamingOutput() { |
---|
101 | public void write(OutputStream stream) throws IOException, |
---|
102 | WebApplicationException { |
---|
103 | Format format = getOutputFormat(); |
---|
104 | registry.getMarshaller().marshal(stream, format, vc); |
---|
105 | } |
---|
106 | }; |
---|
107 | return Response.ok(writer).build(); |
---|
108 | } |
---|
109 | |
---|
110 | @PUT |
---|
111 | @Path("/virtualcollection/{id}") |
---|
112 | @Consumes({ MediaType.TEXT_XML, |
---|
113 | MediaType.TEXT_XML, |
---|
114 | MediaType.APPLICATION_JSON }) |
---|
115 | @Produces({ MediaType.TEXT_XML, |
---|
116 | MediaType.APPLICATION_XML, |
---|
117 | MediaType.APPLICATION_JSON }) |
---|
118 | public Response updateVirtualCollection(@PathParam("id") long id, |
---|
119 | InputStream input) throws VirtualCollectionRegistryException { |
---|
120 | Principal principal = security.getUserPrincipal(); |
---|
121 | if (principal == null) { |
---|
122 | throw new NullPointerException("princial == null"); |
---|
123 | } |
---|
124 | try { |
---|
125 | Format format = getInputFormat(); |
---|
126 | String encoding = getInputEncoding(); |
---|
127 | VirtualCollection vc = registry.getMarshaller() |
---|
128 | .unmarshal(input, format, encoding); |
---|
129 | registry.updateVirtualCollection(principal, id, vc); |
---|
130 | RestResponse response = new RestResponse(); |
---|
131 | response.setIsSuccess(true); |
---|
132 | response.setInfo("updated"); |
---|
133 | response.setId(id); |
---|
134 | return Response.ok(response).build(); |
---|
135 | } catch (IOException e) { |
---|
136 | throw new VirtualCollectionRegistryException("update", e); |
---|
137 | |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | @DELETE |
---|
142 | @Path("/virtualcollection/{id}") |
---|
143 | @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML }) |
---|
144 | public Response deleteVirtualCollection(@PathParam("id") long id) |
---|
145 | throws VirtualCollectionRegistryException { |
---|
146 | Principal principal = security.getUserPrincipal(); |
---|
147 | if (principal == null) { |
---|
148 | throw new NullPointerException("princial == null"); |
---|
149 | } |
---|
150 | registry.deleteVirtualCollection(principal, id); |
---|
151 | RestResponse response = new RestResponse(); |
---|
152 | response.setIsSuccess(true); |
---|
153 | response.setInfo("deleted"); |
---|
154 | response.setId(id); |
---|
155 | return Response.ok(response).build(); |
---|
156 | } |
---|
157 | |
---|
158 | @GET |
---|
159 | @Path("/virtualcollections") |
---|
160 | @Produces({ MediaType.TEXT_XML, |
---|
161 | MediaType.APPLICATION_XML, |
---|
162 | MediaType.APPLICATION_JSON }) |
---|
163 | public Response getVirtualCollections(@QueryParam("q") String query, |
---|
164 | @DefaultValue("0") @QueryParam("offset") int offset, |
---|
165 | @DefaultValue("-1") @QueryParam("count") int count) |
---|
166 | throws VirtualCollectionRegistryException { |
---|
167 | final VirtualCollectionList vcs = |
---|
168 | registry.getVirtualCollections(query, |
---|
169 | (offset > 0) ? offset : 0, count); |
---|
170 | StreamingOutput writer = new StreamingOutput() { |
---|
171 | public void write(OutputStream stream) throws IOException, |
---|
172 | WebApplicationException { |
---|
173 | Format format = getOutputFormat(); |
---|
174 | registry.getMarshaller().marshal(stream, format, vcs); |
---|
175 | } |
---|
176 | }; |
---|
177 | return Response.ok(writer).build(); |
---|
178 | } |
---|
179 | |
---|
180 | @GET |
---|
181 | @Path("/my-virtualcollections") |
---|
182 | @Produces({ MediaType.TEXT_XML, |
---|
183 | MediaType.APPLICATION_XML, |
---|
184 | MediaType.APPLICATION_JSON }) |
---|
185 | public Response getMyVirtualCollections(@QueryParam("q") String query, |
---|
186 | @DefaultValue("0") @QueryParam("offset") int offset, |
---|
187 | @DefaultValue("-1") @QueryParam("count") int count) |
---|
188 | throws VirtualCollectionRegistryException { |
---|
189 | Principal principal = security.getUserPrincipal(); |
---|
190 | if (principal == null) { |
---|
191 | String path = uriInfo.getPath(); |
---|
192 | if (path.endsWith("/")) { |
---|
193 | /* |
---|
194 | * fix bad client request and remove tailing slash |
---|
195 | */ |
---|
196 | path = path.substring(0, path.length() - 1); |
---|
197 | URI uri = uriInfo.getBaseUriBuilder().path(path).build(); |
---|
198 | return Response.seeOther(uri).build(); |
---|
199 | } |
---|
200 | /* |
---|
201 | * should never happen, because servlet container should |
---|
202 | * supply a valid principal |
---|
203 | */ |
---|
204 | throw new AssertionError("prinicial == null"); |
---|
205 | } |
---|
206 | final VirtualCollectionList vcs = |
---|
207 | registry.getVirtualCollections(principal, query, |
---|
208 | (offset > 0) ? offset : 0, count); |
---|
209 | StreamingOutput writer = new StreamingOutput() { |
---|
210 | public void write(OutputStream stream) throws IOException, |
---|
211 | WebApplicationException { |
---|
212 | Format format = getOutputFormat(); |
---|
213 | registry.getMarshaller().marshal(stream, format, vcs); |
---|
214 | } |
---|
215 | }; |
---|
216 | return Response.ok(writer).build(); |
---|
217 | } |
---|
218 | |
---|
219 | @GET |
---|
220 | @Path("/clarin-virtualcollection/{id}") |
---|
221 | @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML }) |
---|
222 | public Response getClarinVirtualCollection(@PathParam("id") String id) |
---|
223 | throws VirtualCollectionRegistryException { |
---|
224 | if (id == null) { |
---|
225 | /* |
---|
226 | * null indicated client error, so use IllegalArgumentException |
---|
227 | * here, so we will signal bad request error to client |
---|
228 | */ |
---|
229 | throw new IllegalArgumentException("id path argument is missing"); |
---|
230 | } |
---|
231 | VirtualCollection vc = null; |
---|
232 | try { |
---|
233 | UUID vc_id = UUID.fromString(id.trim()); |
---|
234 | vc = registry.retrieveVirtualCollection(vc_id.toString()); |
---|
235 | } catch (IllegalArgumentException e) { |
---|
236 | try { |
---|
237 | long vc_id = Long.parseLong(id); |
---|
238 | vc = registry.retrieveVirtualCollection(vc_id); |
---|
239 | } catch (NumberFormatException e1) { |
---|
240 | throw new IllegalArgumentException("invalid id: " + |
---|
241 | "not nor a number or a uuid"); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | // sanity check |
---|
246 | if (vc == null) { |
---|
247 | throw new AssertionError("vc == null"); |
---|
248 | } |
---|
249 | |
---|
250 | final ClarinVirtualCollection cvc = new ClarinVirtualCollection(vc); |
---|
251 | StreamingOutput writer = new StreamingOutput() { |
---|
252 | public void write(OutputStream output) throws IOException, |
---|
253 | WebApplicationException { |
---|
254 | registry.getMarshaller().marshal(output, Format.XML, cvc); |
---|
255 | } |
---|
256 | }; |
---|
257 | return Response.ok(writer).build(); |
---|
258 | } |
---|
259 | |
---|
260 | private Format getInputFormat() { |
---|
261 | Format format = getMediaType(headers.getMediaType()); |
---|
262 | return (format != null) ? format : Format.UNSUPPORTED; |
---|
263 | } |
---|
264 | |
---|
265 | private String getInputEncoding() { |
---|
266 | String encoding = |
---|
267 | headers.getMediaType().getParameters().get("encoding"); |
---|
268 | return (encoding != null) ? encoding : "utf-8"; |
---|
269 | } |
---|
270 | |
---|
271 | private Format getOutputFormat() { |
---|
272 | for (MediaType type : headers.getAcceptableMediaTypes()) { |
---|
273 | Format format = getMediaType(type); |
---|
274 | if (format != null) { |
---|
275 | return format; |
---|
276 | } |
---|
277 | } |
---|
278 | return Format.UNSUPPORTED; |
---|
279 | } |
---|
280 | |
---|
281 | private static Format getMediaType(MediaType type) { |
---|
282 | if (type.isCompatible(MediaType.APPLICATION_XML_TYPE) |
---|
283 | || type.isCompatible(MediaType.TEXT_XML_TYPE)) { |
---|
284 | return Format.XML; |
---|
285 | } |
---|
286 | if (type.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { |
---|
287 | return Format.JSON; |
---|
288 | } |
---|
289 | return null; |
---|
290 | } |
---|
291 | |
---|
292 | } // class VirtualCollectionRegistryRestService |
---|