FileDocCategorySizeDatePackage
DialogCallbackHandler.javaAPI DocJava SE 6 API8556Tue Jun 10 00:23:22 BST 2008com.sun.security.auth.callback

DialogCallbackHandler

public class DialogCallbackHandler extends Object implements CallbackHandler

Uses a Swing dialog window to query the user for answers to authentication questions. This can be used by a JAAS application to instantiate a CallbackHandler

see
javax.security.auth.callback

Fields Summary
private Component
parentComponent
private static final int
JPasswordFieldLen
private static final int
JTextFieldLen
Constructors Summary
public DialogCallbackHandler()
Creates a callback dialog with the default parent window.


    /* -- Methods -- */

                  
       
public DialogCallbackHandler(Component parentComponent)
Creates a callback dialog and specify the parent window.

param
parentComponent the parent window -- specify null for the default parent

	this.parentComponent = parentComponent;
    
Methods Summary
public voidhandle(javax.security.auth.callback.Callback[] callbacks)
Handles the specified set of callbacks.

param
callbacks the callbacks to handle
throws
UnsupportedCallbackException if the callback is not an instance of NameCallback or PasswordCallback

	/* Collect messages to display in the dialog */
	final List messages = new ArrayList(3);

	/* Collection actions to perform if the user clicks OK */
	final List okActions = new ArrayList(2);

	ConfirmationInfo confirmation = new ConfirmationInfo();

	for (int i = 0; i < callbacks.length; i++) {
	    if (callbacks[i] instanceof TextOutputCallback) {
		TextOutputCallback tc = (TextOutputCallback) callbacks[i];

		switch (tc.getMessageType()) {
		case TextOutputCallback.INFORMATION:
		    confirmation.messageType = JOptionPane.INFORMATION_MESSAGE;
		    break;
		case TextOutputCallback.WARNING:
		    confirmation.messageType = JOptionPane.WARNING_MESSAGE;
		    break;
		case TextOutputCallback.ERROR:
		    confirmation.messageType = JOptionPane.ERROR_MESSAGE;
		    break;
		default:
		    throw new UnsupportedCallbackException(
			callbacks[i], "Unrecognized message type");
		}

		messages.add(tc.getMessage());

	    } else if (callbacks[i] instanceof NameCallback) {
 		final NameCallback nc = (NameCallback) callbacks[i];

		JLabel prompt = new JLabel(nc.getPrompt());

		final JTextField name = new JTextField(JTextFieldLen);
		String defaultName = nc.getDefaultName();
		if (defaultName != null) {
		    name.setText(defaultName);
		}
		
		/*
		 * Put the prompt and name in a horizontal box, 
		 * and add that to the set of messages.
		 */
		Box namePanel = Box.createHorizontalBox();
		namePanel.add(prompt);
		namePanel.add(name);
		messages.add(namePanel);

		/* Store the name back into the callback if OK */
		okActions.add(new Action() {
		    public void perform() {
			nc.setName(name.getText());
		    }
		});

 	    } else if (callbacks[i] instanceof PasswordCallback) {
 		final PasswordCallback pc = (PasswordCallback) callbacks[i];

		JLabel prompt = new JLabel(pc.getPrompt());

		final JPasswordField password =
					new JPasswordField(JPasswordFieldLen);
		if (!pc.isEchoOn()) {
		    password.setEchoChar('*");
		}

		Box passwordPanel = Box.createHorizontalBox();
		passwordPanel.add(prompt);
		passwordPanel.add(password);
		messages.add(passwordPanel);
  
		okActions.add(new Action() {
		    public void perform() {
			pc.setPassword(password.getPassword());
		    }
		});

	    } else if (callbacks[i] instanceof ConfirmationCallback) {
		ConfirmationCallback cc = (ConfirmationCallback)callbacks[i];

		confirmation.setCallback(cc);
		if (cc.getPrompt() != null) {
		    messages.add(cc.getPrompt());
		}

 	    } else {
 		throw new UnsupportedCallbackException(
		    callbacks[i], "Unrecognized Callback");
 	    }
	}

	/* Display the dialog */
	int result = JOptionPane.showOptionDialog(
	    parentComponent,
	    messages.toArray(),
	    "Confirmation",			/* title */
	    confirmation.optionType,
	    confirmation.messageType,
	    null,				/* icon */
	    confirmation.options,		/* options */
	    confirmation.initialValue);		/* initialValue */

	/* Perform the OK actions */
	if (result == JOptionPane.OK_OPTION
	    || result == JOptionPane.YES_OPTION)
	{
	    Iterator iterator = okActions.iterator();
	    while (iterator.hasNext()) {
		((Action) iterator.next()).perform();
	    }
	}
	confirmation.handleResult(result);