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: import org.jboss.security.auth.callback.ByteArrayCallback;
32:
33: /** An implemeentation of the JAAS CallbackHandler interface that handles
34: NameCallbacks, PasswordCallbac, TextInputCallback and the JBoss
35: ByteArrayCallback
36:
37: @author Scott.Stark@jboss.org
38: @version $Revision: 57211 $
39: */
40: public class AppCallbackHandler implements CallbackHandler {
41: private String username;
42: private char[] password;
43: private byte[] data;
44: private String text;
45:
46: public AppCallbackHandler(String username, char[] password) {
47: this .username = username;
48: this .password = password;
49: }
50:
51: public AppCallbackHandler(String username, char[] password,
52: byte[] data) {
53: this .username = username;
54: this .password = password;
55: this .data = data;
56: }
57:
58: public AppCallbackHandler(String username, char[] password,
59: byte[] data, String text) {
60: this .username = username;
61: this .password = password;
62: this .data = data;
63: this .text = text;
64: }
65:
66: public void handle(Callback[] callbacks) throws IOException,
67: UnsupportedCallbackException {
68: for (int i = 0; i < callbacks.length; i++) {
69: Callback c = callbacks[i];
70: if (c instanceof NameCallback) {
71: NameCallback nc = (NameCallback) c;
72: nc.setName(username);
73: } else if (c instanceof PasswordCallback) {
74: PasswordCallback pc = (PasswordCallback) c;
75: pc.setPassword(password);
76: } else if (c instanceof TextInputCallback) {
77: TextInputCallback tc = (TextInputCallback) c;
78: tc.setText(text);
79: } else if (c instanceof ByteArrayCallback) {
80: ByteArrayCallback bac = (ByteArrayCallback) c;
81: bac.setByteArray(data);
82: } else {
83: throw new UnsupportedCallbackException(c,
84: "Unrecognized Callback");
85: }
86: }
87: }
88: }
|