Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible HttpClient configuration for interactions with Fedora #113

Merged
merged 13 commits into from
May 15, 2017
Merged
13 changes: 12 additions & 1 deletion fcrepo-api-x-binding/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fcrepo.apix</groupId>
Expand Down Expand Up @@ -35,7 +36,17 @@
<groupId>org.fcrepo.client</groupId>
<artifactId>fcrepo-java-client</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-osgi</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -36,10 +37,13 @@
import org.fcrepo.apix.model.components.OntologyService;
import org.fcrepo.apix.model.components.Registry;
import org.fcrepo.apix.model.components.ResourceNotFoundException;
import org.fcrepo.client.FcrepoClient;
import org.fcrepo.client.FcrepoResponse;
import org.fcrepo.client.FcrepoLink;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -58,19 +62,25 @@
@Component(configurationPolicy = ConfigurationPolicy.REQUIRE)
public class RuntimeExtensionBinding implements ExtensionBinding {

private static final URI NON_RDF_SOURCE = URI.create("http://www.w3.org/ns/ldp#NonRDFSource");

private static final Logger LOG = LoggerFactory.getLogger(RuntimeExtensionBinding.class);

// TODO: Inject this
private static final FcrepoClient client = FcrepoClient.client().throwExceptionOnFailure().build();
private CloseableHttpClient httpClient;

private ExtensionRegistry extensionRegistry;

private OntologyService ontologySvc;

private Registry registry;

/**
* Set the http client
*
* @param client client;
*/
public void setHttpClient(final CloseableHttpClient client) {
this.httpClient = client;
}

/**
* Set the underlying registry containing extensions that may be bound.
*
Expand Down Expand Up @@ -170,33 +180,44 @@ public Collection<Extension> getExtensionsFor(final URI resourceURI, final Colle
}

// Use object contents for reasoning, or if binary the binary's description
try (FcrepoResponse head = client.head(resourceURI).perform()) {
if (head.getLinkHeaders("type").contains(NON_RDF_SOURCE)) {
final List<URI> describedby = head.getLinkHeaders("describedby");
try (final CloseableHttpResponse response = httpClient.execute(new HttpHead(resourceURI))) {

if (describedby.size() > 1) {
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException(String.format("Got unexpected status code %s in HEAD to <%s>",
response.getStatusLine().getStatusCode(),
resourceURI));
}

final List<FcrepoLink> describedByLinks =
Arrays.asList(response.getHeaders("Link")).stream().map(Header::getValue)
.map(FcrepoLink::new)
.filter(l -> "describedby".equals(l.getRel()))
.collect(Collectors.toList());

if (!describedByLinks.isEmpty()) {
if (describedByLinks.size() > 1) {
throw new RuntimeException(
String.format("Ambiguous; more than one describes header for <%s>", resourceURI));
} else if (describedby.size() == 0) {
LOG.warn("No rdf description for binary <{}>", resourceURI);
return Collections.emptyList();
}

LOG.debug("Using <{}> for inference about binary <{}>", describedby.get(0), resourceURI);
LOG.debug("Using <{}> for inference about binary <{}>", describedByLinks.get(0).getUri(),
resourceURI);

try (WebResource resource = registry.get(describedby.get(0))) {
try (WebResource resource = registry.get(describedByLinks.get(0).getUri())) {
return getExtensionsFor(WebResource.of(
resource.representation(),
resource.contentType(),
resourceURI, null), from);
}

} else {
try (WebResource resource = registry.get(resourceURI)) {
return getExtensionsFor(resource, from);
}
}

} catch (final Exception e) {
throw new RuntimeException(e);
throw new RuntimeException("Could not get triples for reasoning over " + resourceURI, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
<reference id="delegateRegistry" interface="org.fcrepo.apix.model.components.Registry"
filter="(org.fcrepo.apix.registry.role=default)" />

<reference id="httpClientFetcher" ext:proxy-method="classes"
interface="org.fcrepo.apix.registry.HttpClientFetcher" />

<bean id="httpClient" factory-ref="httpClientFetcher"
factory-method="getClient" />

<bean id="runtimeExtensionBindingImpl" class="org.fcrepo.apix.binding.impl.RuntimeExtensionBinding">
<property name="extensionRegistry" ref="extensionRegistry" />
<property name="ontologyService" ref="ontologyService" />
<property name="delegateRegistry" ref="delegateRegistry" />
<property name="httpClient" ref="httpClient" />
</bean>

<service id="runtimeExtensionBinding" ref="runtimeExtensionBindingImpl"
Expand Down
11 changes: 6 additions & 5 deletions fcrepo-api-x-indexing/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fcrepo.apix</groupId>
Expand Down Expand Up @@ -75,11 +76,11 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.fcrepo.apix</groupId>
<artifactId>fcrepo-api-x-jena</artifactId>
<version>${project.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -146,7 +147,7 @@ public void configure() throws Exception {

// This is annoying, no easy way around
.doTry()
.to("http://get-servicedoc-uri")
.to("http://get-servicedoc-uri?httpClient=#httpClient")
.doCatch(HttpOperationFailedException.class)
.to("direct:410")
.doFinally()
Expand Down Expand Up @@ -175,7 +176,7 @@ public void configure() throws Exception {
.routeId("perform-delete")
.setHeader(FCREPO_NAMED_GRAPH, bodyAs(URI.class))
.process(SPARQL_DELETE_PROCESSOR)
.log(LoggingLevel.INFO, LOG,
.log(LoggingLevel.DEBUG, LOG,
"Deleting service doc of ${headers[CamelFcrepoUri]}")
.to("{{triplestore.baseUrl}}");

Expand All @@ -188,7 +189,7 @@ public void configure() throws Exception {
.setHeader(FCREPO_NAMED_GRAPH, header(Exchange.HTTP_URI))
.removeHeaders("CamelHttp*")
.process(SPARQL_UPDATE_PROCESSOR)
.log(LoggingLevel.INFO, LOG,
.log(LoggingLevel.DEBUG, LOG,
"Indexing service doc of ${headers[CamelFcrepoUri]}")
.to("{{triplestore.baseUrl}}");

Expand All @@ -207,6 +208,13 @@ public void configure() throws Exception {
@SuppressWarnings("unchecked")
static final Processor GET_SERVICE_DOC_HEADER = ex -> {

final StringBuilder headers = new StringBuilder();
for (final Map.Entry<String, Object> entry : ex.getIn().getHeaders().entrySet()) {
headers.append(entry + "\n");
}

LOG.debug("Getting serice doc header from " + headers.toString());

final Set<String> rawLinkHeaders = new HashSet<>();

final Object linkHeader = ex.getIn().getHeader("Link");
Expand All @@ -232,7 +240,8 @@ public void configure() throws Exception {
final Model model = ModelFactory.createDefaultModel();

RDFDataMgr.read(model, ex.getIn().getBody(InputStream.class),
contentTypeToLang(parse(ex.getIn().getHeader(Exchange.CONTENT_TYPE, String.class)).getMimeType()));
contentTypeToLang(parse(ex.getIn().getHeader(Exchange.CONTENT_TYPE, String.class))
.getMimeType()));

model.write(serializedGraph, "N-TRIPLE");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

</cm:property-placeholder>

<reference id="httpClientFetcher" ext:proxy-method="classes"
interface="org.fcrepo.apix.registry.HttpClientFetcher" />

<bean id="httpClient" factory-ref="httpClientFetcher"
factory-method="getClient" />

<bean id="indexerRoutes" class="org.fcrepo.apix.indexing.impl.ServiceIndexingRoutes">
<property name="extensionContainer" value="${ldp.path.extension.container}" />
<property name="reindexStream" value="${service.reindex.stream}" />
Expand All @@ -34,6 +40,7 @@
<bean id="http" class="org.apache.camel.component.http4.HttpComponent" />
<bean id="https" class="org.apache.camel.component.http4.HttpComponent" />


<reference id="broker" interface="org.apache.camel.Component"
filter="(osgi.jndi.service.name=fcrepo/Broker)" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void init() throws Exception {

/**
* Advise routes before the camel context starts
*
* @throws Exception
*/
@Override
Expand Down Expand Up @@ -154,9 +155,10 @@ public void configure() throws Exception {
});

