Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #319 from futures/preconditions
Browse files Browse the repository at this point in the history
Refactoring preconditions
  • Loading branch information
barmintor committed Apr 25, 2014
2 parents ebb08c8 + 094c9ec commit dafc946
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
Expand Up @@ -659,12 +659,16 @@ public Response deleteObject(@PathParam("path")
Throwable inner = riex.getCause();
if ( inner instanceof ReferentialIntegrityException) {
for ( NodeKey node : ((ReferentialIntegrityException)inner).getReferrers() ) {
msg.append( " " + session.getNodeByIdentifier(node.getIdentifier()).getPath() );
try {
msg.append( " " + session.getNodeByIdentifier(node.getIdentifier()).getPath() );
} catch ( Exception ex ) {
msg.append( " <" + node.getIdentifier() + ">");
}
}
}
return status(SC_PRECONDITION_FAILED).entity(msg.toString()).build();
} catch (WebApplicationException ex) {
return status(SC_PRECONDITION_FAILED).entity(ex.getMessage()).build();
return (Response)ex.getResponse();
} finally {
session.logout();
}
Expand Down
Expand Up @@ -176,28 +176,7 @@ public static final String toPath(final List<PathSegment> paths) {
protected static void checkCacheControlHeaders(final Request request,
final HttpServletResponse servletResponse,
final FedoraResource resource) throws RepositoryException {

final EntityTag etag = new EntityTag(resource.getEtagValue());
final Date date = resource.getLastModifiedDate();

final Date roundedDate = new Date();
if (date != null) {
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
}
final Response.ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

if (builder != null) {
final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);
// here we are implicitly emitting a 304
// the exception is not an error, it's genuinely
// an exceptional condition
throw new WebApplicationException(builder.cacheControl(cc)
.lastModified(date).tag(etag).build());
}

evaluateRequestPreconditions(request, resource, true);
addCacheControlHeaders(servletResponse, resource);
}

Expand Down Expand Up @@ -229,6 +208,21 @@ protected static void addCacheControlHeaders(final HttpServletResponse servletRe
*/
protected static void evaluateRequestPreconditions(final Request request,
final FedoraResource resource) throws RepositoryException {
evaluateRequestPreconditions(request, resource, false);
}

protected void addResponseInformationToStream(
final FedoraResource resource, final RdfStream dataset,
final UriInfo uriInfo, final IdentifierTranslator subjects)
throws RepositoryException {
if (httpTripleUtil != null) {
httpTripleUtil.addHttpComponentModelsForResourceToStream(dataset, resource,
uriInfo, subjects);
}
}

private static void evaluateRequestPreconditions( final Request request, final FedoraResource resource,
boolean cacheControl ) throws RepositoryException {

final EntityTag etag = new EntityTag(resource.getEtagValue());
final Date date = resource.getLastModifiedDate();
Expand All @@ -238,21 +232,27 @@ protected static void evaluateRequestPreconditions(final Request request,
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
}

final Response.ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

if (builder != null) {
throw new WebApplicationException(builder.entity("ETag mismatch").build());
Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
if ( builder != null ) {
builder = builder.entity("ETag mismatch");
} else {
builder = request.evaluatePreconditions(roundedDate);
if ( builder != null ) {
builder = builder.entity("Date mismatch");
}
}
}

protected void addResponseInformationToStream(
final FedoraResource resource, final RdfStream dataset,
final UriInfo uriInfo, final IdentifierTranslator subjects)
throws RepositoryException {
if (httpTripleUtil != null) {
httpTripleUtil.addHttpComponentModelsForResourceToStream(dataset, resource,
uriInfo, subjects);
if (builder != null && cacheControl ) {
final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);
// here we are implicitly emitting a 304
// the exception is not an error, it's genuinely
// an exceptional condition
builder = builder.cacheControl(cc).lastModified(date).tag(etag);
}
if (builder != null) {
throw new WebApplicationException(builder.build());
}
}
}

0 comments on commit dafc946

Please sign in to comment.