Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileSubjectRepository |
|
| 6.75;6.75 | ||||
FileSubjectRepository$1 |
|
| 6.75;6.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.repository.basic; | |
11 | ||
12 | import java.io.BufferedInputStream; | |
13 | import java.io.File; | |
14 | import java.io.FileFilter; | |
15 | import java.io.FileInputStream; | |
16 | import java.io.IOException; | |
17 | import java.io.InputStream; | |
18 | import java.security.NoSuchAlgorithmException; | |
19 | import java.security.NoSuchProviderException; | |
20 | ||
21 | import org.slf4j.Logger; | |
22 | import org.slf4j.LoggerFactory; | |
23 | ||
24 | import org.openpermis.cert.CertificateVerifier; | |
25 | import org.openpermis.repository.SubjectRepositoryException; | |
26 | ||
27 | ||
28 | /** | |
29 | * A subject repository containing certificates from a local directory. | |
30 | * <p>Reads all <tt>.ace</tt> files in the directory and its sub directories specified at | |
31 | * construction time.</p> | |
32 | * @since 0.1.0 | |
33 | * @since 0.3.0 Added support for sub directories. | |
34 | */ | |
35 | public class FileSubjectRepository | |
36 | extends StreamSubjectRepository | |
37 | { | |
38 | ||
39 | //---- Static | |
40 | ||
41 | /** | |
42 | * The logger object of this class. | |
43 | * @since 0.3.0 | |
44 | */ | |
45 | 0 | private static final Logger LOGGER = |
46 | LoggerFactory.getLogger(FileSubjectRepository.class); | |
47 | ||
48 | /** | |
49 | * Filter for <tt>.ace</tt> files and sub directories. | |
50 | * @since 0.3.0 | |
51 | */ | |
52 | 0 | private static final FileFilter ACE_FILE_FILTER = new FileFilter() { |
53 | 0 | public boolean accept (File file) { |
54 | 0 | if (file == null) { |
55 | 0 | return false; |
56 | } | |
57 | 0 | if (file.isDirectory()) { |
58 | 0 | return true; |
59 | } | |
60 | 0 | final String name = file.getName(); |
61 | 0 | return name != null && name.toLowerCase().endsWith(".ace"); |
62 | } | |
63 | }; | |
64 | ||
65 | //---- Constructors | |
66 | ||
67 | /** | |
68 | * Creates a file directory subject repository from directory and the specified certificate | |
69 | * verifier. | |
70 | * @param certificateVerifier the certificate verifier user to verify the the attribute | |
71 | * certificates, must not be {@code null}. | |
72 | * @param directories a list of directories to be searched for <tt>.ace</tt> files, | |
73 | * must not be {@code null}. | |
74 | * @throws SubjectRepositoryException if the repository can not be build up. | |
75 | * @since 0.1.0 | |
76 | * @since 0.3.0 Changed order of parameters and used variable length list of directories. | |
77 | */ | |
78 | public FileSubjectRepository (CertificateVerifier certificateVerifier, File... directories) | |
79 | throws SubjectRepositoryException | |
80 | { | |
81 | 0 | super(certificateVerifier); |
82 | 0 | for (File directory : directories) { |
83 | 0 | if (directory == null) { |
84 | 0 | throw new IllegalArgumentException("File directory must not be [null]."); |
85 | } | |
86 | 0 | if (!directory.isDirectory() || !directory.canRead()) { |
87 | 0 | throw new IllegalArgumentException( |
88 | "Can not read from directory [" + directory.getAbsolutePath() + "]." | |
89 | ); | |
90 | } | |
91 | try { | |
92 | 0 | process(directory); |
93 | 0 | } catch (NoSuchAlgorithmException e) { |
94 | 0 | throw new SubjectRepositoryException( |
95 | "Cannot decode attribute certificate because a " + | |
96 | "crypto algorithm is not available from the crypto provider.", | |
97 | e | |
98 | ); | |
99 | 0 | } catch (NoSuchProviderException e) { |
100 | 0 | throw new SubjectRepositoryException( |
101 | "Cannot decode attribute certificate because " + | |
102 | "there is no default crypto provider.", | |
103 | e | |
104 | ); | |
105 | 0 | } catch (IOException e) { |
106 | 0 | throw new SubjectRepositoryException( |
107 | "Cannot read attribute certificates from file.", | |
108 | e | |
109 | ); | |
110 | 0 | } |
111 | } | |
112 | 0 | } |
113 | ||
114 | //---- Methods | |
115 | ||
116 | /** | |
117 | * Adds an attribute certificate for the specified file. | |
118 | * @param file the file containing the DER encoded attribute certificate, | |
119 | * must not be {@code null}. | |
120 | * @throws NoSuchAlgorithmException passed on. | |
121 | * @throws NoSuchProviderException passed on. | |
122 | * @throws IOException if the file cannot be opened or | |
123 | * {@link #addAttributeCertificate(InputStream)} reports an error. | |
124 | * @see #addAttributeCertificate(InputStream) | |
125 | * @since 0.3.0 | |
126 | */ | |
127 | private void addAttributeCertificate ( | |
128 | File file | |
129 | ) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { | |
130 | 0 | final InputStream is = new BufferedInputStream(new FileInputStream(file)); |
131 | try { | |
132 | 0 | addAttributeCertificate(is); |
133 | } finally { | |
134 | 0 | try { |
135 | 0 | is.close(); |
136 | 0 | } catch (IOException e) { |
137 | 0 | LOGGER.warn("Failed to close input stream of file [" + file + "].", e); |
138 | 0 | } |
139 | 0 | } |
140 | 0 | } |
141 | ||
142 | /** | |
143 | * Loads all attribute certificates stored in the specified directory. | |
144 | * @param directory the directory to load the certificates from. | |
145 | * @throws IOException passed on. | |
146 | * @throws NoSuchProviderException passed on. | |
147 | * @throws NoSuchAlgorithmException passed on. | |
148 | * @see #addAttributeCertificate(File) | |
149 | * @since 0.3.0 | |
150 | */ | |
151 | private void process ( | |
152 | File directory | |
153 | ) | |
154 | throws NoSuchAlgorithmException, NoSuchProviderException, IOException | |
155 | { | |
156 | 0 | final File[] files = directory == null ? null : directory.listFiles(ACE_FILE_FILTER); |
157 | 0 | if (files == null) { |
158 | 0 | return; |
159 | } | |
160 | 0 | for (File file : files) { |
161 | 0 | if (file.isFile()) { |
162 | 0 | addAttributeCertificate(file); |
163 | } else { | |
164 | 0 | process(file); |
165 | } | |
166 | } | |
167 | 0 | } |
168 | ||
169 | } |