Skip to content

Commit

Permalink
Insert the 'check fixity' link in the HTML serialization by using the…
Browse files Browse the repository at this point in the history
… hasFixityService predicate
  • Loading branch information
cbeer committed Oct 29, 2014
1 parent 9eafbbc commit 0fab585
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 86 deletions.
Expand Up @@ -87,7 +87,9 @@ public RdfStream getDatastreamFixity(@PathParam("path")
throw new NotFoundException(resource + " is not a binary");
}

return ((FedoraBinary)resource).getFixity(translator()).session(session);
return ((FedoraBinary)resource).getFixity(translator())
.topic(translator().reverse().convert(resource).asNode())
.session(session);

}

Expand Down
Expand Up @@ -33,11 +33,12 @@
import com.hp.hpl.jena.rdf.model.Resource;

import org.fcrepo.http.api.FedoraExport;
import org.fcrepo.http.api.FedoraFixity;
import org.fcrepo.http.api.FedoraVersioning;
import org.fcrepo.http.api.repository.FedoraRepositoryExport;
import org.fcrepo.http.api.repository.FedoraRepositoryTransactions;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.serialization.SerializerUtil;
Expand Down Expand Up @@ -74,21 +75,22 @@ public Model createModelForResource(final FedoraResource resource,
addNodeStatements(resource, uriInfo, model, s);
}

if (resource.hasContent()) {
addContentStatements(resource, uriInfo, model, s);
if (resource instanceof Datastream) {
addContentStatements(idTranslator, ((Datastream) resource).getBinary(), model);
} else if (resource instanceof FedoraBinary) {
addContentStatements(idTranslator, (FedoraBinary)resource, model);
}

return model;
}

