jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [28/34] jena git commit: Move Auth classes to "web" package. RDFConneection support LibSec.
Date Sat, 17 Nov 2018 17:20:54 GMT
Move Auth classes to "web" package. RDFConneection support LibSec.

Rename AuthMode->AuthScheme

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/8b73b7d9
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/8b73b7d9
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/8b73b7d9

Branch: refs/heads/master
Commit: 8b73b7d996cbaadfe8a96720e2dae54dd5d7fe31
Parents: 8e661d6
Author: Andy Seaborne <andy@apache.org>
Authored: Wed Nov 14 10:27:58 2018 +0000
Committer: Andy Seaborne <andy@apache.org>
Committed: Wed Nov 14 10:27:58 2018 +0000

----------------------------------------------------------------------
 .../org/apache/jena/atlas/web/AuthScheme.java   |  37 +++++
 .../java/org/apache/jena/web/AuthSetup.java     |  50 +++++++
 .../apache/jena/fuseki/access/AuthSetup.java    |  36 -----
 .../org/apache/jena/fuseki/access/LibSec.java   | 149 -------------------
 .../org/apache/jena/fuseki/jetty/AuthMode.java  |  34 -----
 .../org/apache/jena/fuseki/jetty/JettyLib.java  |   9 +-
 .../apache/jena/fuseki/main/FusekiServer.java   |   6 +-
 .../org/apache/jena/rdfconnection/LibSec.java   | 148 ++++++++++++++++++
 8 files changed, 243 insertions(+), 226 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-arq/src/main/java/org/apache/jena/atlas/web/AuthScheme.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/web/AuthScheme.java b/jena-arq/src/main/java/org/apache/jena/atlas/web/AuthScheme.java
