KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > auth > callback > LoginCallbackHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
22  * --------------------------------------------------------------------------
23  * $Id: LoginCallbackHandler.java,v 1.3 2004/05/25 15:13:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.auth.callback;
28
29 import java.io.IOException JavaDoc;
30 import java.io.PushbackInputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 import javax.security.auth.callback.CallbackHandler JavaDoc;
37 import javax.security.auth.callback.Callback JavaDoc;
38 import javax.security.auth.callback.NameCallback JavaDoc;
39 import javax.security.auth.callback.PasswordCallback JavaDoc;
40 import javax.security.auth.callback.TextOutputCallback JavaDoc;
41 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
42
43 /**
44  * Come from the JAAS authentication tutorial The application implements the
45  * CallbackHandler. <p>This application is text-based. Therefore it displays
46  * information to the user using the OutputStreams System.out and System.err,
47  * and gathers input from the user using the InputStream System.in.
48  * @author Sun Tutorial
49  */

50 public class LoginCallbackHandler implements CallbackHandler JavaDoc {
51
52     /**
53      * Size of the buffer
54      */

55     private static final int BUFFER_SIZE = 128;
56
57     /**
58      * Invoke an array of Callbacks.
59      * @param callbacks an array of <code>Callback</code> objects which
60      * contain the information requested by an underlying security
61      * service to be retrieved or displayed.
62      * @throws IOException if an input or output error occurs. <p>
63      * @throws UnsupportedCallbackException if the implementation of this method
64      * does not support one or more of the Callbacks specified in the
65      * <code>callbacks</code> parameter.
66      */

67     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
68
69         for (int i = 0; i < callbacks.length; i++) {
70             if (callbacks[i] instanceof TextOutputCallback JavaDoc) {
71
72                 // display the message according to the specified type
73
TextOutputCallback JavaDoc toc = (TextOutputCallback JavaDoc) callbacks[i];
74                 switch (toc.getMessageType()) {
75                     case TextOutputCallback.INFORMATION:
76                         System.out.println(toc.getMessage());
77                         break;
78                     case TextOutputCallback.ERROR:
79                         System.out.println("ERROR: " + toc.getMessage());
80                         break;
81                     case TextOutputCallback.WARNING:
82                         System.out.println("WARNING: " + toc.getMessage());
83                         break;
84                     default:
85                         throw new IOException JavaDoc("Unsupported message type: " + toc.getMessageType());
86                 }
87
88             } else if (callbacks[i] instanceof NameCallback JavaDoc) {
89
90                 // prompt the user for a username
91
NameCallback JavaDoc nc = (NameCallback JavaDoc) callbacks[i];
92
93                 System.err.print(nc.getPrompt());
94                 System.err.flush();
95                 nc.setName((new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in))).readLine());
96
97             } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
98
99                 // prompt the user for sensitive information
100
PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) callbacks[i];
101                 System.err.print(pc.getPrompt());
102                 System.err.flush();
103                 pc.setPassword(readPassword(System.in));
104             } else {
105                 throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
106             }
107         }
108     }
109
110     /**
111      * Reads user password from given input stream.
112      * @param in given input stream
113      * @return the password
114      * @throws IOException if it fails
115      */

116     private char[] readPassword(InputStream JavaDoc in) throws IOException JavaDoc {
117
118         char[] lineBuffer;
119         char[] buf;
120         int i;
121
122         lineBuffer = new char[BUFFER_SIZE];
123         buf = lineBuffer;
124
125         int room = buf.length;
126         int offset = 0;
127         int c;
128
129         loop: while (true) {
130             switch (c = in.read()) {
131                 case -1:
132                 case '\n':
133                     break loop;
134
135                 case '\r':
136                     int c2 = in.read();
137                     if ((c2 != '\n') && (c2 != -1)) {
138                         if (!(in instanceof PushbackInputStream JavaDoc)) {
139                             in = new PushbackInputStream JavaDoc(in);
140                         }
141                         ((PushbackInputStream JavaDoc) in).unread(c2);
142                     } else {
143                         break loop;
144                     }
145                 default:
146                     if (--room < 0) {
147                         buf = new char[offset + BUFFER_SIZE];
148                         room = buf.length - offset - 1;
149                         System.arraycopy(lineBuffer, 0, buf, 0, offset);
150                         Arrays.fill(lineBuffer, ' ');
151                         lineBuffer = buf;
152                     }
153                     buf[offset++] = (char) c;
154                     break;
155             }
156         }
157
158         if (offset == 0) {
159             return null;
160         }
161
162         char[] ret = new char[offset];
163         System.arraycopy(buf, 0, ret, 0, offset);
164         Arrays.fill(buf, ' ');
165
166         return ret;
167     }
168 }
Popular Tags