Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a way for modules to inject triples into the describe responses f…
…or a resource (to add e.g. HATEOS responsess)
- Loading branch information
Showing
13 changed files
with
289 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
fcrepo-http-api/src/main/java/org/fcrepo/url/HttpApiResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.fcrepo.url; | ||
|
||
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.FedoraSitemap; | ||
import org.fcrepo.api.FedoraTransactions; | ||
import org.fcrepo.api.FedoraVersions; | ||
import org.fcrepo.api.rdf.UriAwareResourceModelFactory; | ||
import org.fcrepo.api.repository.FedoraRepositoryNamespaces; | ||
import org.fcrepo.rdf.GraphSubjects; | ||
import org.fcrepo.serialization.FedoraObjectSerializer; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.core.UriInfo; | ||
|
||
import java.util.Map; | ||
|
||
@Component | ||
public class HttpApiResources implements UriAwareResourceModelFactory { | ||
|
||
@javax.annotation.Resource | ||
protected Map<String, FedoraObjectSerializer> serializers; | ||
|
||
@Override | ||
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/links.html#link-type-search"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraFieldSearch.class).build().toASCIIString())); | ||
model.add(s, model.createProperty("http://microformats.org/wiki/rel-sitemap"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraSitemap.class).build().toASCIIString())); | ||
model.add(s, model.createProperty("info:fedora/hasTransactionProvider"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraTransactions.class).build().toASCIIString())); | ||
model.add(s, model.createProperty("info:fedora/hasNamespaces"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class).build().toASCIIString())); | ||
|
||
} else { | ||
|
||
for (String key : serializers.keySet()) { | ||
final Map<String, String> pathMap = ImmutableBiMap.of("path", resource.getPath().substring(1), "format", key); | ||
model.add(s, model.createProperty("info:fedora/exportsAs"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraExport.class).buildFromMap(pathMap).toASCIIString())); | ||
} | ||
|
||
final Map<String, String> pathMap = ImmutableBiMap.of("path", resource.getPath().substring(1)); | ||
model.add(s, model.createProperty("info:fedora/hasVersions"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraVersions.class).buildFromMap(pathMap).toASCIIString())); | ||
|
||
} | ||
|
||
if (resource.hasContent()) { | ||
final Map<String, String> pathMap = ImmutableBiMap.of("path", resource.getPath().substring(1)); | ||
model.add(s, model.createProperty("info:fedora/runFixityCheck"), model.createResource(uriInfo.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(pathMap).toASCIIString())); | ||
} | ||
|
||
return model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
fcrepo-http-commons/src/main/java/org/fcrepo/api/rdf/HttpTripleUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
fcrepo-http-commons/src/main/java/org/fcrepo/api/rdf/UriAwareResourceModelFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
fcrepo-http-commons/src/test/java/org/fcrepo/api/rdf/HttpTripleUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.fcrepo.api.rdf; | ||
|
||
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.ModelFactory; | ||
import org.fcrepo.FedoraResource; | ||
import org.fcrepo.rdf.GraphSubjects; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.core.UriInfo; | ||
|
||
import java.util.Map; | ||
|
||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class HttpTripleUtilTest { | ||
|
||
private HttpTripleUtil testObj; | ||
private Dataset dataset; | ||
private UriInfo mockUriInfo; | ||
private GraphSubjects mockSubjects; | ||
private ApplicationContext mockContext; | ||
|
||
@Before | ||
public void setUp() { | ||
mockContext = mock(ApplicationContext.class); | ||
testObj = new HttpTripleUtil(); | ||
testObj.setApplicationContext(mockContext); | ||
|
||
dataset = DatasetFactory.create(ModelFactory.createDefaultModel()); | ||
mockUriInfo = mock(UriInfo.class); | ||
mockSubjects = mock(GraphSubjects.class); | ||
|
||
|
||
} | ||
|
||
@Test | ||
public void shouldAddTriplesFromRegisteredBeans() throws RepositoryException { | ||
final FedoraResource mockResource = mock(FedoraResource.class); | ||
|
||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
fcrepo-rss/src/main/java/org/fcrepo/syndication/RssResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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.UriAwareResourceModelFactory; | ||
import org.fcrepo.rdf.GraphSubjects; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.core.UriInfo; | ||
|
||
public class RssResources implements UriAwareResourceModelFactory { | ||
@Override | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
fcrepo-webhooks/src/main/java/org/fcrepo/webhooks/WebhooksResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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.UriAwareResourceModelFactory; | ||
import org.fcrepo.rdf.GraphSubjects; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.core.UriInfo; | ||
|
||
public class WebhooksResources implements UriAwareResourceModelFactory { | ||
@Override | ||
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; | ||
} | ||
} |