View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.jaas;
21  
22  
23  import java.io.IOException;
24  
25  import javax.security.auth.callback.Callback;
26  import javax.security.auth.callback.CallbackHandler;
27  import javax.security.auth.callback.NameCallback;
28  import javax.security.auth.callback.PasswordCallback;
29  import javax.security.auth.callback.UnsupportedCallbackException;
30  
31  
32  /**
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Di, 22 Mai 2007) $
35   */
36  public class CallbackHandlerBean implements CallbackHandler
37  {
38      private String name;
39      private String password;
40  
41  
42      /**
43       * Creates a new instance of CallbackHandlerBean.
44       *
45       * @param name
46       * @param password
47       */
48      public CallbackHandlerBean( String name, String password )
49      {
50          this.name = name;
51          this.password = password;
52      }
53  
54  
55      public void handle( Callback[] callbacks ) throws UnsupportedCallbackException, IOException
56      {
57          for ( int ii = 0; ii < callbacks.length; ii++ )
58          {
59              Callback callBack = callbacks[ii];
60  
61              // Handles username callback.
62              if ( callBack instanceof NameCallback )
63              {
64                  NameCallback nameCallback = ( NameCallback ) callBack;
65                  nameCallback.setName( name );
66                  // Handles password callback.
67              }
68              else if ( callBack instanceof PasswordCallback )
69              {
70                  PasswordCallback passwordCallback = ( PasswordCallback ) callBack;
71                  passwordCallback.setPassword( password.toCharArray() );
72              }
73              else
74              {
75                  throw new UnsupportedCallbackException( callBack, "Callback not supported" );
76              }
77          }
78      }
79  }