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