01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test.util;
23:
24: import java.io.IOException;
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.TextInputCallback;
30: import javax.security.auth.callback.UnsupportedCallbackException;
31:
32: /** An implemeentation of the JAAS CallbackHandler interface that handles
33: NameCallbacks, PasswordCallbac, TextInputCallback and the JBoss
34: ByteArrayCallback
35:
36: @author Scott.Stark@jboss.org
37: @version $Revision: 57204 $
38: */
39: public class AppCallbackHandler implements CallbackHandler {
40: private String username;
41: private char[] password;
42: private byte[] data;
43: private String text;
44:
45: public AppCallbackHandler(String username, char[] password) {
46: this .username = username;
47: this .password = password;
48: }
49:
50: public AppCallbackHandler(String username, char[] password,
51: byte[] data) {
52: this .username = username;
53: this .password = password;
54: this .data = data;
55: }
56:
57: public AppCallbackHandler(String username, char[] password,
58: byte[] data, String text) {
59: this .username = username;
60: this .password = password;
61: this .data = data;
62: this .text = text;
63: }
64:
65: public void handle(Callback[] callbacks) throws IOException,
66: UnsupportedCallbackException {
67: for (int i = 0; i < callbacks.length; i++) {
68: Callback c = callbacks[i];
69: if (c instanceof NameCallback) {
70: NameCallback nc = (NameCallback) c;
71: nc.setName(username);
72: } else if (c instanceof PasswordCallback) {
73: PasswordCallback pc = (PasswordCallback) c;
74: pc.setPassword(password);
75: } else if (c instanceof TextInputCallback) {
76: TextInputCallback tc = (TextInputCallback) c;
77: tc.setText(text);
78: } else if (c instanceof ByteArrayCallback) {
79: ByteArrayCallback bac = (ByteArrayCallback) c;
80: bac.setByteArray(data);
81: } else {
82: throw new UnsupportedCallbackException(c,
83: "Unrecognized Callback");
84: }
85: }
86: }
87: }
|