KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > storage > implementations > CertsInFilesystemDirectoryResolver


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.keys.storage.implementations;
19
20
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.security.cert.CertificateException JavaDoc;
27 import java.security.cert.CertificateExpiredException JavaDoc;
28 import java.security.cert.CertificateFactory JavaDoc;
29 import java.security.cert.CertificateNotYetValidException JavaDoc;
30 import java.security.cert.X509Certificate JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverException;
36 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverSpi;
37 import com.sun.org.apache.xml.internal.security.utils.Base64;
38
39
40 /**
41  * This {@link StorageResolverSpi} makes all raw (binary) {@link X509Certificate}s
42  * which reside as files in a single directory available to the {@link com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver}.
43  *
44  * @author $Author: raul $
45  */

46 public class CertsInFilesystemDirectoryResolver extends StorageResolverSpi {
47
48    /** {@link java.util.logging} logging facility */
49     static java.util.logging.Logger JavaDoc log =
50         java.util.logging.Logger.getLogger(
51                     CertsInFilesystemDirectoryResolver.class.getName());
52
53    /** Field _merlinsCertificatesDir */
54    String JavaDoc _merlinsCertificatesDir = null;
55
56    /** Field _certs */
57    private List JavaDoc _certs = new ArrayList JavaDoc();
58
59    /** Field _iterator */
60    Iterator JavaDoc _iterator = null;
61
62    /**
63     *
64     *
65     * @param directoryName
66     * @throws StorageResolverException
67     */

68    public CertsInFilesystemDirectoryResolver(String JavaDoc directoryName)
69            throws StorageResolverException {
70
71       this._merlinsCertificatesDir = directoryName;
72
73       this.readCertsFromHarddrive();
74
75       this._iterator = new FilesystemIterator(this._certs);
76    }
77
78    /**
79     * Method readCertsFromHarddrive
80     *
81     * @throws StorageResolverException
82     */

83    private void readCertsFromHarddrive() throws StorageResolverException {
84
85       File JavaDoc certDir = new File JavaDoc(this._merlinsCertificatesDir);
86       ArrayList JavaDoc al = new ArrayList JavaDoc();
87       String JavaDoc[] names = certDir.list();
88
89       for (int i = 0; i < names.length; i++) {
90          String JavaDoc currentFileName = names[i];
91
92          if (currentFileName.endsWith(".crt")) {
93             al.add(names[i]);
94          }
95       }
96
97       CertificateFactory JavaDoc cf = null;
98
99       try {
100          cf = CertificateFactory.getInstance("X.509");
101       } catch (CertificateException JavaDoc ex) {
102          throw new StorageResolverException("empty", ex);
103       }
104
105       if (cf == null) {
106          throw new StorageResolverException("empty");
107       }
108
109       for (int i = 0; i < al.size(); i++) {
110          String JavaDoc filename = certDir.getAbsolutePath() + File.separator
111                            + (String JavaDoc) al.get(i);
112          File JavaDoc file = new File JavaDoc(filename);
113          boolean added = false;
114          String JavaDoc dn = null;
115
116          try {
117             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
118             X509Certificate JavaDoc cert =
119                (X509Certificate JavaDoc) cf.generateCertificate(fis);
120
121             fis.close();
122
123             //add to ArrayList
124
cert.checkValidity();
125             this._certs.add(cert);
126
127             dn = cert.getSubjectDN().getName();
128             added = true;
129          } catch (FileNotFoundException JavaDoc ex) {
130             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex);
131          } catch (IOException JavaDoc ex) {
132             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex);
133          } catch (CertificateNotYetValidException JavaDoc ex) {
134             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex);
135          } catch (CertificateExpiredException JavaDoc ex) {
136             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex);
137          } catch (CertificateException JavaDoc ex) {
138             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex);
139          }
140
141          if (added) {
142             if (true)
143                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Added certificate: " + dn);
144          }
145       }
146    }
147
148    /** @inheritDoc */
149    public Iterator JavaDoc getIterator() {
150       return this._iterator;
151    }
152
153    /**
154     * Class FilesystemIterator
155     *
156     * @author $Author: raul $
157     * @version $Revision: 1.9 $
158     */

159    class FilesystemIterator implements Iterator JavaDoc {
160
161       /** Field _certs */
162       List JavaDoc _certs = null;
163
164       /** Field _i */
165       int _i;
166
167       /**
168        * Constructor FilesystemIterator
169        *
170        * @param certs
171        */

172       public FilesystemIterator(List JavaDoc certs) {
173          this._certs = certs;
174          this._i = 0;
175       }
176
177       /** @inheritDoc */
178       public boolean hasNext() {
179          return (this._i < this._certs.size());
180       }
181
182       /** @inheritDoc */
183       public Object JavaDoc next() {
184          return this._certs.get(this._i++);
185       }
186
187       /**
188        * Method remove
189        *
190        */

191       public void remove() {
192          throw new UnsupportedOperationException JavaDoc(
193             "Can't remove keys from KeyStore");
194       }
195    }
196
197    /**
198     * Method main
199     *
200     * @param unused
201     * @throws Exception
202     */

203    public static void main(String JavaDoc unused[]) throws Exception JavaDoc {
204
205       CertsInFilesystemDirectoryResolver krs =
206          new CertsInFilesystemDirectoryResolver(
207             "data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/certs");
208
209       for (Iterator JavaDoc i = krs.getIterator(); i.hasNext(); ) {
210          X509Certificate JavaDoc cert = (X509Certificate JavaDoc) i.next();
211          byte[] ski =
212             com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI
213                .getSKIBytesFromCert(cert);
214
215          System.out.println();
216          System.out.println("Base64(SKI())= \""
217                             + Base64.encode(ski) + "\"");
218          System.out.println("cert.getSerialNumber()= \""
219                             + cert.getSerialNumber().toString() + "\"");
220          System.out.println("cert.getSubjectDN().getName()= \""
221                             + cert.getSubjectDN().getName() + "\"");
222          System.out.println("cert.getIssuerDN().getName()= \""
223                             + cert.getIssuerDN().getName() + "\"");
224       }
225    }
226 }
227
Popular Tags