Skip to content

Commit

Permalink
convert HTTP-injected triples to use RdfStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 30, 2013
1 parent f4fede9 commit 219b658
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 87 deletions.
Expand Up @@ -35,6 +35,10 @@
import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableSet;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import org.fcrepo.http.api.FedoraExport;
import org.fcrepo.http.api.FedoraFieldSearch;
import org.fcrepo.http.api.FedoraFixity;
Expand All @@ -46,6 +50,8 @@
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.impl.NodeRdfContext;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.fcrepo.serialization.SerializerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -63,85 +69,82 @@ public class HttpApiResources implements UriAwareResourceModelFactory {
protected SerializerUtil serializers;

@Override
public Model createModelForResource(final FedoraResource resource,
public RdfStream createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final GraphSubjects graphSubjects)
throws RepositoryException {

final Model model = createDefaultModel();
final HttpNodeRdfContext triples = new HttpNodeRdfContext(graphSubjects, resource, uriInfo);

final Resource s = graphSubjects.getGraphSubject(resource.getNode());
triples.concatSerializers(serializers);

if (resource.getNode().getPrimaryNodeType().isNodeType(ROOT)) {
addRepositoryStatements(uriInfo, model, s);
} else {
addNodeStatements(resource, uriInfo, model, s);
return triples;
}

class HttpNodeRdfContext extends RdfStream {
private final Resource subject;
private FedoraResource resource;
private UriInfo uriInfo;

public HttpNodeRdfContext(final GraphSubjects graphSubjects, final FedoraResource resource, final UriInfo uriInfo) throws RepositoryException {
super();

this.resource = resource;
this.uriInfo = uriInfo;
this.subject = graphSubjects.getGraphSubject(resource.getNode());

addContentStatements();

if (resource.getNode().getPrimaryNodeType().isNodeType(ROOT)) {
addRepositoryStatements();
} else {
addNodeStatements();
}
}

if (resource.hasContent()) {
addContentStatements(resource, uriInfo, model, s);
private void addContentStatements() throws RepositoryException {
// fcr:fixity
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
concat(Triple.create(subject.asNode(), HAS_FIXITY_SERVICE.asNode(), NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(pathMap).toASCIIString())));
}

// fcr:export?format=xyz
for (final String key : serializers.keySet()) {
private void addNodeStatements() throws RepositoryException {

// fcr:versions
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
final Resource format =
createResource(uriInfo.getBaseUriBuilder().path(
FedoraExport.class).queryParam("format", key)
.buildFromMap(pathMap).toASCIIString());
model.add(s, HAS_SERIALIZATION, format);
model.add(format, RDFS_LABEL, key);
concat(Triple.create(subject.asNode(), HAS_VERSION_HISTORY.asNode(), NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraVersions.class).buildFromMap(pathMap).toASCIIString())));

}

return model;
}
private void addRepositoryStatements() {
final ImmutableSet.Builder<Triple> tripleBuilder = new ImmutableSet.Builder<Triple>();

private void addContentStatements(final FedoraResource resource, final UriInfo uriInfo,
final Model model, final Resource s) throws RepositoryException {
// 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).toASCIIString()));
}
tripleBuilder.add(Triple.create(subject.asNode(), HAS_SEARCH_SERVICE.asNode(),NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraFieldSearch.class).build().toASCIIString())));
tripleBuilder.add(Triple.create(subject.asNode(), HAS_TRANSACTION_SERVICE.asNode(),NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraRepositoryTransactions.class).build().toASCIIString())));
tripleBuilder.add(Triple.create(subject.asNode(), HAS_NAMESPACE_SERVICE.asNode(),NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class).build().toASCIIString())));
tripleBuilder.add(Triple.create(subject.asNode(), HAS_WORKSPACE_SERVICE.asNode(),NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraRepositoryWorkspaces.class).build().toASCIIString())));

private void addNodeStatements(final FedoraResource resource, final UriInfo uriInfo,
final Model model, final Resource s) throws RepositoryException {
concat(tripleBuilder.build());
}

// fcr:versions
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
model.add(s, HAS_VERSION_HISTORY, createResource(uriInfo
.getBaseUriBuilder().path(FedoraVersions.class).buildFromMap(
pathMap).toASCIIString()));
}
public void concatSerializers(SerializerUtil serializers) throws RepositoryException {

private void addRepositoryStatements(final UriInfo uriInfo, final Model model,
final Resource s) {
// fcr:search
model.add(s, HAS_SEARCH_SERVICE, createResource(uriInfo
.getBaseUriBuilder().path(FedoraFieldSearch.class).build()
.toASCIIString()));

// sitemap
model.add(s, HAS_SITEMAP, createResource(uriInfo.getBaseUriBuilder()
.path(FedoraSitemap.class).build().toASCIIString()));

// fcr:tx
model.add(s, HAS_TRANSACTION_SERVICE, createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryTransactions.class)
.build().toASCIIString()));

// fcr:namespaces
model.add(s, HAS_NAMESPACE_SERVICE, createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class)
.build().toASCIIString()));

// fcr:workspaces
model.add(s, HAS_WORKSPACE_SERVICE, createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryWorkspaces.class)
.build().toASCIIString()));
final ImmutableSet.Builder<Triple> tripleBuilder = new ImmutableSet.Builder<Triple>();

