Closes #20: Clean error message if document or file does not exist

This commit is contained in:
jendib 2015-08-26 22:11:39 +02:00
parent 5cbdb5d87d
commit 86cae53789
9 changed files with 50 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
@ -55,7 +56,7 @@ public class AuditLogResource extends BaseResource {
// Check ACL on the document // Check ACL on the document
AclDao aclDao = new AclDao(); AclDao aclDao = new AclDao();
if (!aclDao.checkPermission(documentId, PermType.READ, principal.getId())) { if (!aclDao.checkPermission(documentId, PermType.READ, principal.getId())) {
throw new ForbiddenClientException(); return Response.status(Status.NOT_FOUND).build();
} }
criteria.setDocumentId(documentId); criteria.setDocumentId(documentId);
} }

View File

@ -20,6 +20,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
@ -92,7 +93,7 @@ public class DocumentResource extends BaseResource {
throw new ForbiddenClientException(); throw new ForbiddenClientException();
} }
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); return Response.status(Status.NOT_FOUND).build();
} }
JSONObject document = new JSONObject(); JSONObject document = new JSONObject();
@ -430,7 +431,7 @@ public class DocumentResource extends BaseResource {
try { try {
document = documentDao.getDocument(id, PermType.WRITE, principal.getId()); document = documentDao.getDocument(id, PermType.WRITE, principal.getId());
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", id)); return Response.status(Status.NOT_FOUND).build();
} }
// Update the document // Update the document
@ -514,7 +515,7 @@ public class DocumentResource extends BaseResource {
document = documentDao.getDocument(id, PermType.WRITE, principal.getId()); document = documentDao.getDocument(id, PermType.WRITE, principal.getId());
fileList = fileDao.getByDocumentId(principal.getId(), id); fileList = fileDao.getByDocumentId(principal.getId(), id);
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", id)); return Response.status(Status.NOT_FOUND).build();
} }
// Delete the document // Delete the document

View File

@ -102,7 +102,7 @@ public class FileResource extends BaseResource {
try { try {
document = documentDao.getDocument(documentId, PermType.WRITE, principal.getId()); document = documentDao.getDocument(documentId, PermType.WRITE, principal.getId());
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); return Response.status(Status.NOT_FOUND).build();
} }
} }
@ -199,7 +199,7 @@ public class FileResource extends BaseResource {
file = fileDao.getFile(id, principal.getId()); file = fileDao.getFile(id, principal.getId());
document = documentDao.getDocument(documentId, PermType.WRITE, principal.getId()); document = documentDao.getDocument(documentId, PermType.WRITE, principal.getId());
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); return Response.status(Status.NOT_FOUND).build();
} }
// Check that the file is orphan // Check that the file is orphan
@ -259,7 +259,7 @@ public class FileResource extends BaseResource {
try { try {
documentDao.getDocument(documentId, PermType.WRITE, principal.getId()); documentDao.getDocument(documentId, PermType.WRITE, principal.getId());
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); return Response.status(Status.NOT_FOUND).build();
} }
// Reorder files // Reorder files
@ -295,13 +295,9 @@ public class FileResource extends BaseResource {
// Check document visibility // Check document visibility
if (documentId != null) { if (documentId != null) {
try { AclDao aclDao = new AclDao();
AclDao aclDao = new AclDao(); if (!aclDao.checkPermission(documentId, PermType.READ, shareId == null ? principal.getId() : shareId)) {
if (!aclDao.checkPermission(documentId, PermType.READ, shareId == null ? principal.getId() : shareId)) { return Response.status(Status.NOT_FOUND).build();
throw new ForbiddenClientException();
}
} catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId));
} }
} else if (!authenticated) { } else if (!authenticated) {
throw new ForbiddenClientException(); throw new ForbiddenClientException();
@ -358,7 +354,7 @@ public class FileResource extends BaseResource {
documentDao.getDocument(file.getDocumentId(), PermType.WRITE, principal.getId()); documentDao.getDocument(file.getDocumentId(), PermType.WRITE, principal.getId());
} }
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("FileNotFound", MessageFormat.format("File not found: {0}", id)); return Response.status(Status.NOT_FOUND).build();
} }
// Delete the file // Delete the file
@ -498,7 +494,7 @@ public class FileResource extends BaseResource {
throw new ForbiddenClientException(); throw new ForbiddenClientException();
} }
} catch (NoResultException e) { } catch (NoResultException e) {
throw new ClientException("DocumentNotFound", MessageFormat.format("Document not found: {0}", documentId)); return Response.status(Status.NOT_FOUND).build();
} }
// Get files and user associated with this document // Get files and user associated with this document

View File

@ -7,6 +7,8 @@ angular.module('docs').controller('DocumentView', function ($scope, $state, $sta
// Load document data from server // Load document data from server
Restangular.one('document', $stateParams.id).get().then(function(data) { Restangular.one('document', $stateParams.id).get().then(function(data) {
$scope.document = data; $scope.document = data;
}, function(response) {
$scope.error = response;
}); });
// Load audit log data from server // Load audit log data from server

View File

@ -0,0 +1,16 @@
'use strict';
/**
* Image error event directive.
*/
angular.module('docs').directive('imgError', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('error', function() {
//call the function that was passed
scope.$apply(attrs.imgError);
});
}
};
})

View File

@ -64,6 +64,7 @@
<script src="app/docs/directive/SelectTag.js" type="text/javascript"></script> <script src="app/docs/directive/SelectTag.js" type="text/javascript"></script>
<script src="app/docs/directive/AuditLog.js" type="text/javascript"></script> <script src="app/docs/directive/AuditLog.js" type="text/javascript"></script>
<script src="app/docs/directive/InlineEdit.js" type="text/javascript"></script> <script src="app/docs/directive/InlineEdit.js" type="text/javascript"></script>
<script src="app/docs/directive/ImgError.js" type="text/javascript"></script>
<!-- endref --> <!-- endref -->
</head> </head>
<body> <body>

View File

@ -1,4 +1,11 @@
<img src="img/loader.gif" ng-show="!document" /> <img src="img/loader.gif" ng-show="!document && !error" />
<div ng-show="error" class="well-lg">
<p class="text-center" ng-show="error.status == 404">
<span class="glyphicon glyphicon-warning-sign"></span>
Document not found
</p>
</div>
<div ng-show="document"> <div ng-show="document">
<div class="text-right" ng-show="document.writable"> <div class="text-right" ng-show="document.writable">

View File

@ -22,5 +22,12 @@
<div class="text-center" ng-if="$stateParams.fileId"> <div class="text-center" ng-if="$stateParams.fileId">
<img ng-src="../api/file/{{ $stateParams.fileId }}/data?size=web" /> <img ng-src="../api/file/{{ $stateParams.fileId }}/data?size=web"
ng-init="error = false"
img-error="error = true"
ng-show="!error" />
<p class="well-lg" ng-show="error">
<span class="glyphicon glyphicon-warning-sign"></span>
File not found
</p>
</div> </div>

View File

@ -282,8 +282,7 @@ public class TestDocumentResource extends BaseJerseyTest {
documentResource = resource().path("/document/" + document1Id); documentResource = resource().path("/document/" + document1Id);
documentResource.addFilter(new CookieAuthenticationFilter(document1Token)); documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
response = documentResource.get(ClientResponse.class); response = documentResource.get(ClientResponse.class);
json = response.getEntity(JSONObject.class); Assert.assertEquals(Status.NOT_FOUND, Status.fromStatusCode(response.getStatus()));
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
} }
/** /**