Skip to content

Commit

Permalink
Re-Work true blank-node implementation
Browse files Browse the repository at this point in the history
- Unit tests for new skolemization classes
- Unit tests for hash-URI machinery

Resolves: https://jira.duraspace.org/browse/FCREPO-1385
  • Loading branch information
ajs6f authored and Andrew Woods committed Apr 14, 2015
1 parent 6e7e22e commit ceacc98
Show file tree
Hide file tree
Showing 34 changed files with 1,074 additions and 544 deletions.
Expand Up @@ -16,10 +16,12 @@
package org.fcrepo.auth.integration;

import org.apache.http.auth.BasicUserPrincipal;

import org.fcrepo.auth.common.FedoraAuthorizationDelegate;
import org.fcrepo.auth.common.ServletContainerAuthenticationProvider;
import org.fcrepo.kernel.impl.services.ContainerServiceImpl;
import org.fcrepo.kernel.services.ContainerService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -38,8 +40,8 @@
import javax.servlet.http.HttpServletRequest;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -60,7 +62,7 @@ public class ContainerRolesPrincipalProviderIT {
@Autowired
private FedoraAuthorizationDelegate fad;

private HttpServletRequest request = mock(HttpServletRequest.class);
private final HttpServletRequest request = mock(HttpServletRequest.class);

@Test
public void testFactory() {
Expand Down Expand Up @@ -91,7 +93,7 @@ public void testEmptyPrincipalProvider() throws RepositoryException {
}
final ContainerService os = new ContainerServiceImpl();
os.findOrCreate(session, "/myobject");
verify(fad, times(9)).hasPermission(any(Session.class), any(Path.class), any(String[].class));
verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}


Expand Down
Expand Up @@ -16,10 +16,12 @@
package org.fcrepo.auth.integration;

import org.apache.http.auth.BasicUserPrincipal;

import org.fcrepo.auth.common.FedoraAuthorizationDelegate;
import org.fcrepo.auth.common.ServletContainerAuthenticationProvider;
import org.fcrepo.kernel.impl.services.ContainerServiceImpl;
import org.fcrepo.kernel.services.ContainerService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -38,8 +40,8 @@
import javax.servlet.http.HttpServletRequest;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -60,7 +62,7 @@ public class HttpHeaderPrincipalProviderIT {
@Autowired
private FedoraAuthorizationDelegate fad;

private HttpServletRequest request = mock(HttpServletRequest.class);
private final HttpServletRequest request = mock(HttpServletRequest.class);

@Test
public void testFactory() {
Expand Down Expand Up @@ -91,7 +93,7 @@ public void testEmptyPrincipalProvider() throws RepositoryException {
}
final ContainerService os = new ContainerServiceImpl();
os.findOrCreate(session, "/myobject");
verify(fad, times(9)).hasPermission(any(Session.class), any(Path.class), any(String[].class));
verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}

}
Expand Up @@ -16,17 +16,19 @@
package org.fcrepo.auth.integration;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.slf4j.LoggerFactory.getLogger;

import org.apache.http.auth.BasicUserPrincipal;

import org.fcrepo.auth.common.FedoraAuthorizationDelegate;
import org.fcrepo.auth.common.ServletContainerAuthenticationProvider;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.impl.services.ContainerServiceImpl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -103,7 +105,7 @@ public void testPermissiveFAD() throws RepositoryException {
}
final ContainerService os = new ContainerServiceImpl();
os.findOrCreate(session, "/myobject");
verify(fad, times(9)).hasPermission(any(Session.class), any(Path.class), any(String[].class));
verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}

