Skip to content

Commit

Permalink
#251 added eTag verification for delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
GordeaS authored and GordeaS committed Jul 6, 2020
1 parent db473aa commit 1d81cc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import eu.europeana.api.common.config.I18nConstants;
import eu.europeana.api.commons.web.definitions.WebFields;
import eu.europeana.api.commons.web.exception.ApplicationAuthenticationException;
import eu.europeana.api.commons.web.exception.HeaderValidationException;
import eu.europeana.api.commons.web.exception.HttpException;
import eu.europeana.api.commons.web.http.HttpHeaders;

Expand Down Expand Up @@ -449,11 +448,12 @@ protected ResponseEntity<String> updateAnnotation(String identifier, String anno

try {
// String userId = authentication.getPrincipal().toString();

// 1. authorize user
// already performed in verify write access
// getAuthorizationService().authorizeUser(userId, authentication, annoId, Operations.UPDATE);
//check permissions for update
// getAuthorizationService().authorizeUser(userId, authentication, annoId,
// Operations.UPDATE);
// check permissions for update
Annotation storedAnnotation = verifyOwnerOrAdmin(identifier, authentication);

// 2. check time stamp
Expand All @@ -472,13 +472,13 @@ protected ResponseEntity<String> updateAnnotation(String identifier, String anno
String apiVersion = getConfiguration().getAnnotationApiVersion();
String eTagOrigin = generateETag(storedAnnotation.getGenerated(), WebFields.FORMAT_JSONLD, apiVersion);

checkIfMatchHeader(eTagOrigin, request);
checkIfMatchHeader(eTagOrigin, request);
getAnnotationService().validateWebAnnotation(updateWebAnnotation);

// 6. apply updates - merge current and updated annotation
// 7. and call database update method
Annotation updatedAnnotation = getAnnotationService().updateAnnotation((PersistentAnnotation)storedAnnotation,
updateWebAnnotation);
Annotation updatedAnnotation = getAnnotationService()
.updateAnnotation((PersistentAnnotation) storedAnnotation, updateWebAnnotation);

String eTag = generateETag(updatedAnnotation.getGenerated(), WebFields.FORMAT_JSONLD, apiVersion);

Expand Down Expand Up @@ -524,12 +524,12 @@ protected ResponseEntity<String> updateAnnotation(String identifier, String anno
* @return response entity that comprises response body, headers and status code
* @throws HttpException
*/
protected ResponseEntity<String> deleteAnnotation(String identifier, Authentication authentication)
protected ResponseEntity<String> deleteAnnotation(String identifier, Authentication authentication, HttpServletRequest request)
throws HttpException {

try {
// String userId = authentication.getPrincipal().toString();

// 5. authorize user
// already performed in verify write access
// getAuthorizationService().authorizeUser(userId, authentication, annoId, Operations.DELETE);
Expand All @@ -538,6 +538,12 @@ protected ResponseEntity<String> deleteAnnotation(String identifier, Authenticat
// Verify if user is allowed to perform the deletion.
Annotation storedAnno = verifyOwnerOrAdmin(identifier, authentication);

// validate annotation
String apiVersion = getConfiguration().getAnnotationApiVersion();
String eTagOrigin = generateETag(storedAnno.getGenerated(), WebFields.FORMAT_JSONLD, apiVersion);

checkIfMatchHeader(eTagOrigin, request);

// call database delete method that deactivates existing Annotation
// in Mongo
getAnnotationService().disableAnnotation(storedAnno);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ResponseEntity<String> deleteAnnotation(
Authentication authentication = verifyWriteAccess(Operations.DELETE, request);

// String action = "delete:/annotation/{identifier}[.{format}]";
return deleteAnnotation(identifier, authentication);
return deleteAnnotation(identifier, authentication, request);
}

}

0 comments on commit 1d81cc7

Please sign in to comment.