1   package com.atlassian.security.auth.trustedapps;
2   
3   import org.apache.commons.httpclient.HttpClient;
4   import org.apache.commons.httpclient.HttpMethod;
5   import org.apache.commons.httpclient.methods.GetMethod;
6   import org.apache.commons.httpclient.params.HttpClientParams;
7   import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
8   
9   import java.io.Closeable;
10  import java.io.FileNotFoundException;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.net.MalformedURLException;
14  
15  /**
16   * Take a URL and produce an {@link Application}.
17   */
18  public class URLApplicationRetriever implements ApplicationRetriever
19  {
20      private final String baseUrl;
21      private final EncryptionProvider encryptionProvider;
22  
23      public URLApplicationRetriever(String baseUrl, EncryptionProvider encryptionProvider)
24      {
25          Null.not("baseUrl", baseUrl);
26          Null.not("encryptionProvider", encryptionProvider);
27  
28          this.baseUrl = baseUrl;
29          this.encryptionProvider = encryptionProvider;
30      }
31  
32      public Application getApplication() throws RetrievalException
33      {
34          try
35          {
36  
37  
38              final String certUrl = baseUrl + TrustedApplicationUtils.Constant.CERTIFICATE_URL_PATH;
39              final HttpMethod get = new GetMethod(certUrl);
40              get.setFollowRedirects(true);
41              final HttpClient client = new HttpClient();
42              configureHttpClient(client);
43              
44              int responseCode  = client.executeMethod(get);
45              if (responseCode >= 300)
46              {
47                  throw new ApplicationNotFoundException("Invalid response code of " + responseCode + " returned from: " + certUrl);
48              }
49              final InputStream in = get.getResponseBodyAsStream();
50              try {
51                  return new InputStreamApplicationRetriever(in, encryptionProvider).getApplication();
52              }
53              finally
54              {
55                  closeQuietly(in);
56              }
57          }
58          catch (FileNotFoundException e)
59          {
60              throw new ApplicationNotFoundException(e);
61          }
62          catch (MalformedURLException e)
63          {
64              throw new RemoteSystemNotFoundException(e);
65          }
66          catch (IOException e)
67          {
68              throw new RemoteSystemNotFoundException(e);
69          }
70      }
71  
72      private void configureHttpClient(HttpClient client)
73      {
74          //The default time to wait without retrieving data from the remote connection
75          final int socketTimeout = Integer.parseInt(System.getProperty("http.socketTimeout", "10000"));
76          //The default time allowed for establishing a connection
77          final int connectionTimeout = Integer.parseInt(System.getProperty("http.connectionTimeout", "10000"));
78  
79          final HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
80          params.setConnectionTimeout(connectionTimeout);
81          params.setSoTimeout(socketTimeout);
82      }
83  
84      private void closeQuietly(Closeable closeable)
85      {
86          if (closeable != null)
87          {
88              try
89              {
90                  closeable.close();
91              }
92              catch (IOException e)
93              {
94                  // ignore
95              }
96          }
97      }
98  }