01: package org.obe.client.api.base;
02:
03: import javax.security.auth.callback.*;
04: import org.apache.commons.logging.Log;
05: import org.apache.commons.logging.LogFactory;
06: import java.io.IOException;
07:
08: /**
09: * The standard JAAS callback handler for OBE. Some application servers use
10: * non-standard callbacks which require a custom callback handler. For example,
11: * WebLogic Server passes a URLCallback to collect the server's URL.
12: *
13: * @author Adrian Price
14: */
15: public class OBECallbackHandler implements CallbackHandler {
16: private static final Log _logger = LogFactory
17: .getLog(OBECallbackHandler.class);
18: private static final String JAAS = "JAAS: ";
19: protected final String _url;
20: protected final String _principal;
21: protected final String _credentials;
22:
23: public OBECallbackHandler(String url, String principal,
24: String credentials) {
25:
26: _url = url;
27: _principal = principal;
28: _credentials = credentials;
29: }
30:
31: public final void handle(Callback[] callbacks) throws IOException,
32: UnsupportedCallbackException {
33:
34: for (int i = 0; i < callbacks.length; i++)
35: handle(callbacks[i]);
36: }
37:
38: /**
39: * Handles a specific callback. Subclasses can override this method in
40: * order to handle non-standard callback types. This class handles the
41: * standard callback types
42: * <code>javax.security.auth.callback.TextOutputCallback</code>,
43: * <code>javax.security.auth.callback.NameCallback</code>,
44: * <code>javax.security.auth.callback.PasswordCallback</code>.
45: *
46: * @param callback The callback to handle.
47: * @throws IOException If an I/O error occurs.
48: * @throws UnsupportedCallbackException If this CallbackHandler does not
49: * support this Callback type.
50: */
51: protected void handle(Callback callback) throws IOException,
52: UnsupportedCallbackException {
53: if (callback instanceof TextOutputCallback) {
54: // Display the message according to the specified type.
55: TextOutputCallback toc = (TextOutputCallback) callback;
56: switch (toc.getMessageType()) {
57: case TextOutputCallback.INFORMATION:
58: _logger.info(JAAS + toc.getMessage());
59: break;
60: case TextOutputCallback.ERROR:
61: _logger.error(JAAS + toc.getMessage());
62: break;
63: case TextOutputCallback.WARNING:
64: _logger.warn(JAAS + toc.getMessage());
65: break;
66: default:
67: throw new IOException("Unsupported message type: "
68: + toc.getMessageType());
69: }
70: } else if (callback instanceof NameCallback) {
71: ((NameCallback) callback).setName(_principal);
72: } else if (callback instanceof PasswordCallback) {
73: ((PasswordCallback) callback).setPassword(_credentials
74: .toCharArray());
75: } else {
76: throw new UnsupportedCallbackException(callback);
77: }
78: }
79: }
|