Changeset 5679 for DASISH


Ignore:
Timestamp:
09/26/14 09:14:41 (10 years ago)
Author:
olhsha@mpi.nl
Message:

bug fixed with the little code restructuring. 403 was not thrown (but 500 for another reason was thrown) when a not-owner tries to update permissions of an annotation. Now it is fixed by removing catching exceptions and messaging errors from auxiliary request wrappers to REST methods directly.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/UPDATE.txt

    r5676 r5679  
    33standard MPI deployment procedure, for webannotator-basic. 
    44Consult the instructions from INSTALL.txt  about placing war file if you have forgotten.
    5 
     5 override="true
    662. The web.xml stays intact  because currently it is set for basic authentication.
    77
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r5661 r5679  
    1919
    2020import eu.dasish.annotation.backend.BackendConstants;
     21import eu.dasish.annotation.backend.ForbiddenException;
    2122import eu.dasish.annotation.backend.NotInDataBaseException;
    2223import eu.dasish.annotation.backend.Resource;
     
    8586    public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException {
    8687        Map params = new HashMap();
    87         Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
    88         if (result != null) {
    89             return (new ObjectFactory()).createAnnotation(result);
    90         } else {
     88        try {
     89            Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
     90            if (result != null) {
     91                return (new ObjectFactory()).createAnnotation(result);
     92            } else {
     93                return (new ObjectFactory()).createAnnotation(new Annotation());
     94            }
     95        } catch (NotInDataBaseException e1) {
     96            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     97            return (new ObjectFactory()).createAnnotation(new Annotation());
     98        } catch (ForbiddenException e2) {
     99            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    91100            return (new ObjectFactory()).createAnnotation(new Annotation());
    92101        }
     
    108117    public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String externalIdentifier) throws IOException {
    109118        Map params = new HashMap();
    110         ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
    111         if (result != null) {
    112             return (new ObjectFactory()).createTargetList(result);
    113         } else {
    114             return (new ObjectFactory()).createTargetList(new ReferenceList());
     119        try {
     120            ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
     121            if (result != null) {
     122                return (new ObjectFactory()).createTargetList(result);
     123            } else {
     124                return (new ObjectFactory()).createTargetList(new ReferenceList());
     125            }
     126        } catch (NotInDataBaseException e1) {
     127            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     128            return (new ObjectFactory()).createReferenceList(new ReferenceList());
     129        } catch (ForbiddenException e2) {
     130            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     131            return (new ObjectFactory()).createReferenceList(new ReferenceList());
    115132        }
    116133    }
     
    168185    public JAXBElement<PermissionList> getAnnotationPermissions(@PathParam("annotationid") String externalIdentifier) throws IOException {
    169186        Map params = new HashMap();
    170         PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
    171         if (result != null) {
    172             return (new ObjectFactory()).createPermissionList(result);
    173         } else {
     187        try {
     188            PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
     189            if (result != null) {
     190                return (new ObjectFactory()).createPermissionList(result);
     191            } else {
     192                return (new ObjectFactory()).createPermissionList(new PermissionList());
     193            }
     194        } catch (NotInDataBaseException e1) {
     195            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     196            return (new ObjectFactory()).createPermissionList(new PermissionList());
     197        } catch (ForbiddenException e2) {
     198            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    174199            return (new ObjectFactory()).createPermissionList(new PermissionList());
    175200        }
     
    189214    public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException {
    190215        Map params = new HashMap();
    191         int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier);
    192         if (result != null) {
    193             return result[0] + " annotation(s) is(are) deleted.";
    194         } else {
    195             return "Nothing is deleted.";
     216        try {
     217            int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier);
     218            if (result != null) {
     219                return result[0] + " annotation(s) is(are) deleted.";
     220            } else {
     221                return "Nothing is deleted.";
     222            }
     223        } catch (NotInDataBaseException e1) {
     224            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     225            return e1.getMessage();
     226        } catch (ForbiddenException e2) {
     227            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     228            return e2.getMessage();
    196229        }
    197230    }
     
    247280            return null;
    248281        }
    249         Map params = new HashMap();
    250         params.put("annotation", annotation);
    251         ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId);
    252         if (result != null) {
    253             return (new ObjectFactory()).createResponseBody(result);
    254         } else {
    255             return (new ObjectFactory()).createResponseBody(new ResponseBody());
    256         }
    257 
    258 
     282        try {
     283            Map params = new HashMap();
     284            params.put("annotation", annotation);
     285            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId);
     286            if (result != null) {
     287                return (new ObjectFactory()).createResponseBody(result);
     288            } else {
     289                return (new ObjectFactory()).createResponseBody(new ResponseBody());
     290            }
     291        } catch (NotInDataBaseException e1) {
     292            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     293            return (new ObjectFactory()).createResponseBody(new ResponseBody());
     294        } catch (ForbiddenException e2) {
     295            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     296            return (new ObjectFactory()).createResponseBody(new ResponseBody());
     297        }
    259298    }
    260299
     
    279318        Map params = new HashMap();
    280319        params.put("annotationBody", annotationBody);
    281         ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
    282         if (result != null) {
    283             return (new ObjectFactory()).createResponseBody(result);
    284         } else {
     320        try {
     321            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
     322            if (result != null) {
     323                return (new ObjectFactory()).createResponseBody(result);
     324            } else {
     325                return (new ObjectFactory()).createResponseBody(new ResponseBody());
     326            }
     327        } catch (NotInDataBaseException e1) {
     328            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     329            return (new ObjectFactory()).createResponseBody(new ResponseBody());
     330        } catch (ForbiddenException e2) {
     331            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    285332            return (new ObjectFactory()).createResponseBody(new ResponseBody());
    286333        }
     
    307354        Map params = new HashMap();
    308355        params.put("headline", newHeadline);
    309         ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
    310         if (result != null) {
    311             return (new ObjectFactory()).createResponseBody(result);
    312         } else {
     356        try {
     357            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
     358            if (result != null) {
     359                return (new ObjectFactory()).createResponseBody(result);
     360            } else {
     361                return (new ObjectFactory()).createResponseBody(new ResponseBody());
     362            }
     363        } catch (NotInDataBaseException e1) {
     364            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     365            return (new ObjectFactory()).createResponseBody(new ResponseBody());
     366        } catch (ForbiddenException e2) {
     367            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    313368            return (new ObjectFactory()).createResponseBody(new ResponseBody());
    314369        }
     
    327382        }
    328383    }
    329    
     384
    330385    //////////////////////////////////////////
    331    
    332      //////////////////////////////////////////////
     386    //////////////////////////////////////////////
    333387    @POST
    334388    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     
    340394            @FormParam("access") String access)
    341395            throws IOException {
    342        
     396
    343397        if (access.trim().equals("")) {
    344398            access = "none";
    345399        }
    346        
     400
    347401        Access accessTyped = Access.fromValue(access);
    348402        int updatedAnnotations = 0;
    349        
     403
    350404        if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
    351405            List<Number> annotationIDs = dbDispatcher.getAnnotationInternalIDsFromHeadline(annotationHeadline);
     
    356410            for (Number annotationID : annotationIDs) {
    357411                count = dbDispatcher.updatePublicAttribute(annotationID, accessTyped);;
    358                 updatedAnnotations = updatedAnnotations+count;
    359             }           
     412                updatedAnnotations = updatedAnnotations + count;
     413            }
    360414            return (updatedAnnotations + " row(s) are updated");
    361415        } else {
    362416            try {
    363             Number annotationID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(annotationDatabaseId), Resource.ANNOTATION);
    364             updatedAnnotations= dbDispatcher.updatePublicAttribute(annotationID, accessTyped);
     417                Number annotationID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(annotationDatabaseId), Resource.ANNOTATION);
     418                updatedAnnotations = dbDispatcher.updatePublicAttribute(annotationID, accessTyped);
    365419            } catch (NotInDataBaseException e) {
    366               httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());   
     420                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
    367421            }
    368422        }
    369423        return (updatedAnnotations + " annotation(s) are updated.");
    370424    }
    371    
     425
    372426    ////////////////////////////////////////////////////
    373 
    374427    @PUT
    375428    @Consumes(MediaType.APPLICATION_XML)
     
    378431    public String updateAccess(@PathParam("annotationid") String annotationExternalId,
    379432            @PathParam("principalid") String principalExternalId, Access access) throws IOException {
    380         return this.genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access);
    381     }
    382    
    383 
     433        try {
     434            return this.genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access);
     435        } catch (NotInDataBaseException e1) {
     436            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     437            return e1.getMessage();
     438        } catch (ForbiddenException e2) {
     439            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     440            return e2.getMessage();
     441        }
     442    }
    384443
    385444    //////////////////////////////////////////////
     
    395454            @FormParam("access") String access)
    396455            throws IOException {
    397        
    398         if (access.trim().equals("")) {
    399             access = null;
    400         }
    401        
    402         try {
     456
     457        try {
     458
     459            if (access.trim().equals("")) {
     460                access = null;
     461            }
     462
    403463            if (principalDatabaseId == null || principalDatabaseId.trim().equals("")) {
    404464                if (remoteID != null && !remoteID.trim().equals("")) {
     
    412472                }
    413473            }
    414         } catch (NotInDataBaseException e) {
    415             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
    416         }
    417 
    418         if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
    419             List<UUID> annotationIds = dbDispatcher.getAnnotationExternalIdsFromHeadline(annotationHeadline);
    420             if (annotationIds == null || annotationIds.isEmpty()) {
    421                 return "No annotations with this headline found";
    422             };
    423             int count = 0;
    424             String tmp = null;
    425             for (UUID annotationId : annotationIds) {
    426                 tmp = this.genericUpdateDeleteAccess(annotationId.toString(), principalDatabaseId, Access.fromValue(access));
    427                 if (!tmp.startsWith("0")) {
    428                     count++;
     474
     475            if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
     476                List<UUID> annotationIds = dbDispatcher.getAnnotationExternalIdsFromHeadline(annotationHeadline);
     477                if (annotationIds == null || annotationIds.isEmpty()) {
     478                    return "No annotations with this headline found";
     479                };
     480                int count = 0;
     481                String tmp = null;
     482                for (UUID annotationId : annotationIds) {
     483                    tmp = this.genericUpdateDeleteAccess(annotationId.toString(), principalDatabaseId, Access.fromValue(access));
     484                    if (!tmp.startsWith("0")) {
     485                        count++;
     486                    }
    429487                }
    430             }           
    431             return (count + " row(s) are updated");
     488                return (count + " row(s) are updated");
     489            } else {
     490                return this.genericUpdateDeleteAccess(annotationDatabaseId, principalDatabaseId, Access.fromValue(access));
     491            }
     492
     493        } catch (NotInDataBaseException e1) {
     494            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     495            return e1.getMessage();
     496        } catch (ForbiddenException e2) {
     497            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     498            return e2.getMessage();
     499        }
     500    }
     501
     502    ////////////////////////////////////////////
     503    private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException, NotInDataBaseException, ForbiddenException {
     504        Map params = new HashMap();
     505        params.put("access", access);
     506        final Number inputPrincipalID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL);
     507        params.put("inputPrincipalID", inputPrincipalID);
     508        Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId);
     509        if (result != null) {
     510            return result + " row(s) is(are) updated.";
    432511        } else {
    433             return this.genericUpdateDeleteAccess(annotationDatabaseId, principalDatabaseId, Access.fromValue(access));
    434         }
    435     }
    436 
    437     ////////////////////////////////////////////
    438     private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException {
    439         Map params = new HashMap();
    440         params.put("access", access);
    441         try {
    442             final Number inputPrincipalID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL);
    443             params.put("inputPrincipalID", inputPrincipalID);
    444             Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId);
    445             if (result != null) {
    446                 return result + " row(s) is(are) updated.";
    447             } else {
    448                 return "Nothing is updated.";
    449             }
    450 
    451         } catch (NotInDataBaseException e2) {
    452             loggerServer.debug(e2.toString());
    453             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
    454             return "Nothing is deleted.";
    455         }
     512            return "0 rows are updated.";
     513        }
     514
     515
    456516    }
    457517
     
    476536        Map params = new HashMap();
    477537        params.put("permissions", permissions);
    478 
    479         ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId);
    480         if (result != null) {
    481             return new ObjectFactory().createResponseBody(result);
    482         } else {
     538        try {
     539            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId);
     540            if (result != null) {
     541                return new ObjectFactory().createResponseBody(result);
     542            } else {
     543                return new ObjectFactory().createResponseBody(new ResponseBody());
     544            }
     545        } catch (NotInDataBaseException e1) {
     546            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
    483547            return new ObjectFactory().createResponseBody(new ResponseBody());
    484         }
    485 
     548        } catch (ForbiddenException e2) {
     549            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     550            return new ObjectFactory().createResponseBody(new ResponseBody());
     551        }
    486552
    487553    }
     
    505571    public String deletePrincipalsAccess(@PathParam("annotationId") String annotationId,
    506572            @PathParam("principalId") String principalId) throws IOException {
    507         return this.genericUpdateDeleteAccess(annotationId, principalId, null);
     573        try {
     574            return this.genericUpdateDeleteAccess(annotationId, principalId, null);
     575        } catch (NotInDataBaseException e1) {
     576            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     577            return e1.getMessage();
     578        } catch (ForbiddenException e2) {
     579            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     580            return e2.getMessage();
     581        }
    508582    }
    509583}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java

    r5468 r5679  
    2121import com.sun.jersey.multipart.MultiPart;
    2222import eu.dasish.annotation.backend.BackendConstants;
     23import eu.dasish.annotation.backend.ForbiddenException;
    2324import eu.dasish.annotation.backend.NotInDataBaseException;
    2425import eu.dasish.annotation.backend.Resource;
     
    3738import javax.imageio.ImageIO;
    3839import javax.servlet.http.HttpServletRequest;
     40import javax.servlet.http.HttpServletResponse;
    3941import javax.ws.rs.Consumes;
    4042import javax.ws.rs.GET;
     
    6870    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws IOException {
    6971        Map params = new HashMap();
    70         CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
    71         if (result != null) {
    72             return (new ObjectFactory()).createCashedRepresentationInfo(result);
    73         } else {
    74             return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo());
     72        try {
     73            CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
     74            if (result != null) {
     75                return (new ObjectFactory()).createCashedRepresentationInfo(result);
     76            } else {
     77                return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo());
     78            }
     79        } catch (NotInDataBaseException e1) {
     80            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     81            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
     82        } catch (ForbiddenException e2) {
     83            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     84            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
    7585        }
    7686    }
     
    92102    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws IOException {
    93103        Map params = new HashMap();
    94         InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
    95         if (result != null) {
    96             ImageIO.setUseCache(false);
    97             try {
    98                 BufferedImage retVal = ImageIO.read(result);
    99                 return retVal;
    100             } catch (IOException e1) {
    101                 loggerServer.info(e1.toString());
    102 
     104        try {
     105            InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
     106            if (result != null) {
     107                ImageIO.setUseCache(false);
     108                try {
     109                    BufferedImage retVal = ImageIO.read(result);
     110                    return retVal;
     111                } catch (IOException e1) {
     112                    loggerServer.info(e1.toString());
     113
     114                    return null;
     115                }
     116            } else {
     117                loggerServer.info(" The cached representation with the id " + externalId + " has null blob.");
    103118                return null;
    104119            }
    105         } else {
    106             loggerServer.info(" The cached representation with the id " + externalId + " has null blob.");
     120        } catch (NotInDataBaseException e1) {
     121            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     122            return null;
     123        } catch (ForbiddenException e2) {
     124            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    107125            return null;
    108126        }
     
    116134    public InputStream getCachedRepresentationContentStream(@PathParam("cachedid") String externalId) throws IOException {
    117135        Map params = new HashMap();
    118         return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
     136        try {
     137            return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
     138        } catch (NotInDataBaseException e1) {
     139            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     140            return null;
     141        } catch (ForbiddenException e2) {
     142            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     143            return null;
     144        }
    119145    }
    120146
     
    138164        BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity();
    139165        params.put("stream", bpe.getInputStream());
    140         Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
    141         if (result != null) {
    142             return result + "rows are updated";
    143         } else {
    144             return "Nothing is updated. ";
    145         }
    146 
     166        try {
     167            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
     168            if (result != null) {
     169                return result + "rows are updated";
     170            } else {
     171                return "Nothing is updated. ";
     172            }
     173        } catch (NotInDataBaseException e1) {
     174            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     175            return e1.getMessage();
     176        } catch (ForbiddenException e2) {
     177            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     178            return e2.getMessage();
     179        }
    147180
    148181    }
     
    161194            input = blob.openStream();
    162195        } else {
    163             input = new FileInputStream (blobPath);
     196            input = new FileInputStream(blobPath);
    164197        }
    165198
    166199        params.put("stream", input);
    167         Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
    168         input.close();
    169         if (result != null) {
    170             return result + "rows are updated";
    171         } else {
    172             return "Nothing is updated. ";
    173         }
    174 
     200        try {
     201            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
     202            input.close();
     203            if (result != null) {
     204                return result + "rows are updated";
     205            } else {
     206                return "Nothing is updated. ";
     207            }
     208        } catch (NotInDataBaseException e1) {
     209            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     210            return e1.getMessage();
     211        } catch (ForbiddenException e2) {
     212            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     213            return e2.getMessage();
     214        }
    175215
    176216    }
     
    199239        Map params = new HashMap();
    200240        params.put("info", cachedInfo);
    201         Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getId());
    202         if (result != null) {
    203             return result + "rows are updated";
    204         } else {
    205             return "Nothing is updated. ";
    206         }
    207 
     241        try {
     242            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getId());
     243            if (result != null) {
     244                return result + "rows are updated";
     245            } else {
     246                return "Nothing is updated. ";
     247            }
     248        } catch (NotInDataBaseException e1) {
     249            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     250            return e1.getMessage();
     251        } catch (ForbiddenException e2) {
     252            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
     253            return e2.getMessage();
     254        }
    208255    }
    209256
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java

    r5385 r5679  
    1818package eu.dasish.annotation.backend.rest;
    1919
     20import eu.dasish.annotation.backend.ForbiddenException;
    2021import eu.dasish.annotation.backend.NotInDataBaseException;
    2122import eu.dasish.annotation.backend.PrincipalCannotBeDeleted;
     
    217218
    218219        dbDispatcher.setResourcesPaths(this.getRelativeServiceURI());
    219         return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
     220        try {
     221            return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
     222        } catch (NotInDataBaseException e1) {
     223            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     224            return new ObjectFactory().createPrincipal(new Principal());
     225        } catch (PrincipalExists e2) {
     226            httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, e2.getMessage());
     227            return new ObjectFactory().createPrincipal(new Principal());
     228        }
    220229    }
    221230
     
    251260
    252261        dbDispatcher.setResourcesPaths(this.getRelativeServiceURI());
    253         return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterShibbolizedPrincipal());
     262        try {
     263            return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterShibbolizedPrincipal());
     264        } catch (NotInDataBaseException e1) {
     265            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     266            return new ObjectFactory().createPrincipal(new Principal());
     267        } catch (PrincipalExists e2) {
     268            httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, e2.getMessage());
     269            return new ObjectFactory().createPrincipal(new Principal());
     270        }
    254271    }
    255272
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java

    r5385 r5679  
    1818package eu.dasish.annotation.backend.rest;
    1919
     20import eu.dasish.annotation.backend.ForbiddenException;
    2021import eu.dasish.annotation.backend.NotInDataBaseException;
    2122import eu.dasish.annotation.backend.PrincipalExists;
     
    6768    }
    6869
    69     public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId) throws IOException {
     70    public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId) throws IOException, ForbiddenException, NotInDataBaseException {
    7071        Number principalID = resourceResource.getPrincipalID();
    7172        if (principalID == null) {
     
    7374        }
    7475        params.put(_principalID, principalID);
    75         try {
    76             final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource);
    77             if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) {
    78                 params.put(_externalId, externalId);           
    79                 params.put(_internalID, resourceID);
    80                 params.put(_resourceType, resource);
    81                 return dbRequestor.apply(params);
    82             } else {
    83                 this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name());
    84                 resourceResource.loggerServer.debug("Principal " + resourceResource.dbDispatcher.getResourceExternalIdentifier(principalID, Resource.PRINCIPAL) + " cannot " + action.name() + " " + resource.name() + " with the id " + externalId);
    85                 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
    86                 return null;
    87             }
    88         } catch (NotInDataBaseException e2) {
    89             resourceResource.loggerServer.debug(e2.toString());
    90             resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
    91             return null;
    92         }
    93     }
    94 
    95     public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException {
    96 
    97         try {
    98             try {
    99                 return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
    100             } catch (NotInDataBaseException e1) {
    101                 resourceResource.loggerServer.debug(e1.toString());
    102                 resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
    103                 return new ObjectFactory().createPrincipal(new Principal());
    104             }
    105         } catch (PrincipalExists e) {
    106             resourceResource.loggerServer.debug(e.toString());
    107             resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    108             return new ObjectFactory().createPrincipal(new Principal());
     76        final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource);
     77        if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) {
     78            params.put(_externalId, externalId);
     79            params.put(_internalID, resourceID);
     80            params.put(_resourceType, resource);
     81            return dbRequestor.apply(params);
     82        } else {
     83            throw new ForbiddenException(this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name()));
    10984        }
    11085
    11186    }
     87
     88    public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException, NotInDataBaseException, PrincipalExists {
     89        return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
     90    }
    11291}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java

    r5385 r5679  
    2121import com.sun.jersey.multipart.MultiPart;
    2222import eu.dasish.annotation.backend.BackendConstants;
     23import eu.dasish.annotation.backend.ForbiddenException;
    2324import eu.dasish.annotation.backend.NotInDataBaseException;
    2425import eu.dasish.annotation.backend.Resource;
     
    2829import eu.dasish.annotation.schema.ObjectFactory;
    2930import eu.dasish.annotation.schema.ReferenceList;
     31import eu.dasish.annotation.schema.ResponseBody;
    3032import eu.dasish.annotation.schema.Target;
    3133import java.io.IOException;
     
    7981    public JAXBElement<Target> getTarget(@PathParam("targetid") String externalIdentifier) throws IOException {
    8082        Map params = new HashMap();
    81         Target result = (Target) (new RequestWrappers(this)).wrapRequestResource(params, new GetTarget(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
    82         if (result != null) {
    83             return new ObjectFactory().createTarget(result);
    84         } else {
     83        try {
     84            Target result = (Target) (new RequestWrappers(this)).wrapRequestResource(params, new GetTarget(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
     85            if (result != null) {
     86                return new ObjectFactory().createTarget(result);
     87            } else {
     88                return new ObjectFactory().createTarget(new Target());
     89            }
     90        } catch (NotInDataBaseException e1) {
     91            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     92            return new ObjectFactory().createTarget(new Target());
     93        } catch (ForbiddenException e2) {
     94            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    8595            return new ObjectFactory().createTarget(new Target());
    8696        }
     
    103113    public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String externalIdentifier) throws HTTPException, IOException {
    104114        Map params = new HashMap();
    105         ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetSiblingTargets(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
    106         if (result != null) {
    107             return new ObjectFactory().createReferenceList(result);
    108         } else {
     115        try {
     116            ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetSiblingTargets(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
     117            if (result != null) {
     118                return new ObjectFactory().createReferenceList(result);
     119            } else {
     120                return new ObjectFactory().createReferenceList(new ReferenceList());
     121            }
     122        } catch (NotInDataBaseException e1) {
     123            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     124            return new ObjectFactory().createReferenceList(new ReferenceList());
     125        } catch (ForbiddenException e2) {
     126            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    109127            return new ObjectFactory().createReferenceList(new ReferenceList());
    110128        }
     
    134152        params.put("cachedBlob", bpe.getInputStream());
    135153        params.put("fragmentDescriptor", fragmentDescriptor);
    136        
    137         CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new PostCached(), Resource.TARGET, ResourceAction.WRITE_W_METAINFO, targetIdentifier);
    138         if (result != null) {
    139             return new ObjectFactory().createCashedRepresentationInfo(result);
    140         } else {
     154        try {
     155            CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new PostCached(), Resource.TARGET, ResourceAction.WRITE_W_METAINFO, targetIdentifier);
     156            if (result != null) {
     157                return new ObjectFactory().createCashedRepresentationInfo(result);
     158            } else {
     159                return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
     160            }
     161        } catch (NotInDataBaseException e1) {
     162            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
     163            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
     164        } catch (ForbiddenException e2) {
     165            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
    141166            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
    142167        }
     
    152177            InputStream cachedSource = (InputStream) params.get("cachedBlob");
    153178            try {
    154             final Number[] respondDB = dbDispatcher.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
    155             return dbDispatcher.getCachedRepresentationInfo(respondDB[1]);
     179                final Number[] respondDB = dbDispatcher.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
     180                return dbDispatcher.getCachedRepresentationInfo(respondDB[1]);
    156181            } catch (IOException e) {
    157182                loggerServer.info(e.toString());
Note: See TracChangeset for help on using the changeset viewer.