@Test(expected = AccessDeniedException.class)
Expand All @@ -128,6 +130,6 @@ public void testRestrictiveFAD() throws Throwable {
} catch (final RepositoryRuntimeException e) {
throw e.getCause();
}
verify(fad, times(5)).hasPermission(any(Session.class), any(Path.class), any(String[].class));
verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}
}
Expand Up @@ -71,7 +71,7 @@ public void testWriteProperty() throws RepositoryException {


// Write the properties
object.updateProperties(new DefaultIdentifierTranslator(session), sparql, new RdfStream());
object.updateProperties(new DefaultIdentifierTranslator(session), sparql, new RdfStream(), containerService);

// Verify
final Property property = object.getNode().getProperty("fedora:name");
Expand All @@ -97,7 +97,7 @@ public void testRemoveProperty() throws RepositoryException {

// Write the properties
final DefaultIdentifierTranslator graphSubjects = new DefaultIdentifierTranslator(session);
object.updateProperties(graphSubjects, sparql, new RdfStream());
object.updateProperties(graphSubjects, sparql, new RdfStream(), containerService);

// Verify property exists
final Property property = object.getNode().getProperty("fedora:remove");
Expand All @@ -114,7 +114,7 @@ public void testRemoveProperty() throws RepositoryException {
// Remove the properties
object.updateProperties(graphSubjects,
sparqlRemove,
object.getTriples(graphSubjects, PropertiesRdfContext.class));
object.getTriples(graphSubjects, PropertiesRdfContext.class), containerService);

// Persist the object (although the propery will be removed from memory without this.)
session.save();
Expand Down
Expand Up @@ -74,6 +74,7 @@
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.impl.rdf.Deskolemizer;
import org.fcrepo.kernel.impl.rdf.ManagedRdf;
import org.fcrepo.kernel.impl.rdf.impl.AclRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.BlankNodeRdfContext;
Expand Down Expand Up @@ -116,12 +117,16 @@
* content.
*
* @author Mike Durbin
* @author ajs6f
*/
public abstract class ContentExposingResource extends FedoraBaseResource {

public static final MediaType MESSAGE_EXTERNAL_BODY = MediaType.valueOf("message/external-body");

@Context protected Request request;

private Deskolemizer deskolemizer;

@Context protected HttpServletResponse servletResponse;

@Inject
Expand All @@ -141,6 +146,13 @@ public abstract class ContentExposingResource extends FedoraBaseResource {

protected abstract String externalPath();

private Deskolemizer deskolemizer() {
if (deskolemizer != null) {
return deskolemizer;
}
return deskolemizer = new Deskolemizer(translator(), null);
}

protected Response getContent(final String rangeValue,
final RdfStream rdfStream) throws IOException {
if (resource() instanceof FedoraBinary) {
Expand Down Expand Up @@ -192,7 +204,7 @@ public Triple apply(final Statement input) {
}
servletResponse.addHeader("Vary", "Accept, Range, Accept-Encoding, Accept-Language");

return Response.ok(rdfStream).build();
return ok(rdfStream.map(deskolemizer())).build();
}

protected RdfStream getResourceTriples() {
Expand Down Expand Up @@ -294,8 +306,7 @@ public RdfStream apply(final FedoraResource child) {
httpTripleUtil.addHttpComponentModelsForResourceToStream(rdfStream, resource(), uriInfo, translator());
}


return rdfStream;
return rdfStream.map(deskolemizer());
}

/**
Expand Down Expand Up @@ -595,14 +606,14 @@ protected void replaceResourceWithStream(final FedoraResource resource,
final Model inputModel = createDefaultModel()
.read(requestBodyStream, getUri(resource).toString(), format.getName().toUpperCase());

resource.replaceProperties(translator(), inputModel, resourceTriples);
resource.replaceProperties(translator(), inputModel, resourceTriples, containerService);
}

protected void patchResourcewithSparql(final FedoraResource resource,
final String requestBody,
final RdfStream resourceTriples)
throws MalformedRdfException, AccessDeniedException {
resource.updateProperties(translator(), requestBody, resourceTriples);
resource.updateProperties(translator(), requestBody, resourceTriples, containerService);
}

/**
Expand Down
Expand Up @@ -51,9 +51,8 @@ abstract public class FedoraBaseResource extends AbstractResource {
protected IdentifierConverter<Resource, FedoraResource> translator() {
if (idTranslator == null) {
idTranslator = new HttpResourceConverter(session(),
uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class));
uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class));
}

return idTranslator;
}

Expand Down Expand Up @@ -90,7 +89,7 @@ protected void setUpJMSInfo(final UriInfo uriInfo, final HttpHeaders headers) {
json.addProperty("userAgent",headers.getHeaderString("user-agent"));
}
obs.setUserData(json.toString());
} catch ( Exception ex ) {
} catch ( final Exception ex ) {
LOGGER.warn("Error setting baseURL", ex);
}
}
Expand Down
14 changes: 5 additions & 9 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraLdp.java
Expand Up @@ -316,15 +316,14 @@ public Response createOrReplaceObjectRdf(
*
* @param requestBodyStream the request body stream
* @return 201
* @throws MalformedRdfException if malformed rdf exception occurred
* @throws AccessDeniedException if exception updating property occurred
* @throws IOException if IO exception occurred
*/
@PATCH
@Consumes({contentTypeSPARQLUpdate})
@Timed
public Response updateSparql(@ContentLocation final InputStream requestBodyStream)
throws IOException, MalformedRdfException, AccessDeniedException {
throws IOException, AccessDeniedException {

if (null == requestBodyStream) {
throw new BadRequestException("SPARQL-UPDATE requests must have content!");
Expand Down Expand Up @@ -359,13 +358,15 @@ public Response updateSparql(@ContentLocation final InputStream requestBodyStrea

return noContent().build();
} catch ( final RuntimeException ex ) {
LOGGER.debug("Caught exception:", ex);
final Throwable cause = ex.getCause();
if (cause instanceof PathNotFoundException) {
// the sparql update referred to a repository resource that doesn't exist
throw new BadRequestException(cause.getMessage());
}
throw ex;
} catch (final RepositoryException e) {
LOGGER.debug("Caught exception:", e);
if (e instanceof AccessDeniedException) {
throw new AccessDeniedException(e.getMessage());
}
Expand Down Expand Up @@ -419,13 +420,8 @@ public Response createObject(@QueryParam("checksum") final String checksum,
effectiveContentType,
contentDisposition);

final RdfStream resourceTriples;

if (result.isNew()) {
resourceTriples = new RdfStream();
} else {
resourceTriples = getResourceTriples();
}
final RdfStream resourceTriples =
result.isNew() ? new RdfStream().session(session) : getResourceTriples().session(session);

if (requestBodyStream == null) {
LOGGER.trace("No request body detected");
Expand Down

0 comments on commit ceacc98

Please sign in to comment.