Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StreamSubjectRepository |
|
| 1.25;1.25 |
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.repository.basic; | |
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 | ||
27 | ||
28 | /** | |
29 | * Abstract subject repository that reads attribute certificates from streams. | |
30 | * <p>The stream subject repository manages its own subject map and provides all the necessary | |
31 | * methods for concrete implementations.</p> | |
32 | * <p>Implementation classes are expected to populate the subject repository using either | |
33 | * {@link #addAttributeCertificate(InputStream)} or | |
34 | * {@link #addAttributeCertificate(AttributeCertificate)}.</p> | |
35 | * @since 0.3.0 | |
36 | */ | |
37 | public class StreamSubjectRepository | |
38 | extends AbstractSubjectRepository | |
39 | { | |
40 | ||
41 | //---- State | |
42 | ||
43 | /** | |
44 | * The internal map caching the information loaded from the attribute certificate files. | |
45 | * @since 0.3.0 | |
46 | */ | |
47 | private final Map<URI, InternalSubject> subjectMap; | |
48 | ||
49 | //---- Constructors | |
50 | ||
51 | /** | |
52 | * Creates an abstract subject repository and uses the specified certificate verifier | |
53 | * to validate the attribute certificates. | |
54 | * @param certificateVerifier the certificate verifier user to verify the the attribute | |
55 | * certificates, must not be {@code null}. | |
56 | * @since 0.3.0 | |
57 | */ | |
58 | public StreamSubjectRepository (CertificateVerifier certificateVerifier) { | |
59 | 3 | super(certificateVerifier); |
60 | 3 | this.subjectMap = new HashMap<URI, InternalSubject>(); |
61 | 3 | } |
62 | ||
63 | //---- Methods | |
64 | ||
65 | /** | |
66 | * Adds an attribute certificate to the subject repository. | |
67 | * <p>The input stream passed in is not closed.</p> | |
68 | * @param is the input stream containing the DER encoded attribute certificate, | |
69 | * must not be {@code null}. | |
70 | * @throws IOException if there is a read or decoding error. | |
71 | * @throws NoSuchProviderException passed on. | |
72 | * @throws NoSuchAlgorithmException passed on. | |
73 | * @see AttributeCertificate#AttributeCertificate(InputStream) | |
74 | * @see #updateSubjectMap(Map, AttributeCertificate) | |
75 | * @see #addAttributeCertificate(AttributeCertificate) | |
76 | * @since 0.3.0 | |
77 | */ | |
78 | protected void addAttributeCertificate ( | |
79 | InputStream is | |
80 | ) | |
81 | throws IOException, | |
82 | NoSuchAlgorithmException, | |
83 | NoSuchProviderException | |
84 | { | |
85 | 6 | addAttributeCertificate(new AttributeCertificate(is)); |
86 | 6 | } |
87 | ||
88 | /** | |
89 | * Adds an attribute certificate to the subject repository. | |
90 | * @param ac the attribute certificate to add, must not be {@code null}. | |
91 | * @throws NoSuchProviderException passed on. | |
92 | * @throws NoSuchAlgorithmException passed on. | |
93 | * @see #updateSubjectMap(Map, AttributeCertificate) | |
94 | * @since 0.3.0 | |
95 | */ | |
96 | protected void addAttributeCertificate ( | |
97 | AttributeCertificate ac | |
98 | ) | |
99 | throws NoSuchAlgorithmException, NoSuchProviderException | |
100 | { | |
101 | 6 | updateSubjectMap(this.subjectMap, ac); |
102 | 6 | } |
103 | ||
104 | //---- SubjectRepository | |
105 | ||
106 | /** | |
107 | * @since 0.3.0 | |
108 | */ | |
109 | public Subject retrieveSubject (URI identity) throws SubjectRepositoryException { | |
110 | 4 | final Subject subject = this.subjectMap.get(identity); |
111 | 4 | return subject != null ? subject : new InternalSubject(identity); |
112 | } | |
113 | ||
114 | } |