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