Home » geronimo-2.2-source-release » org.apache.geronimo.security.realm.providers » [javadoc | source]

    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;
   20   import java.security.cert.X509Certificate;
   21   import java.security.cert.Certificate;
   22   
   23   import javax.security.auth.callback.Callback;
   24   import javax.security.auth.callback.UnsupportedCallbackException;
   25   import javax.security.auth.callback.CallbackHandler;
   26   
   27   import org.slf4j.Logger;
   28   import org.slf4j.LoggerFactory;
   29   
   30   /**
   31    * @version $Rev: 653740 $ $Date: 2008-05-06 03:44:18 -0700 (Tue, 06 May 2008) $
   32    */
   33   public class CertificateChainCallbackHandler implements CallbackHandler {
   34       private static final Logger log = LoggerFactory.getLogger(CertificateChainCallbackHandler.class);
   35       Certificate[] certificateChain;
   36   
   37       public CertificateChainCallbackHandler(Certificate[] certificateChain) {
   38           this.certificateChain = certificateChain;
   39       }
   40   
   41       public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   42           for (int i = 0; i < callbacks.length; i++) {
   43               Callback 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) {
   51                       CertificateCallback cc = (CertificateCallback) callback;
   52                       cc.setCertificate((X509Certificate) certificateChain[0]);
   53                   } else {
   54                       StringBuffer buf = new StringBuffer("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).append("\n");
   62                           }
   63                       }
   64                       throw new UnsupportedCallbackException(callback, buf.toString());
   65                   }
   66               } else {
   67                   throw new UnsupportedCallbackException(callback, "Wrong callback type: " + callback.getClass());
   68               }
   69           }
   70       }
   71   
   72   }

Home » geronimo-2.2-source-release » org.apache.geronimo.security.realm.providers » [javadoc | source]