View Javadoc

1   package com.atlassian.sal.core.net;
2   
3   import com.atlassian.sal.api.user.UserManager;
4   import com.atlassian.sal.core.net.auth.TrustedTokenScheme;
5   import com.atlassian.sal.core.trusted.CertificateFactory;
6   
7   import org.apache.http.HttpHost;
8   import org.apache.http.auth.AuthScope;
9   import org.apache.http.auth.UsernamePasswordCredentials;
10  import org.apache.http.client.protocol.HttpClientContext;
11  import org.apache.http.impl.client.CloseableHttpClient;
12  
13  /**
14   * HttpClient implementation of Request interface
15   */
16  public class HttpClientTrustedRequest extends HttpClientRequest
17  {
18  
19      private final UserManager userManager;
20      private final CertificateFactory certificateFactory;
21  
22      public HttpClientTrustedRequest(final UserManager userManager, final CertificateFactory certificateFactory, final CloseableHttpClient httpClient, final HttpClientContext httpClientContext, final MethodType initialMethodType, final String initialUrl)
23      {
24          super(httpClient, httpClientContext, initialMethodType, initialUrl);
25          this.userManager = userManager;
26          this.certificateFactory = certificateFactory;
27      }
28  
29      public HttpClientTrustedRequest addTrustedTokenAuthentication(final String hostname)
30      {
31          return addTrustedTokenAuthentication(hostname, userManager.getRemoteUsername());
32      }
33  
34      public HttpClientTrustedRequest addTrustedTokenAuthentication(final String hostname, final String username)
35      {
36          httpClientContext.getCredentialsProvider().setCredentials(
37                  new AuthScope(hostname, AuthScope.ANY_PORT),
38                  new UsernamePasswordCredentials(username, null));
39          httpClientContext.getAuthCache().put(new HttpHost(hostname), new TrustedTokenScheme(certificateFactory));
40          return this;
41      }
42  }