Skip to content

Commit

Permalink
add Link[rel=first] header to RDF responses
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jul 25, 2013
1 parent c7e62ad commit d6b07dc
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
Expand Up @@ -51,6 +51,7 @@
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.hp.hpl.jena.sparql.util.Symbol;
import org.fcrepo.AbstractResource;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.responses.HtmlTemplate;
Expand Down Expand Up @@ -211,22 +212,27 @@ private Dataset getSearchDataset(final String terms, final long offset,
.queryParam("q", terms)
.queryParam("offset", offset + limit)
.queryParam("limit", limit)
.build()
.toString());
searchModel.add(pageResource, NEXT_PAGE, nextPageResource);
} else {
searchModel.add(pageResource, NEXT_PAGE, RDF.nil);
}

final String firstPage = uriInfo
.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.queryParam("offset", 0)
.queryParam("limit", limit)
.build()
.toString();
final Resource firstPageResource =
searchModel.createResource(uriInfo
.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.queryParam("offset", 0)
.queryParam("limit", limit)
.toString());
searchModel.createResource(firstPage);
searchModel.add(subjects.getContext(), FIRST_PAGE, firstPageResource);

dataset.getContext().put(Symbol.create("firstPage"), firstPage);


dataset.addNamedModel("search-pagination", searchModel);
}
Expand Down
24 changes: 15 additions & 9 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -35,6 +35,7 @@
import static org.fcrepo.http.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.RDFMediaType.RDF_XML;
import static org.fcrepo.http.RDFMediaType.TURTLE;
import static org.fcrepo.rdf.GraphProperties.INLINED_RESOURCES_MODEL;
import static org.fcrepo.rdf.GraphProperties.PROBLEMS_MODEL_NAME;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -70,6 +71,7 @@