new file mode 100644
index 0000000..80edcd2
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/atlas/web/AuthScheme.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.jena.atlas.web;
+
+/** Authorization scheme */
+public enum AuthScheme { 
+    BASIC, 
+    DIGEST ;
+    
+    public static AuthScheme scheme(String name) {
+        if ( name == null )
+            return null;
+        name = name.toLowerCase();
+        switch(name) {
+            case "basic": return BASIC;
+            case "digest": return DIGEST;
+            default:
+                throw new IllegalArgumentException("no recognized as an authorization scheme:
"+name);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-arq/src/main/java/org/apache/jena/web/AuthSetup.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/web/AuthSetup.java b/jena-arq/src/main/java/org/apache/jena/web/AuthSetup.java
new file mode 100644
index 0000000..ffc58c1
--- /dev/null
+++ b/jena-arq/src/main/java/org/apache/jena/web/AuthSetup.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.jena.web;
+
+import org.apache.http.auth.AuthScope;
+
+/** Struct for the authentication information.
+ * See {@link AuthScope} for "ANY" constants.
+ */
+public class AuthSetup {
+    public final String host;
+    public final int port;
+    public final String user;
+    public final String password;
+    public final String realm;
+    
+    public AuthSetup(String host, Integer port, String user, String password, String realm)
{
+        this.host = any(host, AuthScope.ANY_HOST);
+        this.port = (port == null || port <= 0 ) ? AuthScope.ANY_PORT : port; 
+        this.user = user;
+        this.password = password;
+        this.realm = any(host, AuthScope.ANY_REALM);
+    }
+    
+    public AuthScope authScope() {
+        return new AuthScope(host, port, realm, AuthScope.ANY_SCHEME);
+    }
+    
+    private <X> X any(X value, X anyVal) {
+        if ( value == null )
+            return anyVal;
+        return value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AuthSetup.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AuthSetup.java
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AuthSetup.java
deleted file mode 100644
index e94ebff..0000000
--- a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/AuthSetup.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.apache.jena.fuseki.access;
-
-/** Struct for the authentication information */
-public class AuthSetup {
-    public final String host;
-    public final int port;
-    public final String user;
-    public final String password;
-    public final String realm;
-    
-    public AuthSetup(String host, int port, String user, String password, String realm) {
-        this.host = host;
-        this.port = port;
-        this.user = user;
-        this.password = password;
-        this.realm = realm;
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/LibSec.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/LibSec.java
b/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/LibSec.java
deleted file mode 100644
index 9afe8aa..0000000
--- a/jena-fuseki2/jena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/LibSec.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.apache.jena.fuseki.access;
-
-import java.util.function.Consumer;
-
-import org.apache.http.HttpHost;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.AuthCache;
-import org.apache.http.client.CredentialsProvider;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.protocol.HttpClientContext;
-import org.apache.http.impl.auth.BasicScheme;
-import org.apache.http.impl.auth.DigestScheme;
-import org.apache.http.impl.auth.RFC2617Scheme;
-import org.apache.http.impl.client.BasicAuthCache;
-import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.protocol.HttpContext;
-import org.apache.jena.atlas.lib.InternalErrorException;
-import org.apache.jena.fuseki.jetty.AuthMode;
-import org.apache.jena.rdfconnection.RDFConnection;
-import org.apache.jena.rdfconnection.RDFConnectionRemote;
-import org.apache.jena.riot.web.HttpOp;
-
-/** Library for client side use of access control. */ 
-public class LibSec {
-    // See also DataAccessLib (package lib)
-    
-    // [AuthScheme] default
-    public static AuthMode authMode = AuthMode.DIGEST;
-    
-    public static void withAuth(String urlStr, AuthSetup auth, Consumer<RDFConnection>
action) {
-        CredentialsProvider credsProvider = credsProvider(auth);
-        HttpHost target = new HttpHost(auth.host, auth.port, "http");
-        // --- AuthCache : not necessary
-        // Create AuthCache instance - necessary for non-repeatable request entity. (i.e.
streaming)
-        
-        // [AuthScheme]
-        AuthCache authCache = new BasicAuthCache();
-        if ( LibSec.authMode == AuthMode.BASIC ) {
-            RFC2617Scheme authScheme = authScheme(auth.realm);
-            // Can force the client to use basic first time by setting authCache.
-            // This does not work for digest because the nonce's will be wrong.
-            authCache.put(target, authScheme);
-        }
-        
-        HttpContext httpContext = httpContext(authCache, credsProvider);
-        HttpClient httpClient = httpClient(auth); 
-        
-        // Needs retryable mods to RDFConnectionRemote??
-        try ( RDFConnection conn = RDFConnectionRemote.create()
-                .destination(urlStr)
-                .httpClient(httpClient)
-                .httpContext(httpContext)
-                .build() ) {
-            action.accept(conn);
-        }
-    }
-
-    /** Create digest auth {@link DigestScheme} */
-    private static RFC2617Scheme authScheme(String realm) {
-        switch (authMode) {
-            case BASIC: return authBasicScheme(realm);
-            case DIGEST : return authDigestScheme(realm);
-            default:
-                throw new InternalErrorException("RFC2617 auth scheme not reocgnized: "+authMode);
-        }
-    }
-    
-    /** Create digest auth {@link DigestScheme} */
-    private static DigestScheme authDigestScheme(String realm) {
-        //Objects.requireNonNull(realm);
-        DigestScheme authScheme = new DigestScheme();
-        authScheme.overrideParamter("realm", realm);
-        authScheme.overrideParamter("nonce", "whatever");
-        return authScheme;
-    }
-
-    /** Create basic auth {@link BasicScheme} */
-    private static BasicScheme authBasicScheme(String realm) {
-        BasicScheme authScheme = new BasicScheme();
-        return authScheme;
-    }
-
-    /**
-     * Create an {@link HttpClient} with authentication as given by
-     * the {@link AuthSetup} for a particular host and port.
-     */
-    public static HttpClient httpClient(AuthSetup auth) {
-        // HttpClient with password.
-        CredentialsProvider credsProvider = credsProvider(auth);
-        HttpClient client = HttpOp.createPoolingHttpClientBuilder()
-            .setDefaultCredentialsProvider(credsProvider)
-            .build();
-        return client;
-    }
-    
-    /**
-     * Create an {@link HttpClient} with authentication by user/password
-     * a particular host and port.
-     */
-    public static HttpClient httpClient(String host, int port, String user, String password,
String realm) {
-        AuthSetup auth = new AuthSetup(host, port, user, password, realm);
-        return httpClient(auth);
-    }
-
-    public static HttpClientContext httpContext(AuthCache authCache, CredentialsProvider
provider) {
-        // Add AuthCache to the execution context
-        HttpClientContext localContext = HttpClientContext.create();
-        return httpContext(localContext, authCache, provider);
-    }
-
-    public static HttpClientContext httpContext(HttpClientContext localContext, AuthCache
authCache, CredentialsProvider provider) {
-        // Add AuthCache to the execution context
-        if ( authCache != null )
-            localContext.setAuthCache(authCache);
-        localContext.setCredentialsProvider(provider);
-        return localContext;
-    }
-
-    public static CredentialsProvider credsProvider(AuthSetup auth) {
-        return credsProvider(auth.host, auth.port, auth.user, auth.password);
-    }
-    
-    private static CredentialsProvider credsProvider(String host, int port, String user,
String password) {
-        CredentialsProvider credsProvider = new BasicCredentialsProvider();
-        credsProvider.setCredentials(
-            new AuthScope(host, port),
-            new UsernamePasswordCredentials(user, password));
-        return credsProvider;
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/AuthMode.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/AuthMode.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/AuthMode.java
deleted file mode 100644
index 47db5b2..0000000
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/AuthMode.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.apache.jena.fuseki.jetty;
-
-/** Authorization scheme */
-public enum AuthMode { BASIC, DIGEST ;
-    public static AuthMode scheme(String name) {
-        if ( name == null )
-            return null;
-        name = name.toLowerCase();
-        switch(name) {
-            case "basic": return BASIC;
-            case "digest": return DIGEST;
-            default:
-                throw new IllegalArgumentException("no recognized as an authorization scheme:
"+name);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/JettyLib.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/JettyLib.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/JettyLib.java
index 30d061a..f516e80 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/JettyLib.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/jetty/JettyLib.java
@@ -20,6 +20,7 @@ package org.apache.jena.fuseki.jetty;
 
 import java.util.Objects;
 
+import org.apache.jena.atlas.web.AuthScheme;
 import org.apache.jena.riot.WebContent;
 import org.eclipse.jetty.http.MimeTypes;
 import org.eclipse.jetty.security.*;
@@ -71,7 +72,7 @@ public class JettyLib {
       * See {@linkplain #addPathConstraint(ConstraintSecurityHandler, String)}
       * for adding the {@code pathspec} to apply it to.
       */
-      public static ConstraintSecurityHandler makeSecurityHandler(String realm, UserStore
userStore, AuthMode authMode) {
+      public static ConstraintSecurityHandler makeSecurityHandler(String realm, UserStore
userStore, AuthScheme authMode) {
           return makeSecurityHandler(realm, userStore, "**", authMode);
       }
 
@@ -79,13 +80,13 @@ public class JettyLib {
      * Digest requires an extra round trip so it is unfriendly to API
      * or scripts that stream.
      */
-     public static AuthMode dftAuthMode = AuthMode.DIGEST; 
+     public static AuthScheme dftAuthMode = AuthScheme.DIGEST; 
 
       /** Create a Jetty {@link SecurityHandler} for basic authentication. 
      * See {@linkplain #addPathConstraint(ConstraintSecurityHandler, String)}
      * for adding the {@code pathspec} to apply it to.
      */
-     public static ConstraintSecurityHandler makeSecurityHandler(String realm, UserStore
userStore, String role, AuthMode authMode) {
+     public static ConstraintSecurityHandler makeSecurityHandler(String realm, UserStore
userStore, String role, AuthScheme authMode) {
         // role can be "**" for any authenticated user.
         Objects.requireNonNull(userStore);
         Objects.requireNonNull(role);
@@ -101,7 +102,7 @@ public class JettyLib {
         loginService.setIdentityService(identService);
         
         securityHandler.setLoginService(loginService);
-        securityHandler.setAuthenticator( authMode == AuthMode.BASIC ? new BasicAuthenticator()
: new DigestAuthenticator() );
+        securityHandler.setAuthenticator( authMode == AuthScheme.BASIC ? new BasicAuthenticator()
: new DigestAuthenticator() );
         if ( realm != null )
             securityHandler.setRealmName(realm);
         return securityHandler;

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/FusekiServer.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/FusekiServer.java
b/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/FusekiServer.java
index ded17e6..d465ebe 100644
--- a/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/FusekiServer.java
+++ b/jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/FusekiServer.java
@@ -29,6 +29,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServlet;
 
 import org.apache.jena.atlas.lib.Pair;
+import org.apache.jena.atlas.web.AuthScheme;
 import org.apache.jena.fuseki.Fuseki;
 import org.apache.jena.fuseki.FusekiConfigException;
 import org.apache.jena.fuseki.FusekiException;
@@ -38,7 +39,6 @@ import org.apache.jena.fuseki.build.FusekiConfig;
 import org.apache.jena.fuseki.build.RequestAuthorization;
 import org.apache.jena.fuseki.ctl.ActionPing;
 import org.apache.jena.fuseki.ctl.ActionStats;
-import org.apache.jena.fuseki.jetty.AuthMode;
 import org.apache.jena.fuseki.jetty.FusekiErrorHandler1;
 import org.apache.jena.fuseki.jetty.JettyHttps;
 import org.apache.jena.fuseki.jetty.JettyLib;
@@ -238,7 +238,7 @@ public class FusekiServer {
         private RequestAuthorization     serverAllowedUsers = null;
         private String                   passwordFile       = null;
         private String                   realm              = null;
-        private AuthMode                 authMode           = null;
+        private AuthScheme                 authMode           = null;
         private String                   httpsKeystore        = null;
         private String                   httpsKeystorePasswd  = null;
         // Other servlets to add.
@@ -485,7 +485,7 @@ public class FusekiServer {
             realm(realm);
             
             String authStr = GraphUtils.getAsStringValue(server, FusekiVocab.pAuth);
-            authMode = AuthMode.scheme(authStr);
+            authMode = AuthScheme.scheme(authStr);
             
             serverAllowedUsers = FusekiBuilder.allowedUsers(server);
         }

http://git-wip-us.apache.org/repos/asf/jena/blob/8b73b7d9/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/LibSec.java
----------------------------------------------------------------------
diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/LibSec.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/LibSec.java
new file mode 100644
index 0000000..3a5d0b8
--- /dev/null
+++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/LibSec.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.jena.rdfconnection;
+
+import java.util.function.Consumer;
+
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.AuthCache;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.auth.DigestScheme;
+import org.apache.http.impl.auth.RFC2617Scheme;
+import org.apache.http.impl.client.BasicAuthCache;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.protocol.HttpContext;
+import org.apache.jena.atlas.lib.InternalErrorException;
+import org.apache.jena.atlas.web.AuthScheme;
+import org.apache.jena.riot.web.HttpOp;
+import org.apache.jena.web.AuthSetup;
+
+/** Library for client side use of access control. */ 
+public class LibSec {
+    // See also DataAccessLib (package lib)
+    
+    // [AuthScheme] default
+    public static AuthScheme authMode = AuthScheme.DIGEST;
+    
+    public static void withAuth(String urlStr, AuthSetup auth, Consumer<RDFConnection>
action) {
+        CredentialsProvider credsProvider = credsProvider(auth);
+        HttpHost target = new HttpHost(auth.host, auth.port, "http");
+        // --- AuthCache : not necessary
+        // Create AuthCache instance - necessary for non-repeatable request entity. (i.e.
streaming)
+        
+        // [AuthScheme]
+        AuthCache authCache = new BasicAuthCache();
+        if ( LibSec.authMode == AuthScheme.BASIC ) {
+            RFC2617Scheme authScheme = authScheme(auth.realm);
+            // Can force the client to use basic first time by setting authCache.
+            // This does not work for digest because the nonce's will be wrong.
+            authCache.put(target, authScheme);
+        }
+        
+        HttpContext httpContext = httpContext(authCache, credsProvider);
+        HttpClient httpClient = httpClient(auth); 
+        
+        // Needs retryable mods to RDFConnectionRemote??
+        try ( RDFConnection conn = RDFConnectionRemote.create()
+                .destination(urlStr)
+                .httpClient(httpClient)
+                .httpContext(httpContext)
+                .build() ) {
+            action.accept(conn);
+        }
+    }
+
+    /** Create digest auth {@link DigestScheme} */
+    private static RFC2617Scheme authScheme(String realm) {
+        switch (authMode) {
+            case BASIC: return authBasicScheme(realm);
+            case DIGEST : return authDigestScheme(realm);
+            default:
+                throw new InternalErrorException("RFC2617 auth scheme not reocgnized: "+authMode);
+        }
+    }
+    
+    /** Create digest auth {@link DigestScheme} */
+    private static DigestScheme authDigestScheme(String realm) {
+        //Objects.requireNonNull(realm);
+        DigestScheme authScheme = new DigestScheme();
+        authScheme.overrideParamter("realm", realm);
+        authScheme.overrideParamter("nonce", "whatever");
+        return authScheme;
+    }
+
+    /** Create basic auth {@link BasicScheme} */
+    private static BasicScheme authBasicScheme(String realm) {
+        BasicScheme authScheme = new BasicScheme();
+        return authScheme;
+    }
+
+    /**
+     * Create an {@link HttpClient} with authentication as given by
+     * the {@link AuthSetup} for a particular host and port.
+     */
+    public static HttpClient httpClient(AuthSetup auth) {
+        // HttpClient with password.
+        CredentialsProvider credsProvider = credsProvider(auth);
+        HttpClient client = HttpOp.createPoolingHttpClientBuilder()
+            .setDefaultCredentialsProvider(credsProvider)
+            .build();
+        return client;
+    }
+    
+    /**
+     * Create an {@link HttpClient} with authentication by user/password
+     * a particular host and port.
+     */
+    public static HttpClient httpClient(String host, int port, String user, String password,
String realm) {
+        AuthSetup auth = new AuthSetup(host, port, user, password, realm);
+        return httpClient(auth);
+    }
+
+    public static HttpClientContext httpContext(AuthCache authCache, CredentialsProvider
provider) {
+        // Add AuthCache to the execution context
+        HttpClientContext localContext = HttpClientContext.create();
+        return httpContext(localContext, authCache, provider);
+    }
+
+    public static HttpClientContext httpContext(HttpClientContext localContext, AuthCache
authCache, CredentialsProvider provider) {
+        // Add AuthCache to the execution context
+        if ( authCache != null )
+            localContext.setAuthCache(authCache);
+        localContext.setCredentialsProvider(provider);
+        return localContext;
+    }
+
+    public static CredentialsProvider credsProvider(AuthSetup auth) {
+        return credsProvider(auth.host, auth.port, auth.user, auth.password);
+    }
+    
+    private static CredentialsProvider credsProvider(String host, int port, String user,
String password) {
+        CredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials(
+            new AuthScope(host, port),
+            new UsernamePasswordCredentials(user, password));
+        return credsProvider;
+    }
+}


Mime
View raw message