Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/UPDATE.txt
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/UPDATE.txt	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/UPDATE.txt	(revision 5679)
@@ -3,5 +3,5 @@
 standard MPI deployment procedure, for webannotator-basic.  
 Consult the instructions from INSTALL.txt  about placing war file if you have forgotten.
-
+ override="true
 2. The web.xml stays intact  because currently it is set for basic authentication.
 
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/ForbiddenException.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/ForbiddenException.java	(revision 5679)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/ForbiddenException.java	(revision 5679)
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2013 DASISH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+package eu.dasish.annotation.backend;
+
+/**
+ *
+ * @author olhsha
+ */
+public class ForbiddenException extends Exception{
+    public ForbiddenException(String message){
+        super(message);
+    }
+}
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java	(revision 5679)
@@ -19,4 +19,5 @@
 
 import eu.dasish.annotation.backend.BackendConstants;
+import eu.dasish.annotation.backend.ForbiddenException;
 import eu.dasish.annotation.backend.NotInDataBaseException;
 import eu.dasish.annotation.backend.Resource;
@@ -85,8 +86,16 @@
     public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException {
         Map params = new HashMap();
-        Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
-        if (result != null) {
-            return (new ObjectFactory()).createAnnotation(result);
-        } else {
+        try {
+            Annotation result = (Annotation) (new RequestWrappers(this)).wrapRequestResource(params, new GetAnnotation(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
+            if (result != null) {
+                return (new ObjectFactory()).createAnnotation(result);
+            } else {
+                return (new ObjectFactory()).createAnnotation(new Annotation());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createAnnotation(new Annotation());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return (new ObjectFactory()).createAnnotation(new Annotation());
         }
@@ -108,9 +117,17 @@
     public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String externalIdentifier) throws IOException {
         Map params = new HashMap();
-        ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
-        if (result != null) {
-            return (new ObjectFactory()).createTargetList(result);
-        } else {
-            return (new ObjectFactory()).createTargetList(new ReferenceList());
+        try {
+            ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetTargetList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
+            if (result != null) {
+                return (new ObjectFactory()).createTargetList(result);
+            } else {
+                return (new ObjectFactory()).createTargetList(new ReferenceList());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createReferenceList(new ReferenceList());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return (new ObjectFactory()).createReferenceList(new ReferenceList());
         }
     }
@@ -168,8 +185,16 @@
     public JAXBElement<PermissionList> getAnnotationPermissions(@PathParam("annotationid") String externalIdentifier) throws IOException {
         Map params = new HashMap();
-        PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
-        if (result != null) {
-            return (new ObjectFactory()).createPermissionList(result);
-        } else {
+        try {
+            PermissionList result = (PermissionList) (new RequestWrappers(this)).wrapRequestResource(params, new GetPermissionList(), Resource.ANNOTATION, ResourceAction.READ, externalIdentifier);
+            if (result != null) {
+                return (new ObjectFactory()).createPermissionList(result);
+            } else {
+                return (new ObjectFactory()).createPermissionList(new PermissionList());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createPermissionList(new PermissionList());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return (new ObjectFactory()).createPermissionList(new PermissionList());
         }
@@ -189,9 +214,17 @@
     public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException {
         Map params = new HashMap();
-        int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier);
-        if (result != null) {
-            return result[0] + " annotation(s) is(are) deleted.";
-        } else {
-            return "Nothing is deleted.";
+        try {
+            int[] result = (int[]) (new RequestWrappers(this)).wrapRequestResource(params, new DeleteAnnotation(), Resource.ANNOTATION, ResourceAction.DELETE, externalIdentifier);
+            if (result != null) {
+                return result[0] + " annotation(s) is(are) deleted.";
+            } else {
+                return "Nothing is deleted.";
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
         }
     }
@@ -247,14 +280,20 @@
             return null;
         }
-        Map params = new HashMap();
-        params.put("annotation", annotation);
-        ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId);
-        if (result != null) {
-            return (new ObjectFactory()).createResponseBody(result);
-        } else {
-            return (new ObjectFactory()).createResponseBody(new ResponseBody());
-        }
-
-
+        try {
+            Map params = new HashMap();
+            params.put("annotation", annotation);
+            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotation(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, externalId);
+            if (result != null) {
+                return (new ObjectFactory()).createResponseBody(result);
+            } else {
+                return (new ObjectFactory()).createResponseBody(new ResponseBody());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createResponseBody(new ResponseBody());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return (new ObjectFactory()).createResponseBody(new ResponseBody());
+        }
     }
 
@@ -279,8 +318,16 @@
         Map params = new HashMap();
         params.put("annotationBody", annotationBody);
-        ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
-        if (result != null) {
-            return (new ObjectFactory()).createResponseBody(result);
-        } else {
+        try {
+            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationBody(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
+            if (result != null) {
+                return (new ObjectFactory()).createResponseBody(result);
+            } else {
+                return (new ObjectFactory()).createResponseBody(new ResponseBody());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createResponseBody(new ResponseBody());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return (new ObjectFactory()).createResponseBody(new ResponseBody());
         }
@@ -307,8 +354,16 @@
         Map params = new HashMap();
         params.put("headline", newHeadline);
-        ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
-        if (result != null) {
-            return (new ObjectFactory()).createResponseBody(result);
-        } else {
+        try {
+            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateAnnotationHeadline(), Resource.ANNOTATION, ResourceAction.WRITE, externalIdentifier);
+            if (result != null) {
+                return (new ObjectFactory()).createResponseBody(result);
+            } else {
+                return (new ObjectFactory()).createResponseBody(new ResponseBody());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return (new ObjectFactory()).createResponseBody(new ResponseBody());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return (new ObjectFactory()).createResponseBody(new ResponseBody());
         }
@@ -327,8 +382,7 @@
         }
     }
-    
+
     //////////////////////////////////////////
-    
-     //////////////////////////////////////////////
+    //////////////////////////////////////////////
     @POST
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@@ -340,12 +394,12 @@
             @FormParam("access") String access)
             throws IOException {
-        
+
         if (access.trim().equals("")) {
             access = "none";
         }
-        
+
         Access accessTyped = Access.fromValue(access);
         int updatedAnnotations = 0;
-        
+
         if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
             List<Number> annotationIDs = dbDispatcher.getAnnotationInternalIDsFromHeadline(annotationHeadline);
@@ -356,20 +410,19 @@
             for (Number annotationID : annotationIDs) {
                 count = dbDispatcher.updatePublicAttribute(annotationID, accessTyped);;
-                updatedAnnotations = updatedAnnotations+count;
-            }            
+                updatedAnnotations = updatedAnnotations + count;
+            }
             return (updatedAnnotations + " row(s) are updated");
         } else {
             try {
-            Number annotationID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(annotationDatabaseId), Resource.ANNOTATION);
-            updatedAnnotations= dbDispatcher.updatePublicAttribute(annotationID, accessTyped);
+                Number annotationID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(annotationDatabaseId), Resource.ANNOTATION);
+                updatedAnnotations = dbDispatcher.updatePublicAttribute(annotationID, accessTyped);
             } catch (NotInDataBaseException e) {
-              httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());   
+                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString());
             }
         }
         return (updatedAnnotations + " annotation(s) are updated.");
     }
-    
+
     ////////////////////////////////////////////////////
-
     @PUT
     @Consumes(MediaType.APPLICATION_XML)
@@ -378,8 +431,14 @@
     public String updateAccess(@PathParam("annotationid") String annotationExternalId,
             @PathParam("principalid") String principalExternalId, Access access) throws IOException {
-        return this.genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access);
-    }
-    
-
+        try {
+            return this.genericUpdateDeleteAccess(annotationExternalId, principalExternalId, access);
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
+    }
 
     //////////////////////////////////////////////
@@ -395,10 +454,11 @@
             @FormParam("access") String access)
             throws IOException {
-        
-        if (access.trim().equals("")) {
-            access = null;
-        }
-        
-        try {
+
+        try {
+
+            if (access.trim().equals("")) {
+                access = null;
+            }
+
             if (principalDatabaseId == null || principalDatabaseId.trim().equals("")) {
                 if (remoteID != null && !remoteID.trim().equals("")) {
@@ -412,46 +472,46 @@
                 }
             }
-        } catch (NotInDataBaseException e) {
-            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.toString()); 
-        }
-
-        if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
-            List<UUID> annotationIds = dbDispatcher.getAnnotationExternalIdsFromHeadline(annotationHeadline);
-            if (annotationIds == null || annotationIds.isEmpty()) {
-                return "No annotations with this headline found";
-            };
-            int count = 0;
-            String tmp = null;
-            for (UUID annotationId : annotationIds) {
-                tmp = this.genericUpdateDeleteAccess(annotationId.toString(), principalDatabaseId, Access.fromValue(access));
-                if (!tmp.startsWith("0")) {
-                    count++;
+
+            if (annotationDatabaseId == null || annotationDatabaseId.trim().equals("")) {
+                List<UUID> annotationIds = dbDispatcher.getAnnotationExternalIdsFromHeadline(annotationHeadline);
+                if (annotationIds == null || annotationIds.isEmpty()) {
+                    return "No annotations with this headline found";
+                };
+                int count = 0;
+                String tmp = null;
+                for (UUID annotationId : annotationIds) {
+                    tmp = this.genericUpdateDeleteAccess(annotationId.toString(), principalDatabaseId, Access.fromValue(access));
+                    if (!tmp.startsWith("0")) {
+                        count++;
+                    }
                 }
-            }            
-            return (count + " row(s) are updated");
+                return (count + " row(s) are updated");
+            } else {
+                return this.genericUpdateDeleteAccess(annotationDatabaseId, principalDatabaseId, Access.fromValue(access));
+            }
+
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
+    }
+
+    ////////////////////////////////////////////
+    private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException, NotInDataBaseException, ForbiddenException {
+        Map params = new HashMap();
+        params.put("access", access);
+        final Number inputPrincipalID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL);
+        params.put("inputPrincipalID", inputPrincipalID);
+        Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId);
+        if (result != null) {
+            return result + " row(s) is(are) updated.";
         } else {
-            return this.genericUpdateDeleteAccess(annotationDatabaseId, principalDatabaseId, Access.fromValue(access));
-        }
-    }
-
-    ////////////////////////////////////////////
-    private String genericUpdateDeleteAccess(String annotationId, String principalId, Access access) throws IOException {
-        Map params = new HashMap();
-        params.put("access", access);
-        try {
-            final Number inputPrincipalID = dbDispatcher.getResourceInternalIdentifier(UUID.fromString(principalId), Resource.PRINCIPAL);
-            params.put("inputPrincipalID", inputPrincipalID);
-            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePrincipalAccess(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationId);
-            if (result != null) {
-                return result + " row(s) is(are) updated.";
-            } else {
-                return "Nothing is updated.";
-            }
-
-        } catch (NotInDataBaseException e2) {
-            loggerServer.debug(e2.toString());
-            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
-            return "Nothing is deleted.";
-        }
+            return "0 rows are updated.";
+        }
+
+
     }
 
