Skip to content

Commit

Permalink
Convert Resource <=> FedoraResource
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 16, 2014
1 parent 0206da1 commit 596c464
Show file tree
Hide file tree
Showing 72 changed files with 754 additions and 593 deletions.
Expand Up @@ -17,7 +17,8 @@

import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
Expand Down Expand Up @@ -75,8 +76,8 @@ private Response doRequest(final String path) throws RepositoryException {
return Response.created(location).build();
}

protected IdentifierConverter<Resource,Node> translator() {
return new UriAwareIdentifierConverter(session,
protected IdentifierConverter<Resource,FedoraResource> translator() {
return new UriAwareResourceConverter(session,
uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
}

Expand Down
Expand Up @@ -51,7 +51,6 @@

import javax.inject.Inject;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -328,7 +327,7 @@ protected RdfStream getTriples(final FedoraResource resource, final Class<? exte

protected URI getUri(final FedoraResource resource) {
try {
final String uri = translator().reverse().convert(resource.getNode()).getURI();
final String uri = translator().reverse().convert(resource).getURI();
return new URI(uri);
} catch (final URISyntaxException e) {
throw new BadRequestException(e);
Expand All @@ -346,7 +345,7 @@ protected FedoraResource resource() {

protected void addResponseInformationToStream(
final FedoraResource resource, final RdfStream dataset,
final UriInfo uriInfo, final IdentifierConverter<Resource,Node> subjects) {
final UriInfo uriInfo, final IdentifierConverter<Resource,FedoraResource> subjects) {
if (httpTripleUtil != null) {
httpTripleUtil.addHttpComponentModelsForResourceToStream(dataset, resource,
uriInfo, subjects);
Expand Down
Expand Up @@ -18,22 +18,17 @@
import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;
import org.slf4j.Logger;

import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.observation.ObservationManager;
import javax.ws.rs.core.UriInfo;

import java.net.URI;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand All @@ -44,13 +39,13 @@ abstract public class FedoraBaseResource extends AbstractResource {

private static final Logger LOGGER = getLogger(FedoraBaseResource.class);

protected IdentifierConverter<Resource,Node> identifierTranslator;
protected IdentifierConverter<Resource, FedoraResource> identifierTranslator;

protected abstract Session session();

protected IdentifierConverter<Resource,Node> translator() {
protected IdentifierConverter<Resource, FedoraResource> translator() {
if (identifierTranslator == null) {
identifierTranslator = new UriAwareIdentifierConverter(session(),
identifierTranslator = new UriAwareResourceConverter(session(),
uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class));
}

Expand All @@ -64,26 +59,7 @@ protected IdentifierConverter<Resource,Node> translator() {
*/
@VisibleForTesting
public FedoraResource getResourceFromPath(final String externalPath) {
final FedoraResource resource;
final boolean metadata = externalPath != null
&& externalPath.endsWith(FCR_METADATA);

final Node node = translator().convert(translator().toDomain(externalPath));

if (DatastreamImpl.hasMixin(node)) {
final DatastreamImpl datastream = new DatastreamImpl(node);

if (metadata) {
resource = datastream;
} else {
resource = datastream.getBinary();
}
} else if (FedoraBinaryImpl.hasMixin(node)) {
resource = new FedoraBinaryImpl(node);
} else {
resource = new FedoraObjectImpl(node);
}
return resource;
return translator().convert(translator().toDomain(externalPath));
}


Expand Down
Expand Up @@ -174,7 +174,7 @@ public Response describe(@HeaderParam("Prefer") final Prefer prefer,
addResourceHttpHeaders(resource());

final RdfStream rdfStream = new RdfStream().session(session)
.topic(translator().reverse().convert(resource().getNode()).asNode());
.topic(translator().reverse().convert(resource()).asNode());

return getContent(prefer, rangeValue, rdfStream);

Expand Down Expand Up @@ -418,7 +418,7 @@ protected void addResourceHttpHeaders(final FedoraResource resource) {

if (getCurrentTransactionId(session) != null) {
final String canonical = translator().reverse()
.convert(resource.getNode())
.convert(resource)
.toString()
.replaceFirst("/tx:[^/]+", "");

Expand Down
Expand Up @@ -173,7 +173,7 @@ public RdfStream getVersionList() {

return resource().getTriples(translator(), VersionsRdfContext.class)
.session(session)
.topic(translator().reverse().convert(resource().getNode()).asNode());
.topic(translator().reverse().convert(resource()).asNode());
}

protected FedoraResource resource() {
Expand Down
Expand Up @@ -143,7 +143,7 @@ public Response getVersion(@HeaderParam("Prefer") final Prefer prefer,
label);
checkCacheControlHeaders(request, servletResponse, resource(), session);
final RdfStream rdfStream = new RdfStream().session(session).topic(
translator().reverse().convert(resource().getNode()).asNode());
translator().reverse().convert(resource()).asNode());
return getContent(prefer, rangeValue, rdfStream);
}

Expand Down
Expand Up @@ -46,7 +46,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.core.UriInfo;
Expand All @@ -66,12 +65,12 @@ public class HttpApiResources implements UriAwareResourceModelFactory {

@Override
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final IdentifierConverter<Resource,Node> graphSubjects) {
final UriInfo uriInfo, final IdentifierConverter<Resource,FedoraResource> graphSubjects) {

final Model model = createDefaultModel();
try {

final Resource s = graphSubjects.reverse().convert(resource.getNode());
final Resource s = graphSubjects.reverse().convert(resource);

if (resource.getNode().getPrimaryNodeType().isNodeType(ROOT)) {
addRepositoryStatements(uriInfo, model, s);
Expand Down
Expand Up @@ -25,7 +25,7 @@
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import org.apache.commons.io.IOUtils;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.http.commons.domain.Prefer;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
Expand Down Expand Up @@ -122,7 +122,7 @@ public class FedoraLdpTest {
@Mock
private Node mockBinaryNode;

private IdentifierConverter<Resource, Node> identifierConverter;
private IdentifierConverter<Resource, FedoraResource> identifierConverter;

@Mock
private NodeService mockNodeService;
Expand All @@ -143,7 +143,7 @@ public void setUp() throws Exception {
mockSession = mockSession(testObj);
setField(testObj, "session", mockSession);

identifierConverter = new UriAwareIdentifierConverter(mockSession,
identifierConverter = new UriAwareResourceConverter(mockSession,
UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}"));

setField(testObj, "request", mockRequest);
Expand All @@ -156,15 +156,18 @@ public void setUp() throws Exception {

when(mockObject.getNode()).thenReturn(mockObjectNode);
when(mockObject.getEtagValue()).thenReturn("");
when(mockObject.getPath()).thenReturn(path);
when(mockObjectNode.getPath()).thenReturn(path);

when(mockDatastream.getNode()).thenReturn(mockDatastreamNode);
when(mockDatastream.getEtagValue()).thenReturn("");
when(mockDatastream.getPath()).thenReturn(binaryDescriptionPath);
when(mockDatastreamNode.getPath()).thenReturn(binaryDescriptionPath);
when(mockDatastream.getBinary()).thenReturn(mockBinary);

when(mockBinary.getNode()).thenReturn(mockBinaryNode);
when(mockBinary.getEtagValue()).thenReturn("");
when(mockBinary.getPath()).thenReturn(binaryPath);
when(mockBinaryNode.getPath()).thenReturn(binaryPath);
when(mockBinary.getDescription()).thenReturn(mockDatastream);
}
Expand All @@ -174,6 +177,7 @@ private FedoraResource setResource(final Class<? extends FedoraResource> klass)

doReturn(mockResource).when(testObj).resource();
when(mockResource.getNode()).thenReturn(mockNode);
when(mockResource.getPath()).thenReturn(path);
when(mockNode.getPath()).thenReturn(path);
when(mockResource.getEtagValue()).thenReturn("");
when(mockResource.getTriples(eq(identifierConverter), any(Class.class))).thenAnswer(new Answer<RdfStream>() {
Expand Down Expand Up @@ -224,7 +228,8 @@ public void testHeadWithBinary() throws Exception {
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath) + ">;rel=\"describedby\""));
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
}

@Test
Expand Down Expand Up @@ -269,7 +274,8 @@ public void testOptionWithBinary() throws Exception {
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath) + ">;rel=\"describedby\""));
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
}

@Test
Expand Down Expand Up @@ -470,7 +476,8 @@ public void testGetWithBinary() throws Exception {
assertFalse("Should not advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath) + ">;rel=\"describedby\""));
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
assertTrue(IOUtils.toString((InputStream)actual.getEntity()).equals("xyz"));
}

Expand Down
Expand Up @@ -48,7 +48,7 @@
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
Expand Down Expand Up @@ -154,7 +154,7 @@ public void setUp() throws Exception {
when(mockSession.getNode(path)).thenReturn(mockNode);

setField(testObj, "identifierTranslator",
new UriAwareIdentifierConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
new UriAwareResourceConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
}

@Test
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package org.fcrepo.http.api;

import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void setUp() throws Exception {
doReturn(mockResource).when(testObj).resource();

setField(testObj, "identifierTranslator",
new UriAwareIdentifierConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
new UriAwareResourceConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
}


Expand Down
Expand Up @@ -36,7 +36,7 @@
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.Variant;

import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.VersionService;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void setUp() throws Exception {
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);

setField(testObj, "identifierTranslator",
new UriAwareIdentifierConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
new UriAwareResourceConverter(mockSession, UriBuilder.fromUri("http://localhost/fcrepo/{path: .*}")));
}

@Test
Expand Down
Expand Up @@ -34,7 +34,7 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceConverter;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.serialization.SerializerUtil;
import org.junit.Before;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class HttpApiResourcesTest {
@Mock
private NodeType mockNodeType;

private UriAwareIdentifierConverter mockSubjects;
private UriAwareResourceConverter mockSubjects;

@Mock
private SerializerUtil mockSerializers;
Expand All @@ -94,7 +94,7 @@ public void setUp() {
when(mockSession.getRepository()).thenReturn(mockRepository);
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getName()).thenReturn("default");
mockSubjects = new UriAwareIdentifierConverter(mockSession, UriBuilder.fromUri("http://localhost/{path: .*}"));
mockSubjects = new UriAwareResourceConverter(mockSession, UriBuilder.fromUri("http://localhost/{path: .*}"));
setField(testObj, "serializers", mockSerializers);
}

Expand All @@ -106,7 +106,7 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks()
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getPath()).thenReturn("/");

final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

final Model model =
testObj.createModelForResource(mockResource, uriInfo, mockSubjects);
Expand All @@ -124,7 +124,7 @@ public void shouldDecorateNodesWithLinksToVersionsAndExport()
when(mockNode.getPath()).thenReturn("/some/path/to/object");

when(mockSerializers.keySet()).thenReturn(of("a", "b"));
final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

final Model model =
testObj.createModelForResource(mockResource, uriInfo, mockSubjects);
Expand All @@ -144,7 +144,7 @@ public void shouldNotDecorateNodesWithLinksToVersionsUnlessVersionable()
when(mockNode.getPath()).thenReturn("/some/path/to/object");

when(mockSerializers.keySet()).thenReturn(of("a", "b"));
final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

final Model model =
testObj.createModelForResource(mockResource, uriInfo, mockSubjects);
Expand All @@ -160,7 +160,7 @@ public void shouldDecorateDatastreamsWithLinksToFixityChecks()
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getPath()).thenReturn("/some/path/to/datastream");
when(mockSerializers.keySet()).thenReturn(new HashSet<String>());
final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

final Model model =
testObj.createModelForResource(mockResource, uriInfo, mockSubjects);
Expand All @@ -178,7 +178,7 @@ public void shouldDecorateRootNodeWithCorrectResourceURI()
when(mockSerializers.keySet()).thenReturn(of("a"));
when(mockNode.getPath()).thenReturn("/");

final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
final Model model =
testObj.createModelForResource(mockResource, uriInfo,
mockSubjects);
Expand All @@ -198,7 +198,7 @@ public void shouldDecorateOtherNodesWithCorrectResourceURI()
when(mockSerializers.keySet()).thenReturn(of("a"));
when(mockNode.getPath()).thenReturn("/some/path/to/object");

final Resource graphSubject = mockSubjects.reverse().convert(mockNode);
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
final Model model =
testObj.createModelForResource(mockResource, uriInfo,
mockSubjects);
Expand Down

0 comments on commit 596c464

Please sign in to comment.