View Javadoc

1   package com.atlassian.sal.core.net.auth;
2   
3   import org.apache.commons.httpclient.HttpClient;
4   import org.apache.commons.httpclient.HttpMethod;
5   import org.apache.commons.httpclient.URIException;
6   import org.apache.commons.httpclient.UsernamePasswordCredentials;
7   import org.apache.commons.httpclient.auth.AuthScope;
8   import org.apache.log4j.Logger;
9   
10  public class BaseAuthenticator implements HttpClientAuthenticator
11  {
12  	private static final Logger log = Logger.getLogger(BaseAuthenticator.class);
13  	private final String username;
14  	private final String password;
15  	
16  	public BaseAuthenticator(String username, String password)
17  	{
18  		this.username = username;
19  		this.password = password;
20  	}
21  
22  	public void process(HttpClient httpClient, HttpMethod method)
23  	{
24          try
25          {
26          	AuthScope authScope = new AuthScope(method.getURI().getHost(), AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
27          	httpClient.getParams().setAuthenticationPreemptive(true);
28  			httpClient.getState().setCredentials(authScope, new UsernamePasswordCredentials(username, password));
29          }
30          catch (URIException e)
31          {
32              log.error("Unable to parse URI to set credentials: " + e, e);
33          }
34      }
35  }