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.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  public class BaseAuthenticator implements HttpClientAuthenticator
12  {
13      private static final Logger log = LoggerFactory.getLogger(BaseAuthenticator.class);
14      private final String username;
15      private final String password;
16  
17      public BaseAuthenticator(String username, String password)
18      {
19          this.username = username;
20          this.password = password;
21      }
22  
23      public void process(HttpClient httpClient, HttpMethod method)
24      {
25          try
26          {
27              AuthScope authScope = new AuthScope(method.getURI().getHost(), AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
28              httpClient.getParams().setAuthenticationPreemptive(true);
29              httpClient.getState().setCredentials(authScope, new UsernamePasswordCredentials(username, password));
30          }
31          catch (URIException e)
32          {
33              log.error("Unable to parse URI to set credentials: " + e, e);
34          }
35      }
36  }