Changeset 4217 for DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java
- Timestamp:
- 01/06/14 17:20:05 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.