Permalink
Browse files

Use Callimachus credentials for SERVICE block authentication

  • Loading branch information...
jamesrdf committed Apr 23, 2015
1 parent e703238 commit 1bb564709bd852d2be1a92ee79b40a06ce0134e2
View
@@ -1,5 +1,5 @@
# dependencies.properties
-sesame.url = http://downloads.sourceforge.net/project/sesame/Sesame%202/2.8.1/openrdf-sesame-2.8.1-onejar.jar
+sesame.url = http://downloads.sourceforge.net/project/sesame/Sesame%202/2.8.2/openrdf-sesame-2.8.2-SNAPSHOT-onejar.jar
alibaba.url = http://downloads.sourceforge.net/project/sesame/AliBabaElmo/2.1/openrdf-alibaba-2.1-SNAPSHOT.zip
httpclient.url = http://archive.apache.org/dist/httpcomponents/httpasyncclient/binary/httpcomponents-asyncclient-4.1-beta1-bin.zip
mail.url = http://download.java.net/maven/1/javax.mail/jars/mail-1.4.jar
@@ -3,3 +3,4 @@ org.callimachusproject.interceptors.AlternativeHandler
org.callimachusproject.interceptors.LinksFilter
org.callimachusproject.interceptors.ActivityProcessor
org.callimachusproject.interceptors.SchemaGraphSynchronizer
+org.callimachusproject.interceptors.HttpClientConnectionFilter
@@ -0,0 +1,85 @@
+package org.callimachusproject.interceptors;
+
+import java.io.IOException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.protocol.HttpContext;
+import org.callimachusproject.traits.CalliObject;
+import org.openrdf.OpenRDFException;
+import org.openrdf.http.client.HttpClientDependent;
+import org.openrdf.http.client.SesameClientImpl;
+import org.openrdf.http.object.chain.HttpRequestChainInterceptor;
+import org.openrdf.http.object.client.HttpUriClient;
+import org.openrdf.http.object.helpers.ObjectContext;
+import org.openrdf.query.algebra.evaluation.federation.FederatedServiceResolverClient;
+import org.openrdf.query.algebra.evaluation.federation.FederatedServiceResolverImpl;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.base.RepositoryConnectionWrapper;
+import org.openrdf.repository.object.ObjectConnection;
+import org.openrdf.repository.sail.SailRepositoryConnection;
+import org.openrdf.sail.SailConnection;
+import org.openrdf.sail.helpers.SailConnectionWrapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpClientConnectionFilter implements HttpRequestChainInterceptor {
+ private final Logger logger = LoggerFactory.getLogger(HttpClientConnectionFilter.class);
+ private final ExecutorService executor = Executors.newCachedThreadPool();
+
+ @Override
+ public HttpResponse intercept(HttpRequest request, HttpContext context)
+ throws HttpException, IOException {
+ ObjectContext ctx = ObjectContext.adapt(context);
+ CalliObject target = (CalliObject) ctx.getResourceTarget().getTargetObject();
+ try {
+ HttpUriClient client = target.getHttpClient();
+ ObjectConnection con = target.getObjectConnection();
+ FederatedServiceResolverImpl resolver = new FederatedServiceResolverImpl();
+ resolver.setSesameClient(new SesameClientImpl(client, executor));
+ setClient(con, resolver);
+ } catch (OpenRDFException e) {
+ logger.error(e.toString(), e);
+ }
+ return null;
+ }
+
+ @Override
+ public void process(HttpRequest request, HttpResponse response,
+ HttpContext context) throws HttpException, IOException {
+ // HttpClient is setup to auto-close
+ }
+
+ private void setClient(RepositoryConnection con,
+ FederatedServiceResolverImpl resolver) {
+ if (con instanceof SailRepositoryConnection) {
+ setClient(((SailRepositoryConnection) con).getSailConnection(),
+ resolver);
+ } else if (con instanceof FederatedServiceResolverClient) {
+ ((FederatedServiceResolverClient) con)
+ .setFederatedServiceResolver(resolver);
+ } else if (con instanceof HttpClientDependent) {
+ ((HttpClientDependent) con).setHttpClient(resolver.getHttpClient());
+ } else if (con instanceof RepositoryConnectionWrapper) {
+ setClient(((RepositoryConnectionWrapper) con).getDelegate(),
+ resolver);
+ }
+ }
+
+ private void setClient(SailConnection con,
+ FederatedServiceResolverImpl resolver) {
+ if (con instanceof FederatedServiceResolverClient) {
+ ((FederatedServiceResolverClient) con)
+ .setFederatedServiceResolver(resolver);
+ } else if (con instanceof HttpClientDependent) {
+ ((HttpClientDependent) con).setHttpClient(resolver.getHttpClient());
+ } else if (con instanceof SailConnectionWrapper) {
+ setClient(((SailConnectionWrapper) con).getWrappedConnection(),
+ resolver);
+ }
+ }
+
+}

0 comments on commit 1bb5647

Please sign in to comment.