1 /*
2  * @(#) $Id: SampleCallbackHandler.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package client;
11
12import java.io.*;
13import javax.security.auth.callback.Callback;
14import javax.security.auth.callback.CallbackHandler;
15import javax.security.auth.callback.UnsupportedCallbackException;
16import javax.security.auth.callback.TextOutputCallback;
17import javax.security.auth.callback.PasswordCallback;
18import javax.security.auth.callback.TextInputCallback;
19import javax.security.auth.callback.NameCallback;
20import weblogic.security.auth.callback.URLCallback;
21
22/**
23 * SampleCallbackHandler.java
24 * Implementation of the CallbackHandler Interface
25 *
26 * @author Copyright (c) 2000-2002 by BEA Systems, Inc. All Rights Reserved.
27 */
28class SampleCallbackHandler implements CallbackHandler
29{
30  private String username = null;
31  private String password = null;
32  private String url = null;
33
34  public SampleCallbackHandler() { }
35
36  public SampleCallbackHandler(String pUsername, String pPassword, String pUrl)
37  {
38    username = pUsername;
39    password = pPassword;
40    url = pUrl;
41  }
42
43  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
44  {
45    for(int i = 0; i < callbacks.length; i++)
46    {
47      if(callbacks[i] instanceof TextOutputCallback)
48      {
49        // Display the message according to the specified type
50        TextOutputCallback toc = (TextOutputCallback)callbacks[i];
51        switch(toc.getMessageType())
52        {
53        case TextOutputCallback.INFORMATION:
54          System.out.println(toc.getMessage());
55          break;
56        case TextOutputCallback.ERROR:
57          System.out.println("ERROR: " + toc.getMessage());
58          break;
59        case TextOutputCallback.WARNING:
60          System.out.println("WARNING: " + toc.getMessage());
61          break;
62        default:
63          throw new IOException("Unsupported message type: " + toc.getMessageType());
64        }
65      }
66      else if(callbacks[i] instanceof NameCallback)
67      {
68        // If username not supplied on cmd line, prompt the user for the username.
69        NameCallback nc = (NameCallback)callbacks[i];
70        if (username == null) {
71          System.err.print(nc.getPrompt());
72          System.err.flush();
73          nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
74        }
75        else {
76          System.out.println("username: "+username);
77          nc.setName(username);
78        }
79      }
80      else if(callbacks[i] instanceof URLCallback)
81      {
82        // If url not supplied on cmd line, prompt the user for the url.
83        // This example requires the url.
84        URLCallback uc = (URLCallback)callbacks[i];
85        if (url == null) {
86          System.err.print(uc.getPrompt());
87          System.err.flush();
88          uc.setURL((new BufferedReader(new InputStreamReader(System.in))).readLine());
89        }
90        else {
91          System.out.println("URL: "+url);
92          uc.setURL(url);
93        }
94      }
95      else if(callbacks[i] instanceof PasswordCallback)
96      {
97        PasswordCallback pc = (PasswordCallback)callbacks[i];
98
99        // If password not supplied on cmd line, prompt the user for the password.
00        if (password == null) {
01          System.err.print(pc.getPrompt());
02          System.err.flush();
03
04          // Note: JAAS specifies that the password is a char[] rather than a String
05          String tmpPassword = (new BufferedReader(new InputStreamReader(System.in))).readLine();
06          int passLen = tmpPassword.length();
07          char[] passwordArray = new char[passLen];
08          for(int passIdx = 0; passIdx < passLen; passIdx++)
09            passwordArray[passIdx] = tmpPassword.charAt(passIdx);
10          pc.setPassword(passwordArray);
11        }
12        else {
13          String tPass = new String();
14          for(int p = 0; p < password.length(); p++)
15            tPass += "*";
16          System.out.println("password: "+tPass);
17          pc.setPassword(password.toCharArray());
18        }
19      }
20      else if(callbacks[i] instanceof TextInputCallback)
21      {
22        // Prompt the user for the username
23        TextInputCallback callback = (TextInputCallback)callbacks[i];
24        System.err.print(callback.getPrompt());
25        System.err.flush();
26        callback.setText((new BufferedReader(new InputStreamReader(System.in))).readLine());
27      }
28      else
29      {
30        throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
31      }
32    }
33  }
34}
35