Changeset 4217
- Timestamp:
- 01/06/14 17:20:05 (10 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/CHANGES.txt
r4211 r4217 1 1 January 2, 2014: the first deployment on lux16. 2 2 3 January 3, 2014: files README, ISNTALL, UPDATE, CHANGES are added to the deployment tar-ball which 4 is from now on is generated using maven assembly plug-in 3 January 3, 2014: files README, INSTALL, UPDATE, CHANGES are added to the 4 deployment tar-ball which is from now on is generated using maven assembly 5 plug-in. -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/INSTALL.txt
r4211 r4217 21 21 = Installation and configuration = 22 22 ========================================================================================= 23 == Pla sing war-file and ds#webannotator.xml at the tomcat's conf/Catalina/localhost ==23 == Placing war-file and ds#webannotator.xml at the tomcat's conf/Catalina/localhost == 24 24 25 25 You must deploy the annotator's .war file to the tomcat-webuser. Make a directory for the … … 42 42 43 43 <Resource 44 name="jdbc/DASISH "44 name="jdbc/DASISHAnnotator" 45 45 auth="Container" 46 46 type="javax.sql.DataSource" -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/README.txt
r4211 r4217 8 8 The DASISH web-annotator back-end consists of two parts: the database, where the 9 9 annotations are stored, and the Jerseys web-application that by means of REST requests 10 performs authentified re d/write access to the database.10 performs authentified read/write access to the database. 11 11 12 12 === Requests === -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java
r4209 r4217 80 80 @Context 81 81 private Providers providers; 82 83 84 82 final String default_permission = "reader"; 85 83 private static final Logger logger = LoggerFactory.getLogger(AnnotationResource.class); … … 97 95 } 98 96 99 100 101 97 public void setProviders(Providers providers) { 102 98 this.providers = providers; … … 116 112 dbIntegrityService.setServiceURI(baseURIstr); 117 113 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier)); 118 String remoteUser = httpServletRequest.getRemoteUser(); 119 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); 120 if (canRead(userID, annotationID)) { 121 final Annotation annotation = dbIntegrityService.getAnnotation(annotationID); 122 JAXBElement<Annotation> rootElement = new ObjectFactory().createAnnotation(annotation); 123 logger.info("getAnnotation method: OK"); 124 return rootElement; 125 } else { 126 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 114 if (annotationID != null) { 115 String remoteUser = httpServletRequest.getRemoteUser(); 116 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); 117 if (canRead(userID, annotationID)) { 118 final Annotation annotation = dbIntegrityService.getAnnotation(annotationID); 119 JAXBElement<Annotation> rootElement = new ObjectFactory().createAnnotation(annotation); 120 logger.info("getAnnotation method: OK"); 121 return rootElement; 122 } else { 123 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 124 return null; 125 } 126 } else { 127 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 127 128 return null; 128 129 } … … 139 140 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 140 141 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier)); 141 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 142 if (canRead(userID, annotationID)) { 143 final ReferenceList TargetList = dbIntegrityService.getAnnotationTargets(annotationID); 144 logger.info("getAnnotationTargets method: OK"); 145 return new ObjectFactory().createTargetList(TargetList); 146 } else { 147 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 142 if (annotationID != null) { 143 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 144 if (canRead(userID, annotationID)) { 145 final ReferenceList TargetList = dbIntegrityService.getAnnotationTargets(annotationID); 146 logger.info("getAnnotationTargets method: OK"); 147 return new ObjectFactory().createTargetList(TargetList); 148 } else { 149 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 150 return null; 151 } 152 } else { 153 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 148 154 return null; 149 155 } … … 183 189 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier)); 184 190 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 185 if (canRead(userID, annotationID)) { 186 final UserWithPermissionList permissionList = dbIntegrityService.getPermissionsForAnnotation(annotationID); 187 logger.info("getAnnotationPermissions method: OK"); 188 return new ObjectFactory().createPermissionList(permissionList); 189 } else { 190 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 191 return null; 192 } 193 191 if (annotationID != null) { 192 if (canRead(userID, annotationID)) { 193 final UserWithPermissionList permissionList = dbIntegrityService.getPermissionsForAnnotation(annotationID); 194 logger.info("getAnnotationPermissions method: OK"); 195 return new ObjectFactory().createPermissionList(permissionList); 196 } else { 197 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot read the annotation."); 198 return null; 199 } 200 } else { 201 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 202 return null; 203 } 194 204 } 195 205 … … 203 213 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier)); 204 214 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 205 if (isOwner(userID, annotationID)) { 206 int[] resultDelete = dbIntegrityService.deleteAnnotation(annotationID); 207 String result = Integer.toString(resultDelete[0]); 208 logger.info("deleteAnnotation method: OK"); 209 return result + " annotation(s) deleted."; 210 } else { 211 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot delete the annotation. Only the owner can delete the annotation."); 212 return null; 213 } 214 215 if (annotationID != null) { 216 if (isOwner(userID, annotationID)) { 217 int[] resultDelete = dbIntegrityService.deleteAnnotation(annotationID); 218 String result = Integer.toString(resultDelete[0]); 219 logger.info("deleteAnnotation method: OK"); 220 return result + " annotation(s) deleted."; 221 } else { 222 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user cannot delete the annotation. Only the owner can delete the annotation."); 223 return null; 224 } 225 } else { 226 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 227 return null; 228 } 215 229 } 216 230 … … 246 260 } 247 261 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier)); 248 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 249 if (canWrite(userID, annotationID)) { 250 int updatedRows = dbIntegrityService.updateUsersAnnotation(userID, annotation); 251 logger.info("updateAnnotation method: OK"); 252 return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID)); 253 254 } else { 255 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 256 return null; 257 } 258 262 if (annotationID != null) { 263 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 264 if (canWrite(userID, annotationID)) { 265 int updatedRows = dbIntegrityService.updateUsersAnnotation(userID, annotation); 266 logger.info("updateAnnotation method: OK"); 267 return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID)); 268 269 } else { 270 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 271 return null; 272 } 273 } else { 274 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 275 return null; 276 } 259 277 } 260 278 … … 270 288 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier)); 271 289 final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 272 if (canWrite(userID, annotationID)) { 273 int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody); 274 logger.info("updateAnnotationBody method: OK"); 275 return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID)); 276 } else { 277 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 278 return null; 279 } 280 290 if (annotationID != null) { 291 if (canWrite(userID, annotationID)) { 292 int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody); 293 logger.info("updateAnnotationBody method: OK"); 294 return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID)); 295 } else { 296 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 297 return null; 298 } 299 } else { 300 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 301 return null; 302 } 281 303 } 282 304 … … 291 313 final Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 292 314 final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(userExternalId)); 293 if (isOwner(remoteUserID, annotationID)) { 294 int result = (dbIntegrityService.getPermission(annotationID, userID) != null) 295 ? dbIntegrityService.updateAnnotationPrincipalPermission(annotationID, userID, permission) 296 : dbIntegrityService.addAnnotationPrincipalPermission(annotationID, userID, permission); 297 logger.info("updatePermission method: OK"); 298 return result + " rows are updated/added"; 299 300 } else { 301 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 315 if (annotationID != null) { 316 if (isOwner(remoteUserID, annotationID)) { 317 int result = (dbIntegrityService.getPermission(annotationID, userID) != null) 318 ? dbIntegrityService.updateAnnotationPrincipalPermission(annotationID, userID, permission) 319 : dbIntegrityService.addAnnotationPrincipalPermission(annotationID, userID, permission); 320 logger.info("updatePermission method: OK"); 321 return result + " rows are updated/added"; 322 323 } else { 324 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 325 return null; 326 } 327 } else { 328 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 302 329 return null; 303 330 } … … 313 340 final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId)); 314 341 final Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser()); 315 if (isOwner(remoteUserID, annotationID)) { 316 int updatedRows = dbIntegrityService.updatePermissions(annotationID, permissions); 317 logger.info("updatePermissions method: OK"); 318 return new ObjectFactory().createResponseBody(makePermissionResponseEnvelope(annotationID)); 319 } else { 320 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 342 if (annotationID != null) { 343 if (isOwner(remoteUserID, annotationID)) { 344 int updatedRows = dbIntegrityService.updatePermissions(annotationID, permissions); 345 logger.info("updatePermissions method: OK"); 346 return new ObjectFactory().createResponseBody(makePermissionResponseEnvelope(annotationID)); 347 } else { 348 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED); 349 return null; 350 } 351 } else { 352 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The annotation with the given id is not found in the database"); 321 353 return null; 322 354 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java
r4207 r4217 29 29 import javax.imageio.ImageIO; 30 30 import javax.servlet.http.HttpServletRequest; 31 import javax.servlet.http.HttpServletResponse; 31 32 import javax.ws.rs.GET; 32 33 import javax.ws.rs.Path; … … 49 50 @Component 50 51 @Path("/cached") 51 @Transactional(rollbackFor ={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})52 @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) 52 53 public class CachedRepresentationResource { 53 54 … … 56 57 @Context 57 58 private HttpServletRequest httpServletRequest; 59 @Context 60 private HttpServletResponse httpServletResponse; 58 61 @Context 59 62 private UriInfo uriInfo; … … 68 71 @Produces(MediaType.TEXT_XML) 69 72 @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata") 70 @Secured("ROLE_USER") 71 @Transactional(readOnly =true)72 public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException {73 @Secured("ROLE_USER") 74 @Transactional(readOnly = true) 75 public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException, IOException { 73 76 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 74 77 final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId)); 75 final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID); 76 return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); 78 if (cachedID != null) { 79 final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(cachedID); 80 return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); 81 } else { 82 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database"); 83 return null; 84 } 77 85 } 78 86 … … 80 88 @Produces({"image/jpeg", "image/png"}) 81 89 @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content") 82 @Secured("ROLE_USER") 83 @Transactional(readOnly =true)90 @Secured("ROLE_USER") 91 @Transactional(readOnly = true) 84 92 public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException { 85 93 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 86 94 final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(externalId)); 87 InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID); 88 ImageIO.setUseCache(false); 89 BufferedImage result = ImageIO.read(dbRespond); 90 return result; 95 if (cachedID != null) { 96 InputStream dbRespond = dbIntegrityService.getCachedRepresentationBlob(cachedID); 97 ImageIO.setUseCache(false); 98 BufferedImage result = ImageIO.read(dbRespond); 99 return result; 100 } else { 101 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database"); 102 return null; 103 } 91 104 } 92 93 94 105 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java
r4207 r4217 31 31 import java.util.UUID; 32 32 import javax.servlet.http.HttpServletRequest; 33 import javax.servlet.http.HttpServletResponse; 33 34 import javax.ws.rs.Consumes; 34 35 import javax.ws.rs.DELETE; … … 58 59 @Component 59 60 @Path("/targets") 60 @Transactional(rollbackFor ={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})61 @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) 61 62 public class TargetResource { 62 63 … … 65 66 @Context 66 67 private HttpServletRequest httpServletRequest; 68 @Context 69 private HttpServletResponse httpServletResponse; 67 70 @Context 68 71 private UriInfo uriInfo; … … 79 82 @Produces(MediaType.TEXT_XML) 80 83 @Path("{targetid: " + BackendConstants.regExpIdentifier + "}") 81 @Secured("ROLE_USER") 82 @Transactional(readOnly =true)83 public JAXBElement<Target> getTarget(@PathParam("targetid") String ExternalIdentifier) throws SQLException {84 @Secured("ROLE_USER") 85 @Transactional(readOnly = true) 86 public JAXBElement<Target> getTarget(@PathParam("targetid") String ExternalIdentifier) throws SQLException, IOException { 84 87 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 85 88 final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(ExternalIdentifier)); 86 final Target target = dbIntegrityService.getTarget(targetID); 87 return new ObjectFactory().createTarget(target); 89 if (targetID != null) { 90 final Target target = dbIntegrityService.getTarget(targetID); 91 return new ObjectFactory().createTarget(target); 92 } else { 93 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database"); 94 return null; 95 } 88 96 } 89 97 … … 92 100 @Produces(MediaType.TEXT_XML) 93 101 @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/versions") 94 @Secured("ROLE_USER") 95 @Transactional(readOnly =true)96 public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String ExternalIdentifier) throws SQLException {102 @Secured("ROLE_USER") 103 @Transactional(readOnly = true) 104 public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String ExternalIdentifier) throws SQLException, IOException { 97 105 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 98 106 final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(ExternalIdentifier)); 99 final ReferenceList siblings = dbIntegrityService.getTargetsForTheSameLinkAs(targetID); 100 return new ObjectFactory().createReferenceList(siblings); 107 if (targetID != null) { 108 final ReferenceList siblings = dbIntegrityService.getTargetsForTheSameLinkAs(targetID); 109 return new ObjectFactory().createReferenceList(siblings); 110 } else { 111 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database"); 112 return null; 113 } 101 114 } 102 115 103 104 105 106 116 // TODO both unit tests 117 //changed path, /Targetpart is removed 118 //how to overwork the input stream to make it downloadable 119 // using mime type as well 107 120 // @DELETE 108 121 // @Produces(MediaType.TEXT_XML) … … 116 129 // return result[1]; 117 130 // } 118 119 131 @POST 120 132 @Consumes("multipart/mixed") … … 123 135 @Secured("ROLE_USER") 124 136 public JAXBElement<CachedRepresentationInfo> postCached(@PathParam("targetid") String targetIdentifier, 125 @PathParam("fragmentDescriptor") String fragmentDescriptor, 137 @PathParam("fragmentDescriptor") String fragmentDescriptor, 126 138 MultiPart multiPart) throws SQLException { 127 128 139 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 129 140 final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(targetIdentifier)); … … 133 144 final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource); 134 145 final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(respondDB[1]); 135 return new ObjectFactory().createCashedRepresentationInfo(cachedInfo); 146 return new ObjectFactory() 147 .createCashedRepresentationInfo(cachedInfo); 136 148 137 149 } 138 139 150 140 151 @DELETE 141 152 @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}") 142 153 @Secured("ROLE_ADMIN") 143 public String deleteCachedForTarget(@PathParam("targetid") String targetExternalIdentifier, 144 @PathParam("cachedid") String cachedExternalIdentifier) throws SQLException {154 public String deleteCachedForTarget(@PathParam("targetid") String targetExternalIdentifier, 155 @PathParam("cachedid") String cachedExternalIdentifier) throws SQLException, IOException { 145 156 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 146 157 final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(targetExternalIdentifier)); 147 final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedExternalIdentifier)); 148 int[] resultDelete = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID); 149 String result = Integer.toString(resultDelete[0]); 150 return result + " pair(s) target-cached deleted."; 158 if (targetID != null) { 159 final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedExternalIdentifier)); 160 if (cachedID != null) { 161 int[] resultDelete = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID); 162 String result = Integer.toString(resultDelete[0]); 163 return result + " pair(s) target-cached deleted."; 164 } else { 165 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database"); 166 return null; 167 } 168 } else { 169 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id is not found in the database"); 170 return null; 171 } 151 172 } 152 153 154 173 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java
r4207 r4217 27 27 import java.util.UUID; 28 28 import javax.servlet.http.HttpServletRequest; 29 import javax.servlet.http.HttpServletResponse; 29 30 import javax.ws.rs.Consumes; 30 31 import javax.ws.rs.GET; … … 51 52 @Component 52 53 @Path("/users") 53 @Transactional(rollbackFor ={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})54 @Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class}) 54 55 public class UserResource { 56 55 57 @Autowired 56 58 private DBIntegrityService dbIntegrityService; 57 59 @Context 58 60 private HttpServletRequest httpServletRequest; 61 @Context 62 private HttpServletResponse httpServletResponse; 59 63 @Context 60 64 private UriInfo uriInfo; … … 66 70 public UserResource() { 67 71 } 68 72 69 73 @GET 70 74 @Produces(MediaType.TEXT_XML) 71 75 @Path("{userid: " + BackendConstants.regExpIdentifier + "}") 72 @Secured("ROLE_USER") 73 @Transactional(readOnly =true)74 public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException {75 76 @Secured("ROLE_USER") 77 @Transactional(readOnly = true) 78 public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException, IOException { 79 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 76 80 final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); 77 final User user = dbIntegrityService.getUser(userID); 78 return new ObjectFactory().createUser(user); 81 if (userID != null) { 82 final User user = dbIntegrityService.getUser(userID); 83 return new ObjectFactory().createUser(user); 84 } else { 85 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); 86 return null; 87 } 79 88 } 80 89 81 90 @GET 82 91 @Produces(MediaType.TEXT_XML) 83 92 @Path("/info") 84 @Secured("ROLE_USER") 85 @Transactional(readOnly =true)86 public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException {93 @Secured("ROLE_USER") 94 @Transactional(readOnly = true) 95 public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException, IOException { 87 96 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 88 97 final User user = dbIntegrityService.getUserByInfo(email); 89 return new ObjectFactory().createUser(user); 98 if (user != null) { 99 return new ObjectFactory().createUser(user); 100 } else { 101 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given info is not found in the database"); 102 return null; 103 } 90 104 } 91 105 92 106 @GET 93 107 @Produces(MediaType.TEXT_XML) 94 108 @Path("{userid: " + BackendConstants.regExpIdentifier + "}/current") 95 @Secured("ROLE_USER") 96 @Transactional(readOnly =true)97 public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier) {109 @Secured("ROLE_USER") 110 @Transactional(readOnly = true) 111 public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier) throws IOException { 98 112 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 99 113 final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(ExternalIdentifier)); 100 final CurrentUserInfo userInfo = new CurrentUserInfo(); 101 userInfo.setRef(dbIntegrityService.getUserURI(userID)); 102 userInfo.setCurrentUser(ifLoggedIn(userID)); 103 return new ObjectFactory().createCurrentUserInfo(userInfo); 114 if (userID != null) { 115 final CurrentUserInfo userInfo = new CurrentUserInfo(); 116 userInfo.setRef(dbIntegrityService.getUserURI(userID)); 117 userInfo.setCurrentUser(ifLoggedIn(userID)); 118 return new ObjectFactory().createCurrentUserInfo(userInfo); 119 } else { 120 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); 121 return null; 122 } 104 123 } 105 124 106 125 @POST 107 126 @Consumes(MediaType.TEXT_XML) … … 110 129 @Secured("ROLE_ADMIN") 111 130 public JAXBElement<User> addUser(@PathParam("userid") String remoteId, User user) throws SQLException { 112 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 131 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 113 132 final Number userID = dbIntegrityService.addUser(user, remoteId); 114 133 final User addedUser = dbIntegrityService.getUser(userID); 115 134 return new ObjectFactory().createUser(addedUser); 116 135 } 117 136 118 137 @PUT 119 138 @Consumes(MediaType.TEXT_XML) … … 121 140 @Path("") 122 141 @Secured("ROLE_ADMIN") 123 public JAXBElement<User> updateUser(User user) {124 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 142 public JAXBElement<User> updateUser(User user) throws IOException{ 143 dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); 125 144 final Number userID = dbIntegrityService.updateUser(user); 126 final User addedUser = dbIntegrityService.getUser(userID); 127 return new ObjectFactory().createUser(addedUser); 145 if (userID != null) { 146 final User addedUser = dbIntegrityService.getUser(userID); 147 return new ObjectFactory().createUser(addedUser); 148 } else { 149 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database"); 150 return null; 151 } 128 152 } 129 130 131 132 private boolean ifLoggedIn(Number userID){ 153 154 private boolean ifLoggedIn(Number userID) { 133 155 return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID)); 134 156 }
Note: See TracChangeset
for help on using the changeset viewer.