001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.kerberos.shared.jaas;
021    
022    
023    import java.io.IOException;
024    
025    import javax.security.auth.callback.Callback;
026    import javax.security.auth.callback.CallbackHandler;
027    import javax.security.auth.callback.NameCallback;
028    import javax.security.auth.callback.PasswordCallback;
029    import javax.security.auth.callback.UnsupportedCallbackException;
030    
031    
032    /**
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 540371 $, $Date: 2007-05-22 03:00:43 +0300 (Tue, 22 May 2007) $
035     */
036    public class CallbackHandlerBean implements CallbackHandler
037    {
038        private String name;
039        private String password;
040    
041    
042        /**
043         * Creates a new instance of CallbackHandlerBean.
044         *
045         * @param name
046         * @param password
047         */
048        public CallbackHandlerBean( String name, String password )
049        {
050            this.name = name;
051            this.password = password;
052        }
053    
054    
055        public void handle( Callback[] callbacks ) throws UnsupportedCallbackException, IOException
056        {
057            for ( int ii = 0; ii < callbacks.length; ii++ )
058            {
059                Callback callBack = callbacks[ii];
060    
061                // Handles username callback.
062                if ( callBack instanceof NameCallback )
063                {
064                    NameCallback nameCallback = ( NameCallback ) callBack;
065                    nameCallback.setName( name );
066                    // Handles password callback.
067                }
068                else if ( callBack instanceof PasswordCallback )
069                {
070                    PasswordCallback passwordCallback = ( PasswordCallback ) callBack;
071                    passwordCallback.setPassword( password.toCharArray() );
072                }
073                else
074                {
075                    throw new UnsupportedCallbackException( callBack, "Callback not supported" );
076                }
077            }
078        }
079    }