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    import org.apache.directory.server.i18n.I18n;
032    
033    
034    /**
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 902319 $, $Date: 2010-01-23 01:17:50 +0100 (Sat, 23 Jan 2010) $
037     */
038    public class CallbackHandlerBean implements CallbackHandler
039    {
040        private String name;
041        private String password;
042    
043    
044        /**
045         * Creates a new instance of CallbackHandlerBean.
046         *
047         * @param name
048         * @param password
049         */
050        public CallbackHandlerBean( String name, String password )
051        {
052            this.name = name;
053            this.password = password;
054        }
055    
056    
057        public void handle( Callback[] callbacks ) throws UnsupportedCallbackException, IOException
058        {
059            for ( int ii = 0; ii < callbacks.length; ii++ )
060            {
061                Callback callBack = callbacks[ii];
062    
063                // Handles username callback.
064                if ( callBack instanceof NameCallback )
065                {
066                    NameCallback nameCallback = ( NameCallback ) callBack;
067                    nameCallback.setName( name );
068                    // Handles password callback.
069                }
070                else if ( callBack instanceof PasswordCallback )
071                {
072                    PasswordCallback passwordCallback = ( PasswordCallback ) callBack;
073                    passwordCallback.setPassword( password.toCharArray() );
074                }
075                else
076                {
077                    throw new UnsupportedCallbackException( callBack, I18n.err( I18n.ERR_617 ) );
078                }
079            }
080        }
081    }