KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > ssl > UnifiedX509TrustManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.security.ssl;
24
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.security.cert.CertificateException JavaDoc;
28 import java.security.cert.X509Certificate JavaDoc;
29 import javax.net.ssl.X509TrustManager;
30
31 /**
32  * This class combines an array of X509TrustManagers into one.
33  * @author Shing Wai Chan
34  **/

35 public class UnifiedX509TrustManager implements X509TrustManager {
36     private X509TrustManager[] mgrs = null;
37     private X509Certificate JavaDoc[] issuers = {};
38
39     public UnifiedX509TrustManager(X509TrustManager[] mgrs) {
40         if (mgrs == null) {
41             throw new IllegalArgumentException JavaDoc("Null array of X509TrustManagers");
42         }
43         this.mgrs = mgrs;
44
45         HashSet JavaDoc tset = new HashSet JavaDoc(); //for uniqueness
46
for (int i = 0; i < mgrs.length; i++) {
47             X509Certificate JavaDoc[] tcerts = mgrs[i].getAcceptedIssuers();
48             if (tcerts != null && tcerts.length > 0) {
49                 for (int j = 0; j < tcerts.length; j++) {
50                     tset.add(tcerts[j]);
51                 }
52             }
53         }
54         issuers = new X509Certificate JavaDoc[tset.size()];
55         Iterator JavaDoc iter = tset.iterator();
56         for (int i = 0; iter.hasNext(); i++) {
57             issuers[i] = (X509Certificate JavaDoc)iter.next();
58         }
59     }
60
61     // ---------- implements X509TrustManager -----------
62
public void checkClientTrusted(X509Certificate JavaDoc[] chain, String JavaDoc authType)
63             throws CertificateException JavaDoc {
64         CertificateException JavaDoc cex = null;
65         for (int i = 0; i < mgrs.length; i++) {
66             try {
67                 cex = null; //reset exception status
68
mgrs[i].checkClientTrusted(chain, authType);
69                 break;
70             } catch(CertificateException JavaDoc ex) {
71                 cex = ex;
72             }
73         }
74         if (cex != null) {
75             throw cex;
76         }
77     }
78
79     public void checkServerTrusted(X509Certificate JavaDoc[] chain, String JavaDoc authType)
80             throws CertificateException JavaDoc {
81         CertificateException JavaDoc cex = null;
82         for (int i = 0; i < mgrs.length; i++) {
83             try {
84                 cex = null; //reset exception status
85
mgrs[i].checkServerTrusted(chain, authType);
86                 break;
87             } catch(CertificateException JavaDoc ex) {
88                 cex = ex;
89             }
90         }
91         if (cex != null) {
92             throw cex;
93         }
94     }
95
96     public X509Certificate JavaDoc[] getAcceptedIssuers() {
97         return issuers;
98     }
99 }
100
Popular Tags