@@ -476,12 +536,18 @@
         Map params = new HashMap();
         params.put("permissions", permissions);
-
-        ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId);
-        if (result != null) {
-            return new ObjectFactory().createResponseBody(result);
-        } else {
+        try {
+            ResponseBody result = (ResponseBody) (new RequestWrappers(this)).wrapRequestResource(params, new UpdatePermissions(), Resource.ANNOTATION, ResourceAction.WRITE_W_METAINFO, annotationExternalId);
+            if (result != null) {
+                return new ObjectFactory().createResponseBody(result);
+            } else {
+                return new ObjectFactory().createResponseBody(new ResponseBody());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
             return new ObjectFactory().createResponseBody(new ResponseBody());
-        }
-
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return new ObjectFactory().createResponseBody(new ResponseBody());
+        }
 
     }
@@ -505,5 +571,13 @@
     public String deletePrincipalsAccess(@PathParam("annotationId") String annotationId,
             @PathParam("principalId") String principalId) throws IOException {
-        return this.genericUpdateDeleteAccess(annotationId, principalId, null);
+        try {
+            return this.genericUpdateDeleteAccess(annotationId, principalId, null);
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
     }
 }
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java	(revision 5679)
@@ -21,4 +21,5 @@
 import com.sun.jersey.multipart.MultiPart;
 import eu.dasish.annotation.backend.BackendConstants;
