0N/A/*
2362N/A * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.security.auth.callback;
0N/A
0N/A/**
0N/A * <p> Underlying security services instantiate and pass a
0N/A * <code>ConfirmationCallback</code> to the <code>handle</code>
0N/A * method of a <code>CallbackHandler</code> to ask for YES/NO,
0N/A * OK/CANCEL, YES/NO/CANCEL or other similar confirmations.
0N/A *
0N/A * @see javax.security.auth.callback.CallbackHandler
0N/A */
0N/Apublic class ConfirmationCallback implements Callback, java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = -9095656433782481624L;
0N/A
0N/A /**
0N/A * Unspecified option type.
0N/A *
0N/A * <p> The <code>getOptionType</code> method returns this
0N/A * value if this <code>ConfirmationCallback</code> was instantiated
0N/A * with <code>options</code> instead of an <code>optionType</code>.
0N/A */
0N/A public static final int UNSPECIFIED_OPTION = -1;
0N/A
0N/A /**
0N/A * YES/NO confirmation option.
0N/A *
0N/A * <p> An underlying security service specifies this as the
0N/A * <code>optionType</code> to a <code>ConfirmationCallback</code>
0N/A * constructor if it requires a confirmation which can be answered
0N/A * with either <code>YES</code> or <code>NO</code>.
0N/A */
0N/A public static final int YES_NO_OPTION = 0;
0N/A
0N/A /**
0N/A * YES/NO/CANCEL confirmation confirmation option.
0N/A *
0N/A * <p> An underlying security service specifies this as the
0N/A * <code>optionType</code> to a <code>ConfirmationCallback</code>
0N/A * constructor if it requires a confirmation which can be answered
0N/A * with either <code>YES</code>, <code>NO</code> or <code>CANCEL</code>.
0N/A */
0N/A public static final int YES_NO_CANCEL_OPTION = 1;
0N/A
0N/A /**
0N/A * OK/CANCEL confirmation confirmation option.
0N/A *
0N/A * <p> An underlying security service specifies this as the
0N/A * <code>optionType</code> to a <code>ConfirmationCallback</code>
0N/A * constructor if it requires a confirmation which can be answered
0N/A * with either <code>OK</code> or <code>CANCEL</code>.
0N/A */
0N/A public static final int OK_CANCEL_OPTION = 2;
0N/A
0N/A /**
0N/A * YES option.
0N/A *
0N/A * <p> If an <code>optionType</code> was specified to this
0N/A * <code>ConfirmationCallback</code>, this option may be specified as a
0N/A * <code>defaultOption</code> or returned as the selected index.
0N/A */
0N/A public static final int YES = 0;
0N/A
0N/A /**
0N/A * NO option.
0N/A *
0N/A * <p> If an <code>optionType</code> was specified to this
0N/A * <code>ConfirmationCallback</code>, this option may be specified as a
0N/A * <code>defaultOption</code> or returned as the selected index.
0N/A */
0N/A public static final int NO = 1;
0N/A
0N/A /**
0N/A * CANCEL option.
0N/A *
0N/A * <p> If an <code>optionType</code> was specified to this
0N/A * <code>ConfirmationCallback</code>, this option may be specified as a
0N/A * <code>defaultOption</code> or returned as the selected index.
0N/A */
0N/A public static final int CANCEL = 2;
0N/A
0N/A /**
0N/A * OK option.
0N/A *
0N/A * <p> If an <code>optionType</code> was specified to this
0N/A * <code>ConfirmationCallback</code>, this option may be specified as a
0N/A * <code>defaultOption</code> or returned as the selected index.
0N/A */
0N/A public static final int OK = 3;
0N/A
0N/A /** INFORMATION message type. */
0N/A public static final int INFORMATION = 0;
0N/A
0N/A /** WARNING message type. */
0N/A public static final int WARNING = 1;
0N/A
0N/A /** ERROR message type. */
0N/A public static final int ERROR = 2;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private String prompt;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private int messageType;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private int optionType = UNSPECIFIED_OPTION;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private int defaultOption;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private String[] options;
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private int selection;
0N/A
0N/A /**
0N/A * Construct a <code>ConfirmationCallback</code> with a
0N/A * message type, an option type and a default option.
0N/A *
0N/A * <p> Underlying security services use this constructor if
0N/A * they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL
0N/A * confirmation.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param messageType the message type (<code>INFORMATION</code>,
0N/A * <code>WARNING</code> or <code>ERROR</code>). <p>
0N/A *
0N/A * @param optionType the option type (<code>YES_NO_OPTION</code>,
0N/A * <code>YES_NO_CANCEL_OPTION</code> or
0N/A * <code>OK_CANCEL_OPTION</code>). <p>
0N/A *
0N/A * @param defaultOption the default option
0N/A * from the provided optionType (<code>YES</code>,
0N/A * <code>NO</code>, <code>CANCEL</code> or
0N/A * <code>OK</code>).
0N/A *
0N/A * @exception IllegalArgumentException if messageType is not either
0N/A * <code>INFORMATION</code>, <code>WARNING</code>,
0N/A * or <code>ERROR</code>, if optionType is not either
0N/A * <code>YES_NO_OPTION</code>,
0N/A * <code>YES_NO_CANCEL_OPTION</code>, or
0N/A * <code>OK_CANCEL_OPTION</code>,
0N/A * or if <code>defaultOption</code>
0N/A * does not correspond to one of the options in
0N/A * <code>optionType</code>.
0N/A */
0N/A public ConfirmationCallback(int messageType,
0N/A int optionType, int defaultOption) {
0N/A
0N/A if (messageType < INFORMATION || messageType > ERROR ||
0N/A optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)
0N/A throw new IllegalArgumentException();
0N/A
0N/A switch (optionType) {
0N/A case YES_NO_OPTION:
0N/A if (defaultOption != YES && defaultOption != NO)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A case YES_NO_CANCEL_OPTION:
0N/A if (defaultOption != YES && defaultOption != NO &&
0N/A defaultOption != CANCEL)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A case OK_CANCEL_OPTION:
0N/A if (defaultOption != OK && defaultOption != CANCEL)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A }
0N/A
0N/A this.messageType = messageType;
0N/A this.optionType = optionType;
0N/A this.defaultOption = defaultOption;
0N/A }
0N/A
0N/A /**
0N/A * Construct a <code>ConfirmationCallback</code> with a
0N/A * message type, a list of options and a default option.
0N/A *
0N/A * <p> Underlying security services use this constructor if
0N/A * they require a confirmation different from the available preset
0N/A * confirmations provided (for example, CONTINUE/ABORT or STOP/GO).
0N/A * The confirmation options are listed in the <code>options</code> array,
0N/A * and are displayed by the <code>CallbackHandler</code> implementation
0N/A * in a manner consistent with the way preset options are displayed.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param messageType the message type (<code>INFORMATION</code>,
0N/A * <code>WARNING</code> or <code>ERROR</code>). <p>
0N/A *
0N/A * @param options the list of confirmation options. <p>
0N/A *
0N/A * @param defaultOption the default option, represented as an index
0N/A * into the <code>options</code> array.
0N/A *
0N/A * @exception IllegalArgumentException if messageType is not either
0N/A * <code>INFORMATION</code>, <code>WARNING</code>,
0N/A * or <code>ERROR</code>, if <code>options</code> is null,
0N/A * if <code>options</code> has a length of 0,
0N/A * if any element from <code>options</code> is null,
0N/A * if any element from <code>options</code>
0N/A * has a length of 0, or if <code>defaultOption</code>
0N/A * does not lie within the array boundaries of
0N/A * <code>options</code>.
0N/A */
0N/A public ConfirmationCallback(int messageType,
0N/A String[] options, int defaultOption) {
0N/A
0N/A if (messageType < INFORMATION || messageType > ERROR ||
0N/A options == null || options.length == 0 ||
0N/A defaultOption < 0 || defaultOption >= options.length)
0N/A throw new IllegalArgumentException();
0N/A
0N/A for (int i = 0; i < options.length; i++) {
0N/A if (options[i] == null || options[i].length() == 0)
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A this.messageType = messageType;
0N/A this.options = options;
0N/A this.defaultOption = defaultOption;
0N/A }
0N/A
0N/A /**
0N/A * Construct a <code>ConfirmationCallback</code> with a prompt,
0N/A * message type, an option type and a default option.
0N/A *
0N/A * <p> Underlying security services use this constructor if
0N/A * they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL
0N/A * confirmation.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param prompt the prompt used to describe the list of options. <p>
0N/A *
0N/A * @param messageType the message type (<code>INFORMATION</code>,
0N/A * <code>WARNING</code> or <code>ERROR</code>). <p>
0N/A *
0N/A * @param optionType the option type (<code>YES_NO_OPTION</code>,
0N/A * <code>YES_NO_CANCEL_OPTION</code> or
0N/A * <code>OK_CANCEL_OPTION</code>). <p>
0N/A *
0N/A * @param defaultOption the default option
0N/A * from the provided optionType (<code>YES</code>,
0N/A * <code>NO</code>, <code>CANCEL</code> or
0N/A * <code>OK</code>).
0N/A *
0N/A * @exception IllegalArgumentException if <code>prompt</code> is null,
0N/A * if <code>prompt</code> has a length of 0,
0N/A * if messageType is not either
0N/A * <code>INFORMATION</code>, <code>WARNING</code>,
0N/A * or <code>ERROR</code>, if optionType is not either
0N/A * <code>YES_NO_OPTION</code>,
0N/A * <code>YES_NO_CANCEL_OPTION</code>, or
0N/A * <code>OK_CANCEL_OPTION</code>,
0N/A * or if <code>defaultOption</code>
0N/A * does not correspond to one of the options in
0N/A * <code>optionType</code>.
0N/A */
0N/A public ConfirmationCallback(String prompt, int messageType,
0N/A int optionType, int defaultOption) {
0N/A
0N/A if (prompt == null || prompt.length() == 0 ||
0N/A messageType < INFORMATION || messageType > ERROR ||
0N/A optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)
0N/A throw new IllegalArgumentException();
0N/A
0N/A switch (optionType) {
0N/A case YES_NO_OPTION:
0N/A if (defaultOption != YES && defaultOption != NO)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A case YES_NO_CANCEL_OPTION:
0N/A if (defaultOption != YES && defaultOption != NO &&
0N/A defaultOption != CANCEL)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A case OK_CANCEL_OPTION:
0N/A if (defaultOption != OK && defaultOption != CANCEL)
0N/A throw new IllegalArgumentException();
0N/A break;
0N/A }
0N/A
0N/A this.prompt = prompt;
0N/A this.messageType = messageType;
0N/A this.optionType = optionType;
0N/A this.defaultOption = defaultOption;
0N/A }
0N/A
0N/A /**
0N/A * Construct a <code>ConfirmationCallback</code> with a prompt,
0N/A * message type, a list of options and a default option.
0N/A *
0N/A * <p> Underlying security services use this constructor if
0N/A * they require a confirmation different from the available preset
0N/A * confirmations provided (for example, CONTINUE/ABORT or STOP/GO).
0N/A * The confirmation options are listed in the <code>options</code> array,
0N/A * and are displayed by the <code>CallbackHandler</code> implementation
0N/A * in a manner consistent with the way preset options are displayed.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param prompt the prompt used to describe the list of options. <p>
0N/A *
0N/A * @param messageType the message type (<code>INFORMATION</code>,
0N/A * <code>WARNING</code> or <code>ERROR</code>). <p>
0N/A *
0N/A * @param options the list of confirmation options. <p>
0N/A *
0N/A * @param defaultOption the default option, represented as an index
0N/A * into the <code>options</code> array.
0N/A *
0N/A * @exception IllegalArgumentException if <code>prompt</code> is null,
0N/A * if <code>prompt</code> has a length of 0,
0N/A * if messageType is not either
0N/A * <code>INFORMATION</code>, <code>WARNING</code>,
0N/A * or <code>ERROR</code>, if <code>options</code> is null,
0N/A * if <code>options</code> has a length of 0,
0N/A * if any element from <code>options</code> is null,
0N/A * if any element from <code>options</code>
0N/A * has a length of 0, or if <code>defaultOption</code>
0N/A * does not lie within the array boundaries of
0N/A * <code>options</code>.
0N/A */
0N/A public ConfirmationCallback(String prompt, int messageType,
0N/A String[] options, int defaultOption) {
0N/A
0N/A if (prompt == null || prompt.length() == 0 ||
0N/A messageType < INFORMATION || messageType > ERROR ||
0N/A options == null || options.length == 0 ||
0N/A defaultOption < 0 || defaultOption >= options.length)
0N/A throw new IllegalArgumentException();
0N/A
0N/A for (int i = 0; i < options.length; i++) {
0N/A if (options[i] == null || options[i].length() == 0)
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A this.prompt = prompt;
0N/A this.messageType = messageType;
0N/A this.options = options;
0N/A this.defaultOption = defaultOption;
0N/A }
0N/A
0N/A /**
0N/A * Get the prompt.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the prompt, or null if this <code>ConfirmationCallback</code>
0N/A * was instantiated without a <code>prompt</code>.
0N/A */
0N/A public String getPrompt() {
0N/A return prompt;
0N/A }
0N/A
0N/A /**
0N/A * Get the message type.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the message type (<code>INFORMATION</code>,
0N/A * <code>WARNING</code> or <code>ERROR</code>).
0N/A */
0N/A public int getMessageType() {
0N/A return messageType;
0N/A }
0N/A
0N/A /**
0N/A * Get the option type.
0N/A *
0N/A * <p> If this method returns <code>UNSPECIFIED_OPTION</code>, then this
0N/A * <code>ConfirmationCallback</code> was instantiated with
0N/A * <code>options</code> instead of an <code>optionType</code>.
0N/A * In this case, invoke the <code>getOptions</code> method
0N/A * to determine which confirmation options to display.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the option type (<code>YES_NO_OPTION</code>,
0N/A * <code>YES_NO_CANCEL_OPTION</code> or
0N/A * <code>OK_CANCEL_OPTION</code>), or
0N/A * <code>UNSPECIFIED_OPTION</code> if this
0N/A * <code>ConfirmationCallback</code> was instantiated with
0N/A * <code>options</code> instead of an <code>optionType</code>.
0N/A */
0N/A public int getOptionType() {
0N/A return optionType;
0N/A }
0N/A
0N/A /**
0N/A * Get the confirmation options.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the list of confirmation options, or null if this
0N/A * <code>ConfirmationCallback</code> was instantiated with
0N/A * an <code>optionType</code> instead of <code>options</code>.
0N/A */
0N/A public String[] getOptions() {
0N/A return options;
0N/A }
0N/A
0N/A /**
0N/A * Get the default option.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the default option, represented as
0N/A * <code>YES</code>, <code>NO</code>, <code>OK</code> or
0N/A * <code>CANCEL</code> if an <code>optionType</code>
0N/A * was specified to the constructor of this
0N/A * <code>ConfirmationCallback</code>.
0N/A * Otherwise, this method returns the default option as
0N/A * an index into the
0N/A * <code>options</code> array specified to the constructor
0N/A * of this <code>ConfirmationCallback</code>.
0N/A */
0N/A public int getDefaultOption() {
0N/A return defaultOption;
0N/A }
0N/A
0N/A /**
0N/A * Set the selected confirmation option.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param selection the selection represented as <code>YES</code>,
0N/A * <code>NO</code>, <code>OK</code> or <code>CANCEL</code>
0N/A * if an <code>optionType</code> was specified to the constructor
0N/A * of this <code>ConfirmationCallback</code>.
0N/A * Otherwise, the selection represents the index into the
0N/A * <code>options</code> array specified to the constructor
0N/A * of this <code>ConfirmationCallback</code>.
0N/A *
0N/A * @see #getSelectedIndex
0N/A */
0N/A public void setSelectedIndex(int selection) {
0N/A this.selection = selection;
0N/A }
0N/A
0N/A /**
0N/A * Get the selected confirmation option.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the selected confirmation option represented as
0N/A * <code>YES</code>, <code>NO</code>, <code>OK</code> or
0N/A * <code>CANCEL</code> if an <code>optionType</code>
0N/A * was specified to the constructor of this
0N/A * <code>ConfirmationCallback</code>.
0N/A * Otherwise, this method returns the selected confirmation
0N/A * option as an index into the
0N/A * <code>options</code> array specified to the constructor
0N/A * of this <code>ConfirmationCallback</code>.
0N/A *
0N/A * @see #setSelectedIndex
0N/A */
0N/A public int getSelectedIndex() {
0N/A return selection;
0N/A }
0N/A}