context.getRouteDefinition("get-servicedoc-uri").adviceWith(context, new AdviceWithRouteBuilder() {

@Override
public void configure() throws Exception {
interceptSendToEndpoint("http://get-servicedoc-uri")
interceptSendToEndpoint("http://get-servicedoc-uri*")
.skipSendToOriginalEndpoint().to("direct:apix-head");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<bean id="http" class="org.apache.camel.component.http4.HttpComponent" />
<bean id="https" class="org.apache.camel.component.http4.HttpComponent" />

<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create" />

<bean id="httpClient" factory-ref="httpClientBuilder"
factory-method="build" />

<camelContext id="FcrepoServiceIndexer"
xmlns="http://camel.apache.org/schema/blueprint">
<routeBuilder ref="indexerRoutes" />
Expand Down
39 changes: 39 additions & 0 deletions fcrepo-api-x-indexing/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright 2017 Johns Hopkins University ~ ~ 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. -->

<!DOCTYPE configuration>

<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

<encoder>

<pattern>%p %d{HH:mm:ss.SSS} \(%c{0}\) %m%n</pattern>

</encoder>

</appender>



<logger name="org.fcrepo" additivity="false" level="${fcrepo.log:-INFO}">

<appender-ref ref="STDOUT" />

</logger>

<root additivity="false" level="INFO">

<appender-ref ref="STDOUT" />

</root>

</configuration>
Loading