private static void addContentStatements(final FedoraResource resource, final UriInfo uriInfo,
final Model model, final Resource s) {
private static void addContentStatements(final IdentifierConverter<Resource,FedoraResource> idTranslator,
final FedoraBinary resource,
final Model model) {
// fcr:fixity
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
model.add(s, HAS_FIXITY_SERVICE, createResource(uriInfo
.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(
pathMap, false).toASCIIString()));
final Resource subject = idTranslator.reverse().convert(resource);
model.add(subject, HAS_FIXITY_SERVICE, createResource(subject.getURI() +
"/fcr:fixity"));
}

private void addNodeStatements(final FedoraResource resource, final UriInfo uriInfo,
Expand Down
5 changes: 5 additions & 0 deletions fcrepo-http-api/src/main/resources/views/common.js
Expand Up @@ -134,6 +134,11 @@ function checkIfNonRdfResource(e) {
var url = this.href;

$.ajax({type: "HEAD", url: url}).success(function(data, status, xhr) {
if (status != 200) {
location.href = url;
return;
}

var headers = xhr.getResponseHeader("Link").split(", ");

var types = $.grep(headers, function(h) {
Expand Down
7 changes: 6 additions & 1 deletion fcrepo-http-api/src/main/resources/views/common.vsl
@@ -1,6 +1,7 @@
#* @vtlvariable name="quad" type="com.hp.hpl.jena.sparql.core.Quad" *#
#* @vtlvariable name="rdf" type="com.hp.hpl.jena.graph.Graph" *#
#* @vtlvariable name="model" type="com.hp.hpl.jena.rdf.model.Model" *#
#* @vtlvariable name="triple" type="com.hp.hpl.jena.graph.Triple" *#
#macro( triples $sub )
<dl>

Expand Down Expand Up @@ -31,7 +32,11 @@
<span property="$triple.getPredicate().toString()">$esc.html($triple.getObject())</span>
#end
#if( $triple.getPredicate().toString().equals("http://fedora.info/definitions/v4/rest-api#digest") )
<a href="$topic/fcr:fixity">check fixity</a>
#set ($fixity_service = $helpers.getObjects($rdf, $triple.getSubject(), $rdfLexicon.HAS_FIXITY_SERVICE))

#if($fixity_service.hasNext())
<a href="$fixity_service.next().getObject().getURI()">check fixity</a>
#end
#end
</dd>
#set ($last_quad = $triple)
Expand Down
Expand Up @@ -79,6 +79,7 @@ public void testGetDatastreamFixity() throws RepositoryException {

doReturn(mockBinary).when(testObj).getResourceFromPath(externalPath);
when(mockBinary.getFixity(any(IdentifierConverter.class))).thenReturn(expected);
when(mockBinary.getPath()).thenReturn(externalPath);

final RdfStream actual = testObj.getDatastreamFixity(externalPath, mockRequest, uriInfo);

Expand Down
Expand Up @@ -33,7 +33,8 @@
import com.hp.hpl.jena.rdf.model.Resource;

import org.fcrepo.http.commons.api.rdf.HttpResourceConverter;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.serialization.SerializerUtil;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -66,7 +67,10 @@ public class HttpApiResourcesTest {
private Session mockSession;

@Mock
private FedoraResourceImpl mockResource;
private FedoraResource mockResource;

@Mock
private FedoraBinary mockBinary;

@Before
public void setUp() {
Expand Down Expand Up @@ -128,13 +132,12 @@ public void shouldNotDecorateNodesWithLinksToVersionsUnlessVersionable()
@Test
public void shouldDecorateDatastreamsWithLinksToFixityChecks()
throws RepositoryException {
when(mockResource.hasContent()).thenReturn(true);
when(mockResource.getPath()).thenReturn("/some/path/to/datastream");
when(mockBinary.getPath()).thenReturn("/some/path/to/datastream");
when(mockSerializers.keySet()).thenReturn(new HashSet<String>());
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
final Resource graphSubject = mockSubjects.reverse().convert(mockBinary);

final Model model =
testObj.createModelForResource(mockResource, uriInfo, mockSubjects);
testObj.createModelForResource(mockBinary, uriInfo, mockSubjects);

assertTrue(model.contains(graphSubject, HAS_FIXITY_SERVICE));
}
Expand Down
Expand Up @@ -213,6 +213,10 @@ public String getVersionDate(final Graph graph,
*/
public String getObjectTitle(final Graph graph, final Node subject) {

if (subject == null) {
return "";
}

final Property[] properties = new Property[] {RDFS_LABEL, DC_TITLE};

for (final Property p : properties) {
Expand Down
Expand Up @@ -53,12 +53,7 @@ public FedoraBinary getBinary() {
return new FedoraBinaryImpl(getContentNode());
}

/*
* (non-Javadoc)
* @see org.fcrepo.kernel.Datastream#getContent()
*/
@Override
public Node getContentNode() {
private Node getContentNode() {
LOGGER.trace("Retrieved datastream content node.");
try {
return node.getNode(JCR_CONTENT);
Expand All @@ -67,19 +62,6 @@ public Node getContentNode() {
}
}

/*
* (non-Javadoc)
* @see org.fcrepo.kernel.Datastream#getContent()
*/
@Override
public boolean hasContent() {
try {
return node.hasNode(JCR_CONTENT);
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

/**
* Check if the node has a fedora:datastream mixin
*
Expand Down
Expand Up @@ -131,15 +131,6 @@ public javax.jcr.Binary getBinaryContent() {
}
}

/*
* (non-Javadoc)
* @see org.fcrepo.kernel.Datastream#getContent()
*/
@Override
public boolean hasContent() {
return true;
}

/*
* (non-Javadoc)
* @see org.fcrepo.kernel.Datastream#setContent(java.io.InputStream,
Expand Down
Expand Up @@ -96,18 +96,6 @@ public FedoraResourceImpl(final Node node) {
this.node = node;
}

/* (non-Javadoc)
* @see org.fcrepo.kernel.FedoraResource#hasContent()
*/
@Override
public boolean hasContent() {
try {
return node.hasNode(JCR_CONTENT);
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

/* (non-Javadoc)
* @see org.fcrepo.kernel.FedoraResource#getNode()
*/
Expand Down
Expand Up @@ -78,20 +78,12 @@ public Iterator<Triple> apply(final FixityResult blob) {
final com.hp.hpl.jena.graph.Node resultSubject = getTransientFixitySubject();
final ImmutableSet.Builder<Triple> b = builder();
try {
b.add(create(resultSubject, RDF.type.asNode(), FIXITY_TYPE.asNode()));
b.add(create(idTranslator.reverse().convert(resource).asNode(),
HAS_FIXITY_RESULT.asNode(), resultSubject));
b.add(create(resultSubject, RDF.type.asNode(), FIXITY_TYPE.asNode()));
final String storeIdentifier = blob.getStoreIdentifier();
final com.hp.hpl.jena.graph.Node contentLocation = createResource(storeIdentifier)
.asNode();
b.add(create(resultSubject, HAS_CONTENT_LOCATION.asNode(),
contentLocation));
b.add(create(contentLocation,
RDF.type.asNode(),
CONTENT_LOCATION_TYPE.asNode()));
b.add(create(contentLocation,
HAS_CONTENT_LOCATION_VALUE.asNode(),
createLiteral(storeIdentifier)));

for (final FixityResult.FixityState state : blob.getStatus(size, digest)) {
b.add(create(resultSubject, HAS_FIXITY_STATE
Expand All @@ -106,6 +98,15 @@ public Iterator<Triple> apply(final FixityResult blob) {
createTypedLiteral(
blob.getComputedSize())
.asNode()));
b.add(create(resultSubject, HAS_CONTENT_LOCATION.asNode(),
contentLocation));
b.add(create(contentLocation,
RDF.type.asNode(),
CONTENT_LOCATION_TYPE.asNode()));
b.add(create(contentLocation,
HAS_CONTENT_LOCATION_VALUE.asNode(),
createLiteral(storeIdentifier)));

return b.build().iterator();
} catch (final RepositoryException e) {
throw propagate(e);
Expand Down
Expand Up @@ -121,12 +121,6 @@ public void testGetPath() throws RepositoryException {
verify(mockNode).getPath();
}

@Test
public void testHasContent() throws RepositoryException {
testObj.hasContent();
verify(mockNode).hasNode(JCR_CONTENT);
}

@Test
public void testGetCreatedDate() throws RepositoryException {
final Calendar someDate = Calendar.getInstance();
Expand Down
7 changes: 0 additions & 7 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/Datastream.java
Expand Up @@ -15,19 +15,12 @@
*/
package org.fcrepo.kernel;

import javax.jcr.Node;

/**
* @author bbpennel
* @since Feb 18, 2014
*/
public interface Datastream extends FedoraResource {

/**
* @return The Node of content associated with this datastream.
*/
Node getContentNode();

/**
* @return The binary stored in this datastream
*/
Expand Down
Expand Up @@ -35,12 +35,6 @@
*/
public interface FedoraResource {

/**
* Does the resource have a jcr:content child node?
* @return has content
*/
boolean hasContent();

/**
* @return The JCR node that backs this object.
*/
Expand Down

0 comments on commit 0fab585

Please sign in to comment.