KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > auth > login > X509CertificateCredential


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
24 package com.sun.enterprise.security.auth.login;
25
26 import java.security.cert.X509Certificate JavaDoc;
27
28 /**
29  * This class holds the user certificate for the certificate realm and the
30  * realm name. This credential is added as a public credential to the
31  * JAAS subject.
32  */

33
34 public class X509CertificateCredential {
35     private X509Certificate JavaDoc[] certChain;
36     private String JavaDoc realm;
37     private String JavaDoc alias;
38
39     /**
40      * Construct a credential with the specified X509Certificate certificate
41      * chain, realm name and alias.
42      * @param the X509Certificate.
43      * @param the alias for the certificate
44      * @param the realm name. The only value supported for now is "certificate".
45      */

46
47     public X509CertificateCredential(X509Certificate JavaDoc[] certChain,
48                     String JavaDoc alias, String JavaDoc realm)
49     {
50     this.certChain = certChain;
51     this.alias = alias;
52     this.realm = realm;
53     }
54
55     /**
56      * Return the alias for the certificate.
57      * @return the alias.
58      */

59     public String JavaDoc getAlias() {
60     return alias;
61     }
62
63     /**
64      * Return the realm name.
65      * @return the realm name. Only value supported for now is "certificate".
66      */

67     public String JavaDoc getRealm() {
68     return realm;
69     }
70
71     /**
72      * Return the chain of certificates.
73      * @return the chain of X509Certificates.
74      */

75     public X509Certificate JavaDoc[] getX509CertificateChain() {
76     return certChain;
77     }
78
79     /**
80      * Compare two instances of the credential and return true if they are
81      * the same and false otherwise.
82      * @return true if the instances are equal, false otherwise.
83      */

84     public boolean equals(Object JavaDoc o) {
85     if(o instanceof X509CertificateCredential) {
86         X509CertificateCredential pc = (X509CertificateCredential) o;
87         if(pc.getRealm().equals(realm) && pc.getAlias().equals(alias)) {
88         X509Certificate JavaDoc[] certs = pc.getX509CertificateChain();
89         for(int i = 0; i < certs.length; i++) {
90             if(!certs[i].equals(certChain[i])) {
91             return false;
92             }
93         }
94         return true;
95         }
96     }
97     return false;
98     }
99
100     /**
101      * Return the hashCode computed from the certificate, realm and alias.
102      * @return the hash code.
103      */

104     public int hashCode() {
105     return certChain.hashCode() + realm.hashCode() + alias.hashCode();
106     }
107
108     /**
109      * String representation of the credential.
110      */

111     public String JavaDoc toString() {
112     String JavaDoc s = "Realm=" + realm;
113     s = s + " alias=" + alias;
114     s = s + " X509Certificate=" + certChain;
115     return s;
116     }
117
118 }
119
Popular Tags