Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelloWorldSubjectRepository |
|
| 3.75;3.75 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.examples.ejb.server; | |
11 | ||
12 | ||
13 | import java.io.IOException; | |
14 | import java.io.InputStream; | |
15 | import java.net.URI; | |
16 | import java.security.NoSuchAlgorithmException; | |
17 | import java.security.NoSuchProviderException; | |
18 | import java.util.HashMap; | |
19 | import java.util.Map; | |
20 | ||
21 | import org.openpermis.Subject; | |
22 | import org.openpermis.basic.InternalSubject; | |
23 | import org.openpermis.cert.AttributeCertificate; | |
24 | import org.openpermis.cert.CertificateVerifier; | |
25 | import org.openpermis.repository.SubjectRepositoryException; | |
26 | import org.openpermis.repository.basic.AbstractSubjectRepository; | |
27 | ||
28 | ||
29 | /** | |
30 | * A subject repository implementation that reads certificates from the classpath. | |
31 | * @since 0.3.0 | |
32 | */ | |
33 | public class HelloWorldSubjectRepository | |
34 | extends AbstractSubjectRepository | |
35 | { | |
36 | ||
37 | //---- State | |
38 | ||
39 | /** | |
40 | * The internal map caching the information loaded from the attribute certificate files. | |
41 | * @since 0.3.0 | |
42 | */ | |
43 | private final Map<URI, InternalSubject> subjectMap; | |
44 | ||
45 | //---- Constructors | |
46 | ||
47 | /** | |
48 | * Creates a subject repository from a list of classpath entries. | |
49 | * <p>The public key of the subject in the SoA certificate is considered to be authentic. | |
50 | * It is the callers duty to ensure that it really is authentic. Further, by specifiying | |
51 | * the SoA certificate, its subject is trusted.</p> | |
52 | * @param certificateVerifier The certificate verifier used to verify the attribute | |
53 | * certificates. Must not be {@code null}. | |
54 | * @param entries the classpath entries pointing to certificates on the classpath (relative | |
55 | * to the repository class) to be read and added. | |
56 | * @throws SubjectRepositoryException if the repository can not be build up. | |
57 | * @since 0.3.0 | |
58 | */ | |
59 | public HelloWorldSubjectRepository (CertificateVerifier certificateVerifier, String... entries) | |
60 | throws SubjectRepositoryException | |
61 | { | |
62 | 0 | super(certificateVerifier); |
63 | 0 | this.subjectMap = new HashMap<URI, InternalSubject>(); |
64 | try { | |
65 | 0 | process(entries); |
66 | 0 | } catch (NoSuchAlgorithmException e) { |
67 | 0 | throw new SubjectRepositoryException( |
68 | "Cannot decode attribute certificate(s) because a " + | |
69 | "crypto algorithm is not available from the crypto provider(s).", | |
70 | e | |
71 | ); | |
72 | 0 | } catch (NoSuchProviderException e) { |
73 | 0 | throw new SubjectRepositoryException( |
74 | "Cannot decode attribute certificate(s) because a " + | |
75 | "there is no default crypto provider.", | |
76 | e | |
77 | ); | |
78 | 0 | } catch (IOException e) { |
79 | 0 | throw new SubjectRepositoryException( |
80 | "Read error while processing attribute certificate(s).", | |
81 | e | |
82 | ); | |
83 | 0 | } |
84 | 0 | } |
85 | ||
86 | //---- Methods | |
87 | ||
88 | /** | |
89 | * @since 0.3.0 | |
90 | */ | |
91 | private InputStream getInputStream (String entry) { | |
92 | 0 | InputStream is = getClass().getResourceAsStream(entry); |
93 | 0 | if (is == null) { |
94 | 0 | is = Thread.currentThread().getContextClassLoader().getResourceAsStream(entry); |
95 | } | |
96 | 0 | return is; |
97 | } | |
98 | ||
99 | /** | |
100 | * @since 0.3.0 | |
101 | */ | |
102 | private void process (String... entries) | |
103 | throws NoSuchAlgorithmException, NoSuchProviderException, IOException | |
104 | { | |
105 | 0 | if (entries == null) { |
106 | 0 | return; |
107 | } | |
108 | 0 | for (String entry : entries) { |
109 | 0 | final InputStream is = getInputStream(entry); |
110 | 0 | if (is != null) { |
111 | try { | |
112 | 0 | updateSubjectMap(this.subjectMap, new AttributeCertificate(is)); |
113 | } finally { | |
114 | 0 | is.close(); |
115 | 0 | } |
116 | } | |
117 | } | |
118 | 0 | } |
119 | ||
120 | //---- SubjectRepository | |
121 | ||
122 | /** | |
123 | * @since 0.3.0 | |
124 | */ | |
125 | public Subject retrieveSubject (URI identity) throws SubjectRepositoryException { | |
126 | 0 | return this.subjectMap.get(identity); |
127 | } | |
128 | ||
129 | } |