Skip to content

Commit

Permalink
Redirecting to external content on GET of fcr:content
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Sep 11, 2014
1 parent 59d3447 commit a4553ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Expand Up @@ -52,6 +52,7 @@
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.status;
import static org.apache.http.HttpStatus.SC_CONFLICT;
import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand Down Expand Up @@ -229,13 +230,21 @@ public Response getContent(@PathParam("path") final List<PathSegment> pathList,
@HeaderParam("Range") final String rangeValue,
@Context final Request request,
@Context final HttpServletResponse servletResponse)
throws RepositoryException, IOException {
throws RepositoryException, IOException, URISyntaxException {
try {
final String path = toPath(pathList);
LOGGER.info("Attempting get of {}.", path);

final Datastream ds =
datastreamService.getDatastream(session, path);

if ( ds.getNode().hasProperty("fedorarelsext:hasExternalContent") ) {
final URI externalURI = new URI(ds.getNode().getProperty("fedorarelsext:hasExternalContent")
.getValues()[0].getString());
LOGGER.debug("Redirecting to external content {}", externalURI.toString());
return status(SC_MOVED_TEMPORARILY).header("Location",externalURI.toString()).build();
}

final HttpIdentifierTranslator subjects =
new HttpIdentifierTranslator(session, FedoraNodes.class,
uriInfo);
Expand Down
Expand Up @@ -291,7 +291,7 @@ public void testModifyContent()
}

@Test
public void testGetContent() throws RepositoryException, IOException {
public void testGetContent() throws RepositoryException, IOException, URISyntaxException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String path = "/" + pid + "/" + dsId;
Expand Down
Expand Up @@ -17,6 +17,8 @@

import static java.util.TimeZone.getTimeZone;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand All @@ -32,6 +34,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -442,8 +445,33 @@ public void testFederatedChecksumCaching() throws Exception {
assertTrue( readonlyMeta.exists() );
assertTrue( readonlyMeta.length() > 0 );
final String readonlyMetaContent = IOUtils.toString(new FileReader(readonlyMeta));
logger.warn("XXX: " + readonlyMetaContent);
assertTrue( readonlyMetaContent.contains("urn:sha1:18835fd8075c1e1f266366c1bdfd1ac6a357f242") );
}

@Test
public void testExternalContent() throws Exception {
final String pid = getRandomUniquePid();
final String dsURL = serverAddress + pid + "/ds1";
final String extPID = getRandomUniquePid();
final String extURL = serverAddress + extPID;

createObject(extPID);
createObject(pid);
createDatastream(pid, "ds1", "");

final HttpPatch patch = new HttpPatch(dsURL);
patch.addHeader("Content-Type", "application/sparql-update");
final StringEntity e = new StringEntity(
"INSERT { <> <http://fedora.info/definitions/v4/rels-ext#hasExternalContent> "
+ "<" + extURL + "> } WHERE {}");
patch.setEntity(e);
assertEquals(NO_CONTENT.getStatusCode(), getStatus(patch));

final HttpGet get = new HttpGet(dsURL + "/fcr:content");
final HttpResponse response = client.execute(get);
assertEquals(OK.getStatusCode(), response.getStatusLine().getStatusCode());
assertEquals("text/turtle", response.getFirstHeader("Content-Type").getValue());
assertTrue( IOUtils.toString(response.getEntity().getContent()).contains(extURL) );
}

}

0 comments on commit a4553ff

Please sign in to comment.