KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > test > SimpleCallbackHandler


1 package hero.client.test;
2
3 /*
4 *
5 * NodeTests.java -
6 * Copyright (C) 2002 Ecoo Team
7 * valdes@loria.fr
8 *
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */

24
25 import javax.security.auth.callback.Callback JavaDoc;
26 import javax.security.auth.callback.CallbackHandler JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
30
31
32 public class SimpleCallbackHandler implements CallbackHandler JavaDoc {
33     private String JavaDoc username;
34     private char[] password;
35     
36     public SimpleCallbackHandler(String JavaDoc username, char[] password)
37     {
38     this.username = username;
39     this.password = password;
40     }
41     
42     public void handle(Callback JavaDoc[] callbacks) throws
43     java.io.IOException JavaDoc, UnsupportedCallbackException JavaDoc
44     {
45     for (int i = 0; i < callbacks.length; i++)
46             {
47                 if (callbacks[i] instanceof NameCallback JavaDoc)
48             {
49             NameCallback JavaDoc nc = (NameCallback JavaDoc)callbacks[i];
50             nc.setName(username);
51             }
52                 else if (callbacks[i] instanceof PasswordCallback JavaDoc)
53             {
54             PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc)callbacks[i];
55             pc.setPassword(password);
56             }
57               /* else
58             {
59             throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
60             }*/

61             }
62     }
63 }
64
Popular Tags