+import eu.dasish.annotation.backend.ForbiddenException;
 import eu.dasish.annotation.backend.NotInDataBaseException;
 import eu.dasish.annotation.backend.Resource;
@@ -37,4 +38,5 @@
 import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
@@ -68,9 +70,17 @@
     public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws IOException {
         Map params = new HashMap();
-        CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
-        if (result != null) {
-            return (new ObjectFactory()).createCashedRepresentationInfo(result);
-        } else {
-            return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo());
+        try {
+            CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInfo(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
+            if (result != null) {
+                return (new ObjectFactory()).createCashedRepresentationInfo(result);
+            } else {
+                return (new ObjectFactory()).createCashedRepresentationInfo(new CachedRepresentationInfo());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
         }
     }
@@ -92,17 +102,25 @@
     public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws IOException {
         Map params = new HashMap();
-        InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
-        if (result != null) {
-            ImageIO.setUseCache(false);
-            try {
-                BufferedImage retVal = ImageIO.read(result);
-                return retVal;
-            } catch (IOException e1) {
-                loggerServer.info(e1.toString());
-
+        try {
+            InputStream result = (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
+            if (result != null) {
+                ImageIO.setUseCache(false);
+                try {
+                    BufferedImage retVal = ImageIO.read(result);
+                    return retVal;
+                } catch (IOException e1) {
+                    loggerServer.info(e1.toString());
+
+                    return null;
+                }
+            } else {
+                loggerServer.info(" The cached representation with the id " + externalId + " has null blob.");
                 return null;
             }
-        } else {
-            loggerServer.info(" The cached representation with the id " + externalId + " has null blob.");
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return null;
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return null;
         }
@@ -116,5 +134,13 @@
     public InputStream getCachedRepresentationContentStream(@PathParam("cachedid") String externalId) throws IOException {
         Map params = new HashMap();
-        return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
+        try {
+            return (InputStream) (new RequestWrappers(this)).wrapRequestResource(params, new GetCachedRepresentationInputStream(), Resource.CACHED_REPRESENTATION, ResourceAction.READ, externalId);
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return null;
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return null;
+        }
     }
 
@@ -138,11 +164,18 @@
         BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity();
         params.put("stream", bpe.getInputStream());
-        Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
-        if (result != null) {
-            return result + "rows are updated";
-        } else {
-            return "Nothing is updated. ";
-        }
-
+        try {
+            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
+            if (result != null) {
+                return result + "rows are updated";
+            } else {
+                return "Nothing is updated. ";
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
 
     }
@@ -161,16 +194,23 @@
             input = blob.openStream();
         } else {
-            input = new FileInputStream (blobPath);
+            input = new FileInputStream(blobPath);
         }
 
         params.put("stream", input);
-        Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
-        input.close();
-        if (result != null) {
-            return result + "rows are updated";
-        } else {
-            return "Nothing is updated. ";
-        }
-
+        try {
+            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedBlob(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE, cachedIdentifier);
+            input.close();
+            if (result != null) {
+                return result + "rows are updated";
+            } else {
+                return "Nothing is updated. ";
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
 
     }
@@ -199,11 +239,18 @@
         Map params = new HashMap();
         params.put("info", cachedInfo);
-        Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getId());
-        if (result != null) {
-            return result + "rows are updated";
-        } else {
-            return "Nothing is updated. ";
-        }
-
+        try {
+            Integer result = (Integer) (new RequestWrappers(this)).wrapRequestResource(params, new UpdateCachedMetadata(), Resource.CACHED_REPRESENTATION, ResourceAction.WRITE_W_METAINFO, cachedInfo.getId());
+            if (result != null) {
+                return result + "rows are updated";
+            } else {
+                return "Nothing is updated. ";
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return e1.getMessage();
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
+            return e2.getMessage();
+        }
     }
 
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/PrincipalResource.java	(revision 5679)
@@ -18,4 +18,5 @@
 package eu.dasish.annotation.backend.rest;
 
+import eu.dasish.annotation.backend.ForbiddenException;
 import eu.dasish.annotation.backend.NotInDataBaseException;
 import eu.dasish.annotation.backend.PrincipalCannotBeDeleted;
@@ -217,5 +218,13 @@
 
         dbDispatcher.setResourcesPaths(this.getRelativeServiceURI());
-        return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
+        try {
+            return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterNonShibbolizedPrincipal());
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createPrincipal(new Principal());
+        } catch (PrincipalExists e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, e2.getMessage());
+            return new ObjectFactory().createPrincipal(new Principal());
+        }
     }
 
@@ -251,5 +260,13 @@
 
         dbDispatcher.setResourcesPaths(this.getRelativeServiceURI());
-        return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterShibbolizedPrincipal());
+        try {
+            return (new RequestWrappers(this)).wrapAddPrincipalRequest(params, new RegisterShibbolizedPrincipal());
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createPrincipal(new Principal());
+        } catch (PrincipalExists e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, e2.getMessage());
+            return new ObjectFactory().createPrincipal(new Principal());
+        }
     }
 
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/RequestWrappers.java	(revision 5679)
@@ -18,4 +18,5 @@
 package eu.dasish.annotation.backend.rest;
 