// fcr:export?format=xyz
for (final String key : serializers.keySet()) {
final Map<String, String> pathMap =
singletonMap("path", resource.getPath().substring(1));
final Resource format =
createResource(uriInfo.getBaseUriBuilder().path(FedoraExport.class).queryParam("format", key).buildFromMap(pathMap).toASCIIString());
tripleBuilder.add(Triple.create(subject.asNode(), HAS_SERIALIZATION.asNode(), format.asNode()));
tripleBuilder.add(Triple.create(format.asNode(), RDFS_LABEL.asNode(), NodeFactory.createLiteral(key)));
}

concat(tripleBuilder.build());
}
}


}
Expand Up @@ -96,7 +96,7 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks()
final Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

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

assertTrue(model.contains(graphSubject, HAS_SEARCH_SERVICE));
assertTrue(model.contains(graphSubject, HAS_SITEMAP));
Expand All @@ -116,7 +116,7 @@ public void shouldDecorateNodesWithLinksToVersionsAndExport()
final Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

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

assertTrue(model.contains(graphSubject, HAS_VERSION_HISTORY));
assertEquals(2, model.listObjectsOfProperty(graphSubject,
Expand All @@ -133,7 +133,7 @@ public void shouldDecorateDatastreamsWithLinksToFixityChecks()
final Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

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

assertTrue(model.contains(graphSubject, HAS_FIXITY_SERVICE));
}
Expand Down
Expand Up @@ -25,6 +25,7 @@

import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -71,10 +72,10 @@ public void addHttpComponentModelsForResource(Dataset dataset,
e.getValue();
LOGGER.debug("Adding response information using {}", beanName);

final Model m =
final RdfStream m =
uriAwareResourceModelFactory.createModelForResource(
resource, uriInfo, graphSubjects);
dataset.addNamedModel(beanName, m);
dataset.addNamedModel(beanName, m.asModel());
}

}
Expand Down
Expand Up @@ -22,7 +22,7 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.rdf.GraphSubjects;

import com.hp.hpl.jena.rdf.model.Model;
import org.fcrepo.kernel.utils.iterators.RdfStream;

/**
* Helper to generate an RDF model for a FedoraResource that (likely) creates
Expand All @@ -41,7 +41,7 @@ public interface UriAwareResourceModelFactory {
* @return
* @throws RepositoryException
*/
Model createModelForResource(final FedoraResource resource,
RdfStream createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, GraphSubjects graphSubjects)
throws RepositoryException;
}
18 changes: 11 additions & 7 deletions fcrepo-rss/src/main/java/org/fcrepo/syndication/RssResources.java
Expand Up @@ -19,11 +19,15 @@
import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -37,20 +41,20 @@
public class RssResources implements UriAwareResourceModelFactory {

@Override
public Model createModelForResource(FedoraResource resource,
public RdfStream createModelForResource(FedoraResource resource,
UriInfo uriInfo, GraphSubjects graphSubjects)
throws RepositoryException {

final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());
final RdfStream triples = new RdfStream();
final Node s = graphSubjects.getGraphSubject(resource.getNode()).asNode();

if (resource.getNode().getPrimaryNodeType().isNodeType(
FedoraJcrTypes.ROOT)) {
model.add(s, RdfLexicon.HAS_FEED, model.createResource(uriInfo
.getBaseUriBuilder().path(RSSPublisher.class).build()
.toASCIIString()));
triples.concat(Triple.create(s, RdfLexicon.HAS_FEED.asNode(), NodeFactory.createURI(uriInfo
.getBaseUriBuilder().path(RSSPublisher.class).build()
.toASCIIString())));
}

return model;
return triples;
}
}
Expand Up @@ -22,6 +22,7 @@
import org.fcrepo.kernel.rdf.impl.DefaultGraphSubjects;
import org.fcrepo.http.commons.test.util.TestHelpers;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -63,8 +64,8 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks() throws Reposito

Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

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

assertTrue(model.contains(graphSubject, HAS_FEED));
assertTrue(model.asModel().contains(graphSubject, HAS_FEED));
}
}
Expand Up @@ -16,6 +16,9 @@

package org.fcrepo.webhooks;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
Expand All @@ -24,6 +27,7 @@
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.springframework.stereotype.Component;

import javax.jcr.RepositoryException;
Expand All @@ -36,19 +40,19 @@
public class WebhooksResources implements UriAwareResourceModelFactory {

@Override
public Model createModelForResource(FedoraResource resource,
public RdfStream createModelForResource(FedoraResource resource,
UriInfo uriInfo, GraphSubjects graphSubjects)
throws RepositoryException {
final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());
final Node s = graphSubjects.getGraphSubject(resource.getNode()).asNode();

final RdfStream triples = new RdfStream();

if (resource.getNode().getPrimaryNodeType().isNodeType(
FedoraJcrTypes.ROOT)) {
model.add(s, RdfLexicon.HAS_SUBSCRIPTION_SERVICE, model
.createResource(uriInfo.getBaseUriBuilder().path(
FedoraWebhooks.class).build().toASCIIString()));
triples.concat(Triple.create(s, RdfLexicon.HAS_SUBSCRIPTION_SERVICE.asNode(), NodeFactory.createURI(uriInfo.getBaseUriBuilder().path(FedoraWebhooks.class).build().toASCIIString())));
}

return model;
return triples;

}
}
Expand Up @@ -32,6 +32,7 @@
import org.fcrepo.kernel.rdf.impl.DefaultGraphSubjects;
import org.fcrepo.http.commons.test.util.TestHelpers;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -71,10 +72,10 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks()

Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

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

assertTrue(model.contains(graphSubject, HAS_SUBSCRIPTION_SERVICE));
assertTrue(model.asModel().contains(graphSubject, HAS_SUBSCRIPTION_SERVICE));
}
}

0 comments on commit 219b658

Please sign in to comment.