Changeset 5086
- Timestamp:
- 04/28/14 14:44:33 (10 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
- Files:
-
- 1 added
- 1 deleted
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/NotInDataBaseException.java
r4834 r5086 26 26 27 27 28 public NotInDataBaseException(String typeOfResource, String externalID){29 super( typeOfResource + " with the external ID "+externalID + " is not found in the database");28 public NotInDataBaseException(String typeOfResource, String criterium, String value){ 29 super("Cannot find a "+typeOfResource + " with the "+criterium + " equal to "+value); 30 30 } 31 32 public NotInDataBaseException(String typeOfResource){ 33 super(typeOfResource + " with the given info is not found in the database"); 34 } 31 35 32 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java
r4865 r5086 163 163 public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml); 164 164 165 public int updateAnnotationHeadline(Number annotationID, String text); 166 165 167 166 168 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/DBIntegrityService.java
r4995 r5086 22 22 import eu.dasish.annotation.backend.PrincipalExists; 23 23 import eu.dasish.annotation.backend.Resource; 24 import eu.dasish.annotation.backend.ResourceAction; 24 25 import eu.dasish.annotation.schema.Annotation; 25 26 import eu.dasish.annotation.schema.AnnotationBody; … … 194 195 Principal getDataBaseAdmin() ; 195 196 196 boolean canDo( Access access, Number principalID, Number annotationID);197 boolean canDo(ResourceAction action, Number principalID, Number resourceID, Resource resource); 197 198 198 199 … … 231 232 */ 232 233 int updateAnnotationBody(Number internalID, AnnotationBody annotationBody); 234 235 int updateAnnotationHeadline(Number internalID, String newHeader); 233 236 234 237 /** -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/ResourceDao.java
r4865 r5086 20 20 import eu.dasish.annotation.backend.NotInDataBaseException; 21 21 import eu.dasish.annotation.schema.Access; 22 import eu.dasish.annotation.schema.Permission;23 22 import java.util.List; 24 23 import java.util.Map; -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceImlp.java
r4995 r5086 23 23 import eu.dasish.annotation.backend.PrincipalCannotBeDeleted; 24 24 import eu.dasish.annotation.backend.PrincipalExists; 25 import eu.dasish.annotation.backend.ResourceAction; 25 26 import eu.dasish.annotation.backend.dao.AnnotationDao; 26 27 import eu.dasish.annotation.backend.dao.CachedRepresentationDao; … … 413 414 } 414 415 415 @Override 416 public boolean canDo(Access access, Number principalID, Number annotationID) { 417 if (principalID.equals(annotationDao.getOwner(annotationID)) || principalDao.getTypeOfPrincipalAccount(principalID).equals(admin)) { 418 return true; 419 } 420 return this.getAccess(annotationID, principalID).equals(access); 421 } 422 423 ////// noetbooks /////// 424 /// TODO update for having attribute public!!! ///// 416 // !!!so far implemented only for annotations!!! 417 @Override 418 public boolean canDo(ResourceAction action, Number principalID, Number resourceID, Resource resource) { 419 420 switch (resource) { 421 case ANNOTATION: { 422 if (principalID.equals(annotationDao.getOwner(resourceID)) || principalDao.getTypeOfPrincipalAccount(principalID).equals(admin)) { 423 return true; 424 } 425 return this.getAccess(resourceID, principalID).value().equals(action.name()); 426 } 427 case CACHED_REPRESENTATION: { 428 return true; 429 } 430 case TARGET: { 431 return true; 432 } 433 case PRINCIPAL: { 434 return true; 435 } 436 default: 437 return false; 438 } 439 440 } 441 ////// noetbooks /////// 442 /// TODO update for having attribute public!!! ///// 443 425 444 @Override 426 445 public NotebookInfoList getNotebooks(Number principalID, Access access) { … … 598 617 return annotationDao.updateAnnotationBody(internalID, body[0], body[1], annotationBody.getXmlBody() != null); 599 618 } 619 620 @Override 621 public int updateAnnotationHeadline(Number internalID, String newHeader) { 622 return annotationDao.updateAnnotationHeadline(internalID, newHeader); 623 } 600 624 601 625 @Override … … 603 627 return principalDao.updatePrincipal(principal); 604 628 } 605 606 @Override 607 public int updateTargetCachedFragment(Number targetID, Number cachedID, String fragmentDescriptor) throws NotInDataBaseException {629 630 @Override 631 public int updateTargetCachedFragment(Number targetID, Number cachedID, String fragmentDescriptor) throws NotInDataBaseException { 608 632 return targetDao.updateTargetCachedRepresentationFragment(targetID, cachedID, fragmentDescriptor); 609 633 } 610 611 @Override 612 public int updateCachedMetada(CachedRepresentationInfo cachedInfo) throws NotInDataBaseException {634 635 @Override 636 public int updateCachedMetada(CachedRepresentationInfo cachedInfo) throws NotInDataBaseException { 613 637 Number internalID = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI()); 614 638 return cachedRepresentationDao.updateCachedRepresentationMetadata(internalID, cachedInfo); 615 639 } 616 617 @Override 618 public int updateCachedBlob(Number internalID, InputStream cachedBlob) throws IOException {640 641 @Override 642 public int updateCachedBlob(Number internalID, InputStream cachedBlob) throws IOException { 619 643 return cachedRepresentationDao.updateCachedRepresentationBlob(internalID, cachedBlob); 620 644 } 621 645 622 646 /// notebooks /// 623 624 647 @Override 625 648 public boolean updateNotebookMetadata(Number notebookID, NotebookInfo upToDateNotebookInfo) throws NotInDataBaseException { … … 648 671 649 672 } 650 651 652 673 653 674 @Override … … 706 727 return notebookDao.addAnnotationToNotebook(notebookID, newAnnotationID); 707 728 } 708 709 @Override 710 public int addSpringUser(String username, String password, int strength, String salt) {729 730 @Override 731 public int addSpringUser(String username, String password, int strength, String salt) { 711 732 int users = principalDao.addSpringUser(username, password, strength, salt); 712 int authorities = principalDao.addSpringAuthorities(username); 713 return users +authorities;733 int authorities = principalDao.addSpringAuthorities(username); 734 return users + authorities; 714 735 } 715 736 … … 856 877 } 857 878 } 858 859 860 879 861 880 //// priveee /// -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r4886 r5086 387 387 return affectedRows; 388 388 } 389 390 @Override 391 public int updateAnnotationHeadline(Number annotationID, String header) { 392 Map<String, Object> params = new HashMap<String, Object>(); 393 params.put("annotationID", annotationID); 394 params.put("headline", header); 395 396 StringBuilder sql = new StringBuilder("UPDATE "); 397 sql.append(annotationTableName).append(" SET "). 398 append(last_modified).append("= default,"). 399 append(_headline).append("= :headline "). 400 append(" WHERE ").append(annotation_id).append("= :annotationID"); 401 int affectedRows = this.loggedUpdate(sql.toString(), params); 402 return affectedRows; 403 } 389 404 390 405 // TODO Unit test -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcPrincipalDao.java
r4995 r5086 79 79 List<Principal> result = this.loggedQuery(sql.toString(), principalRowMapper, eMail.toLowerCase()); 80 80 if (result.isEmpty()) { 81 throw new NotInDataBaseException("principal" );81 throw new NotInDataBaseException("principal", "e-mail", eMail); 82 82 } 83 83 return result.get(0); … … 137 137 List<Number> result = this.loggedQuery(requestDB.toString(), internalIDRowMapper, remoteID); 138 138 if (result.isEmpty()) { 139 throw new NotInDataBaseException("principal" );139 throw new NotInDataBaseException("principal", "remote ID", remoteID); 140 140 } 141 141 return result.get(0); -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java
r4995 r5086 91 91 final static protected String is_xml = "is_xml"; 92 92 final static protected String public_ = "public_"; 93 final static protected String _headline = "headline"; 93 94 final static protected String fragment_descriptor = "fragment_descriptor"; 94 95 final static protected String fragment_descriptor_in_cached = "fragment_descriptor_in_cached"; … … 170 171 List<Number> sqlResult = this.loggedQuery(sql.toString(), internalIDRowMapper, externalId.toString()); 171 172 if (sqlResult.isEmpty()) { 172 throw new NotInDataBaseException(resourceTableName, externalId.toString());173 throw new NotInDataBaseException(resourceTableName, "external ID", externalId.toString()); 173 174 } 174 175 … … 195 196 } catch (IllegalArgumentException e) { 196 197 _logger.info(externalID + " is not a valid <uuid>. Therefore, I expect that it is a temporary idendifier of a new resource that is not yet."); 197 throw new NotInDataBaseException(resourceTableName, externalID);198 throw new NotInDataBaseException(resourceTableName, "external ID", externalID); 198 199 } 199 200 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java
r5053 r5086 20 20 import eu.dasish.annotation.backend.BackendConstants; 21 21 import eu.dasish.annotation.backend.NotInDataBaseException; 22 import eu.dasish.annotation.backend.PrincipalExists;23 22 import eu.dasish.annotation.backend.Resource; 23 import eu.dasish.annotation.backend.ResourceAction; 24 24 import eu.dasish.annotation.backend.dao.ILambda; 25 25 import eu.dasish.annotation.schema.Annotation; … … 33 33 import java.io.IOException; 34 34 import java.util.Arrays; 35 import java.util.HashMap; 35 36 import java.util.Map; 36 37 import java.util.UUID; … … 86 87 @Transactional(readOnly = true) 87 88 public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException { 88 Number principalID = this.getPrincipalID(); 89 if (principalID == null) { 90 return new ObjectFactory().createAnnotation(new Annotation()); 91 } 92 try { 93 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); 94 95 if (dbIntegrityService.canDo(Access.READ, principalID, annotationID)) { 96 final Annotation annotation = dbIntegrityService.getAnnotation(annotationID); 97 return new ObjectFactory().createAnnotation(annotation); 98 } else { 99 verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 100 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 101 return new ObjectFactory().createAnnotation(new Annotation()); 102 } 103 104 } catch (NotInDataBaseException e2) { 105 loggerServer.debug(e2.toString()); 106 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 107 return new ObjectFactory().createAnnotation(new Annotation()); 108 } 109 } 110 111 //TODO: unit test 89 Map params = new HashMap(); 90 Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier, false); 91 if (result != null) { 92 return (new ObjectFactory()).createAnnotation(result); 93 } else { 94 return (new ObjectFactory()).createAnnotation(new Annotation()); 95 } 96 } 97 98 private class GetAnnotation implements ILambda<Map, Annotation> { 99 100 @Override 101 public Annotation apply(Map params) throws NotInDataBaseException { 102 return dbIntegrityService.getAnnotation((Number) params.get("internalID")); 103 } 104 } 105 106 ////////////////////////////////////////////////// 112 107 @GET 113 108 @Produces(MediaType.TEXT_XML) … … 115 110 @Transactional(readOnly = true) 116 111 public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String externalIdentifier) throws IOException { 117 Number principalID = this.getPrincipalID(); 118 if (principalID == null) { 119 return new ObjectFactory().createReferenceList(new ReferenceList()); 120 } 121 try { 122 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); 123 124 if (dbIntegrityService.canDo(Access.READ, principalID, annotationID)) { 125 final ReferenceList TargetList = dbIntegrityService.getAnnotationTargets(annotationID); 126 return new ObjectFactory().createTargetList(TargetList); 127 } else { 128 verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 129 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 130 return new ObjectFactory().createReferenceList(new ReferenceList()); 131 } 132 133 } catch (NotInDataBaseException e2) { 134 loggerServer.debug(e2.toString()); 135 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 136 return new ObjectFactory().createReferenceList(new ReferenceList()); 112 Map params = new HashMap(); 113 ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier, false); 114 if (result != null) { 115 return (new ObjectFactory()).createTargetList(result); 116 } else { 117 return (new ObjectFactory()).createTargetList(new ReferenceList()); 118 } 119 } 120 121 private class GetTargetList implements ILambda<Map, ReferenceList> { 122 123 @Override 124 public ReferenceList apply(Map params) throws NotInDataBaseException { 125 return dbIntegrityService.getAnnotationTargets((Number) params.get("internalID")); 137 126 } 138 127 } … … 181 170 @Transactional(readOnly = true) 182 171 public JAXBElement<PermissionList> getAnnotationPermissions(@PathParam("annotationid") String externalIdentifier) throws IOException { 183 Number principalID = this.getPrincipalID(); 184 if (principalID == null) { 185 return new ObjectFactory().createPermissionList(new PermissionList()); 186 } 187 try { 188 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); 189 if (dbIntegrityService.canDo(Access.READ, principalID, annotationID)) { 190 final PermissionList permissionList = dbIntegrityService.getPermissions(annotationID, Resource.ANNOTATION); 191 return new ObjectFactory().createPermissionList(permissionList); 192 } else { 193 verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 194 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 195 return new ObjectFactory().createPermissionList(new PermissionList()); 196 } 197 198 } catch (NotInDataBaseException e2) { 199 loggerServer.debug(e2.toString()); 200 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 201 return new ObjectFactory().createPermissionList(new PermissionList()); 172 Map params = new HashMap(); 173 PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier, false); 174 if (result != null) { 175 return (new ObjectFactory()).createPermissionList(result); 176 } else { 177 return (new ObjectFactory()).createPermissionList(new PermissionList()); 178 } 179 } 180 181 private class GetPermissionList implements ILambda<Map, PermissionList> { 182 183 @Override 184 public PermissionList apply(Map params) throws NotInDataBaseException { 185 return dbIntegrityService.getPermissions((Number) params.get("internalID"), (Resource) params.get("resourceType")); 202 186 } 203 187 } … … 207 191 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") 208 192 public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException { 209 Number principalID = this.getPrincipalID(); 210 if (principalID == null) { 211 return "Nothing is deleted"; 212 } 193 Map params = new HashMap(); 194 int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier, false); 195 if (result != null) { 196 return result[0] + " annotation(s) is(are) deleted."; 197 } else { 198 return "Nothing is deleted."; 199 } 200 } 201 202 private class DeleteAnnotation implements ILambda<Map, int[]> { 203 204 @Override 205 public int[] apply(Map params) throws NotInDataBaseException { 206 return dbIntegrityService.deleteAnnotation((Number) params.get("internalID")); 207 } 208 } 209 210 /////////////////////////////////////////////////////// 211 @POST 212 @Consumes(MediaType.APPLICATION_XML) 213 @Produces(MediaType.APPLICATION_XML) 214 @Path("") 215 public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) throws IOException { 216 Map params = new HashMap(); 217 params.put("annotation", annotation); 218 ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new AddAnnotation()); 219 if (result != null) { 220 return (new ObjectFactory()).createResponseBody(result); 221 } else { 222 return (new ObjectFactory()).createResponseBody(new ResponseBody()); 223 } 224 } 225 226 private class AddAnnotation implements ILambda<Map, ResponseBody> { 227 228 @Override 229 public ResponseBody apply(Map params) throws NotInDataBaseException { 230 Number principalID = (Number) params.get("principalID"); 231 Annotation annotation = (Annotation) params.get("annotation"); 232 Number annotationID = dbIntegrityService.addPrincipalsAnnotation(principalID, annotation); 233 return dbIntegrityService.makeAnnotationResponseEnvelope(annotationID); 234 } 235 } 236 237 /////////////////////////////////////////////////////// 238 // TODO: unit test 239 @PUT 240 @Consumes(MediaType.APPLICATION_XML) 241 @Produces(MediaType.APPLICATION_XML) 242 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") 243 public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalId, Annotation annotation) throws IOException { 244 245 String annotationURI = annotation.getURI(); 246 String path = uriInfo.getBaseUri().toString(); 247 if (!(path + "annotations/" + externalId).equals(annotationURI)) { 248 loggerServer.debug("Wrong request: the annotation (notebook) identifier " + externalId + " and the annotation (notebook) ID from the request body do not match."); 249 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST); 250 return null; 251 } 252 Map params = new HashMap(); 253 params.put("annotation", annotation); 254 ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId, false); 255 if (result != null) { 256 return (new ObjectFactory()).createResponseBody(result); 257 } else { 258 return (new ObjectFactory()).createResponseBody(new ResponseBody()); 259 } 260 261 262 } 263 264 /////////////////////////////////////////////////////////// 265 private class UpdateAnnotation implements ILambda<Map, ResponseBody> { 266 267 @Override 268 public ResponseBody apply(Map params) throws NotInDataBaseException { 269 Annotation annotation = (Annotation) params.get("annotation"); 270 Number annotationID = (Number) params.get("internalID"); 271 int updatedRows = dbIntegrityService.updateAnnotation(annotation); 272 return dbIntegrityService.makeAnnotationResponseEnvelope(annotationID); 273 } 274 } 275 /////////////////////////////////////////////////////////////////////////////// 276 277 @PUT 278 @Consumes(MediaType.APPLICATION_XML) 279 @Produces(MediaType.APPLICATION_XML) 280 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/body") 281 public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) throws IOException { 282 Map params = new HashMap(); 283 params.put("annotationBody", annotationBody); 284 ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier, false); 285 if (result != null) { 286 return (new ObjectFactory()).createResponseBody(result); 287 } else { 288 return (new ObjectFactory()).createResponseBody(new ResponseBody()); 289 } 290 291 } 292 293 /////////////////////////////////////////////////////////// 294 private class UpdateAnnotationBody implements ILambda<Map, ResponseBody> { 295 296 @Override 297 public ResponseBody apply(Map params) throws NotInDataBaseException { 298 Number resourceID = (Number) params.get("internalID"); 299 AnnotationBody annotationBody = (AnnotationBody) params.get("annotationBody"); 300 int updatedRows = dbIntegrityService.updateAnnotationBody(resourceID, annotationBody); 301 return dbIntegrityService.makeAnnotationResponseEnvelope(resourceID); 302 } 303 } 304 305 @PUT 306 @Consumes(MediaType.TEXT_PLAIN) 307 @Produces(MediaType.APPLICATION_XML) 308 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/headline") 309 public JAXBElement<ResponseBody> updateAnnotationHeadline(@PathParam("annotationid") String externalIdentifier, String newHeadline) throws IOException { 310 Map params = new HashMap(); 311 params.put("headline", newHeadline); 312 ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier, false); 313 if (result != null) { 314 return (new ObjectFactory()).createResponseBody(result); 315 } else { 316 return (new ObjectFactory()).createResponseBody(new ResponseBody()); 317 } 318 319 } 320 321 /////////////////////////////////////////////////////////// 322 private class UpdateAnnotationHeadline implements ILambda<Map, ResponseBody> { 323 324 @Override 325 public ResponseBody apply(Map params) throws NotInDataBaseException { 326 Number resourceID = (Number) params.get("internalID"); 327 String newHeadline = (String) params.get("headline"); 328 int updatedRows = dbIntegrityService.updateAnnotationHeadline(resourceID, newHeadline); 329 return dbIntegrityService.makeAnnotationResponseEnvelope(resourceID); 330 } 331 } 332 //////////////////////////////////////////////////// 333 334 @PUT 335 @Consumes(MediaType.APPLICATION_XML) 336 @Produces(MediaType.APPLICATION_XML) 337 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/{principalid: " + BackendConstants.regExpIdentifier + "}") 338 public String updateAccess(@PathParam("annotationid") String annotationExternalId, 339 @PathParam("principalid") String principalExternalId, Access access) throws IOException { 340 return genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access); 341 } 342 //////////////////////////////////////////// 343 344 private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException { 345 Map params = new HashMap(); 346 params.put("access", access); 213 347 try { 214 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); 215 if (principalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(principalID).equals(admin)) { 216 int[] result = dbIntegrityService.deleteAnnotation(annotationID); 217 return result[0] + " annotation(s) deleted."; 348 final Number inputPrincipalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL); 349 params.put("inputPrincipalID", inputPrincipalID); 350 Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId, false); 351 if (result != null) { 352 return result + " row(s) is(are) updated."; 218 353 } else { 219 verboseOutput.FORBIDDEN_ANNOTATION_DELETING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 220 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 221 return "Nothings is deleted."; 354 return "Nothing is updated."; 222 355 } 223 356 … … 229 362 } 230 363 231 /////////////////////////////////////////////////////// 232 @POST 233 @Consumes(MediaType.APPLICATION_XML) 234 @Produces(MediaType.APPLICATION_XML) 235 @Path("") 236 public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) throws IOException { 237 Number principalID = this.getPrincipalID(); 238 if (principalID == null) { 239 return new ObjectFactory().createResponseBody(new ResponseBody()); 240 } 241 try { 242 Number annotationID = dbIntegrityService.addPrincipalsAnnotation(principalID, annotation); 243 return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID)); 244 } catch (NotInDataBaseException e2) { 245 loggerServer.debug(e2.toString()); 246 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e2.toString()); 247 return new ObjectFactory().createResponseBody(new ResponseBody()); 248 } 249 } 250 /////////////////////////////////////////////////////// 251 // TODO: unit test 364 private class UpdatePrincipalAccess implements ILambda<Map, Integer> { 365 366 @Override 367 public Integer apply(Map params) throws NotInDataBaseException { 368 Number annotationID = (Number) params.get("internalID"); 369 Number principalID = (Number) params.get("inputPrincipalID"); 370 Access access = (Access) params.get("access"); 371 return dbIntegrityService.updateAnnotationPrincipalAccess(annotationID, principalID, access); 372 } 373 } 374 /////////////////////////////////////////// 252 375 253 376 @PUT 254 377 @Consumes(MediaType.APPLICATION_XML) 255 378 @Produces(MediaType.APPLICATION_XML) 256 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")257 public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalIdentifier, Annotation annotation) throws IOException {258 Number principalID = this.getPrincipalID();259 if (principalID == null) {260 return new ObjectFactory().createResponseBody(new ResponseBody());261 }262 String path = uriInfo.getBaseUri().toString();263 String annotationURI = annotation.getURI();264 if (!(path + "annotations/" + externalIdentifier).equals(annotationURI)) {265 verboseOutput.IDENTIFIER_MISMATCH(externalIdentifier);266 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST);267 return new ObjectFactory().createResponseBody(new ResponseBody());268 }269 try {270 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);271 try {272 if (principalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(principalID).equals(admin)) {273 int updatedRows = dbIntegrityService.updateAnnotation(annotation);274 return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));275 } else {276 verboseOutput.FORBIDDEN_ACCESS_CHANGING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail());277 loggerServer.debug(" Access changing is the part of the full update of the annotation.");278 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);279 return new ObjectFactory().createResponseBody(new ResponseBody());280 }281 } catch (NotInDataBaseException e) {282 loggerServer.debug(e.toString());283 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());284 return new ObjectFactory().createResponseBody(new ResponseBody());285 }286 } catch (NotInDataBaseException e2) {287 loggerServer.debug(e2.toString());288 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());289 return new ObjectFactory().createResponseBody(new ResponseBody());290 }291 }292 293 @PUT294 @Consumes(MediaType.APPLICATION_XML)295 @Produces(MediaType.APPLICATION_XML)296 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/body")297 public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) throws IOException {298 Number principalID = this.getPrincipalID();299 if (principalID == null) {300 return new ObjectFactory().createResponseBody(new ResponseBody());301 }302 try {303 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);304 if (dbIntegrityService.canDo(Access.WRITE, principalID, annotationID)) {305 int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody);306 return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));307 } else {308 verboseOutput.FORBIDDEN_ANNOTATION_BODY_WRITING(externalIdentifier, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail());309 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);310 return new ObjectFactory().createResponseBody(new ResponseBody());311 }312 } catch (NotInDataBaseException e2) {313 loggerServer.debug(e2.toString());314 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());315 return new ObjectFactory().createResponseBody(new ResponseBody());316 }317 }318 319 @PUT320 @Consumes(MediaType.APPLICATION_XML)321 @Produces(MediaType.APPLICATION_XML)322 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/{principalid: " + BackendConstants.regExpIdentifier + "}")323 public String updateAccess(@PathParam("annotationid") String annotationExternalId,324 @PathParam("principalid") String principalExternalId, Access access) throws IOException {325 Number remotePrincipalID = this.getPrincipalID();326 if (remotePrincipalID == null) {327 return "Nothing is updated";328 }329 try {330 final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(principalExternalId), Resource.PRINCIPAL);331 try {332 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationExternalId), Resource.ANNOTATION);333 if (remotePrincipalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) {334 int result = dbIntegrityService.updateAnnotationPrincipalAccess(annotationID, principalID, access);335 return result + " rows are updated/added";336 } else {337 verboseOutput.FORBIDDEN_ACCESS_CHANGING(annotationExternalId, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail());338 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);339 return "Nothing is updated.";340 }341 342 } catch (NotInDataBaseException e1) {343 loggerServer.debug(e1.toString());344 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.toString());345 return "Nothing is updated.";346 }347 } catch (NotInDataBaseException e2) {348 loggerServer.debug(e2.toString());349 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());350 return "Nothing is updated.";351 }352 }353 354 @PUT355 @Consumes(MediaType.APPLICATION_XML)356 @Produces(MediaType.APPLICATION_XML)357 379 @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/") 358 380 public JAXBElement<ResponseBody> updatePermissions(@PathParam("annotationid") String annotationExternalId, PermissionList permissions) throws IOException { 359 Number remotePrincipalID = this.getPrincipalID(); 360 if (remotePrincipalID == null) { 381 382 Map params = new HashMap(); 383 params.put("permissions", permissions); 384 385 ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId, false); 386 if (result != null) { 387 return new ObjectFactory().createResponseBody(result); 388 } else { 361 389 return new ObjectFactory().createResponseBody(new ResponseBody()); 362 390 } 363 try { 364 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationExternalId), Resource.ANNOTATION); 365 try { 366 if (remotePrincipalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { 367 int updatedRows = dbIntegrityService.updatePermissions(annotationID, permissions); 368 return new ObjectFactory().createResponseBody(dbIntegrityService.makeAccessResponseEnvelope(annotationID, Resource.ANNOTATION)); 369 } else { 370 verboseOutput.FORBIDDEN_ACCESS_CHANGING(annotationExternalId, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 371 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 372 return new ObjectFactory().createResponseBody(new ResponseBody()); 373 } 374 } catch (NotInDataBaseException e) { 375 loggerServer.debug(e.toString()); 376 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 377 return new ObjectFactory().createResponseBody(new ResponseBody()); 378 } 379 } catch (NotInDataBaseException e2) { 380 loggerServer.debug(e2.toString());; 381 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 382 return new ObjectFactory().createResponseBody(new ResponseBody()); 383 } 384 } 385 391 392 393 } 394 395 //////////////////////////////////////////// 396 private class UpdatePermissions implements ILambda<Map, ResponseBody> { 397 398 @Override 399 public ResponseBody apply(Map params) throws NotInDataBaseException { 400 Number annotationID = (Number) params.get("internalID"); 401 PermissionList permissions = (PermissionList) params.get("permissions"); 402 int updatedRows = dbIntegrityService.updatePermissions(annotationID, permissions); 403 return dbIntegrityService.makeAccessResponseEnvelope(annotationID, Resource.ANNOTATION); 404 } 405 } 406 407 //////////////////////////////////////////////////////////// 386 408 @DELETE 387 409 @Produces(MediaType.TEXT_PLAIN) … … 389 411 public String deletePrincipalsAccess(@PathParam("annotationId") String annotationId, 390 412 @PathParam("principalId") String principalId) throws IOException { 391 Number remotePrincipalID = this.getPrincipalID(); 392 if (remotePrincipalID == null) { 393 return "Nothing is deleted."; 394 } 395 int deletedRows = 0; 396 try { 397 final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(annotationId), Resource.ANNOTATION); 398 try { 399 if (remotePrincipalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { 400 Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL); 401 deletedRows = dbIntegrityService.updateAnnotationPrincipalAccess(annotationID, principalID, null); 402 return (deletedRows + " is deleted."); 403 } else { 404 verboseOutput.FORBIDDEN_ACCESS_CHANGING(annotationId, dbIntegrityService.getAnnotationOwner(annotationID).getDisplayName(), dbIntegrityService.getAnnotationOwner(annotationID).getEMail()); 405 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 406 return "Nothing is deleted."; 407 } 408 } catch (NotInDataBaseException e) { 409 loggerServer.debug(e.toString()); 410 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 411 return "Nothing is deleted."; 412 } 413 } catch (NotInDataBaseException e2) { 414 loggerServer.debug(e2.toString()); 415 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 416 return "Nothing is deleted."; 417 } 418 } 419 420 //////////////////////////////////////////////// 421 422 423 413 return genericUpdateDeleteAccess(annotationId, principalId, null); 414 } 424 415 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java
r4956 r5086 23 23 import eu.dasish.annotation.backend.NotInDataBaseException; 24 24 import eu.dasish.annotation.backend.Resource; 25 import eu.dasish.annotation.backend.ResourceAction; 26 import eu.dasish.annotation.backend.dao.ILambda; 25 27 import eu.dasish.annotation.schema.CachedRepresentationInfo; 26 28 import eu.dasish.annotation.schema.ObjectFactory; … … 29 31 import java.io.InputStream; 30 32 import java.sql.SQLException; 31 import java.util.UUID; 33 import java.util.HashMap; 34 import java.util.Map; 32 35 import javax.imageio.ImageIO; 33 36 import javax.servlet.http.HttpServletRequest; 34 import javax.servlet.http.HttpServletResponse;35 37 import javax.ws.rs.Consumes; 36 38 import javax.ws.rs.GET; … … 58 60 } 59 61 60 // TODOD both unit tests61 //changed path, /Target/cached part is removed62 62 @GET 63 63 @Produces(MediaType.TEXT_XML) … … 65 65 @Transactional(readOnly = true) 66 66 public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws IOException { 67 Number remotePrincipalID = this.getPrincipalID(); 68 if (remotePrincipalID == null) { 69 return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); 70 } 71 try { 72 final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.CACHED_REPRESENTATION); 73 74 final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID); 75 return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); 76 77 } catch (NotInDataBaseException e2) { 78 loggerServer.debug(e2.toString()); 79 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 80 return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); 67 Map params = new HashMap(); 68 CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId, false); 69 if (result != null) { 70 return (new ObjectFactory()).createCashedRepresentationInfo(result); 71 } else { 72 return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo()); 81 73 } 82 74 } 83 75 76 private class GetCachedRepresentationInfo implements ILambda<Map, CachedRepresentationInfo> { 77 78 @Override 79 public CachedRepresentationInfo apply(Map params) throws NotInDataBaseException { 80 Number cachedID = (Number) params.get("internalID"); 81 return dbIntegrityService.getCachedRepresentationInfo(cachedID); 82 } 83 } 84 85 //////////////////////////////////////////// 84 86 @GET 85 87 @Produces({"image/jpeg", "image/png"}) … … 87 89 @Transactional(readOnly = true) 88 90 public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws IOException { 89 Number remotePrincipalID = this.getPrincipalID(); 90 if (remotePrincipalID == null) { 91 Map params = new HashMap(); 92 InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId, false); 93 if (result != null) { 94 ImageIO.setUseCache(false); 95 try { 96 BufferedImage retVal = ImageIO.read(result); 97 return retVal; 98 } catch (IOException e1) { 99 loggerServer.info(e1.toString()); 100 101 return null; 102 } 103 } else { 104 loggerServer.info(" The cached representation with the id " + externalId + " has null blob."); 91 105 return null; 92 106 } 93 try {94 final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.CACHED_REPRESENTATION);95 InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID);96 if (dbRespond != null) {97 ImageIO.setUseCache(false);98 try {99 BufferedImage result = ImageIO.read(dbRespond);100 return result;101 } catch (IOException e1) {102 loggerServer.debug(e1.toString());103 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());104 return null;105 }106 } else {107 loggerServer.info(" The cached representation with the id " + externalId + " has null blob.");108 return null;109 }110 107 111 } catch (NotInDataBaseException e) {112 loggerServer.debug(e.toString());113 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());114 return null;115 }116 108 } 117 109 … … 121 113 @Transactional(readOnly = true) 122 114 public InputStream getCachedRepresentationContentStream(@PathParam("cachedid") String externalId) throws IOException { 123 Number remotePrincipalID = this.getPrincipalID(); 124 if (remotePrincipalID == null) { 125 return null; 126 } 127 try { 128 final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), Resource.CACHED_REPRESENTATION); 129 InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID); 130 if (dbRespond != null) { 131 return dbRespond; 132 } else { 133 loggerServer.info("The cached representation with the id " + externalId + " has null blob."); 134 return null; 135 } 115 Map params = new HashMap(); 116 return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId, false); 117 } 136 118 137 } catch (NotInDataBaseException e) { 138 loggerServer.debug(e.toString());; 139 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); 140 return null; 119 private class GetCachedRepresentationInputStream implements ILambda<Map, InputStream> { 120 121 @Override 122 public InputStream apply(Map params) throws NotInDataBaseException { 123 Number cachedID = (Number) params.get("internalID"); 124 return dbIntegrityService.getCachedRepresentationBlob(cachedID); 141 125 } 142 126 } 143 127 128 /////////////////////////////////////////////////////////////////// 144 129 @PUT 145 130 @Consumes("multipart/mixed") … … 148 133 public String updateCachedBlob(@PathParam("cachedid") String cachedIdentifier, 149 134 MultiPart multiPart) throws IOException { 150 Number remotePrincipalID = this.getPrincipalID(); 151 if (remotePrincipalID == null) { 152 return "Nothing is updated. You are no tlogged in"; 135 Map params = new HashMap(); 136 BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity(); 137 params.put("stream", bpe.getInputStream()); 138 Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier, false); 139 if (result != null) { 140 return result + "rows are updated"; 141 } else { 142 return "Nothing is updated. "; 153 143 } 154 try { 155 final Number cachedID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(cachedIdentifier), Resource.CACHED_REPRESENTATION); 156 BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity(); 157 InputStream cachedSource = bpe.getInputStream(); 144 145 146 } 147 148 private class UpdateCachedBlob implements ILambda<Map, Integer> { 149 @Override 150 public Integer apply(Map params) throws NotInDataBaseException { 151 Number cachedID = (Number) params.get("internalID"); 152 InputStream stream = (InputStream) params.get("stream"); 158 153 try { 159 final int result = dbIntegrityService.updateCachedBlob(cachedID, cachedSource); 160 return result + "rows are updated"; 154 return dbIntegrityService.updateCachedBlob(cachedID, stream); 161 155 } catch (IOException e) { 162 loggerServer.debug(e.toString()); 163 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 164 return "Nothing is updated. "; 156 loggerServer.info(e.toString()); 157 return 0; 165 158 } 166 } catch (NotInDataBaseException e2) {167 loggerServer.debug(e2.toString());168 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());169 return "Nothing is updated. ";170 159 } 171 160 } 172 161 162 /////////////////////////////////////////////////////////// 173 163 @PUT 174 164 @Consumes(MediaType.TEXT_XML) … … 176 166 @Path("metadata") 177 167 public String updateCachedMetadata(CachedRepresentationInfo cachedInfo) throws IOException { 178 Number remotePrincipalID = this.getPrincipalID(); 179 if (remotePrincipalID == null) { 180 return "Nothing is updated. You are no tlogged in"; 168 Map params = new HashMap(); 169 params.put("info", cachedInfo); 170 Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getURI(), true); 171 if (result != null) { 172 return result + "rows are updated"; 173 } else { 174 return "Nothing is updated. "; 181 175 } 182 try { 183 final int result = dbIntegrityService.updateCachedMetada(cachedInfo); 184 return result + "rows are updated"; 185 } catch (NotInDataBaseException e2) { 186 loggerServer.debug(e2.toString()); 187 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 188 return "Nothing is updated. "; 176 177 } 178 179 private class UpdateCachedMetadata implements ILambda<Map, Integer> { 180 @Override 181 public Integer apply(Map params) throws NotInDataBaseException { 182 CachedRepresentationInfo cachedInfo = (CachedRepresentationInfo) params.get("cachedInfo"); 183 return dbIntegrityService.updateCachedMetada(cachedInfo); 189 184 } 190 185 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java
r5053 r5086 75 75 Map params = new HashMap<String, String>(); 76 76 params.put("externalId", externalIdentifier); 77 Principal result = (Principal) (new RequestWrappers( loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetPrincipal());77 Principal result = (Principal) (new RequestWrappers(this)).wrapRequestResource(params, new GetPrincipal()); 78 78 return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal())); 79 79 } 80 81 private class GetPrincipal implements ILambda<Map, Principal> { 82 83 @Override 84 public Principal apply(Map params) throws NotInDataBaseException { 85 final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL); 86 return dbIntegrityService.getPrincipal(principalID); 87 } 88 } 89 90 ///////////////////////////////// 80 91 81 92 @GET … … 90 101 return "The admin of the server database " + dbIntegrityService.getDataBaseAdmin().getDisplayName() + " is availiable via e-mail " + dbIntegrityService.getDataBaseAdmin().getEMail(); 91 102 } 92 103 104 ///////////////////////////////////////// 93 105 @GET 94 106 @Produces(MediaType.TEXT_XML) … … 98 110 Map params = new HashMap<String, String>(); 99 111 params.put("email", email); 100 Principal result = (Principal) (new RequestWrappers( loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetPrincipalByInfo());112 Principal result = (Principal) (new RequestWrappers(this)).wrapRequestResource(params, new GetPrincipalByInfo()); 101 113 return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal())); 102 114 } 103 115 116 117 private class GetPrincipalByInfo implements ILambda<Map, Principal> { 118 119 @Override 120 public Principal apply(Map params) throws NotInDataBaseException { 121 return dbIntegrityService.getPrincipalByInfo((String) params.get("email")); 122 } 123 } 124 125 //////////////////////////////////////// 104 126 @GET 105 127 @Produces(MediaType.TEXT_XML) … … 110 132 params.put("externalId", externalIdentifier); 111 133 params.put("resource", this); 112 CurrentPrincipalInfo result = (CurrentPrincipalInfo) (new RequestWrappers( loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new GetCurrentPrincipalInfo());134 CurrentPrincipalInfo result = (CurrentPrincipalInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCurrentPrincipalInfo()); 113 135 return (result != null) ? (new ObjectFactory().createCurrentPrincipalInfo(result)) : (new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo())); 114 136 } 137 138 private class GetCurrentPrincipalInfo implements ILambda<Map, CurrentPrincipalInfo> { 139 140 @Override 141 public CurrentPrincipalInfo apply(Map params) throws NotInDataBaseException { 142 final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL); 143 final CurrentPrincipalInfo principalInfo = new CurrentPrincipalInfo(); 144 principalInfo.setRef(dbIntegrityService.getResourceURI(principalID, Resource.PRINCIPAL)); 145 principalInfo.setCurrentPrincipal(((PrincipalResource) params.get("resource")).ifLoggedIn(principalID)); 146 return principalInfo; 147 } 148 } 149 ////////////////////////////// 115 150 116 151 @POST … … 133 168 134 169 } 170 171 /////////////////////////////////////// 135 172 136 173 @POST … … 150 187 151 188 if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { 152 return (new RequestWrappers( loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new AddPrincipal());189 return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new AddPrincipal()); 153 190 } else { 154 191 verboseOutput.ADMIN_RIGHTS_EXPECTED(); … … 158 195 159 196 } 197 198 private class AddPrincipal implements ILambdaPrincipal<Map, Principal> { 199 200 @Override 201 public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists { 202 final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId")); 203 return dbIntegrityService.getPrincipal(principalID); 204 } 205 } 206 207 208 ///////////////////////////////////// 160 209 161 210 @POST … … 177 226 178 227 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 179 return (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal()); 180 } 228 return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal()); 229 } 230 231 private class RegisterNonShibbolizedPrincipal implements ILambdaPrincipal<Map, Principal> { 232 233 @Override 234 public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists { 235 final int updatedSpringTables = dbIntegrityService.addSpringUser((String) params.get("remoteId"), (String) params.get("password"), (Integer) params.get("shaStrength"), (String) params.get("remoteId")); 236 final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId")); 237 return dbIntegrityService.getPrincipal(principalID); 238 } 239 } 240 241 242 /////////////////////////////////////////////// 181 243 182 244 @POST … … 196 258 197 259 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 198 return (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapAddPrincipalRequest(params, new AddPrincipal()); 199 } 260 return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new AddPrincipal()); 261 } 262 263 /////////////////////////////////////////////////// 200 264 201 265 @POST … … 219 283 return new ObjectFactory().createPrincipal(new Principal()); 220 284 } 221 Map params = new HashMap<String, Object>(); 222 params.put("principal", principal); 223 224 if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { 225 Principal result = (Principal) (new RequestWrappers(loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new UpdatePrincipal()); 285 if (dbIntegrityService.getTypeOfPrincipalAccount(remotePrincipalID).equals(admin)) { 286 Map params = new HashMap<String, Object>(); 287 params.put("principal", principal); 288 Principal result = (Principal) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipal()); 226 289 return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal())); 227 290 … … 232 295 } 233 296 } 297 /////////////////////////////////// 234 298 235 299 @POST … … 239 303 public JAXBElement<Principal> updatePrincipalFromForm(@FormParam("name") String name, @FormParam("email") String email) 240 304 throws IOException { 241 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 242 Number remotePrincipalID = this.getPrincipalID(); 243 if (remotePrincipalID == null) { 244 return new ObjectFactory().createPrincipal(new Principal()); 245 } 246 247 String principalURI = dbIntegrityService.getResourceURI(remotePrincipalID, Resource.PRINCIPAL); 305 248 306 Principal newPrincipal = new Principal(); 249 newPrincipal.setURI(principalURI);250 307 newPrincipal.setDisplayName(name); 251 308 newPrincipal.setEMail(email); 252 309 Map params = new HashMap<String, Object>(); 253 310 params.put("newPrincipal", newPrincipal); 254 Principal result = (Principal) (new RequestWrappers( loggerServer, httpServletResponse, this, dbIntegrityService)).wrapRequestResource(params, new UpdatePrincipal());311 Principal result = (Principal) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipal()); 255 312 return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal())); 256 313 } 314 315 316 private class UpdatePrincipal implements ILambda<Map, Principal> { 317 @Override 318 public Principal apply(Map params) throws NotInDataBaseException { 319 Principal principal = (Principal) params.get("newPrincipal"); 320 Number principalID = (Number) params.get("principalID"); 321 String uri = dbIntegrityService.getResourceURI(principalID, Resource.PRINCIPAL); 322 principal.setURI(uri); 323 Number principalIDupd = dbIntegrityService.updatePrincipal(principal); 324 return dbIntegrityService.getPrincipal(principalID); 325 } 326 } 327 328 ////////////////////////////////////// 257 329 258 330 @PUT … … 323 395 } 324 396 325 /////////////// main working methods that call db-services and sent as aparameters to correponding wrappers/////326 private class RegisterNonShibbolizedPrincipal implements ILambdaPrincipal<Map, Principal> {327 328 @Override329 public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {330 final int updatedSpringTables = dbIntegrityService.addSpringUser((String) params.get("remoteId"), (String) params.get("password"), (Integer) params.get("shaStrength"), (String) params.get("remoteId"));331 final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId"));332 return dbIntegrityService.getPrincipal(principalID);333 }334 }335 336 private class AddPrincipal implements ILambdaPrincipal<Map, Principal> {337 338 @Override339 public Principal apply(Map params) throws NotInDataBaseException, PrincipalExists {340 final Number principalID = dbIntegrityService.addPrincipal((Principal) params.get("newPrincipal"), (String) params.get("remoteId"));341 return dbIntegrityService.getPrincipal(principalID);342 }343 }344 345 private class UpdatePrincipal implements ILambda<Map, Principal> {346 347 @Override348 public Principal apply(Map params) throws NotInDataBaseException {349 Principal principal = (Principal) params.get("newPrincipal");350 Number principalID = dbIntegrityService.getResourceInternalIdentifierFromURI(principal.getURI(), Resource.PRINCIPAL);351 Number principalIDupd = dbIntegrityService.updatePrincipal(principal);352 return dbIntegrityService.getPrincipal(principalID);353 }354 }355 356 private class GetPrincipal implements ILambda<Map, Principal> {357 358 @Override359 public Principal apply(Map params) throws NotInDataBaseException {360 final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);361 return dbIntegrityService.getPrincipal(principalID);362 }363 }364 365 private class GetPrincipalByInfo implements ILambda<Map, Principal> {366 367 @Override368 public Principal apply(Map params) throws NotInDataBaseException {369 return dbIntegrityService.getPrincipalByInfo((String) params.get("email"));370 }371 }372 373 private class GetCurrentPrincipalInfo implements ILambda<Map, CurrentPrincipalInfo> {374 375 @Override376 public CurrentPrincipalInfo apply(Map params) throws NotInDataBaseException {377 final Number principalID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString((String) params.get("externalId")), Resource.PRINCIPAL);378 final CurrentPrincipalInfo principalInfo = new CurrentPrincipalInfo();379 principalInfo.setRef(dbIntegrityService.getResourceURI(principalID, Resource.PRINCIPAL));380 principalInfo.setCurrentPrincipal(((PrincipalResource) params.get("resource")).ifLoggedIn(principalID));381 return principalInfo;382 }383 }384 397 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java
r5053 r5086 21 21 import eu.dasish.annotation.backend.PrincipalExists; 22 22 import eu.dasish.annotation.backend.Resource; 23 import eu.dasish.annotation.backend. dao.DBIntegrityService;23 import eu.dasish.annotation.backend.ResourceAction; 24 24 import eu.dasish.annotation.backend.dao.ILambda; 25 25 import eu.dasish.annotation.backend.dao.ILambdaPrincipal; 26 import eu.dasish.annotation.schema.Access;27 import eu.dasish.annotation.schema.Annotation;28 26 import eu.dasish.annotation.schema.ObjectFactory; 29 27 import eu.dasish.annotation.schema.Principal; … … 33 31 import javax.servlet.http.HttpServletResponse; 34 32 import javax.xml.bind.JAXBElement; 35 import org.slf4j.Logger;36 33 37 34 /** … … 41 38 public class RequestWrappers<T> { 42 39 43 Logger loggerServer; 44 HttpServletResponse httpServletResponse; 45 ResourceResource resourceResource; 46 DBIntegrityService dbIntegrityService; 40 41 private ResourceResource resourceResource; 42 private String _internalID = "internalID"; 43 private String _principalID = "principalID"; 44 private String _externalId = "externalID"; 45 private String _resourceType = "resourceType"; 47 46 48 public RequestWrappers(Logger loggerServer, HttpServletResponse httpServletResponse, ResourceResource resourceResource, DBIntegrityService dbIntegrityService) { 49 this.loggerServer = loggerServer; 50 this.httpServletResponse = httpServletResponse; 47 public RequestWrappers(ResourceResource resourceResource) { 51 48 this.resourceResource = resourceResource; 52 this.dbIntegrityService = dbIntegrityService;53 49 } 54 50 55 51 public String FORBIDDEN_RESOURCE_ACTION(String identifier, String resource, String action) { 56 52 return " The logged-in principal cannot " + action + " the " + resource + " with the identifier " + identifier; … … 62 58 return null; 63 59 } 60 params.put(_principalID, remotePrincipalID); 64 61 try { 65 62 return dbRequestor.apply(params); 66 63 } catch (NotInDataBaseException e1) { 67 loggerServer.debug(e1.toString());68 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());64 resourceResource.loggerServer.debug(e1.toString()); 65 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString()); 69 66 return null; 70 67 } 71 68 } 72 69 73 74 public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, Access access, String externalId, String action) throws IOException { 70 public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId, boolean isUri) throws IOException { 75 71 Number principalID = resourceResource.getPrincipalID(); 76 72 if (principalID == null) { 77 73 return null; 78 74 } 75 params.put(_principalID, principalID); 79 76 try { 80 final Number resourceID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), resource); 81 if (dbIntegrityService.canDo(Access.READ, principalID, resourceID)) { 77 final Number resourceID; 78 if (isUri) { 79 resourceID = resourceResource.dbIntegrityService.getResourceInternalIdentifierFromURI(externalId, resource); 80 params.put(_externalId, resourceResource.dbIntegrityService.getResourceExternalIdentifier(resourceID, resource).toString()); 81 } else { 82 resourceID = resourceResource.dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalId), resource); 83 params.put(_externalId, externalId); 84 } 85 if (resourceResource.dbIntegrityService.canDo(action, principalID, resourceID, resource)) { 86 params.put(_internalID, resourceID); 87 params.put(_resourceType, resource); 82 88 return dbRequestor.apply(params); 83 89 } else { 84 this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action );85 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);90 this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name()); 91 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN); 86 92 return null; 87 93 } 88 94 } catch (NotInDataBaseException e2) { 89 loggerServer.debug(e2.toString());90 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());95 resourceResource.loggerServer.debug(e2.toString()); 96 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 91 97 return null; 92 98 } … … 94 100 95 101 public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException { 96 102 97 103 try { 98 104 try { 99 105 return new ObjectFactory().createPrincipal(dbRequestor.apply(params)); 100 106 } catch (NotInDataBaseException e1) { 101 loggerServer.debug(e1.toString());102 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());107 resourceResource.loggerServer.debug(e1.toString()); 108 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString()); 103 109 return new ObjectFactory().createPrincipal(new Principal()); 104 110 } 105 111 } catch (PrincipalExists e) { 106 loggerServer.debug(e.toString());107 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());112 resourceResource.loggerServer.debug(e.toString()); 113 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 108 114 return new ObjectFactory().createPrincipal(new Principal()); 109 115 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/ResourceResource.java
r5030 r5086 61 61 protected String anonym = "anonymous"; 62 62 protected String defaultAccess = "read"; 63 63 64 protected String[] admissibleAccess = {"read", "write", "owner"}; 64 65 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java
r4941 r5086 23 23 import eu.dasish.annotation.backend.NotInDataBaseException; 24 24 import eu.dasish.annotation.backend.Resource; 25 import eu.dasish.annotation.backend.ResourceAction; 26 import eu.dasish.annotation.backend.dao.ILambda; 25 27 import eu.dasish.annotation.schema.CachedRepresentationInfo; 26 28 import eu.dasish.annotation.schema.ObjectFactory; … … 30 32 import java.io.InputStream; 31 33 import java.sql.SQLException; 34 import java.util.HashMap; 35 import java.util.Map; 32 36 import java.util.UUID; 33 37 import javax.servlet.http.HttpServletRequest; … … 74 78 @Transactional(readOnly = true) 75 79 public JAXBElement<Target> getTarget(@PathParam("targetid") String externalIdentifier) throws IOException { 76 Number remotePrincipalID = this.getPrincipalID(); 77 if (remotePrincipalID == null) { 80 Map params = new HashMap(); 81 Target result = (Target) (new RequestWrappers(this)).wrapRequestResource(params, new GetTarget(), Resource.TARGET, ResourceAction.READ, externalIdentifier, false); 82 if (result != null) { 83 return new ObjectFactory().createTarget(result); 84 } else { 78 85 return new ObjectFactory().createTarget(new Target()); 79 86 } 80 try { 81 final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.TARGET); 82 final Target target = dbIntegrityService.getTarget(targetID); 83 return new ObjectFactory().createTarget(target); 84 } catch (NotInDataBaseException e1) { 85 loggerServer.debug(e1.toString()); 86 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.toString()); 87 return new ObjectFactory().createTarget(new Target()); 88 } 89 90 } 91 92 // TODOD both unit tests 87 } 88 89 private class GetTarget implements ILambda<Map, Target> { 90 91 @Override 92 public Target apply(Map params) throws NotInDataBaseException { 93 Number targetID = (Number) params.get("internalID"); 94 return dbIntegrityService.getTarget(targetID); 95 } 96 } 97 98 ///////////////////////////////////////////////// 93 99 @GET 94 100 @Produces(MediaType.TEXT_XML) … … 96 102 @Transactional(readOnly = true) 97 103 public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String externalIdentifier) throws HTTPException, IOException { 98 Number remotePrincipalID = this.getPrincipalID(); 99 if (remotePrincipalID == null) { 104 Map params = new HashMap(); 105 ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetSiblingTargets(), Resource.TARGET, ResourceAction.READ, externalIdentifier, false); 106 if (result != null) { 107 return new ObjectFactory().createReferenceList(result); 108 } else { 100 109 return new ObjectFactory().createReferenceList(new ReferenceList()); 101 110 } 102 try {103 final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.TARGET); 104 final ReferenceList siblings = dbIntegrityService.getTargetsForTheSameLinkAs(targetID);105 return new ObjectFactory().createReferenceList(siblings); 106 } catch (NotInDataBaseException e2) {107 loggerServer.debug(e2.toString());108 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());109 return new ObjectFactory().createReferenceList(new ReferenceList());110 } 111 112 } 113 111 } 112 113 private class GetSiblingTargets implements ILambda<Map, ReferenceList> { 114 115 @Override 116 public ReferenceList apply(Map params) throws NotInDataBaseException { 117 Number targetID = (Number) params.get("internalID"); 118 return dbIntegrityService.getTargetsForTheSameLinkAs(targetID); 119 } 120 } 121 122 //////////////////////////////////////////////// 114 123 @POST 115 124 @Consumes("multipart/mixed") … … 119 128 @PathParam("fragmentDescriptor") String fragmentDescriptor, 120 129 MultiPart multiPart) throws IOException { 121 Number remotePrincipalID = this.getPrincipalID(); 122 if (remotePrincipalID == null) { 130 131 Map params = new HashMap(); 132 params.put("cachedInfo", multiPart.getBodyParts().get(0).getEntityAs(CachedRepresentationInfo.class)); 133 BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(1).getEntity(); 134 params.put("cachedBlob", bpe.getInputStream()); 135 params.put("fragmentDescriptor", fragmentDescriptor); 136 137 CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new PostCached(), Resource.TARGET, ResourceAction.WRITE_W_METAINFO, targetIdentifier, false); 138 if (result != null) { 139 return new ObjectFactory().createCashedRepresentationInfo(result); 140 } else { 123 141 return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); 124 142 } 125 try { 126 final Number targetID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(targetIdentifier), Resource.TARGET); 127 CachedRepresentationInfo metadata = multiPart.getBodyParts().get(0).getEntityAs(CachedRepresentationInfo.class); 128 BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(1).getEntity(); 129 InputStream cachedSource = bpe.getInputStream(); 143 } 144 145 private class PostCached implements ILambda<Map, CachedRepresentationInfo> { 146 147 @Override 148 public CachedRepresentationInfo apply(Map params) throws NotInDataBaseException { 149 Number targetID = (Number) params.get("internalID"); 150 String fragmentDescriptor = (String) params.get("fragmentDescriptor"); 151 CachedRepresentationInfo metadata = (CachedRepresentationInfo) params.get("cachedInfo"); 152 InputStream cachedSource = (InputStream) params.get("cachedBlob"); 130 153 try { 131 final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource); 132 final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(respondDB[1]); 133 return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); 134 135 } catch (NotInDataBaseException e) { 136 loggerServer.debug(e.toString()); 137 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 138 return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); 154 final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource); 155 return dbIntegrityService.getCachedRepresentationInfo(respondDB[1]); 156 } catch (IOException e) { 157 loggerServer.info(e.toString()); 158 return null; 139 159 } 140 } catch (NotInDataBaseException e2) { 141 loggerServer.debug(e2.toString()); 142 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 143 return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo()); 144 } 145 } 146 160 } 161 } 162 163 //////////////////////////////////////////////// 147 164 @PUT 148 165 @Produces(MediaType.APPLICATION_XML) … … 163 180 loggerServer.debug(e1.toString()); 164 181 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.toString()); 165 return "Nothing is updated: " +e1;182 return "Nothing is updated: " + e1; 166 183 } 167 184 } catch (NotInDataBaseException e2) { 168 185 loggerServer.debug(e2.toString()); 169 186 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString()); 170 return "Nothing is updated: " +e2;187 return "Nothing is updated: " + e2; 171 188 } 172 189 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceTest.java
r4995 r5086 797 797 798 798 final UUID mockNewTargetUUID = UUID.randomUUID(); 799 final NotInDataBaseException e = new NotInDataBaseException( tempTargetID);799 final NotInDataBaseException e = new NotInDataBaseException("target", "external ID", tempTargetID); 800 800 801 801 mockeryDao.checking(new Expectations() { … … 1679 1679 1680 1680 final Annotation annotation = (new TestInstances(TestBackendConstants._TEST_SERVLET_URI)).getAnnotationOne(); 1681 final NotInDataBaseException e = new NotInDataBaseException(" 00000000-0000-0000-0000-000000000031");1681 final NotInDataBaseException e = new NotInDataBaseException("annotation", "external ID", "00000000-0000-0000-0000-000000000031"); 1682 1682 final String mockTempID = "00000000-0000-0000-0000-000000000031"; 1683 1683 final UUID mockNewID = UUID.randomUUID(); … … 1758 1758 } 1759 1759 1760 @Test 1761 public void testUpdateHeadline() throws NotInDataBaseException { 1762 1763 System.out.println("test updateAnnotation"); 1764 1765 final Annotation annotation = (new TestInstances(TestBackendConstants._TEST_SERVLET_URI)).getAnnotationOne(); 1766 1767 1768 System.out.println("test updateAnnotation"); 1769 mockeryDao.checking(new Expectations() { 1770 { 1771 oneOf(annotationDao).getInternalIDFromURI(annotation.getURI()); 1772 will(returnValue(1)); 1773 1774 oneOf(principalDao).getInternalIDFromURI(annotation.getOwnerRef()); 1775 will(returnValue(1)); 1776 1777 oneOf(annotationDao).updateAnnotationHeadline(1, "new Headline"); 1778 will(returnValue(1)); 1779 1780 1781 } 1782 }); 1783 assertEquals(1, dbIntegrityService.updateAnnotationHeadline(1, "new Headline")); 1784 } 1785 1760 1786 // public int updateAnnotationPrincipalAccess(Number annotationID, Number principalID, Access access) { 1761 1787 // int result; -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java
r4886 r5086 38 38 import javax.xml.parsers.ParserConfigurationException; 39 39 import static org.junit.Assert.*; 40 import org.junit.Ignore;41 40 import org.junit.Test; 42 41 import org.junit.runner.RunWith; … … 447 446 assertEquals("test", check2.getBody().getXmlBody().getAny().getTextContent()); 448 447 assertEquals("application/xml", check2.getBody().getXmlBody().getMimeType()); 448 } 449 450 @Test 451 public void testUpdateAnnotationHeader() { 452 System.out.println("test updateAnnotationHeader "); 453 int result = jdbcAnnotationDao.updateAnnotationHeadline(1, "new Header"); 454 assertEquals(1, result); 455 Annotation check = jdbcAnnotationDao.getAnnotationWithoutTargetsAndPemissions(1); 456 assertEquals("new Header", check.getHeadline()); 449 457 } 450 458 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.java
r4941 r5086 20 20 import eu.dasish.annotation.backend.NotInDataBaseException; 21 21 import eu.dasish.annotation.backend.Resource; 22 import eu.dasish.annotation.backend.ResourceAction; 22 23 import eu.dasish.annotation.backend.dao.DBIntegrityService; 23 24 import eu.dasish.annotation.backend.TestBackendConstants; … … 115 116 116 117 117 oneOf(mockDbIntegrityService).canDo( Access.READ, 3, 1);118 oneOf(mockDbIntegrityService).canDo(ResourceAction.READ, 3, 1, Resource.ANNOTATION); 118 119 will(returnValue(true)); 119 120 … … 164 165 oneOf(mockDbIntegrityService).getAnnotationOwnerID(4); 165 166 will(returnValue(3)); 167 168 oneOf(mockDbIntegrityService).canDo(ResourceAction.DELETE, 3, 4, Resource.ANNOTATION); 169 will(returnValue(true)); 166 170 167 171 oneOf(mockDbIntegrityService).deleteAnnotation(4); … … 172 176 173 177 String result = annotationResource.deleteAnnotation("00000000-0000-0000-0000-000000000024"); 174 assertEquals("1 annotation(s) deleted.", result);178 assertEquals("1 annotation(s) is(are) deleted.", result); 175 179 } 176 180 … … 296 300 will(returnValue(1)); 297 301 302 oneOf(mockDbIntegrityService).canDo(ResourceAction.WRITE_W_METAINFO, 1, 1, Resource.ANNOTATION); 303 will(returnValue(true)); 304 298 305 oneOf(mockDbIntegrityService).updateAnnotation(annotation); 299 306 will(returnValue(1)); … … 363 370 will(returnValue(1)); 364 371 365 oneOf(mockDbIntegrityService).canDo(Access.WRITE, 1, 1); 366 will(returnValue(true)); 372 oneOf(mockDbIntegrityService).canDo(ResourceAction.WRITE, 1, 1, Resource.ANNOTATION); 373 will(returnValue(true)); 374 367 375 368 376 oneOf(mockDbIntegrityService).updateAnnotationBody(1, ab); … … 382 390 assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody()); 383 391 } 392 393 @Test 394 public void testUpdateAnnotationHeadline() throws NotInDataBaseException, IOException{ 395 System.out.println("test updateAnnotationHeadline"); 396 397 Annotation annotation = (new TestInstances(TestBackendConstants._TEST_SERVLET_URI)).getAnnotationOne(); 398 399 final String newHeadline = "new Headline"; 400 annotation.setHeadline(newHeadline); 401 402 final ResponseBody mockEnvelope = new ResponseBody(); 403 final ActionList actionList = new ActionList(); 404 mockEnvelope.setAnnotation(annotation); 405 mockEnvelope.setActionList(actionList); 406 407 annotationResource.setHttpServletRequest(mockRequest); 408 annotationResource.setUriInfo(mockUriInfo); 409 mockRequest.setRemoteUser("twagoo@mpi.nl"); 410 final URI baseUri = URI.create(TestBackendConstants._TEST_SERVLET_URI); 411 412 final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021"); 413 414 415 mockeryRest.checking(new Expectations() { 416 { 417 oneOf(mockUriInfo).getBaseUri(); 418 will(returnValue(baseUri)); 419 420 oneOf(mockDbIntegrityService).setServiceURI(baseUri.toString()); 421 422 oneOf(mockDbIntegrityService).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl"); 423 will(returnValue(1)); 424 425 426 oneOf(mockDbIntegrityService).getResourceInternalIdentifier(externalId, Resource.ANNOTATION); 427 will(returnValue(1)); 428 429 oneOf(mockDbIntegrityService).canDo(ResourceAction.WRITE, 1, 1, Resource.ANNOTATION); 430 will(returnValue(true)); 431 432 433 oneOf(mockDbIntegrityService).updateAnnotationHeadline(1, newHeadline); 434 will(returnValue(1)); 435 436 oneOf(mockDbIntegrityService).makeAnnotationResponseEnvelope(1); 437 will(returnValue(mockEnvelope)); 438 439 } 440 }); 441 442 443 444 JAXBElement<ResponseBody> result = annotationResource.updateAnnotationHeadline("00000000-0000-0000-0000-000000000021", newHeadline); 445 Annotation newAnnotation = result.getValue().getAnnotation(); 446 assertEquals("new Headline", newAnnotation.getHeadline()); 447 } 384 448 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java
r4956 r5086 173 173 ClientResponse response = responseBuilder.delete(ClientResponse.class); 174 174 assertEquals(200, response.getStatus()); 175 assertEquals("1 annotation(s) deleted.", response.getEntity(String.class));175 assertEquals("1 annotation(s) is(are) deleted.", response.getEntity(String.class)); 176 176 } 177 177 … … 301 301 302 302 } 303 304 @Test 305 public void testUpdateAnnotationHeadline() throws IOException{ 306 307 308 // Authentication 309 Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); 310 ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); 311 assertEquals(200, responseAu.getStatus()); 312 313 314 // updating annotation 315 System.out.println("test updateAnnotationHeadline"); 316 System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021/headline"); 317 String newHeadline = "new Headline"; 318 Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021/headline")).type(MediaType.TEXT_PLAIN).accept(MediaType.APPLICATION_XML); 319 ClientResponse response = responseBuilder.put(ClientResponse.class, newHeadline); 320 assertEquals(200, response.getStatus()); 321 322 ResponseBody entity = response.getEntity(ResponseBody.class); 323 Annotation entityA = entity.getAnnotation(); 324 assertEquals("new Headline", entityA.getHeadline()); 325 } 303 326 304 327 protected Builder getAuthenticatedResource(WebResource resource) {
Note: See TracChangeset
for help on using the changeset viewer.