import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.util.Symbol;
import org.apache.commons.io.IOUtils;
import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
Expand Down Expand Up @@ -166,13 +168,14 @@ public Dataset describe(@PathParam("path")
final Dataset propertiesDataset =
resource.getPropertiesDataset(subjects, offset, realLimit);

if (limit > 0 && propertiesDataset.getDefaultModel()
final Model treeModel = propertiesDataset.getNamedModel(propertiesDataset.getContext().getAsString(INLINED_RESOURCES_MODEL, "NO SUCH MODEL"));
if (limit > 0 && treeModel != null && treeModel
.contains(subjects.getGraphSubject(resource.getNode()),
HAS_CHILD_COUNT)) {

Model requestModel = ModelFactory.createDefaultModel();

final long childCount = propertiesDataset.getDefaultModel()
final long childCount = treeModel
.listObjectsOfProperty(subjects.getGraphSubject(resource.getNode()), HAS_CHILD_COUNT)
.nextNode().asLiteral().getLong();

Expand All @@ -181,20 +184,23 @@ public Dataset describe(@PathParam("path")
final Resource nextPageResource =
requestModel.createResource(uriInfo
.getRequestUriBuilder()
.queryParam("offset", offset + limit)
.queryParam("limit", limit)
.replaceQueryParam("offset", offset + limit)
.replaceQueryParam("limit", limit)
.build()
.toString());
requestModel.add(subjects.getContext(), NEXT_PAGE, nextPageResource);
}

final String firstPage = uriInfo
.getRequestUriBuilder()
.replaceQueryParam("offset", 0)
.replaceQueryParam("limit", limit)
.build().toString();
final Resource firstPageResource =
requestModel.createResource(uriInfo
.getRequestUriBuilder()
.queryParam("offset", 0)
.queryParam("limit", limit)
.toString());
requestModel.createResource(firstPage);
requestModel.add(subjects.getContext(), FIRST_PAGE, firstPageResource);

propertiesDataset.getContext().put(Symbol.create("firstPage"), firstPage);
propertiesDataset.addNamedModel("requestModel", requestModel);

}
Expand Down
Expand Up @@ -55,6 +55,7 @@

import com.hp.hpl.jena.graph.Node_ANY;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.sparql.util.Context;
import org.apache.commons.io.IOUtils;
import org.fcrepo.FedoraObject;
import org.fcrepo.FedoraResource;
Expand Down Expand Up @@ -105,6 +106,9 @@ public class FedoraNodesTest {

@Mock
private Model mockModel;

@Mock
private Context mockContext;

private UriInfo uriInfo;

Expand Down Expand Up @@ -215,6 +219,7 @@ public void testDescribeObject() throws RepositoryException, IOException {
final String path = "/" + pid;

when(mockDataset.getDefaultModel()).thenReturn(mockModel);
when(mockDataset.getContext()).thenReturn(mockContext);

when(mockObject.getLastModifiedDate()).thenReturn(null);
when(
Expand All @@ -236,6 +241,7 @@ public void testDescribeObjectNoInlining() throws RepositoryException, IOExcepti
final String path = "/" + pid;

when(mockDataset.getDefaultModel()).thenReturn(mockModel);
when(mockDataset.getContext()).thenReturn(mockContext);

when(mockObject.getLastModifiedDate()).thenReturn(null);
when(
Expand Down
Expand Up @@ -195,7 +195,6 @@ public void testGetObjectGraph() throws Exception {

logger.debug("Retrieved object graph:\n" + content);


assertTrue(
"Expect inlined resources",
compile(
Expand All @@ -222,6 +221,25 @@ public void testGetObjectGraph() throws Exception {

}

@Test
public void testGetObjectGraphWithChildren() throws Exception {
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph"));
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph/a"));
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph/b"));
client.execute(postObjMethod("FedoraDescribeWithChildrenTestGraph/c"));
final HttpGet getObjMethod =
new HttpGet(serverAddress + "objects/FedoraDescribeWithChildrenTestGraph?limit=1&offset=1");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

logger.debug("Retrieved object graph:\n" + content);

assertEquals(serverAddress + "objects/FedoraDescribeWithChildrenTestGraph?limit=1&offset=0;rel=\"first\"", response.getFirstHeader("Link").getValue());
}

@Test
public void testGetObjectGraphNonMemberProperties() throws Exception {
client.execute(postObjMethod("FedoraDescribeTestGraph"));
Expand Down
Expand Up @@ -32,6 +32,7 @@
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.hp.hpl.jena.sparql.util.Symbol;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -60,7 +61,11 @@ public void writeTo(final Dataset rdf, final Class<?> type,

// add standard headers
httpHeaders.put("Content-type", of((Object) mediaType.toString()));
httpHeaders.putSingle("Link", ";rel=\"first\"");

if (rdf.getContext().isDefined(Symbol.create("firstPage"))) {
httpHeaders.putSingle("Link", rdf.getContext().getAsString(Symbol.create("firstPage")) + ";rel=\"first\"");
}

setCachingHeaders(httpHeaders, rdf);

new GraphStoreStreamingOutput(rdf, mediaType).write(entityStream);
Expand Down
Expand Up @@ -26,6 +26,7 @@
public interface GraphProperties {

public static final Symbol URI_SYMBOL = Symbol.create("uri");
public static final Symbol INLINED_RESOURCES_MODEL = Symbol.create("inlined-resources-model");

public static final String PROBLEMS_MODEL_NAME = "problems";

Expand Down
Expand Up @@ -62,6 +62,7 @@ public Dataset getProperties(final Node node, final GraphSubjects subjects,
String uri = subject.getURI();
com.hp.hpl.jena.sparql.util.Context context = dataset.getContext();
context.set(URI_SYMBOL,uri);
context.set(INLINED_RESOURCES_MODEL,MODEL_NAME);

dataset.addNamedModel(GraphProperties.PROBLEMS_MODEL_NAME,
problemModel);
Expand Down

0 comments on commit d6b07dc

Please sign in to comment.