001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.activemq.jaas;
019
020import java.io.IOException;
021import java.security.cert.X509Certificate;
022
023import javax.security.auth.callback.Callback;
024import javax.security.auth.callback.CallbackHandler;
025import javax.security.auth.callback.UnsupportedCallbackException;
026
027/**
028 * A Standard JAAS callback handler for SSL certificate requests. Will only
029 * handle callbacks of type CertificateCallback.
030 *
031 * @author sepandm@gmail.com (Sepand)
032 */
033public class JaasCertificateCallbackHandler implements CallbackHandler {
034    final X509Certificate certificates[];
035
036    /**
037     * Basic constructor.
038     *
039     * @param certs The certificate returned when calling back.
040     */
041    public JaasCertificateCallbackHandler(X509Certificate certs[]) {
042        certificates = certs;
043    }
044
045    /**
046     * Overriding handle method to handle certificates.
047     *
048     * @param callbacks The callbacks requested.
049     * @throws IOException
050     * @throws UnsupportedCallbackException Thrown if an unkown Callback type is
051     *                 encountered.
052     */
053    @Override
054    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
055        for (int i = 0; i < callbacks.length; i++) {
056            Callback callback = callbacks[i];
057            if (callback instanceof CertificateCallback) {
058                CertificateCallback certCallback = (CertificateCallback)callback;
059
060                certCallback.setCertificates(certificates);
061
062            } else {
063                throw new UnsupportedCallbackException(callback);
064            }
065        }
066    }
067}