KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > realm > providers > CertificateChainCallbackHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.geronimo.security.realm.providers;
18
19 import java.io.IOException JavaDoc;
20 import java.security.cert.X509Certificate JavaDoc;
21 import java.security.cert.Certificate JavaDoc;
22
23 import javax.security.auth.callback.Callback JavaDoc;
24 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * @version $Rev: 483636 $ $Date: 2006-12-07 15:03:43 -0500 (Thu, 07 Dec 2006) $
32  */

33 public class CertificateChainCallbackHandler implements CallbackHandler JavaDoc {
34     private static final Log log = LogFactory.getLog(CertificateChainCallbackHandler.class);
35     Certificate JavaDoc[] certificateChain;
36
37     public CertificateChainCallbackHandler(Certificate JavaDoc[] certificateChain) {
38         this.certificateChain = certificateChain;
39     }
40
41     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
42         for (int i = 0; i < callbacks.length; i++) {
43             Callback JavaDoc callback = callbacks[i];
44             if (callback instanceof CertificateChainCallback) {
45                 CertificateChainCallback cc = (CertificateChainCallback) callback;
46                 cc.setCertificateChain(certificateChain);
47             } else if (callback instanceof CertificateCallback) {
48                 if (certificateChain != null
49                         && certificateChain.length > 0
50                         && certificateChain[0] instanceof X509Certificate JavaDoc) {
51                     CertificateCallback cc = (CertificateCallback) callback;
52                     cc.setCertificate((X509Certificate JavaDoc) certificateChain[0]);
53                 } else {
54                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Invalid certificate chain: \n");
55                     if (certificateChain == null) {
56                         buf.append("certificate chain is null");
57                     } else {
58                         buf.append("certificate chain length: ").append(certificateChain.length).append("\n");
59                         if (certificateChain.length > 0) {
60                             buf.append("first certificate is a: ").append(certificateChain[0].getClass()).append("\n");
61                             buf.append("certificate is an X509Certificate: ").append(certificateChain[0] instanceof X509Certificate JavaDoc).append("\n");
62                         }
63                     }
64                     throw new UnsupportedCallbackException JavaDoc(callback, buf.toString());
65                 }
66             } else {
67                 throw new UnsupportedCallbackException JavaDoc(callback, "Wrong callback type: " + callback.getClass());
68             }
69         }
70     }
71
72 }
73
Popular Tags