+import eu.dasish.annotation.backend.ForbiddenException;
 import eu.dasish.annotation.backend.NotInDataBaseException;
 import eu.dasish.annotation.backend.PrincipalExists;
@@ -67,5 +68,5 @@
     }
 
-    public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId) throws IOException {
+    public T wrapRequestResource(Map params, ILambda<Map, T> dbRequestor, Resource resource, ResourceAction action, String externalId) throws IOException, ForbiddenException, NotInDataBaseException {
         Number principalID = resourceResource.getPrincipalID();
         if (principalID == null) {
@@ -73,40 +74,18 @@
         }
         params.put(_principalID, principalID);
-        try {
-            final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource);
-            if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) {
-                params.put(_externalId, externalId);            
-                params.put(_internalID, resourceID);
-                params.put(_resourceType, resource);
-                return dbRequestor.apply(params);
-            } else {
-                this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name());
-                resourceResource.loggerServer.debug("Principal " + resourceResource.dbDispatcher.getResourceExternalIdentifier(principalID, Resource.PRINCIPAL) + " cannot " + action.name() + " " + resource.name() + " with the id " + externalId);
-                resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
-                return null;
-            }
-        } catch (NotInDataBaseException e2) {
-            resourceResource.loggerServer.debug(e2.toString());
-            resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e2.toString());
-            return null;
-        }
-    }
-
-    public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException {
-
-        try {
-            try {
-                return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
-            } catch (NotInDataBaseException e1) {
-                resourceResource.loggerServer.debug(e1.toString());
-                resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.toString());
-                return new ObjectFactory().createPrincipal(new Principal());
-            }
-        } catch (PrincipalExists e) {
-            resourceResource.loggerServer.debug(e.toString());
-            resourceResource.httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
-            return new ObjectFactory().createPrincipal(new Principal());
+        final Number resourceID = resourceResource.dbDispatcher.getResourceInternalIdentifier(UUID.fromString(externalId), resource);
+        if (resourceResource.dbDispatcher.canDo(action, principalID, resourceID, resource)) {
+            params.put(_externalId, externalId);
+            params.put(_internalID, resourceID);
+            params.put(_resourceType, resource);
+            return dbRequestor.apply(params);
+        } else {
+            throw new ForbiddenException(this.FORBIDDEN_RESOURCE_ACTION(externalId, resource.name(), action.name()));
         }
 
     }
