Skip to content

Commit

Permalink
Merge pull request #451 from fcrepo4/ldp-conformance
Browse files Browse the repository at this point in the history
Updates to conform to the LDP API
  • Loading branch information
Andrew Woods committed Sep 3, 2014
2 parents 6bcaf3d + d50d887 commit d44297e
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 28 deletions.
Expand Up @@ -40,6 +40,7 @@

import static javax.ws.rs.core.Response.ok;
import static javax.ws.rs.core.Response.status;
import static org.fcrepo.kernel.RdfLexicon.NON_RDF_SOURCE;

/**
* An abstract class that sits between AbstractResource and any resource that
Expand Down Expand Up @@ -142,12 +143,12 @@ protected Response getDatastreamContentResponse(final Datastream ds,
.size(ds.getContentSize())
.build();

return builder.type(ds.getMimeType()).header(
"Link",
"<" + subjects.getSubject(ds.getNode().getPath()) +
">;rel=\"describedby\"").header("Accept-Ranges",
"bytes").cacheControl(cc)
return builder.type(ds.getMimeType())
.header("Link", "<" + subjects.getSubject(ds.getNode().getPath()) + ">;rel=\"describedby\"")
.header("Link", "<" + NON_RDF_SOURCE + ">;rel=\"type\"")
.header("Accept-Ranges", "bytes")
.header("Content-Disposition", contentDisposition)
.cacheControl(cc)
.build();
}

Expand Down
21 changes: 3 additions & 18 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -62,7 +62,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.jcr.ItemExistsException;
Expand Down Expand Up @@ -117,7 +116,6 @@
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.rdf.HierarchyRdfContextOptions;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.openrdf.util.iterators.Iterators;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -277,15 +275,13 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,
}
}

final List<String> appliedIncludes = new ArrayList<>();

final boolean membership =
(!contains(includes, LDP_NAMESPACE + "PreferEmptyContainer") ||
(!contains(includes, LDP_NAMESPACE + "PreferMinimalContainer") ||
contains(includes, LDP_NAMESPACE + "PreferMembership"))
&& !contains(omits, LDP_NAMESPACE + "PreferMembership");

final boolean containment =
(!contains(includes, LDP_NAMESPACE + "PreferEmptyContainer") ||
(!contains(includes, LDP_NAMESPACE + "PreferMinimalContainer") ||
contains(includes, LDP_NAMESPACE + "PreferContainment"))
&& !contains(omits, LDP_NAMESPACE + "PreferContainment");

Expand All @@ -295,24 +291,13 @@ public RdfStream describe(@PathParam("path") final List<PathSegment> pathList,
final HierarchyRdfContextOptions hierarchyRdfContextOptions
= new HierarchyRdfContextOptions(limit, offset, membership, containment);

if (hierarchyRdfContextOptions.membershipEnabled()) {
appliedIncludes.add(LDP_NAMESPACE + "PreferMembership");
}

if (hierarchyRdfContextOptions.containmentEnabled()) {
appliedIncludes.add(LDP_NAMESPACE + "PreferContainment");
}

if (references) {
rdfStream.concat(resource.getReferencesTriples(subjects));
appliedIncludes.add(INBOUND_REFERENCES.toString());
}

rdfStream.concat(resource.getHierarchyTriples(subjects, hierarchyRdfContextOptions));

final String preferences = "return=representation; include=\""
+ Iterators.toString(appliedIncludes.iterator(), " ") + "\"";
servletResponse.addHeader("Preference-Applied", preferences);
servletResponse.addHeader("Preference-Applied", "return=representation");

} else {
servletResponse.addHeader("Preference-Applied", "return=minimal");
Expand Down
Expand Up @@ -23,7 +23,9 @@
import static org.fcrepo.http.commons.test.util.TestHelpers.mockDatastream;
import static org.fcrepo.http.commons.test.util.TestHelpers.mockSession;
import static org.fcrepo.http.commons.test.util.TestHelpers.setField;
import static org.fcrepo.kernel.RdfLexicon.NON_RDF_SOURCE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
Expand All @@ -40,6 +42,7 @@
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -306,8 +309,11 @@ public void testGetContent() throws RepositoryException, IOException {
verify(mockSession, never()).save();
final String actualContent =
IOUtils.toString((InputStream) actual.getEntity());
assertEquals("<http://localhost/fcrepo" + path + ">;rel=\"describedby\"", actual
.getMetadata().getFirst("Link"));
final List<Object> linkHeaders = actual.getMetadata().get("Link");
assertTrue("Expected to find describedby Link header",
linkHeaders.contains("<http://localhost/fcrepo" + path + ">;rel=\"describedby\""));
assertTrue("Expected to find NonRDFSource Link header",
linkHeaders.contains("<" + NON_RDF_SOURCE + ">;rel=\"type\""));
assertEquals("asdf", actualContent);
}

Expand Down
Expand Up @@ -393,7 +393,7 @@ public void testDescribeObjectNoInlining() throws RepositoryException, ParseExce
.thenReturn(mockObject);
final Request mockRequest = mock(Request.class);
final Prefer prefer = new Prefer("return=representation;"
+ "include=\"http://www.w3.org/ns/ldp#PreferEmptyContainer\"");
+ "include=\"http://www.w3.org/ns/ldp#PreferMinimalContainer\"");
final RdfStream rdfStream =
testObj.describe(createPathList(path), 0, -1, prefer, mockRequest, mockResponse, mockUriInfo);
assertEquals("Got wrong RDF!", mockRdfStream.concat(mockRdfStream2).concat(mockRdfStream3),
Expand Down
Expand Up @@ -44,12 +44,12 @@ public void testGetReturn() throws ParseException {
@Test
public void testGetReturnParameters() throws ParseException {
final Prefer prefer =
new Prefer("return=representation; include=\"http://www.w3.org/ns/ldp#PreferEmptyContainer\"");
new Prefer("return=representation; include=\"http://www.w3.org/ns/ldp#PreferMinimalContainer\"");

assertTrue(prefer.hasReturn());
assertEquals("representation", prefer.getReturn().getValue());

final String returnParams = prefer.getReturn().getParams().get("include");
assertTrue(returnParams.contains("http://www.w3.org/ns/ldp#PreferEmptyContainer"));
assertTrue(returnParams.contains("http://www.w3.org/ns/ldp#PreferMinimalContainer"));
}
}
Expand Up @@ -73,6 +73,11 @@ protected void operateOnMixin(final Resource mixinResource,
mgr.registerNodeType(type, false);
}

if (subjectNode.isNodeType(mixinName)) {
LOGGER.trace("Subject {} is already a {}; skipping", subjectNode, mixinName);
return;
}

if (subjectNode.canAddMixin(mixinName)) {
LOGGER.debug("Adding mixin: {} to node: {}.", mixinName, subjectNode.getPath());
subjectNode.addMixin(mixinName);
Expand Down
Expand Up @@ -64,6 +64,12 @@ protected void operateOnMixin(final Resource mixinResource,
if (session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
LOGGER.debug("Removing mixin: {} from node: {}.", mixinName,
subjectNode.getPath());

if (subjectNode.getPrimaryNodeType().isNodeType(mixinName)) {
LOGGER.debug("Unable to remove primary type from node");
return;
}

try {
subjectNode.removeMixin(mixinName);
} catch (final NoSuchNodeTypeException e) {
Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_RESOURCE;
import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.getJcrNamespaceForRDFNamespace;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
Expand Down Expand Up @@ -127,6 +128,16 @@ public void testAddingModelWithStreamNamespace() throws Exception {
verify(mockNode).addMixin(anyString());
}

@Test
public void testAddingModelWithPrimaryType() throws Exception {
testAdder = new RdfAdder(mockGraphSubjects, mockSession, testStream);
when(mockNode.isNodeType(mixinShortName)).thenReturn(true);
testAdder.operateOnMixin(createResource(mixinLongName), mockNode);


verify(mockNode, never()).addMixin(mixinShortName);
}

@Test
public void testAddingWithNotYetDefinedNamespace() throws Exception {
// we drop our stream namespace map
Expand Down
Expand Up @@ -99,6 +99,20 @@ public void testRemovingExistentMixin() throws Exception {
verify(mockNode).removeMixin(mixinShortName);
}

@Test
public void testRemovingPrimaryType() throws Exception {

when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.isNodeType(mixinShortName)).thenReturn(true);

testRemover =
new RdfRemover(mockGraphSubjects, mockSession, testStream);
testRemover.operateOnMixin(mixinStmnt.getObject().asResource(),
mockNode);

verify(mockNode, never()).removeMixin(mixinShortName);
}

@Test
public void testRemovingNonExistentMixin() throws Exception {
doThrow(new NoSuchNodeTypeException("Expected.")).when(mockNode)
Expand Down
2 changes: 2 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -203,6 +203,8 @@ public final class RdfLexicon {
createProperty(LDP_NAMESPACE + "hasMemberRelation");
public static final Property CONTAINS =
createProperty(LDP_NAMESPACE + "contains");
public static final Property NON_RDF_SOURCE =
createProperty(LDP_NAMESPACE + "NonRDFSource");

public static final Set<Property> ldpProperties = of(PAGE, PAGE_OF,
FIRST_PAGE, NEXT_PAGE, MEMBERSHIP_RESOURCE,
Expand Down

0 comments on commit d44297e

Please sign in to comment.