Skip to content

Commit

Permalink
rename classes and make the triple-providers create their own models
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 30, 2013
1 parent 070d7c4 commit 8dd277a
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 86 deletions.
Expand Up @@ -2,16 +2,16 @@

import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.FedoraExport;
import org.fcrepo.api.FedoraFieldSearch;
import org.fcrepo.api.FedoraFixity;
import org.fcrepo.api.FedoraNodes;
import org.fcrepo.api.FedoraSitemap;
import org.fcrepo.api.FedoraTransactions;
import org.fcrepo.api.FedoraVersions;
import org.fcrepo.api.rdf.HttpComponentAppender;
import org.fcrepo.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.api.repository.FedoraRepositoryNamespaces;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.serialization.FedoraObjectSerializer;
Expand All @@ -23,14 +23,15 @@
import java.util.Map;

@Component
public class HttpApiResources implements HttpComponentAppender {
public class HttpApiResources implements UriAwareResourceModelFactory {

@javax.annotation.Resource
protected Map<String, FedoraObjectSerializer> serializers;

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

final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());

if (resource.getNode().getPrimaryNodeType().isNodeType("mode:root")) {
Expand All @@ -56,6 +57,6 @@ public void addResponseInformationToDataset(FedoraResource resource, Model model
model.add(s, model.createProperty("info:fedora/runFixityCheck"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(pathMap).toASCIIString()));
}


return model;
}
}
Expand Up @@ -22,7 +22,7 @@
import com.hp.hpl.jena.query.Dataset;
import org.apache.commons.io.IOUtils;
import org.apache.jena.riot.WebContent;
import org.fcrepo.api.rdf.HttpComponentInjector;
import org.fcrepo.api.rdf.HttpTripleUtil;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.identifiers.PidMinter;
Expand Down Expand Up @@ -81,7 +81,7 @@ public abstract class AbstractResource {
protected DatastreamService datastreamService;

@Autowired(required=false)
private HttpComponentInjector httpComponentInjector;
private HttpTripleUtil httpTripleUtil;

/**
* A resource that can mint new Fedora PIDs.
Expand Down Expand Up @@ -188,8 +188,8 @@ protected FedoraResource createObjectOrDatastreamFromRequestContent(
}

protected void addResponseInformationToDataset(final FedoraResource resource, final Dataset dataset, final UriInfo uriInfo, GraphSubjects subjects) throws RepositoryException {
if (httpComponentInjector != null) {
httpComponentInjector.addResponseInformationToDataset(resource, dataset, uriInfo, subjects);
if (httpTripleUtil != null) {
httpTripleUtil.addHttpComponentModelsForResource(dataset, resource, uriInfo, subjects);
}
}

Expand Down

This file was deleted.

This file was deleted.

@@ -0,0 +1,58 @@
package org.fcrepo.api.rdf;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import org.fcrepo.FedoraResource;
import org.fcrepo.rdf.GraphSubjects;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Utility for injecting HTTP-contextual data into a Dataset
*/
@Component
public class HttpTripleUtil implements ApplicationContextAware {
private static final Logger LOGGER = getLogger(HttpTripleUtil.class);
private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

/**
* Add additional models to the RDF dataset for the given resource
* @param dataset the source dataset we'll add named models to
* @param resource the FedoraResource in question
* @param uriInfo a JAX-RS UriInfo object to build URIs to resources
* @param graphSubjects
* @throws RepositoryException
*/
public void addHttpComponentModelsForResource(Dataset dataset, FedoraResource resource, UriInfo uriInfo, GraphSubjects graphSubjects) throws RepositoryException {

LOGGER.debug("Adding additional HTTP context triples to dataset");
for (final Map.Entry<String, UriAwareResourceModelFactory> e : getUriAwareTripleFactories().entrySet()) {
final String beanName = e.getKey();
final UriAwareResourceModelFactory uriAwareResourceModelFactory = e.getValue();
LOGGER.debug("Adding response information using {}", beanName);

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

}

private Map<String, UriAwareResourceModelFactory> getUriAwareTripleFactories() {
return applicationContext.getBeansOfType(UriAwareResourceModelFactory.class);

}
}
@@ -0,0 +1,16 @@
package org.fcrepo.api.rdf;

import com.hp.hpl.jena.rdf.model.Model;
import org.fcrepo.FedoraResource;
import org.fcrepo.rdf.GraphSubjects;

import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

/**
* Helper to generate an RDF model for a FedoraResource that (likely) creates relations
* from our resource to other HTTP components
*/
public interface UriAwareResourceModelFactory {
Model createModelForResource(final FedoraResource resource, final UriInfo uriInfo, GraphSubjects graphSubjects) throws RepositoryException;
}
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import org.fcrepo.FedoraResource;
import org.fcrepo.rdf.GraphSubjects;
Expand All @@ -22,9 +21,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class HttpComponentInjectorTest {
public class HttpTripleUtilTest {

private HttpComponentInjector testObj;
private HttpTripleUtil testObj;
private Dataset dataset;
private UriInfo mockUriInfo;
private GraphSubjects mockSubjects;
Expand All @@ -33,7 +32,7 @@ public class HttpComponentInjectorTest {
@Before
public void setUp() {
mockContext = mock(ApplicationContext.class);
testObj = new HttpComponentInjector();
testObj = new HttpTripleUtil();
testObj.setApplicationContext(mockContext);

dataset = DatasetFactory.create(ModelFactory.createDefaultModel());
Expand All @@ -47,12 +46,15 @@ public void setUp() {
public void shouldAddTriplesFromRegisteredBeans() throws RepositoryException {
final FedoraResource mockResource = mock(FedoraResource.class);

HttpComponentAppender mockBean1 = mock(HttpComponentAppender.class);
HttpComponentAppender mockBean2 = mock(HttpComponentAppender.class);
Map<String, HttpComponentAppender> mockBeans = ImmutableBiMap.of("doesnt", mockBean1, "matter", mockBean2);
when(mockContext.getBeansOfType(HttpComponentAppender.class)).thenReturn(mockBeans);
testObj.addResponseInformationToDataset(mockResource, dataset, mockUriInfo, mockSubjects);
verify(mockBean1).addResponseInformationToDataset(eq(mockResource), any(Model.class), eq(mockUriInfo), eq(mockSubjects));
verify(mockBean2).addResponseInformationToDataset(eq(mockResource), any(Model.class), eq(mockUriInfo), eq(mockSubjects));
UriAwareResourceModelFactory mockBean1 = mock(UriAwareResourceModelFactory.class);
UriAwareResourceModelFactory mockBean2 = mock(UriAwareResourceModelFactory.class);
Map<String, UriAwareResourceModelFactory> mockBeans = ImmutableBiMap.of("doesnt", mockBean1, "matter", mockBean2);
when(mockContext.getBeansOfType(UriAwareResourceModelFactory.class)).thenReturn(mockBeans);
when(mockBean1.createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects))).thenReturn(ModelFactory.createDefaultModel());
when(mockBean2.createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects))).thenReturn(ModelFactory.createDefaultModel());

testObj.addHttpComponentModelsForResource(dataset, mockResource, mockUriInfo, mockSubjects);
verify(mockBean1).createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects));
verify(mockBean2).createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects));
}
}
@@ -1,22 +1,26 @@
package org.fcrepo.syndication;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpComponentAppender;
import org.fcrepo.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.rdf.GraphSubjects;

