Skip to content

Commit

Permalink
Ensure generated link headers include an anchor parameter as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 20, 2014
1 parent b88d360 commit 0caae8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
23 changes: 18 additions & 5 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraLdp.java
Expand Up @@ -53,6 +53,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -428,7 +429,7 @@ && isRdfContentType(contentTypeString)) {

final URI location = getUri(result);

addResourceLinkHeaders(result);
addResourceLinkHeaders(result, true);

return created(location).entity(location.toString()).build();

Expand Down Expand Up @@ -493,13 +494,25 @@ private void addOptionsHttpHeaders() {
}

private void addResourceLinkHeaders(final FedoraResource resource) {
addResourceLinkHeaders(resource, false);
}

private void addResourceLinkHeaders(final FedoraResource resource, final boolean includeAnchor) {
if (resource instanceof Datastream) {
final URI binaryUri = getUri(((Datastream) resource).getBinary());
servletResponse.addHeader("Link", "<" + binaryUri + ">;rel=\"describes\"");
final URI uri = getUri(((Datastream) resource).getBinary());
final Link link = Link.fromUri(uri).rel("describes").build();
servletResponse.addHeader("Link", link.toString());
} else if (resource instanceof FedoraBinary) {
final URI descriptionUri = getUri(((FedoraBinary) resource).getDescription());
servletResponse.addHeader("Link", "<" + descriptionUri + ">;rel=\"describedby\"");
final URI uri = getUri(((FedoraBinary) resource).getDescription());
final Link.Builder builder = Link.fromUri(uri).rel("describedby");

if (includeAnchor) {
builder.param("anchor", getUri(resource).toString());
}
servletResponse.addHeader("Link", builder.build().toString());
}


}

private String getRequestedObjectType(final String mixin,
Expand Down
Expand Up @@ -220,7 +220,7 @@ public void testHeadWithBinary() throws Exception {
assertTrue("Should contain a link to the binary description",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryDescriptionPath + "/fcr:metadata")
+ ">;rel=\"describedby\""));
+ ">; rel=\"describedby\""));
}

@Test
Expand All @@ -235,7 +235,7 @@ public void testHeadWithBinaryDescription() throws Exception {
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
.contains("<" + identifierConverter.toDomain(binaryPath) + ">; rel=\"describes\""));
}

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

@Test
Expand All @@ -279,7 +279,7 @@ public void testOptionWithBinaryDescription() throws Exception {
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
.contains("<" + identifierConverter.toDomain(binaryPath) + ">; rel=\"describes\""));
}


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

Expand All @@ -487,7 +487,7 @@ public void testGetWithBinaryDescription() throws Exception {
assertTrue("Should advertise Accept-Patch flavors", mockResponse.containsHeader("Accept-Patch"));
assertTrue("Should contain a link to the binary",
mockResponse.getHeaders("Link")
.contains("<" + identifierConverter.toDomain(binaryPath) + ">;rel=\"describes\""));
.contains("<" + identifierConverter.toDomain(binaryPath) + ">; rel=\"describes\""));

final RdfStream entity = (RdfStream) actual.getEntity();
final Model model = entity.asModel();
Expand Down
Expand Up @@ -869,7 +869,13 @@ public void testIngestWithBinary() throws Exception {
getStatus(new HttpGet(location)));

final Link link = Link.valueOf(response.getFirstHeader("Link").getValue());

assertEquals("describedby", link.getRel());
assertTrue("Expected an anchor to the newly created resource", link.getParams().containsKey("anchor"));
assertEquals("Expected anchor to point at the newly created resource",
location, link.getParams().get("anchor"));
assertEquals("Expected describedby link to point at the description",
location + "/" + FCR_METADATA, link.getUri().toString());
}

@Test
Expand Down Expand Up @@ -933,7 +939,7 @@ public void testGetDatastream() throws Exception {

final Collection<String> links = getLinkHeaders(response);
assertTrue("Didn't find 'describedby' link header!",
links.contains("<" + serverAddress + pid + "/ds1/" + FCR_METADATA + ">;rel=\"describedby\""));
links.contains("<" + serverAddress + pid + "/ds1/" + FCR_METADATA + ">; rel=\"describedby\""));

}

Expand Down

0 comments on commit 0caae8d

Please sign in to comment.