Skip to content

Commit

Permalink
Redesign of CredentialsProvider interface
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1687908 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ok2c committed Jun 27, 2015
1 parent d5c520a commit fb56b49
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 108 deletions.
22 changes: 11 additions & 11 deletions fluent-hc/src/main/java/org/apache/http/client/fluent/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.CredentialsStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
Expand Down Expand Up @@ -110,7 +110,7 @@ public static Executor newInstance(final HttpClient httpclient) {

private final HttpClient httpclient;
private volatile AuthCache authCache;
private volatile CredentialsProvider credentialsProvider;
private volatile CredentialsStore credentialsStore;
private volatile CookieStore cookieStore;

Executor(final HttpClient httpclient) {
Expand All @@ -122,16 +122,16 @@ public static Executor newInstance(final HttpClient httpclient) {
/**
* @since 4.5
*/
public Executor use(final CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
public Executor use(final CredentialsStore credentialsStore) {
this.credentialsStore = credentialsStore;
return this;
}

public Executor auth(final AuthScope authScope, final Credentials creds) {
if (this.credentialsProvider == null) {
this.credentialsProvider = new BasicCredentialsProvider();
if (this.credentialsStore == null) {
this.credentialsStore = new BasicCredentialsProvider();
}
this.credentialsProvider.setCredentials(authScope, creds);
this.credentialsStore.setCredentials(authScope, creds);
return this;
}

Expand Down Expand Up @@ -207,8 +207,8 @@ public Executor auth(final HttpHost host,
}

public Executor clearAuth() {
if (this.credentialsProvider != null) {
this.credentialsProvider.clear();
if (this.credentialsStore != null) {
this.credentialsStore.clear();
}
return this;
}
Expand Down Expand Up @@ -248,8 +248,8 @@ public Executor clearCookies() {
public Response execute(
final Request request) throws ClientProtocolException, IOException {
final HttpClientContext localContext = HttpClientContext.create();
if (this.credentialsProvider != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
if (this.credentialsStore != null) {
localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsStore);
}
if (this.authCache != null) {
localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.CredentialsStore;
import org.apache.http.osgi.services.ProxyConfiguration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/**
* @since 4.3
*/
final class OSGiCredentialsProvider implements CredentialsProvider {
final class OSGiCredentialsProvider implements CredentialsStore {

private final BundleContext bundleContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.CredentialsStore;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.util.Args;

/**
* {@link org.apache.http.client.CredentialsProvider} implementation that always returns
* {@link CredentialsStore} implementation that always returns
* {@link org.apache.http.impl.auth.win.CurrentWindowsCredentials} instance to NTLM
* and SPNego authentication challenges.
* <p>
Expand All @@ -44,11 +44,11 @@
* @since 4.4
*/
@ThreadSafe
public class WindowsCredentialsProvider implements CredentialsProvider {
public class WindowsCredentialsProvider implements CredentialsStore {

private final CredentialsProvider provider;
private final CredentialsStore provider;

public WindowsCredentialsProvider(final CredentialsProvider provider) {
public WindowsCredentialsProvider(final CredentialsStore provider) {
this.provider = Args.notNull(provider, "Credentials provider");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Locale;

import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

import java.io.IOException;

import com.sun.jna.platform.win32.Sspi.CtxtHandle;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinError;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
Expand All @@ -40,7 +36,7 @@
import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -59,6 +55,11 @@
import org.junit.Before;
import org.junit.Test;

import com.sun.jna.platform.win32.Sspi.CtxtHandle;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinError;

/**
* Unit tests for Windows negotiate authentication.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
Expand All @@ -43,7 +42,7 @@
public class ClientAuthentication {

public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
Expand All @@ -53,7 +52,7 @@ public class ClientPreemptiveBasicAuthentication {

public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
Expand All @@ -54,7 +53,7 @@ public class ClientPreemptiveDigestAuthentication {

public static void main(String[] args) throws Exception {
HttpHost target = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("username", "password"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -45,7 +44,7 @@
public class ClientProxyAuthentication {

public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("username", "password"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* ====================================================================
* 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.auth;

/**
* Provider of authentication credentials.
* <p>
* Implementations of this interface must be thread-safe. Access to shared
* data must be synchronized as methods of this interface may be executed
* from multiple threads.
*
* @since 4.0
*/
public interface CredentialsProvider {

/**
* Returns {@link Credentials credentials} for the given authentication scope,
* if available.
*
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*/
Credentials getCredentials(AuthScope authscope);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;

/**
* Abstract credentials provider that maintains a collection of user
* credentials.
* Abstract store of authentication credentials.
* <p>
* Implementations of this interface must be thread-safe. Access to shared
* data must be synchronized as methods of this interface may be executed
* from multiple threads.
*
* @since 4.0
*/
public interface CredentialsProvider {
public interface CredentialsStore extends CredentialsProvider {

/**
* Sets the {@link Credentials credentials} for the given authentication
Expand All @@ -53,16 +53,6 @@ public interface CredentialsProvider {
*/
void setCredentials(AuthScope authscope, Credentials credentials);

/**
* Get the {@link Credentials credentials} for the given authentication scope.
*
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*
* @see #setCredentials(AuthScope, Credentials)
*/
Credentials getCredentials(AuthScope authscope);

/**
* Clears all credentials.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup;
import org.apache.http.conn.routing.HttpRoute;
Expand Down Expand Up @@ -94,7 +94,7 @@ public class HttpClientContext extends HttpCoreContext {
public static final String COOKIE_STORE = "http.cookie-store";

/**
* Attribute name of a {@link org.apache.http.client.CredentialsProvider}
* Attribute name of a {@link CredentialsProvider}
* object that represents the actual credentials provider.
*/
public static final String CREDS_PROVIDER = "http.auth.credentials-provider";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.routing.RouteInfo;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.auth.MalformedChallengeException;
import org.apache.http.client.AuthCache;
import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.CredentialsStore;
import org.apache.http.util.Args;

/**
* Default implementation of {@link CredentialsProvider}.
* Default implementation of {@link CredentialsStore}.
*
* @since 4.0
*/
@ThreadSafe
public class BasicCredentialsProvider implements CredentialsProvider {
public class BasicCredentialsProvider implements CredentialsStore {

private final ConcurrentHashMap<AuthScope, Credentials> credMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.CredentialsProvider;
import org.apache.http.client.AuthenticationStrategy;
import org.apache.http.client.BackoffManager;
import org.apache.http.client.ConnectionBackoffStrategy;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
Expand Down
Loading

0 comments on commit fb56b49

Please sign in to comment.