View Javadoc

1   package com.atlassian.sal.core.net;
2   
3   import com.atlassian.sal.api.net.NonMarshallingRequestFactory;
4   import com.atlassian.sal.api.net.Request.MethodType;
5   import com.atlassian.sal.api.user.UserManager;
6   import com.atlassian.sal.core.trusted.CertificateFactory;
7   
8   import org.apache.commons.httpclient.HttpClient;
9   import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  /**
14   * Does NOT support json/xml oject marshalling. Use the atlassian-rest implementation of
15   * {@link com.atlassian.sal.api.net.RequestFactory} instead.
16   */
17  public class HttpClientRequestFactory implements NonMarshallingRequestFactory<HttpClientRequest>
18  {
19      private static final Logger log = LoggerFactory.getLogger(HttpClientRequestFactory.class);
20  
21      private final CertificateFactory certificateFactory;
22      private final UserManager userManager;
23  
24      public HttpClientRequestFactory(final CertificateFactory certificateFactory, final UserManager userManager)
25      {
26          this.certificateFactory = certificateFactory;
27          this.userManager = userManager;
28      }
29  
30      /**
31       * The default time to wait without retrieving data from the remote connection
32       */
33      public static final int DEFAULT_SOCKET_TIMEOUT=Integer.parseInt(System.getProperty("http.socketTimeout", "10000"));
34  
35      /**
36       * The default time allowed for establishing a connection
37       */
38      public static final int DEFAULT_CONNECTION_TIMEOUT=Integer.parseInt(System.getProperty("http.connectionTimeout", "10000"));
39  
40      /* (non-Javadoc)
41       * @see com.atlassian.sal.api.net.RequestFactory#createMethod(com.atlassian.sal.api.net.Request.MethodType, java.lang.String)
42       */
43      public HttpClientRequest createRequest(final MethodType methodType, final String url)
44      {
45          final HttpClient httpClient = getHttpClient(url);
46          return new HttpClientRequest(httpClient, methodType, url, certificateFactory, userManager);
47      }
48  
49      /**
50       * @param url The URL
51       * @return The HTTP client
52       */
53      protected HttpClient getHttpClient(final String url)
54      {
55          final HttpClient httpClient = new HttpClient();
56          configureProtocol(httpClient, url);
57          configureProxy(httpClient, url);
58          configureConnectionParameters(httpClient);
59          return httpClient;
60      }
61  
62      /**
63       * Applies a set of parameters to a client
64       *
65       * @param httpClient the client to which parameters are applied
66       */
67      protected void configureConnectionParameters(final HttpClient httpClient)
68      {
69          final HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
70          params.setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
71          params.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
72      }
73  
74      /**
75       * @param client The client to configure the proxy of
76       * @param remoteUrl The remote URL
77       */
78      protected void configureProxy(final HttpClient client, final String remoteUrl)
79      {
80          new HttpClientProxyConfig().configureProxy(client, remoteUrl);
81      }
82  
83      protected void configureProtocol(final HttpClient httpClient, final String remoteUrl)
84      {
85          HttpClientProtocolConfig.configureProtocol(httpClient, remoteUrl);
86      }
87  
88      public boolean supportsHeader()
89      {
90      	return true;
91      }
92  }