Changeset 5086


Ignore:
Timestamp:
04/28/14 14:44:33 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Refactored, and update headers added. unit test: works, localhost: gets are checked and work.

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  
    2626   
    2727   
    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);
    3030    }
    31    
    32      public NotInDataBaseException(String typeOfResource){
    33         super(typeOfResource + " with the given info is not found in the database");
    34     }
     31
    3532}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java

    r4865 r5086  
    163163    public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml);
    164164   
     165    public int updateAnnotationHeadline(Number annotationID, String text);
     166   
    165167   
    166168   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/DBIntegrityService.java

    r4995 r5086  
    2222import eu.dasish.annotation.backend.PrincipalExists;
    2323import eu.dasish.annotation.backend.Resource;
     24import eu.dasish.annotation.backend.ResourceAction;
    2425import eu.dasish.annotation.schema.Annotation;
    2526import eu.dasish.annotation.schema.AnnotationBody;
     
    194195    Principal getDataBaseAdmin() ;
    195196
    196     boolean canDo(Access access, Number principalID, Number annotationID);
     197    boolean canDo(ResourceAction action, Number principalID, Number resourceID, Resource resource);
    197198
    198199   
     
    231232     */
    232233    int updateAnnotationBody(Number internalID, AnnotationBody annotationBody);
     234   
     235    int updateAnnotationHeadline(Number internalID, String newHeader);
    233236
    234237    /**
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/ResourceDao.java

    r4865 r5086  
    2020import eu.dasish.annotation.backend.NotInDataBaseException;
    2121import eu.dasish.annotation.schema.Access;
    22 import eu.dasish.annotation.schema.Permission;
    2322import java.util.List;
    2423import 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  
    2323import eu.dasish.annotation.backend.PrincipalCannotBeDeleted;
    2424import eu.dasish.annotation.backend.PrincipalExists;
     25import eu.dasish.annotation.backend.ResourceAction;
    2526import eu.dasish.annotation.backend.dao.AnnotationDao;
    2627import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
     
    413414    }
    414415
    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
    425444    @Override
    426445    public NotebookInfoList getNotebooks(Number principalID, Access access) {
     
    598617        return annotationDao.updateAnnotationBody(internalID, body[0], body[1], annotationBody.getXmlBody() != null);
    599618    }
     619   
     620    @Override
     621    public int updateAnnotationHeadline(Number internalID, String newHeader) {
     622        return annotationDao.updateAnnotationHeadline(internalID, newHeader);
     623    }
    600624
    601625    @Override
     
    603627        return principalDao.updatePrincipal(principal);
    604628    }
    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 {
    608632        return targetDao.updateTargetCachedRepresentationFragment(targetID, cachedID, fragmentDescriptor);
    609633    }
    610    
    611     @Override
    612     public int updateCachedMetada(CachedRepresentationInfo cachedInfo) throws NotInDataBaseException{
     634
     635    @Override
     636    public int updateCachedMetada(CachedRepresentationInfo cachedInfo) throws NotInDataBaseException {
    613637        Number internalID = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI());
    614638        return cachedRepresentationDao.updateCachedRepresentationMetadata(internalID, cachedInfo);
    615639    }
    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 {
    619643        return cachedRepresentationDao.updateCachedRepresentationBlob(internalID, cachedBlob);
    620644    }
    621    
     645
    622646    /// notebooks ///
    623 
    624647    @Override
    625648    public boolean updateNotebookMetadata(Number notebookID, NotebookInfo upToDateNotebookInfo) throws NotInDataBaseException {
     
    648671
    649672    }
    650 
    651    
    652673
    653674    @Override
     
    706727        return notebookDao.addAnnotationToNotebook(notebookID, newAnnotationID);
    707728    }
    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) {
    711732        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;
    714735    }
    715736
     
    856877        }
    857878    }
    858 
    859  
    860879
    861880    //// priveee ///
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r4886 r5086  
    387387        return affectedRows;
    388388    }
     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    }
    389404
    390405    // 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  
    7979        List<Principal> result = this.loggedQuery(sql.toString(), principalRowMapper, eMail.toLowerCase());
    8080        if (result.isEmpty()) {
    81             throw new NotInDataBaseException("principal");
     81            throw new NotInDataBaseException("principal", "e-mail", eMail);
    8282        }
    8383        return result.get(0);
     
    137137        List<Number> result = this.loggedQuery(requestDB.toString(), internalIDRowMapper, remoteID);
    138138        if (result.isEmpty()) {
    139             throw new NotInDataBaseException("principal");
     139            throw new NotInDataBaseException("principal", "remote ID", remoteID);
    140140        }
    141141        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  
    9191    final static protected String is_xml = "is_xml";
    9292    final static protected String public_ = "public_";
     93    final static protected String _headline = "headline";
    9394    final static protected String fragment_descriptor = "fragment_descriptor";
    9495    final static protected String fragment_descriptor_in_cached = "fragment_descriptor_in_cached";
     
    170171        List<Number> sqlResult = this.loggedQuery(sql.toString(), internalIDRowMapper, externalId.toString());
    171172        if (sqlResult.isEmpty()) {
    172             throw new NotInDataBaseException(resourceTableName, externalId.toString());
     173            throw new NotInDataBaseException(resourceTableName, "external ID", externalId.toString());
    173174        }
    174175
     
    195196        } catch (IllegalArgumentException e) {
    196197            _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);
    198199        }
    199200    }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r5053 r5086  
    2020import eu.dasish.annotation.backend.BackendConstants;
    2121import eu.dasish.annotation.backend.NotInDataBaseException;
    22 import eu.dasish.annotation.backend.PrincipalExists;
    2322import eu.dasish.annotation.backend.Resource;
     23import eu.dasish.annotation.backend.ResourceAction;
    2424import eu.dasish.annotation.backend.dao.ILambda;
    2525import eu.dasish.annotation.schema.Annotation;
     
    3333import java.io.IOException;
    3434import java.util.Arrays;
     35import java.util.HashMap;
    3536import java.util.Map;
    3637import java.util.UUID;
     
    8687    @Transactional(readOnly = true)
    8788    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    //////////////////////////////////////////////////
    112107    @GET
    113108    @Produces(MediaType.TEXT_XML)
     
    115110    @Transactional(readOnly = true)
    116111    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"));
    137126        }
    138127    }
     
    181170    @Transactional(readOnly = true)
    182171    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"));
    202186        }
    203187    }
     
    207191    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
    208192    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);
    213347        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.";
    218353            } 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.";
    222355            }
    223356
     
    229362    }
    230363
    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    ///////////////////////////////////////////
    252375
    253376    @PUT
    254377    @Consumes(MediaType.APPLICATION_XML)
    255378    @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     @PUT
    294     @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     @PUT
    320     @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     @PUT
    355     @Consumes(MediaType.APPLICATION_XML)
    356     @Produces(MediaType.APPLICATION_XML)
    357379    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/")
    358380    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 {
    361389            return new ObjectFactory().createResponseBody(new ResponseBody());
    362390        }
    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    ////////////////////////////////////////////////////////////
    386408    @DELETE
    387409    @Produces(MediaType.TEXT_PLAIN)
     
    389411    public String deletePrincipalsAccess(@PathParam("annotationId") String annotationId,
    390412            @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    }
    424415}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java

    r4956 r5086  
    2323import eu.dasish.annotation.backend.NotInDataBaseException;
    2424import eu.dasish.annotation.backend.Resource;
     25import eu.dasish.annotation.backend.ResourceAction;
     26import eu.dasish.annotation.backend.dao.ILambda;
    2527import eu.dasish.annotation.schema.CachedRepresentationInfo;
    2628import eu.dasish.annotation.schema.ObjectFactory;
     
    2931import java.io.InputStream;
    3032import java.sql.SQLException;
    31 import java.util.UUID;
     33import java.util.HashMap;
     34import java.util.Map;
    3235import javax.imageio.ImageIO;
    3336import javax.servlet.http.HttpServletRequest;
    34 import javax.servlet.http.HttpServletResponse;
    3537import javax.ws.rs.Consumes;
    3638import javax.ws.rs.GET;
     
    5860    }
    5961
    60     // TODOD both unit tests
    61     //changed path, /Target/cached part is removed
    6262    @GET
    6363    @Produces(MediaType.TEXT_XML)
     
    6565    @Transactional(readOnly = true)
    6666    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());
    8173        }
    8274    }
    8375
     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    ////////////////////////////////////////////
    8486    @GET
    8587    @Produces({"image/jpeg", "image/png"})
     
    8789    @Transactional(readOnly = true)
    8890    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.");
    91105            return null;
    92106        }
    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             }
    110107
    111         } catch (NotInDataBaseException e) {
    112             loggerServer.debug(e.toString());
    113             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
    114             return null;
    115         }
    116108    }
    117109
     
    121113    @Transactional(readOnly = true)
    122114    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    }
    136118
    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);
    141125        }
    142126    }
    143127
     128    ///////////////////////////////////////////////////////////////////
    144129    @PUT
    145130    @Consumes("multipart/mixed")
     
    148133    public String updateCachedBlob(@PathParam("cachedid") String cachedIdentifier,
    149134            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. ";
    153143        }
    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");
    158153            try {
    159                 final int result = dbIntegrityService.updateCachedBlob(cachedID, cachedSource);
    160                 return result + "rows are updated";
     154                return dbIntegrityService.updateCachedBlob(cachedID, stream);
    161155            } 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;
    165158            }
    166         } catch (NotInDataBaseException e2) {
    167             loggerServer.debug(e2.toString());
    168             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
    169             return "Nothing is updated. ";
    170159        }
    171160    }
    172161
     162    ///////////////////////////////////////////////////////////
    173163    @PUT
    174164    @Consumes(MediaType.TEXT_XML)
     
    176166    @Path("metadata")
    177167    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. ";
    181175        }
    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);
    189184        }
    190185    }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java

    r5053 r5086  
    7575        Map params = new HashMap<String, String>();
    7676        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());
    7878        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    7979    }
     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     /////////////////////////////////
    8091
    8192    @GET
     
    90101        return "The admin of the server database " + dbIntegrityService.getDataBaseAdmin().getDisplayName() + " is availiable via e-mail " + dbIntegrityService.getDataBaseAdmin().getEMail();
    91102    }
    92 
     103   
     104   /////////////////////////////////////////
    93105    @GET
    94106    @Produces(MediaType.TEXT_XML)
     
    98110        Map params = new HashMap<String, String>();
    99111        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());
    101113        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    102114    }
    103115
     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    ////////////////////////////////////////
    104126    @GET
    105127    @Produces(MediaType.TEXT_XML)
     
    110132        params.put("externalId", externalIdentifier);
    111133        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());
    113135        return (result != null) ? (new ObjectFactory().createCurrentPrincipalInfo(result)) : (new ObjectFactory().createCurrentPrincipalInfo(new CurrentPrincipalInfo()));
    114136    }
     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    //////////////////////////////
    115150
    116151    @POST
     
    133168
    134169    }
     170   
     171    ///////////////////////////////////////
    135172
    136173    @POST
     
    150187
    151188        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());
    153190        } else {
    154191            verboseOutput.ADMIN_RIGHTS_EXPECTED();
     
    158195
    159196    }
     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   /////////////////////////////////////
    160209
    161210    @POST
     
    177226
    178227        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   ///////////////////////////////////////////////
    181243
    182244    @POST
     
    196258
    197259        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    ///////////////////////////////////////////////////
    200264
    201265    @POST
     
    219283            return new ObjectFactory().createPrincipal(new Principal());
    220284        }
    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());
    226289            return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    227290
     
    232295        }
    233296    }
     297    ///////////////////////////////////
    234298
    235299    @POST
     
    239303    public JAXBElement<Principal> updatePrincipalFromForm(@FormParam("name") String name, @FormParam("email") String email)
    240304            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       
    248306        Principal newPrincipal = new Principal();
    249         newPrincipal.setURI(principalURI);
    250307        newPrincipal.setDisplayName(name);
    251308        newPrincipal.setEMail(email);
    252309        Map params = new HashMap<String, Object>();
    253310        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());
    255312        return (result != null) ? (new ObjectFactory().createPrincipal(result)) : (new ObjectFactory().createPrincipal(new Principal()));
    256313    }
     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     //////////////////////////////////////
    257329
    258330    @PUT
     
    323395    }
    324396
    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         @Override
    329         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         @Override
    339         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         @Override
    348         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         @Override
    359         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         @Override
    368         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         @Override
    376         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     }
    384397}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java

    r5053 r5086  
    2121import eu.dasish.annotation.backend.PrincipalExists;
    2222import eu.dasish.annotation.backend.Resource;
    23 import eu.dasish.annotation.backend.dao.DBIntegrityService;
     23import eu.dasish.annotation.backend.ResourceAction;
    2424import eu.dasish.annotation.backend.dao.ILambda;
    2525import eu.dasish.annotation.backend.dao.ILambdaPrincipal;
    26 import eu.dasish.annotation.schema.Access;
    27 import eu.dasish.annotation.schema.Annotation;
    2826import eu.dasish.annotation.schema.ObjectFactory;
    2927import eu.dasish.annotation.schema.Principal;
     
    3331import javax.servlet.http.HttpServletResponse;
    3432import javax.xml.bind.JAXBElement;
    35 import org.slf4j.Logger;
    3633
    3734/**
     
    4138public class RequestWrappers<T> {
    4239
    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";
    4746
    48     public RequestWrappers(Logger loggerServer, HttpServletResponse httpServletResponse, ResourceResource resourceResource, DBIntegrityService dbIntegrityService) {
    49         this.loggerServer = loggerServer;
    50         this.httpServletResponse = httpServletResponse;
     47    public RequestWrappers(ResourceResource resourceResource) {
    5148        this.resourceResource = resourceResource;
    52         this.dbIntegrityService = dbIntegrityService;
    5349    }
    54    
     50
    5551    public String FORBIDDEN_RESOURCE_ACTION(String identifier, String resource, String action) {
    5652        return " The logged-in principal cannot " + action + " the " + resource + " with the identifier " + identifier;
     
    6258            return null;
    6359        }
     60        params.put(_principalID, remotePrincipalID);
    6461        try {
    6562            return dbRequestor.apply(params);
    6663        } 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());
    6966            return null;
    7067        }
    7168    }
    7269
    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 {
    7571        Number principalID = resourceResource.getPrincipalID();
    7672        if (principalID == null) {
    7773            return null;
    7874        }
     75        params.put(_principalID, principalID);
    7976        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);
    8288                return dbRequestor.apply(params);
    8389            } 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);
    8692                return null;
    8793            }
    8894        } 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());
    9197            return null;
    9298        }
     
    94100
    95101    public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException {
    96        
     102
    97103        try {
    98104            try {
    99105                return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
    100106            } 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());
    103109                return new ObjectFactory().createPrincipal(new Principal());
    104110            }
    105111        } 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());
    108114            return new ObjectFactory().createPrincipal(new Principal());
    109115        }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/ResourceResource.java

    r5030 r5086  
    6161    protected String anonym = "anonymous";
    6262    protected String defaultAccess = "read";
     63   
    6364    protected String[] admissibleAccess = {"read", "write", "owner"};
    6465
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java

    r4941 r5086  
    2323import eu.dasish.annotation.backend.NotInDataBaseException;
    2424import eu.dasish.annotation.backend.Resource;
     25import eu.dasish.annotation.backend.ResourceAction;
     26import eu.dasish.annotation.backend.dao.ILambda;
    2527import eu.dasish.annotation.schema.CachedRepresentationInfo;
    2628import eu.dasish.annotation.schema.ObjectFactory;
     
    3032import java.io.InputStream;
    3133import java.sql.SQLException;
     34import java.util.HashMap;
     35import java.util.Map;
    3236import java.util.UUID;
    3337import javax.servlet.http.HttpServletRequest;
     
    7478    @Transactional(readOnly = true)
    7579    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 {
    7885            return new ObjectFactory().createTarget(new Target());
    7986        }
    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    /////////////////////////////////////////////////
    9399    @GET
    94100    @Produces(MediaType.TEXT_XML)
     
    96102    @Transactional(readOnly = true)
    97103    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 {
    100109            return new ObjectFactory().createReferenceList(new ReferenceList());
    101110        }
    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    ////////////////////////////////////////////////
    114123    @POST
    115124    @Consumes("multipart/mixed")
     
    119128            @PathParam("fragmentDescriptor") String fragmentDescriptor,
    120129            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 {
    123141            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
    124142        }
    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");
    130153            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;
    139159            }
    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    ////////////////////////////////////////////////
    147164    @PUT
    148165    @Produces(MediaType.APPLICATION_XML)
     
    163180                loggerServer.debug(e1.toString());
    164181                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.toString());
    165                 return "Nothing is updated: "+e1;
     182                return "Nothing is updated: " + e1;
    166183            }
    167184        } catch (NotInDataBaseException e2) {
    168185            loggerServer.debug(e2.toString());
    169186            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
    170             return "Nothing is updated: "+e2;
     187            return "Nothing is updated: " + e2;
    171188        }
    172189    }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceTest.java

    r4995 r5086  
    797797
    798798        final UUID mockNewTargetUUID = UUID.randomUUID();
    799         final NotInDataBaseException e = new NotInDataBaseException(tempTargetID);
     799        final NotInDataBaseException e = new NotInDataBaseException("target", "external ID", tempTargetID);
    800800
    801801        mockeryDao.checking(new Expectations() {
     
    16791679
    16801680        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");
    16821682        final String mockTempID = "00000000-0000-0000-0000-000000000031";
    16831683        final UUID mockNewID = UUID.randomUUID();
     
    17581758    }
    17591759
     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   
    17601786//    public int updateAnnotationPrincipalAccess(Number annotationID, Number principalID, Access access) {
    17611787//        int result;
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r4886 r5086  
    3838import javax.xml.parsers.ParserConfigurationException;
    3939import static org.junit.Assert.*;
    40 import org.junit.Ignore;
    4140import org.junit.Test;
    4241import org.junit.runner.RunWith;
     
    447446        assertEquals("test", check2.getBody().getXmlBody().getAny().getTextContent());
    448447        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());
    449457    }
    450458
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.java

    r4941 r5086  
    2020import eu.dasish.annotation.backend.NotInDataBaseException;
    2121import eu.dasish.annotation.backend.Resource;
     22import eu.dasish.annotation.backend.ResourceAction;
    2223import eu.dasish.annotation.backend.dao.DBIntegrityService;
    2324import eu.dasish.annotation.backend.TestBackendConstants;
     
    115116
    116117
    117                 oneOf(mockDbIntegrityService).canDo(Access.READ, 3, 1);
     118                oneOf(mockDbIntegrityService).canDo(ResourceAction.READ, 3, 1, Resource.ANNOTATION);
    118119                will(returnValue(true));
    119120
     
    164165                oneOf(mockDbIntegrityService).getAnnotationOwnerID(4);
    165166                will(returnValue(3));
     167               
     168                oneOf(mockDbIntegrityService).canDo(ResourceAction.DELETE, 3, 4, Resource.ANNOTATION);
     169                will(returnValue(true));
    166170
    167171                oneOf(mockDbIntegrityService).deleteAnnotation(4);
     
    172176
    173177        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);
    175179    }
    176180
     
    296300                will(returnValue(1));
    297301               
     302                oneOf(mockDbIntegrityService).canDo(ResourceAction.WRITE_W_METAINFO, 1, 1, Resource.ANNOTATION);
     303                will(returnValue(true));
     304               
    298305                oneOf(mockDbIntegrityService).updateAnnotation(annotation);
    299306                will(returnValue(1));
     
    363370                will(returnValue(1));
    364371               
    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               
    367375               
    368376                oneOf(mockDbIntegrityService).updateAnnotationBody(1, ab);
     
    382390        assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody());
    383391    }
     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    }
    384448}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java

    r4956 r5086  
    173173        ClientResponse response = responseBuilder.delete(ClientResponse.class);   
    174174        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));
    176176    }
    177177
     
    301301       
    302302    }
     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    }
    303326   
    304327    protected Builder getAuthenticatedResource(WebResource resource) {
Note: See TracChangeset for help on using the changeset viewer.