1 package com.atlassian.security.auth.trustedapps;
2
3 import java.io.Closeable;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11
12
13
14
15 public class URLApplicationRetriever implements ApplicationRetriever
16 {
17 private final String baseUrl;
18 private final EncryptionProvider encryptionProvider;
19
20 public URLApplicationRetriever(String baseUrl, EncryptionProvider encryptionProvider)
21 {
22 Null.not("baseUrl", baseUrl);
23 Null.not("encryptionProvider", encryptionProvider);
24
25 this.baseUrl = baseUrl;
26 this.encryptionProvider = encryptionProvider;
27 }
28
29 public Application getApplication() throws RetrievalException
30 {
31 final String certUrl = baseUrl + TrustedApplicationUtils.Constant.CERTIFICATE_URL_PATH;
32 try
33 {
34 final URLConnection con = new URL(certUrl).openConnection();
35 con.connect();
36 final InputStream in = con.getInputStream();
37 try
38 {
39 final InputStreamReader reader = new InputStreamReader(in);
40 final ReaderApplicationRetriever retriever = new ReaderApplicationRetriever(reader, encryptionProvider);
41 return retriever.getApplication();
42 }
43 finally
44 {
45 closeQuietly(in);
46 }
47 }
48 catch (FileNotFoundException e)
49 {
50 throw new ApplicationNotFoundException(e);
51 }
52 catch (MalformedURLException e)
53 {
54 throw new RemoteSystemNotFoundException(e);
55 }
56 catch (IOException e)
57 {
58 throw new RemoteSystemNotFoundException(e);
59 }
60 }
61
62 private void closeQuietly(Closeable closeable)
63 {
64 if (closeable != null)
65 {
66 try
67 {
68 closeable.close();
69 }
70 catch (IOException e)
71 {
72
73 }
74 }
75 }
76 }