Clover Coverage Report - Atlassian Trusted Apps(Aggregated)
Coverage timestamp: Tue Jun 9 2009 19:34:44 CDT
34   106   14   6.8
10   92   0.41   5
5     2.8  
1    
 
 
  ListApplicationRetriever       Line # 13 34 14 85.7% 0.85714287
 
  (16)
 
1    package com.atlassian.security.auth.trustedapps;
2   
3    import com.atlassian.security.auth.trustedapps.Transcoder.Base64Transcoder;
4   
5    import java.security.NoSuchAlgorithmException;
6    import java.security.NoSuchProviderException;
7    import java.security.PublicKey;
8    import java.security.spec.InvalidKeySpecException;
9    import java.util.ArrayList;
10    import java.util.Iterator;
11    import java.util.List;
12   
 
13    public class ListApplicationRetriever implements ApplicationRetriever
14    {
15    private final List /* <String> */values;
16    private final EncryptionProvider encryptionProvider;
17    private final Transcoder transcoder = new Base64Transcoder();
18   
 
19  16 toggle ListApplicationRetriever(EncryptionProvider encryptionProvider, List /* <String> */values)
20    {
21  16 Null.not("encryptionProvider", encryptionProvider);
22  16 Null.not("values", values);
23  16 int i = 0;
24  42 for (Iterator it = values.iterator(); it.hasNext();)
25    {
26  32 Null.not("value: " + i++, it.next());
27    }
28   
29  10 this.encryptionProvider = encryptionProvider;
30  10 this.values = new ArrayList /* <String> */(values);
31    }
32   
 
33  8 toggle public Application getApplication() throws RetrievalException
34    {
35  8 if (values.size() < 2)
36    {
37  2 throw new ApplicationNotFoundException("Application Certificate too small");
38    }
39  6 if (values.size() == 2)
40    {
41  3 return getApplicationProtocolV0();
42    }
43  3 return getApplicationProtocolV1();
44    }
45   
 
46  3 toggle private Application getApplicationProtocolV1() throws RetrievalException
47    {
48    // decorate the protocol zero version
49  3 Application result = getApplicationProtocolV0();
50    // with some validation of the certificate
51  3 String protocol = (String) values.get(2);
52  3 String magic = (String) values.get(3);
53  3 try
54    {
55  3 final Integer protocolVersion = isBlank(protocol) ? null : Integer.valueOf(protocol);
56  3 try
57    {
58  3 TrustedApplicationUtils.validateMagicNumber("application details", result.getID(), protocolVersion, magic);
59    }
60    catch (InvalidCertificateException e)
61    {
62  1 throw new InvalidApplicationDetailsException(e);
63    }
64    }
65    catch (NumberFormatException e)
66    {
67  0 throw new InvalidApplicationDetailsException(e);
68    }
69  2 return result;
70    }
71   
 
72  6 toggle private Application getApplicationProtocolV0() throws RetrievalException
73    {
74  6 try
75    {
76  6 String id = (String) values.get(0);
77  6 String keyStr = (String) values.get(1);
78   
79  6 if (keyStr == null)
80    {
81  0 throw new ApplicationNotFoundException("Public Key not found");
82    }
83   
84  6 final byte[] data = transcoder.decode(keyStr);
85  6 final PublicKey key = encryptionProvider.toPublicKey(data);
86  6 return new SimpleApplication(id, key);
87    }
88    catch (InvalidKeySpecException e)
89    {
90  0 throw new RuntimeException(e);
91    }
92    catch (NoSuchAlgorithmException e)
93    {
94  0 throw new RuntimeException(e);
95    }
96    catch (NoSuchProviderException e)
97    {
98  0 throw new RuntimeException(e);
99    }
100    }
101   
 
102  3 toggle private static boolean isBlank(String input)
103    {
104  3 return (input == null) || input.trim().length() == 0;
105    }
106    }