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

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Dec 7, 2013
1 parent b7f2dde commit 5a07599
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@ FedoraRepository/
indexes/
ObjectStore/
*/.cache
fcrepo-jms-indexer-core/data/**
.cache
.DS_Store
*~
Expand Down
@@ -0,0 +1,62 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package org.fcrepo.indexer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;


/**
* @author ajs6f
* @date Dec 7, 2013
*/
public abstract class CachingRetriever implements IndexableContentRetriever {

private Boolean cached = false;

private byte[] cache;

/* (non-Javadoc)
* @see java.util.concurrent.Callable#call()
*/
@Override
public InputStream call() throws ClientProtocolException, IOException,
CannotTransformToNamedFieldsException, HttpException {
if (cached) {
return new ByteArrayInputStream(cache);
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
retrieveHttpResponse().getEntity().writeTo(out);
cache = out.toByteArray();
}
cached = true;
return new ByteArrayInputStream(cache);
}

protected abstract HttpResponse retrieveHttpResponse()
throws CannotTransformToNamedFieldsException,
ClientProtocolException, IOException, HttpException;

}
Expand Up @@ -163,12 +163,13 @@ public void onMessage(final Message message) {
try (final InputStream result = nfr.call()) {
content = new InputStreamReader(result);
hasContent = true;
} catch (final CannotTransformToNamedFieldsException e) {
hasContent = false;
} catch (final IOException e) {
} catch (final IOException | HttpException e) {
LOGGER.error(
"Could not retrieve content for update!",
e);
hasContent = false;
} catch (final CannotTransformToNamedFieldsException e) {
hasContent = false;
}
case RDF:
try (final InputStream result = rdfr.call()) {
Expand All @@ -178,6 +179,9 @@ public void onMessage(final Message message) {
LOGGER.error(
"Could not retrieve content for update!",
e);
hasContent = false;
} catch (final CannotTransformToNamedFieldsException e1) {
hasContent = false;
}
default:
content = new StringReader(pid);
Expand Down
Expand Up @@ -21,10 +21,7 @@
import static org.fcrepo.indexer.IndexerGroup.INDEXING_TRANSFORM_PREDICATE;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
Expand All @@ -42,18 +39,14 @@
* @author ajs6f
* @date Dec 6, 2013
*/
public class NamedFieldsRetriever implements IndexableContentRetriever {
public class NamedFieldsRetriever extends CachingRetriever {

private final String uri;

private final HttpClient httpClient;

private static final Logger LOGGER = getLogger(NamedFieldsRetriever.class);

private Boolean cached = false;

private byte[] cache;

/**
* @param uri
* @param client
Expand All @@ -64,10 +57,8 @@ public NamedFieldsRetriever(final String uri, final HttpClient client) {
}

@Override
public InputStream call() throws CannotTransformToNamedFieldsException, ClientProtocolException, IOException {
if (cached) {
return new ByteArrayInputStream(cache);
}
public HttpResponse retrieveHttpResponse() throws CannotTransformToNamedFieldsException,
ClientProtocolException, IOException {
LOGGER.debug("Retrieving RDF representation from: {}", uri);
String transformKey;
try {
Expand All @@ -89,14 +80,8 @@ public InputStream call() throws CannotTransformToNamedFieldsException, ClientPr
new HttpGet(uri + "/fcr:transform/" + transformKey);
LOGGER.debug("Retrieving transformed resource from: {}",
transformedResourceRequest.getURI());
final HttpResponse transformedResourceResponse =
return
httpClient.execute(transformedResourceRequest);
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
transformedResourceResponse.getEntity().writeTo(out);
cache = out.toByteArray();
}
cached = true;
return new ByteArrayInputStream(cache);
}

}
Expand Up @@ -18,10 +18,7 @@

import static org.apache.http.HttpStatus.SC_OK;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
Expand All @@ -38,18 +35,14 @@
* @author ajs6f
* @date Dec 6, 2013
*/
public class RdfRetriever implements IndexableContentRetriever {
public class RdfRetriever extends CachingRetriever {

private static final String RDF_SERIALIZATION = "application/rdf+xml";

private final String identifier;

private final HttpClient httpClient;

private Boolean cached = false;

private byte[] cache;

/**
* @param identifier
* @param client
Expand All @@ -60,21 +53,13 @@ public RdfRetriever(final String identifier, final HttpClient client) {
}

@Override
public InputStream call() throws ClientProtocolException, IOException,
public HttpResponse retrieveHttpResponse() throws ClientProtocolException, IOException,
HttpException {
if (cached) {
return new ByteArrayInputStream(cache);
}
final HttpUriRequest request = new HttpGet(identifier);
request.addHeader("Accept", RDF_SERIALIZATION);
final HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == SC_OK) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
response.getEntity().writeTo(out);
cache = out.toByteArray();
}
cached = true;
return new ByteArrayInputStream(cache);
return response;
} else {
throw new HttpException(response.getStatusLine().getReasonPhrase());
}
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.fcrepo.indexer;
package org.fcrepo.indexer.sparql;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.sparql.util.Context.emptyContext;
Expand All @@ -38,6 +38,8 @@
import com.hp.hpl.jena.update.UpdateProcessor;
import com.hp.hpl.jena.update.UpdateRequest;

import org.fcrepo.indexer.Indexer;
import org.fcrepo.indexer.Indexer.IndexerType;
import org.slf4j.Logger;


Expand Down
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

package org.fcrepo.indexer.integration;
package org.fcrepo.indexer.integration.sparql;

import java.io.StringReader;

import javax.inject.Inject;

import org.fcrepo.indexer.SparqlIndexer;
import org.fcrepo.indexer.sparql.SparqlIndexer;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.test.context.ContextConfiguration;
Expand Down
Expand Up @@ -8,7 +8,7 @@
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

<!-- sparql-update indexer -->
<bean id="sparqlUpdate" class="org.fcrepo.indexer.SparqlIndexer">
<bean id="sparqlUpdate" class="org.fcrepo.indexer.sparql.SparqlIndexer">

<!-- fuseki -->
<property name="queryBase"
Expand Down
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- sparql-update indexer -->
<bean id="sparqlUpdate" class="org.fcrepo.indexer.SparqlIndexer">
<bean id="sparqlUpdate" class="org.fcrepo.indexer.sparql.SparqlIndexer">

<!-- fuseki -->
<property name="queryBase" value="http://${fuseki.host:localhost}:${fuseki.port:3030}/test/query"/>
Expand Down

0 comments on commit 5a07599

Please sign in to comment.