Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
RdfRetriever uses HEAD to find descriptions of non-RDF resources, upd…
…ating dependencies so fcrepo-http-commons is test-scoped and required modules are explicitly included
  • Loading branch information
escowles committed Oct 23, 2014
1 parent e8289cd commit 01f4568
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
13 changes: 13 additions & 0 deletions fcrepo-message-consumer-core/pom.xml
Expand Up @@ -94,9 +94,22 @@
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-commons</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>lucene-core</artifactId>
Expand Down
Expand Up @@ -57,8 +57,8 @@
import static java.lang.Integer.MAX_VALUE;
import static javax.jcr.observation.Event.NODE_REMOVED;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;
import static org.fcrepo.kernel.RdfLexicon.CONTAINS;
import static org.fcrepo.kernel.RdfLexicon.HAS_MIXIN_TYPE;
import static org.fcrepo.kernel.RdfLexicon.HAS_PARENT;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.RESTAPI_NAMESPACE;
Expand Down Expand Up @@ -277,14 +277,12 @@ private void index( final URI uri, final String eventType ) throws URISyntaxExce

if (!removal) {
final Model rdf = rdfr.get();
if (rdf.contains(createResource(uri.toString()), type, INDEXABLE_MIXIN)) {
LOGGER.debug("Resource: {} retrieved with indexable type.",
uri);
if (rdf.contains(createResource(uri.toString()), type, INDEXABLE_MIXIN)
|| rdf.contains(createResource(uri.toString() + "/" + FCR_METADATA), type, INDEXABLE_MIXIN)) {
LOGGER.debug("Resource: {} retrieved with indexable type.", uri);
indexable = true;
} else {
LOGGER.debug(
"Resource: {} retrieved without indexable type.",
uri);
LOGGER.debug("Resource: {} retrieved without indexable type.", uri);
}

// if this is a datastream, also index the parent object
Expand Down Expand Up @@ -391,13 +389,7 @@ private void reindexURI( final URI uri, final boolean recursive ) throws URISynt
final NodeIterator children = model.listObjectsOfProperty( CONTAINS );
while ( children.hasNext() ) {
final URI child = new URI(children.nextNode().asResource().getURI());
if (model.contains(createResource(child.toString()), HAS_MIXIN_TYPE,
model.createLiteral("fedora:binary"))) {
final URI childURI = new URI(child.toString() + "/fcr:metadata");
if ( !reindexed.contains(childURI) ) {
reindexURI( childURI, false );
}
} else if ( !reindexed.contains(child) ) {
if ( !reindexed.contains(child) ) {
reindexURI( child, true );
}
}
Expand Down
Expand Up @@ -19,18 +19,21 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.apache.http.HttpStatus.SC_OK;
import static org.apache.jena.riot.WebContent.contentTypeN3;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;

import javax.ws.rs.core.Link;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;

Expand Down Expand Up @@ -66,11 +69,29 @@ public RdfRetriever(final URI identifier, final HttpClient client) {

@Override
public Model get() {
final HttpUriRequest request = new HttpGet(identifier);
request.addHeader("Accept", RDF_SERIALIZATION);
request.addHeader("Prefer", "return=representation; include=\"" + REPOSITORY_NAMESPACE + "EmbedResources\"");
LOGGER.debug("Retrieving RDF content from: {}...", request.getURI());

try {
// make an initial HEAD request and check Link headers for descriptions located elsewhere
final HttpHead headRequest = new HttpHead(identifier);
final HttpResponse headResponse = httpClient.execute(headRequest);
URI descriptionURI = null;
final Header[] links = headResponse.getHeaders("Link");
if ( links != null ) {
for ( Header h : headResponse.getHeaders("Link") ) {
final Link link = Link.valueOf(h.getValue());
if ( link.getRel().equals("describedby") ) {
descriptionURI = link.getUri();
LOGGER.debug("Using URI from Link header: {}", descriptionURI);
}
}
}
if ( descriptionURI == null ) {
descriptionURI = identifier;
}

final HttpUriRequest request = new HttpGet(descriptionURI);
request.addHeader("Accept", RDF_SERIALIZATION);
LOGGER.debug("Retrieving RDF content from: {}...", request.getURI());
final HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == SC_OK) {
try (
Expand Down
Expand Up @@ -24,6 +24,7 @@

import java.net.URI;
import javax.inject.Inject;
import javax.ws.rs.core.Link;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
Expand Down Expand Up @@ -77,11 +78,14 @@ private void createIndexableObject(final String uri) throws Exception {
createResource(uri, objectRdf, contentTypeN3Alt1);
LOGGER.debug("Created object at: {}", uri);
}
private void createResource(final String uri, final String content, final String contentType) throws Exception {
private HttpResponse createResource(final String uri, final String content, final String contentType)
throws Exception {
final HttpPut createRequest = new HttpPut(uri);
createRequest.setEntity(new StringEntity(content));
createRequest.addHeader("Content-Type", contentType);
assertEquals(201, client.execute(createRequest).getStatusLine().getStatusCode());
final HttpResponse response = client.execute(createRequest);
assertEquals(201, response.getStatusLine().getStatusCode());
return response;
}
private void shouldBeIndexed(final String uri) throws Exception {
final Long start = currentTimeMillis();
Expand Down Expand Up @@ -160,18 +164,19 @@ public void testDatastreamIndexing() throws Exception {
// create object with datastream
final String uri = serverAddress + randomUUID();
createIndexableObject(uri);
createResource(uri + "/ds1", "test datastream content", "text/plain");
final HttpResponse response = createResource(uri + "/ds1", "test datastream content", "text/plain");

// make datastream indexable
final HttpPatch patch = new HttpPatch(uri + "/ds1/fcr:metadata");
final URI descURI = Link.valueOf(response.getFirstHeader("Link").getValue()).getUri();
final HttpPatch patch = new HttpPatch(descURI);
final String sparqlUpdate = "insert { <> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "
+ "<http://fedora.info/definitions/v4/indexing#indexable> } where {}";
patch.setEntity(new StringEntity(sparqlUpdate));
patch.addHeader("Content-Type", "application/sparql-update");
assertEquals(204, client.execute(patch).getStatusLine().getStatusCode());

// make sure it was indexed
shouldBeIndexed(uri + "/ds1/fcr:metadata");
shouldBeIndexed(uri + "/ds1");
}

}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -165,7 +165,7 @@
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-fuseki</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<scope>test</scope>
</dependency>

Expand Down

0 comments on commit 01f4568

Please sign in to comment.