1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.BackendConstants; |
---|
21 | import eu.dasish.annotation.backend.ForbiddenException; |
---|
22 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
23 | import eu.dasish.annotation.backend.Resource; |
---|
24 | import eu.dasish.annotation.backend.ResourceAction; |
---|
25 | import eu.dasish.annotation.backend.dao.ILambda; |
---|
26 | import eu.dasish.annotation.schema.Annotation; |
---|
27 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
28 | import eu.dasish.annotation.schema.AnnotationInfoList; |
---|
29 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
30 | import eu.dasish.annotation.schema.Access; |
---|
31 | import eu.dasish.annotation.schema.PermissionList; |
---|
32 | import eu.dasish.annotation.schema.ReferenceList; |
---|
33 | import eu.dasish.annotation.schema.ResponseBody; |
---|
34 | import java.io.IOException; |
---|
35 | import java.util.Arrays; |
---|
36 | import java.util.HashMap; |
---|
37 | import java.util.List; |
---|
38 | import java.util.Map; |
---|
39 | import java.util.UUID; |
---|
40 | import javax.servlet.http.HttpServletRequest; |
---|
41 | import javax.servlet.http.HttpServletResponse; |
---|
42 | import javax.ws.rs.Consumes; |
---|
43 | import javax.ws.rs.DELETE; |
---|
44 | import javax.ws.rs.FormParam; |
---|
45 | import javax.ws.rs.GET; |
---|
46 | import javax.ws.rs.POST; |
---|
47 | import javax.ws.rs.PUT; |
---|
48 | import javax.ws.rs.Path; |
---|
49 | import javax.ws.rs.PathParam; |
---|
50 | import javax.ws.rs.Produces; |
---|
51 | import javax.ws.rs.QueryParam; |
---|
52 | import javax.ws.rs.core.MediaType; |
---|
53 | import javax.ws.rs.ext.Providers; |
---|
54 | import javax.xml.bind.JAXBElement; |
---|
55 | import org.springframework.stereotype.Component; |
---|
56 | import org.springframework.transaction.annotation.Transactional; |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | * @author olhsha |
---|
61 | */ |
---|
62 | @Component |
---|
63 | @Path("/annotations") |
---|
64 | @Transactional(rollbackFor = {Exception.class}) |
---|
65 | public class AnnotationResource extends ResourceResource { |
---|
66 | |
---|
67 | public void setHttpServletResponse(HttpServletResponse httpServletResponse) { |
---|
68 | this.httpServletResponse = httpServletResponse; |
---|
69 | } |
---|
70 | |
---|
71 | public void setHttpServletRequest(HttpServletRequest httpServletRequest) { |
---|
72 | this.httpServletRequest = httpServletRequest; |
---|
73 | } |
---|
74 | |
---|
75 | public void setProviders(Providers providers) { |
---|
76 | this.providers = providers; |
---|
77 | } |
---|
78 | |
---|
79 | public AnnotationResource() { |
---|
80 | } |
---|
81 | |
---|
82 | @GET |
---|
83 | @Produces(MediaType.TEXT_XML) |
---|
84 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") |
---|
85 | @Transactional(readOnly = true) |
---|
86 | public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException { |
---|
87 | Map params = new HashMap(); |
---|
88 | try { |
---|
89 | Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier); |
---|
90 | if (result != null) { |
---|
91 | return (new ObjectFactory()).createAnnotation(result); |
---|
92 | } else { |
---|
93 | return (new ObjectFactory()).createAnnotation(new Annotation()); |
---|
94 | } |
---|
95 | } catch (NotInDataBaseException e1) { |
---|
96 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
97 | return (new ObjectFactory()).createAnnotation(new Annotation()); |
---|
98 | } catch (ForbiddenException e2) { |
---|
99 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
100 | return (new ObjectFactory()).createAnnotation(new Annotation()); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | private class GetAnnotation implements ILambda<Map, Annotation> { |
---|
105 | |
---|
106 | @Override |
---|
107 | public Annotation apply(Map params) throws NotInDataBaseException { |
---|
108 | return dbDispatcher.getAnnotation((Number) params.get("internalID")); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | ////////////////////////////////////////////////// |
---|
113 | @GET |
---|
114 | @Produces(MediaType.TEXT_XML) |
---|
115 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/targets") |
---|
116 | @Transactional(readOnly = true) |
---|
117 | public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String externalIdentifier) throws IOException { |
---|
118 | Map params = new HashMap(); |
---|
119 | try { |
---|
120 | ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier); |
---|
121 | if (result != null) { |
---|
122 | return (new ObjectFactory()).createTargetList(result); |
---|
123 | } else { |
---|
124 | return (new ObjectFactory()).createTargetList(new ReferenceList()); |
---|
125 | } |
---|
126 | } catch (NotInDataBaseException e1) { |
---|
127 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
128 | return (new ObjectFactory()).createReferenceList(new ReferenceList()); |
---|
129 | } catch (ForbiddenException e2) { |
---|
130 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
131 | return (new ObjectFactory()).createReferenceList(new ReferenceList()); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | private class GetTargetList implements ILambda<Map, ReferenceList> { |
---|
136 | |
---|
137 | @Override |
---|
138 | public ReferenceList apply(Map params) throws NotInDataBaseException { |
---|
139 | return dbDispatcher.getAnnotationTargets((Number) params.get("internalID")); |
---|
140 | } |
---|
141 | } |
---|
142 | // TODO Unit test |
---|
143 | |
---|
144 | @GET |
---|
145 | @Produces(MediaType.TEXT_XML) |
---|
146 | @Path("") |
---|
147 | @Transactional(readOnly = true) |
---|
148 | public JAXBElement<AnnotationInfoList> getFilteredAnnotations(@QueryParam("link") String link, |
---|
149 | @QueryParam("text") String text, |
---|
150 | @QueryParam("access") String access, |
---|
151 | @QueryParam("namespace") String namespace, |
---|
152 | @QueryParam("owner") String ownerExternalId, |
---|
153 | @QueryParam("after") String after, |
---|
154 | @QueryParam("before") String before) throws IOException { |
---|
155 | |
---|
156 | Number principalID = this.getPrincipalID(); |
---|
157 | if (principalID == null) { |
---|
158 | return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList()); |
---|
159 | } |
---|
160 | |
---|
161 | UUID ownerExternalUUID = (ownerExternalId != null) ? UUID.fromString(ownerExternalId) : null; |
---|
162 | if (access == null) { |
---|
163 | access = defaultAccess; |
---|
164 | } |
---|
165 | if (!Arrays.asList(admissibleAccess).contains(access)) { |
---|
166 | this.INVALID_ACCESS_MODE(access); |
---|
167 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "ivalide mode acess " + access); |
---|
168 | return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList()); |
---|
169 | } |
---|
170 | try { |
---|
171 | final AnnotationInfoList annotationInfoList = dbDispatcher.getFilteredAnnotationInfos(ownerExternalUUID, link, text, principalID, access, namespace, after, before); |
---|
172 | return new ObjectFactory().createAnnotationInfoList(annotationInfoList); |
---|
173 | } catch (NotInDataBaseException e) { |
---|
174 | loggerServer.debug(e.toString()); |
---|
175 | httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); |
---|
176 | return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList()); |
---|
177 | } |
---|
178 | } |
---|
179 | // TODO Unit test |
---|
180 | |
---|
181 | @GET |
---|
182 | @Produces(MediaType.TEXT_XML) |
---|
183 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions") |
---|
184 | @Transactional(readOnly = true) |
---|
185 | public JAXBElement<PermissionList> getAnnotationPermissions(@PathParam("annotationid") String externalIdentifier) throws IOException { |
---|
186 | Map params = new HashMap(); |
---|
187 | try { |
---|
188 | PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier); |
---|
189 | if (result != null) { |
---|
190 | return (new ObjectFactory()).createPermissionList(result); |
---|
191 | } else { |
---|
192 | return (new ObjectFactory()).createPermissionList(new PermissionList()); |
---|
193 | } |
---|
194 | } catch (NotInDataBaseException e1) { |
---|
195 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
196 | return (new ObjectFactory()).createPermissionList(new PermissionList()); |
---|
197 | } catch (ForbiddenException e2) { |
---|
198 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
199 | return (new ObjectFactory()).createPermissionList(new PermissionList()); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | private class GetPermissionList implements ILambda<Map, PermissionList> { |
---|
204 | |
---|
205 | @Override |
---|
206 | public PermissionList apply(Map params) throws NotInDataBaseException { |
---|
207 | return dbDispatcher.getPermissions((Number) params.get("internalID"), (Resource) params.get("resourceType")); |
---|
208 | } |
---|
209 | } |
---|
210 | /////////////////////////////////////////////////////// |
---|
211 | |
---|
212 | @DELETE |
---|
213 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") |
---|
214 | public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException { |
---|
215 | Map params = new HashMap(); |
---|
216 | try { |
---|
217 | int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier); |
---|
218 | if (result != null) { |
---|
219 | return result[0] + " annotation(s) is(are) deleted."; |
---|
220 | } else { |
---|
221 | return "Nothing is deleted."; |
---|
222 | } |
---|
223 | } catch (NotInDataBaseException e1) { |
---|
224 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
225 | return e1.getMessage(); |
---|
226 | } catch (ForbiddenException e2) { |
---|
227 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
228 | return e2.getMessage(); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | private class DeleteAnnotation implements ILambda<Map, int[]> { |
---|
233 | |
---|
234 | @Override |
---|
235 | public int[] apply(Map params) throws NotInDataBaseException { |
---|
236 | return dbDispatcher.deleteAnnotation((Number) params.get("internalID")); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | /////////////////////////////////////////////////////// |
---|
241 | @POST |
---|
242 | @Consumes(MediaType.APPLICATION_XML) |
---|
243 | @Produces(MediaType.APPLICATION_XML) |
---|
244 | @Path("") |
---|
245 | public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) throws IOException { |
---|
246 | |
---|
247 | Map params = new HashMap(); |
---|
248 | params.put("annotation", annotation); |
---|
249 | ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new AddAnnotation()); |
---|
250 | if (result != null) { |
---|
251 | return (new ObjectFactory()).createResponseBody(result); |
---|
252 | } else { |
---|
253 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | private class AddAnnotation implements ILambda<Map, ResponseBody> { |
---|
258 | |
---|
259 | @Override |
---|
260 | public ResponseBody apply(Map params) throws NotInDataBaseException { |
---|
261 | Number principalID = (Number) params.get("principalID"); |
---|
262 | Annotation annotation = (Annotation) params.get("annotation"); |
---|
263 | Number annotationID = dbDispatcher.addPrincipalsAnnotation(principalID, annotation); |
---|
264 | return dbDispatcher.makeAnnotationResponseEnvelope(annotationID); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | /////////////////////////////////////////////////////// |
---|
269 | // TODO: unit test |
---|
270 | @PUT |
---|
271 | @Consumes(MediaType.APPLICATION_XML) |
---|
272 | @Produces(MediaType.APPLICATION_XML) |
---|
273 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") |
---|
274 | public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalId, Annotation annotation) throws IOException { |
---|
275 | |
---|
276 | String annotationExtId = annotation.getId(); |
---|
277 | if (!(externalId).equals(annotationExtId)) { |
---|
278 | loggerServer.debug("Wrong request: the annotation identifier " + externalId + " and the annotation (notebook) ID from the request body do not match."); |
---|
279 | httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST); |
---|
280 | return null; |
---|
281 | } |
---|
282 | try { |
---|
283 | Map params = new HashMap(); |
---|
284 | params.put("annotation", annotation); |
---|
285 | ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId); |
---|
286 | if (result != null) { |
---|
287 | return (new ObjectFactory()).createResponseBody(result); |
---|
288 | } else { |
---|
289 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
290 | } |
---|
291 | } catch (NotInDataBaseException e1) { |
---|
292 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
293 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
294 | } catch (ForbiddenException e2) { |
---|
295 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
296 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | /////////////////////////////////////////////////////////// |
---|
301 | private class UpdateAnnotation implements ILambda<Map, ResponseBody> { |
---|
302 | |
---|
303 | @Override |
---|
304 | public ResponseBody apply(Map params) throws NotInDataBaseException { |
---|
305 | Annotation annotation = (Annotation) params.get("annotation"); |
---|
306 | Number annotationID = (Number) params.get("internalID"); |
---|
307 | int updatedRows = dbDispatcher.updateAnnotation(annotation); |
---|
308 | return dbDispatcher.makeAnnotationResponseEnvelope(annotationID); |
---|
309 | } |
---|
310 | } |
---|
311 | /////////////////////////////////////////////////////////////////////////////// |
---|
312 | |
---|
313 | @PUT |
---|
314 | @Consumes(MediaType.APPLICATION_XML) |
---|
315 | @Produces(MediaType.APPLICATION_XML) |
---|
316 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/body") |
---|
317 | public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) throws IOException { |
---|
318 | Map params = new HashMap(); |
---|
319 | params.put("annotationBody", annotationBody); |
---|
320 | try { |
---|
321 | ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier); |
---|
322 | if (result != null) { |
---|
323 | return (new ObjectFactory()).createResponseBody(result); |
---|
324 | } else { |
---|
325 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
326 | } |
---|
327 | } catch (NotInDataBaseException e1) { |
---|
328 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
329 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
330 | } catch (ForbiddenException e2) { |
---|
331 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
332 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
333 | } |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | /////////////////////////////////////////////////////////// |
---|
338 | private class UpdateAnnotationBody implements ILambda<Map, ResponseBody> { |
---|
339 | |
---|
340 | @Override |
---|
341 | public ResponseBody apply(Map params) throws NotInDataBaseException { |
---|
342 | Number resourceID = (Number) params.get("internalID"); |
---|
343 | AnnotationBody annotationBody = (AnnotationBody) params.get("annotationBody"); |
---|
344 | int updatedRows = dbDispatcher.updateAnnotationBody(resourceID, annotationBody); |
---|
345 | return dbDispatcher.makeAnnotationResponseEnvelope(resourceID); |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | @PUT |
---|
350 | @Consumes(MediaType.TEXT_PLAIN) |
---|
351 | @Produces(MediaType.APPLICATION_XML) |
---|
352 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/headline") |
---|
353 | public JAXBElement<ResponseBody> updateAnnotationHeadline(@PathParam("annotationid") String externalIdentifier, String newHeadline) throws IOException { |
---|
354 | Map params = new HashMap(); |
---|
355 | params.put("headline", newHeadline); |
---|
356 | try { |
---|
357 | ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier); |
---|
358 | if (result != null) { |
---|
359 | return (new ObjectFactory()).createResponseBody(result); |
---|
360 | } else { |
---|
361 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
362 | } |
---|
363 | } catch (NotInDataBaseException e1) { |
---|
364 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
365 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
366 | } catch (ForbiddenException e2) { |
---|
367 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
368 | return (new ObjectFactory()).createResponseBody(new ResponseBody()); |
---|
369 | } |
---|
370 | |
---|
371 | } |
---|
372 | |
---|
373 | /////////////////////////////////////////////////////////// |
---|
374 | private class UpdateAnnotationHeadline implements ILambda<Map, ResponseBody> { |
---|
375 | |
---|
376 | @Override |
---|
377 | public ResponseBody apply(Map params) throws NotInDataBaseException { |
---|
378 | Number resourceID = (Number) params.get("internalID"); |
---|
379 | String newHeadline = (String) params.get("headline"); |
---|
380 | int updatedRows = dbDispatcher.updateAnnotationHeadline(resourceID, newHeadline); |
---|
381 | return dbDispatcher.makeAnnotationResponseEnvelope(resourceID); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | ////////////////////////////////////////// |
---|
386 | ////////////////////////////////////////////// |
---|
387 | @POST |
---|
388 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
---|
389 | @Produces(MediaType.TEXT_PLAIN) |
---|
390 | @Path("publicaccessform") |
---|
391 | public String updatePubliAccessFromForm( |
---|
392 | @FormParam("annotationId") String annotationDatabaseId, |
---|
393 | @FormParam("annotationHeadline") String annotationHeadline, |
---|
394 | @FormParam("access") String access) |
---|
395 | throws IOException { |
---|
396 | |
---|
397 | if (access.trim().equals("")) { |
---|
398 | access = "none"; |
---|
399 | } |
---|
400 | |
---|
401 | Access accessTyped = Access.fromValue(access); |
---|
402 | int updatedAnnotations = 0; |
---|
403 | |
---|
404 | if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) { |
---|
405 | List<Number> annotationIDs = dbDispatcher.getAnnotationInternalIDsFromHeadline(annotationHeadline); |
---|
406 | if (annotationIDs == null || annotationIDs.isEmpty()) { |
---|
407 | return "No annotations with this headline have been found"; |
---|
408 | }; |
---|
409 | int count = 0; |
---|
410 | for (Number annotationID : annotationIDs) { |
---|
411 | count = dbDispatcher.updatePublicAttribute(annotationID, accessTyped);; |
---|
412 | updatedAnnotations = updatedAnnotations + count; |
---|
413 | } |
---|
414 | return (updatedAnnotations + " row(s) are updated"); |
---|
415 | } else { |
---|
416 | try { |
---|
417 | Number annotationID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(annotationDatabaseId), Resource.ANNOTATION); |
---|
418 | updatedAnnotations = dbDispatcher.updatePublicAttribute(annotationID, accessTyped); |
---|
419 | } catch (NotInDataBaseException e) { |
---|
420 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); |
---|
421 | } |
---|
422 | } |
---|
423 | return (updatedAnnotations + " annotation(s) are updated."); |
---|
424 | } |
---|
425 | |
---|
426 | //////////////////////////////////////////////////// |
---|
427 | @PUT |
---|
428 | @Consumes(MediaType.APPLICATION_XML) |
---|
429 | @Produces(MediaType.APPLICATION_XML) |
---|
430 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/{principalid: " + BackendConstants.regExpIdentifier + "}") |
---|
431 | public String updateAccess(@PathParam("annotationid") String annotationExternalId, |
---|
432 | @PathParam("principalid") String principalExternalId, Access access) throws IOException { |
---|
433 | try { |
---|
434 | return this.genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access); |
---|
435 | } catch (NotInDataBaseException e1) { |
---|
436 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
437 | return e1.getMessage(); |
---|
438 | } catch (ForbiddenException e2) { |
---|
439 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
440 | return e2.getMessage(); |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | ////////////////////////////////////////////// |
---|
445 | @POST |
---|
446 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
---|
447 | @Produces(MediaType.TEXT_PLAIN) |
---|
448 | @Path("permissionform") |
---|
449 | public String updatePermissionFromForm(@FormParam("login") String remoteID, |
---|
450 | @FormParam("fullName") String fullName, |
---|
451 | @FormParam("userId") String principalDatabaseId, |
---|
452 | @FormParam("annotationId") String annotationDatabaseId, |
---|
453 | @FormParam("annotationHeadline") String annotationHeadline, |
---|
454 | @FormParam("access") String access) |
---|
455 | throws IOException { |
---|
456 | |
---|
457 | try { |
---|
458 | |
---|
459 | if (access.trim().equals("")) { |
---|
460 | access = null; |
---|
461 | } |
---|
462 | |
---|
463 | if (principalDatabaseId == null || principalDatabaseId.trim().equals("")) { |
---|
464 | if (remoteID != null && !remoteID.trim().equals("")) { |
---|
465 | principalDatabaseId = dbDispatcher.getPrincipalExternalIDFromRemoteID(remoteID).toString(); |
---|
466 | } else { |
---|
467 | if (fullName != null) { |
---|
468 | principalDatabaseId = dbDispatcher.getPrincipalExternalIdFromName(fullName).toString(); |
---|
469 | } else { |
---|
470 | httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No user information is given"); |
---|
471 | } |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) { |
---|
476 | List<UUID> annotationIds = dbDispatcher.getAnnotationExternalIdsFromHeadline(annotationHeadline); |
---|
477 | if (annotationIds == null || annotationIds.isEmpty()) { |
---|
478 | return "No annotations with this headline found"; |
---|
479 | }; |
---|
480 | int count = 0; |
---|
481 | String tmp = null; |
---|
482 | for (UUID annotationId : annotationIds) { |
---|
483 | tmp = this.genericUpdateDeleteAccess(annotationId.toString(), principalDatabaseId, Access.fromValue(access)); |
---|
484 | if (!tmp.startsWith("0")) { |
---|
485 | count++; |
---|
486 | } |
---|
487 | } |
---|
488 | return (count + " row(s) are updated"); |
---|
489 | } else { |
---|
490 | return this.genericUpdateDeleteAccess(annotationDatabaseId, principalDatabaseId, Access.fromValue(access)); |
---|
491 | } |
---|
492 | |
---|
493 | } catch (NotInDataBaseException e1) { |
---|
494 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
495 | return e1.getMessage(); |
---|
496 | } catch (ForbiddenException e2) { |
---|
497 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
498 | return e2.getMessage(); |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | //////////////////////////////////////////// |
---|
503 | private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException, NotInDataBaseException, ForbiddenException { |
---|
504 | Map params = new HashMap(); |
---|
505 | params.put("access", access); |
---|
506 | final Number inputPrincipalID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL); |
---|
507 | params.put("inputPrincipalID", inputPrincipalID); |
---|
508 | Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId); |
---|
509 | if (result != null) { |
---|
510 | return result + " row(s) is(are) updated."; |
---|
511 | } else { |
---|
512 | return "0 rows are updated."; |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | } |
---|
517 | |
---|
518 | private class UpdatePrincipalAccess implements ILambda<Map, Integer> { |
---|
519 | |
---|
520 | @Override |
---|
521 | public Integer apply(Map params) throws NotInDataBaseException { |
---|
522 | Number annotationID = (Number) params.get("internalID"); |
---|
523 | Number principalID = (Number) params.get("inputPrincipalID"); |
---|
524 | Access access = (Access) params.get("access"); |
---|
525 | return dbDispatcher.updateAnnotationPrincipalAccess(annotationID, principalID, access); |
---|
526 | } |
---|
527 | } |
---|
528 | /////////////////////////////////////////// |
---|
529 | |
---|
530 | @PUT |
---|
531 | @Consumes(MediaType.APPLICATION_XML) |
---|
532 | @Produces(MediaType.APPLICATION_XML) |
---|
533 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/") |
---|
534 | public JAXBElement<ResponseBody> updatePermissions(@PathParam("annotationid") String annotationExternalId, PermissionList permissions) throws IOException { |
---|
535 | |
---|
536 | Map params = new HashMap(); |
---|
537 | params.put("permissions", permissions); |
---|
538 | try { |
---|
539 | ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId); |
---|
540 | if (result != null) { |
---|
541 | return new ObjectFactory().createResponseBody(result); |
---|
542 | } else { |
---|
543 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
544 | } |
---|
545 | } catch (NotInDataBaseException e1) { |
---|
546 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
547 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
548 | } catch (ForbiddenException e2) { |
---|
549 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
550 | return new ObjectFactory().createResponseBody(new ResponseBody()); |
---|
551 | } |
---|
552 | |
---|
553 | } |
---|
554 | |
---|
555 | //////////////////////////////////////////// |
---|
556 | private class UpdatePermissions implements ILambda<Map, ResponseBody> { |
---|
557 | |
---|
558 | @Override |
---|
559 | public ResponseBody apply(Map params) throws NotInDataBaseException { |
---|
560 | Number annotationID = (Number) params.get("internalID"); |
---|
561 | PermissionList permissions = (PermissionList) params.get("permissions"); |
---|
562 | int updatedRows = dbDispatcher.updatePermissions(annotationID, permissions); |
---|
563 | return dbDispatcher.makeAccessResponseEnvelope(annotationID, Resource.ANNOTATION); |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | //////////////////////////////////////////////////////////// |
---|
568 | @DELETE |
---|
569 | @Produces(MediaType.TEXT_PLAIN) |
---|
570 | @Path("{annotationId: " + BackendConstants.regExpIdentifier + "}/principal/{principalId}/delete") |
---|
571 | public String deletePrincipalsAccess(@PathParam("annotationId") String annotationId, |
---|
572 | @PathParam("principalId") String principalId) throws IOException { |
---|
573 | try { |
---|
574 | return this.genericUpdateDeleteAccess(annotationId, principalId, null); |
---|
575 | } catch (NotInDataBaseException e1) { |
---|
576 | httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage()); |
---|
577 | return e1.getMessage(); |
---|
578 | } catch (ForbiddenException e2) { |
---|
579 | httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage()); |
---|
580 | return e2.getMessage(); |
---|
581 | } |
---|
582 | } |
---|
583 | } |
---|