import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

public class RssResources implements HttpComponentAppender {
public class RssResources implements UriAwareResourceModelFactory {
@Override
public void addResponseInformationToDataset(FedoraResource resource, Model model, UriInfo uriInfo, GraphSubjects graphSubjects) throws RepositoryException {
public Model createModelForResource(FedoraResource resource, UriInfo uriInfo, GraphSubjects graphSubjects) throws RepositoryException {

final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());

if (resource.getNode().getPrimaryNodeType().isNodeType("mode:root")) {
model.add(s, model.createProperty("http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#link-type-feed"), model.createResource(uriInfo.getBaseUriBuilder().path(RSSPublisher.class).build().toASCIIString()));
}

return model;
}
}
@@ -1,22 +1,25 @@
package org.fcrepo.webhooks;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.rdf.HttpComponentAppender;
import org.fcrepo.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.rdf.GraphSubjects;

import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriInfo;

public class WebhooksResources implements HttpComponentAppender {
public class WebhooksResources implements UriAwareResourceModelFactory {
@Override
public void addResponseInformationToDataset(FedoraResource resource, Model model, UriInfo uriInfo, GraphSubjects graphSubjects) throws RepositoryException {

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

if (resource.getNode().getPrimaryNodeType().isNodeType("mode:root")) {
model.add(s, model.createProperty("http://microformats.org/wiki/rel-subscription"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraWebhooks.class).build().toASCIIString()));
}

return model;
}
}

0 comments on commit 8dd277a

Please sign in to comment.