[java-opensaml COMMIT] /trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataR...

noreply at shibboleth.net noreply at shibboleth.net
Fri Oct 11 16:29:10 EDT 2013


Author: putmanb
Date: Fri Oct 11 16:29:10 2013
New Revision: 3470

URL: http://svn.shibboleth.net/view/java-opensaml?rev=3470&view=rev
Log:
Fix basic auth usage consistent with HttpClient 4.x.  Use client context to set a local credentials provider, rather than mutating the global settings in HttpClient instance.

Modified:
    trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolver.java

Modified: trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolver.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolver.java?rev=3470&r1=3469&r2=3470&view=diff
==============================================================================
--- trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolver.java (original)
+++ trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/HTTPMetadataResolver.java Fri Oct 11 16:29:10 2013
@@ -35,7 +35,8 @@
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.AbstractHttpClient;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -71,6 +72,12 @@
     /** The Last-Modified information provided when the currently cached metadata was fetched. */
     private String cachedMetadataLastModified;
     
+    /** Username and password credential used for HTTP BASIC authentication. */
+    private UsernamePasswordCredentials usernamePasswordCredentials;
+    
+    /** HttpClient AuthScope instance to use. */
+    private AuthScope authScope;
+    
     /**
      * Constructor.
      * 
@@ -124,31 +131,19 @@
      * 
      * @param username the username
      * @param password the password
-     * @param authScope the HTTP client auth scope with which to scope the credentials, may be null
-     */
-    public void setBasicCredentials(String username, String password, AuthScope authScope) {
+     * @param scope the HTTP client auth scope with which to scope the credentials, may be null
+     */
+    public void setBasicCredentials(String username, String password, AuthScope scope) {
         ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
         ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
 
-        // TODO This approach from client v3 is problematic in client v4,
-        // due to casting below. Also issue with AuthScope collisions if client is used
-        // by multiple components.
-        // May need to rethink how authN support is handled.
-        // Maybe just require setting creds on the HttpClient that is passed in.
-        UsernamePasswordCredentials creds = null;
         if (username != null && password != null) {
-            creds = new UsernamePasswordCredentials(username, password);
-        }
-        
-        AuthScope scope = authScope;
-        if (scope == null) {
-            scope = new AuthScope(metadataURI.getHost(), metadataURI.getPort());
-        }
-        
-        if (httpClient instanceof AbstractHttpClient) {
-            ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(scope, creds);
-        } else {
-            log.warn("Client is not an instance of AbstractHttpClient, can not set HTTP basic auth credentials");
+            usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
+            if (scope == null) {
+                authScope = new AuthScope(metadataURI.getHost(), metadataURI.getPort());
+            } else {
+                authScope = scope;
+            }
         }
         
     }
@@ -156,6 +151,8 @@
     /** {@inheritDoc} */
     protected void doDestroy() {
         httpClient = null;
+        usernamePasswordCredentials = null;
+        authScope = null;
         metadataURI = null;
         cachedMetadataETag = null;
         cachedMetadataLastModified = null;
@@ -178,10 +175,11 @@
      */
     protected byte[] fetchMetadata() throws ResolverException {
         HttpGet httpGet = buildHttpGet();
+        HttpClientContext context = buildHttpClientContext();
 
         try {
             log.debug("Attempting to fetch metadata document from '{}'", metadataURI);
-            HttpResponse response = httpClient.execute(httpGet);
+            HttpResponse response = httpClient.execute(httpGet, context);
             int httpStatusCode = response.getStatusLine().getStatusCode();
 
             if (httpStatusCode == HttpStatus.SC_NOT_MODIFIED) {
@@ -236,6 +234,22 @@
 
         return getMethod;
     }
+    
+    /**
+     * Build the {@link HttpClientContext} instance which will be used to invoke the 
+     * {@link HttpClient} request.
+     * 
+     * @return a new instance of {@link HttpClientContext}

[... 14 lines stripped ...]


More information about the commits mailing list