Changeset 4217


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.

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  
    11January 2, 2014: the first deployment on lux16.
    22
    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
     3January 3, 2014: files README, INSTALL, UPDATE, CHANGES are added to the
     4deployment tar-ball which is from now on is generated using maven assembly
     5plug-in.
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/INSTALL.txt

    r4211 r4217  
    2121= Installation and configuration   =
    2222=========================================================================================
    23 == Plasing 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 ==
    2424
    2525You must deploy the annotator's .war file to the tomcat-webuser. Make a directory for the
     
    4242
    4343         <Resource
    44                  name="jdbc/DASISH"
     44                 name="jdbc/DASISHAnnotator"
    4545                 auth="Container"
    4646                 type="javax.sql.DataSource"
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/README.txt

    r4211 r4217  
    88The DASISH web-annotator back-end consists of two parts: the database, where the
    99annotations are stored, and the Jerseys web-application that by means of REST requests
    10 performs  authentified red/write  access to the database.
     10performs  authentified read/write  access to the database.
    1111
    1212=== Requests ===
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4209 r4217  
    8080    @Context
    8181    private Providers providers;
    82    
    83    
    8482    final String default_permission = "reader";
    8583    private static final Logger logger = LoggerFactory.getLogger(AnnotationResource.class);
     
    9795    }
    9896
    99  
    100    
    10197    public void setProviders(Providers providers) {
    10298        this.providers = providers;
     
    116112        dbIntegrityService.setServiceURI(baseURIstr);
    117113        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");
    127128            return null;
    128129        }
     
    139140        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    140141        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");
    148154            return null;
    149155        }
     
    183189        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier));
    184190        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        }
    194204    }
    195205
     
    203213        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
    204214        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        }
    215229    }
    216230
     
    246260        }
    247261        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        }
    259277    }
    260278
     
    270288        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
    271289        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        }
    281303    }
    282304
     
    291313        final Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
    292314        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");
    302329            return null;
    303330        }
     
    313340        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId));
    314341        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");
    321353            return null;
    322354        }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java

    r4207 r4217  
    2929import javax.imageio.ImageIO;
    3030import javax.servlet.http.HttpServletRequest;
     31import javax.servlet.http.HttpServletResponse;
    3132import javax.ws.rs.GET;
    3233import javax.ws.rs.Path;
     
    4950@Component
    5051@Path("/cached")
    51 @Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
     52@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    5253public class CachedRepresentationResource {
    5354
     
    5657    @Context
    5758    private HttpServletRequest httpServletRequest;
     59    @Context
     60    private HttpServletResponse httpServletResponse;
    5861    @Context
    5962    private UriInfo uriInfo;
     
    6871    @Produces(MediaType.TEXT_XML)
    6972    @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 {
    7376        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    7477        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        }
    7785    }
    7886
     
    8088    @Produces({"image/jpeg", "image/png"})
    8189    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
    82     @Secured("ROLE_USER")   
    83     @Transactional(readOnly=true)
     90    @Secured("ROLE_USER")
     91    @Transactional(readOnly = true)
    8492    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
    8593        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    8694        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        }
    91104    }
    92    
    93    
    94105}
  • 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}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java

    r4207 r4217  
    2727import java.util.UUID;
    2828import javax.servlet.http.HttpServletRequest;
     29import javax.servlet.http.HttpServletResponse;
    2930import javax.ws.rs.Consumes;
    3031import javax.ws.rs.GET;
     
    5152@Component
    5253@Path("/users")
    53 @Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
     54@Transactional(rollbackFor = {Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    5455public class UserResource {
     56
    5557    @Autowired
    5658    private DBIntegrityService dbIntegrityService;
    5759    @Context
    5860    private HttpServletRequest httpServletRequest;
     61    @Context
     62    private HttpServletResponse httpServletResponse;
    5963    @Context
    6064    private UriInfo uriInfo;
     
    6670    public UserResource() {
    6771    }
    68    
     72
    6973    @GET
    7074    @Produces(MediaType.TEXT_XML)
    7175    @Path("{userid: " + BackendConstants.regExpIdentifier + "}")
    72     @Secured("ROLE_USER")   
    73     @Transactional(readOnly=true)
    74     public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException {
    75          dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     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());
    7680        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        }
    7988    }
    80    
     89
    8190    @GET
    8291    @Produces(MediaType.TEXT_XML)
    8392    @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 {
    8796        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    8897        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        }
    90104    }
    91    
     105
    92106    @GET
    93107    @Produces(MediaType.TEXT_XML)
    94108    @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 {
    98112        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    99113        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        }
    104123    }
    105    
     124
    106125    @POST
    107126    @Consumes(MediaType.TEXT_XML)
     
    110129    @Secured("ROLE_ADMIN")
    111130    public JAXBElement<User> addUser(@PathParam("userid") String remoteId, User user) throws SQLException {
    112         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());       
     131        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    113132        final Number userID = dbIntegrityService.addUser(user, remoteId);
    114133        final User addedUser = dbIntegrityService.getUser(userID);
    115134        return new ObjectFactory().createUser(addedUser);
    116135    }
    117    
     136
    118137    @PUT
    119138    @Consumes(MediaType.TEXT_XML)
     
    121140    @Path("")
    122141    @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());
    125144        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        }
    128152    }
    129    
    130    
    131    
    132     private boolean ifLoggedIn(Number userID){
     153
     154    private boolean ifLoggedIn(Number userID) {
    133155        return httpServletRequest.getRemoteUser().equals(dbIntegrityService.getUserRemoteID(userID));
    134156    }
Note: See TracChangeset for help on using the changeset viewer.