Ignore:
Timestamp:
01/06/14 17:20:05 (10 years ago)
Author:
olhsha
Message:

files INSTALL, UPDATED, CHANGES and README are corrected. The bug with the wrong server diagnostic (403 instead of 404), when a resource's give id is not found, is fixed.

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  
    3131import java.util.UUID;
    3232import javax.servlet.http.HttpServletRequest;
     33import javax.servlet.http.HttpServletResponse;
    3334import javax.ws.rs.Consumes;
    3435import javax.ws.rs.DELETE;
     
    5859@Component
    5960@Path("/targets")
    60 @Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
     61@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    6162public class TargetResource {
    6263
     
    6566    @Context
    6667    private HttpServletRequest httpServletRequest;
     68    @Context
     69    private HttpServletResponse httpServletResponse;
    6770    @Context
    6871    private UriInfo uriInfo;
     
    7982    @Produces(MediaType.TEXT_XML)
    8083    @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 {
    8487        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    8588        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        }
    8896    }
    8997
     
    92100    @Produces(MediaType.TEXT_XML)
    93101    @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 {
    97105        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    98106        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        }
    101114    }
    102115
    103     // TODO both unit tests
    104     //changed path, /Targetpart is removed
    105     //how to overwork the input stream to make it downloadable
    106     // using mime type as well
     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
    107120//    @DELETE
    108121//    @Produces(MediaType.TEXT_XML)
     
    116129//        return result[1];
    117130//    }
    118 
    119131    @POST
    120132    @Consumes("multipart/mixed")
     
    123135    @Secured("ROLE_USER")
    124136    public JAXBElement<CachedRepresentationInfo> postCached(@PathParam("targetid") String targetIdentifier,
    125             @PathParam("fragmentDescriptor") String fragmentDescriptor, 
     137            @PathParam("fragmentDescriptor") String fragmentDescriptor,
    126138            MultiPart multiPart) throws SQLException {
    127 
    128139        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    129140        final Number targetID = dbIntegrityService.getTargetInternalIdentifier(UUID.fromString(targetIdentifier));
     
    133144        final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
    134145        final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(respondDB[1]);
    135         return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
     146        return new ObjectFactory()
     147                .createCashedRepresentationInfo(cachedInfo);
    136148
    137149    }
    138    
    139    
     150
    140151    @DELETE
    141152    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
    142153    @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 {
    145156        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    146157        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        }
    151172    }
    152 
    153    
    154173}
Note: See TracChangeset for help on using the changeset viewer.