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