+
+    public JAXBElement<Principal> wrapAddPrincipalRequest(Map params, ILambdaPrincipal<Map, Principal> dbRequestor) throws IOException, NotInDataBaseException, PrincipalExists {
+        return new ObjectFactory().createPrincipal(dbRequestor.apply(params));
+    }
 }
Index: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java
===================================================================
--- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java	(revision 5678)
+++ DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java	(revision 5679)
@@ -21,4 +21,5 @@
 import com.sun.jersey.multipart.MultiPart;
 import eu.dasish.annotation.backend.BackendConstants;
+import eu.dasish.annotation.backend.ForbiddenException;
 import eu.dasish.annotation.backend.NotInDataBaseException;
 import eu.dasish.annotation.backend.Resource;
@@ -28,4 +29,5 @@
 import eu.dasish.annotation.schema.ObjectFactory;
 import eu.dasish.annotation.schema.ReferenceList;
+import eu.dasish.annotation.schema.ResponseBody;
 import eu.dasish.annotation.schema.Target;
 import java.io.IOException;
@@ -79,8 +81,16 @@
     public JAXBElement<Target> getTarget(@PathParam("targetid") String externalIdentifier) throws IOException {
         Map params = new HashMap();
-        Target result = (Target) (new RequestWrappers(this)).wrapRequestResource(params, new GetTarget(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
-        if (result != null) {
-            return new ObjectFactory().createTarget(result);
-        } else {
+        try {
+            Target result = (Target) (new RequestWrappers(this)).wrapRequestResource(params, new GetTarget(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
+            if (result != null) {
+                return new ObjectFactory().createTarget(result);
+            } else {
+                return new ObjectFactory().createTarget(new Target());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createTarget(new Target());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return new ObjectFactory().createTarget(new Target());
         }
@@ -103,8 +113,16 @@
     public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String externalIdentifier) throws HTTPException, IOException {
         Map params = new HashMap();
-        ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetSiblingTargets(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
-        if (result != null) {
-            return new ObjectFactory().createReferenceList(result);
-        } else {
+        try {
+            ReferenceList result = (ReferenceList) (new RequestWrappers(this)).wrapRequestResource(params, new GetSiblingTargets(), Resource.TARGET, ResourceAction.READ, externalIdentifier);
+            if (result != null) {
+                return new ObjectFactory().createReferenceList(result);
+            } else {
+                return new ObjectFactory().createReferenceList(new ReferenceList());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createReferenceList(new ReferenceList());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return new ObjectFactory().createReferenceList(new ReferenceList());
         }
@@ -134,9 +152,16 @@
         params.put("cachedBlob", bpe.getInputStream());
         params.put("fragmentDescriptor", fragmentDescriptor);
-        
-        CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new PostCached(), Resource.TARGET, ResourceAction.WRITE_W_METAINFO, targetIdentifier);
-        if (result != null) {
-            return new ObjectFactory().createCashedRepresentationInfo(result);
-        } else {
+        try {
+            CachedRepresentationInfo result = (CachedRepresentationInfo) (new RequestWrappers(this)).wrapRequestResource(params, new PostCached(), Resource.TARGET, ResourceAction.WRITE_W_METAINFO, targetIdentifier);
+            if (result != null) {
+                return new ObjectFactory().createCashedRepresentationInfo(result);
+            } else {
+                return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
+            }
+        } catch (NotInDataBaseException e1) {
+            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e1.getMessage());
+            return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
+        } catch (ForbiddenException e2) {
+            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, e2.getMessage());
             return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
         }
@@ -152,6 +177,6 @@
             InputStream cachedSource = (InputStream) params.get("cachedBlob");
             try {
-            final Number[] respondDB = dbDispatcher.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
-            return dbDispatcher.getCachedRepresentationInfo(respondDB[1]);
+                final Number[] respondDB = dbDispatcher.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
+                return dbDispatcher.getCachedRepresentationInfo(respondDB[1]);
             } catch (IOException e) {
                 loggerServer.info(e.toString());
