Path: blob/master/src/java.base/share/classes/javax/security/auth/callback/ConfirmationCallback.java
41161 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.security.auth.callback;2627/**28* <p> Underlying security services instantiate and pass a29* {@code ConfirmationCallback} to the {@code handle}30* method of a {@code CallbackHandler} to ask for YES/NO,31* OK/CANCEL, YES/NO/CANCEL or other similar confirmations.32*33* @since 1.434* @see javax.security.auth.callback.CallbackHandler35*/36public class ConfirmationCallback implements Callback, java.io.Serializable {3738@java.io.Serial39private static final long serialVersionUID = -9095656433782481624L;4041/**42* Unspecified option type.43*44* <p> The {@code getOptionType} method returns this45* value if this {@code ConfirmationCallback} was instantiated46* with {@code options} instead of an {@code optionType}.47*/48public static final int UNSPECIFIED_OPTION = -1;4950/**51* YES/NO confirmation option.52*53* <p> An underlying security service specifies this as the54* {@code optionType} to a {@code ConfirmationCallback}55* constructor if it requires a confirmation which can be answered56* with either {@code YES} or {@code NO}.57*/58public static final int YES_NO_OPTION = 0;5960/**61* YES/NO/CANCEL confirmation option.62*63* <p> An underlying security service specifies this as the64* {@code optionType} to a {@code ConfirmationCallback}65* constructor if it requires a confirmation which can be answered66* with either {@code YES}, {@code NO} or {@code CANCEL}.67*/68public static final int YES_NO_CANCEL_OPTION = 1;6970/**71* OK/CANCEL confirmation option.72*73* <p> An underlying security service specifies this as the74* {@code optionType} to a {@code ConfirmationCallback}75* constructor if it requires a confirmation which can be answered76* with either {@code OK} or {@code CANCEL}.77*/78public static final int OK_CANCEL_OPTION = 2;7980/**81* YES option.82*83* <p> If an {@code optionType} was specified to this84* {@code ConfirmationCallback}, this option may be specified as a85* {@code defaultOption} or returned as the selected index.86*/87public static final int YES = 0;8889/**90* NO option.91*92* <p> If an {@code optionType} was specified to this93* {@code ConfirmationCallback}, this option may be specified as a94* {@code defaultOption} or returned as the selected index.95*/96public static final int NO = 1;9798/**99* CANCEL option.100*101* <p> If an {@code optionType} was specified to this102* {@code ConfirmationCallback}, this option may be specified as a103* {@code defaultOption} or returned as the selected index.104*/105public static final int CANCEL = 2;106107/**108* OK option.109*110* <p> If an {@code optionType} was specified to this111* {@code ConfirmationCallback}, this option may be specified as a112* {@code defaultOption} or returned as the selected index.113*/114public static final int OK = 3;115116/** INFORMATION message type. */117public static final int INFORMATION = 0;118119/** WARNING message type. */120public static final int WARNING = 1;121122/** ERROR message type. */123public static final int ERROR = 2;124125/**126* @serial127* @since 1.4128*/129private final String prompt;130/**131* @serial132* @since 1.4133*/134private final int messageType;135/**136* @serial137* @since 1.4138*/139private final int optionType;140/**141* @serial142* @since 1.4143*/144private final int defaultOption;145/**146* @serial147* @since 1.4148*/149private final String[] options;150/**151* @serial152* @since 1.4153*/154private int selection;155156/**157* Construct a {@code ConfirmationCallback} with a158* message type, an option type and a default option.159*160* <p> Underlying security services use this constructor if161* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL162* confirmation.163*164* @param messageType the message type ({@code INFORMATION},165* {@code WARNING} or {@code ERROR}).166*167* @param optionType the option type ({@code YES_NO_OPTION},168* {@code YES_NO_CANCEL_OPTION} or169* {@code OK_CANCEL_OPTION}).170*171* @param defaultOption the default option172* from the provided optionType ({@code YES},173* {@code NO}, {@code CANCEL} or174* {@code OK}).175*176* @exception IllegalArgumentException if messageType is not either177* {@code INFORMATION}, {@code WARNING},178* or {@code ERROR}, if optionType is not either179* {@code YES_NO_OPTION},180* {@code YES_NO_CANCEL_OPTION}, or181* {@code OK_CANCEL_OPTION},182* or if {@code defaultOption}183* does not correspond to one of the options in184* {@code optionType}.185*/186public ConfirmationCallback(int messageType,187int optionType, int defaultOption) {188189if (messageType < INFORMATION || messageType > ERROR ||190optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)191throw new IllegalArgumentException();192193switch (optionType) {194case YES_NO_OPTION:195if (defaultOption != YES && defaultOption != NO)196throw new IllegalArgumentException();197break;198case YES_NO_CANCEL_OPTION:199if (defaultOption != YES && defaultOption != NO &&200defaultOption != CANCEL)201throw new IllegalArgumentException();202break;203case OK_CANCEL_OPTION:204if (defaultOption != OK && defaultOption != CANCEL)205throw new IllegalArgumentException();206break;207}208209this.prompt = null;210this.messageType = messageType;211this.optionType = optionType;212this.options = null;213this.defaultOption = defaultOption;214}215216/**217* Construct a {@code ConfirmationCallback} with a218* message type, a list of options and a default option.219*220* <p> Underlying security services use this constructor if221* they require a confirmation different from the available preset222* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).223* The confirmation options are listed in the {@code options} array,224* and are displayed by the {@code CallbackHandler} implementation225* in a manner consistent with the way preset options are displayed.226*227* @param messageType the message type ({@code INFORMATION},228* {@code WARNING} or {@code ERROR}).229*230* @param options the list of confirmation options. The array is cloned231* to protect against subsequent modification.232*233* @param defaultOption the default option, represented as an index234* into the {@code options} array.235*236* @exception IllegalArgumentException if messageType is not either237* {@code INFORMATION}, {@code WARNING},238* or {@code ERROR}, if {@code options} is null,239* if {@code options} has a length of 0,240* if any element from {@code options} is null,241* if any element from {@code options}242* has a length of 0, or if {@code defaultOption}243* does not lie within the array boundaries of244* {@code options}.245*/246public ConfirmationCallback(int messageType,247String[] options, int defaultOption) {248249if (messageType < INFORMATION || messageType > ERROR ||250options == null || options.length == 0 ||251defaultOption < 0 || defaultOption >= options.length)252throw new IllegalArgumentException();253254for (int i = 0; i < options.length; i++) {255if (options[i] == null || options[i].isEmpty())256throw new IllegalArgumentException();257}258259this.prompt = null;260this.messageType = messageType;261this.optionType = UNSPECIFIED_OPTION;262this.options = options.clone();263this.defaultOption = defaultOption;264}265266/**267* Construct a {@code ConfirmationCallback} with a prompt,268* message type, an option type and a default option.269*270* <p> Underlying security services use this constructor if271* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL272* confirmation.273*274* @param prompt the prompt used to describe the list of options.275*276* @param messageType the message type ({@code INFORMATION},277* {@code WARNING} or {@code ERROR}).278*279* @param optionType the option type ({@code YES_NO_OPTION},280* {@code YES_NO_CANCEL_OPTION} or281* {@code OK_CANCEL_OPTION}).282*283* @param defaultOption the default option284* from the provided optionType ({@code YES},285* {@code NO}, {@code CANCEL} or286* {@code OK}).287*288* @exception IllegalArgumentException if {@code prompt} is null,289* if {@code prompt} has a length of 0,290* if messageType is not either291* {@code INFORMATION}, {@code WARNING},292* or {@code ERROR}, if optionType is not either293* {@code YES_NO_OPTION},294* {@code YES_NO_CANCEL_OPTION}, or295* {@code OK_CANCEL_OPTION},296* or if {@code defaultOption}297* does not correspond to one of the options in298* {@code optionType}.299*/300public ConfirmationCallback(String prompt, int messageType,301int optionType, int defaultOption) {302303if (prompt == null || prompt.isEmpty() ||304messageType < INFORMATION || messageType > ERROR ||305optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)306throw new IllegalArgumentException();307308switch (optionType) {309case YES_NO_OPTION:310if (defaultOption != YES && defaultOption != NO)311throw new IllegalArgumentException();312break;313case YES_NO_CANCEL_OPTION:314if (defaultOption != YES && defaultOption != NO &&315defaultOption != CANCEL)316throw new IllegalArgumentException();317break;318case OK_CANCEL_OPTION:319if (defaultOption != OK && defaultOption != CANCEL)320throw new IllegalArgumentException();321break;322}323324this.prompt = prompt;325this.messageType = messageType;326this.optionType = optionType;327this.options = null;328this.defaultOption = defaultOption;329}330331/**332* Construct a {@code ConfirmationCallback} with a prompt,333* message type, a list of options and a default option.334*335* <p> Underlying security services use this constructor if336* they require a confirmation different from the available preset337* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).338* The confirmation options are listed in the {@code options} array,339* and are displayed by the {@code CallbackHandler} implementation340* in a manner consistent with the way preset options are displayed.341*342* @param prompt the prompt used to describe the list of options.343*344* @param messageType the message type ({@code INFORMATION},345* {@code WARNING} or {@code ERROR}).346*347* @param options the list of confirmation options. The array is cloned348* to protect against subsequent modification.349*350* @param defaultOption the default option, represented as an index351* into the {@code options} array.352*353* @exception IllegalArgumentException if {@code prompt} is null,354* if {@code prompt} has a length of 0,355* if messageType is not either356* {@code INFORMATION}, {@code WARNING},357* or {@code ERROR}, if {@code options} is null,358* if {@code options} has a length of 0,359* if any element from {@code options} is null,360* if any element from {@code options}361* has a length of 0, or if {@code defaultOption}362* does not lie within the array boundaries of363* {@code options}.364*/365public ConfirmationCallback(String prompt, int messageType,366String[] options, int defaultOption) {367368if (prompt == null || prompt.isEmpty() ||369messageType < INFORMATION || messageType > ERROR ||370options == null || options.length == 0 ||371defaultOption < 0 || defaultOption >= options.length)372throw new IllegalArgumentException();373374for (int i = 0; i < options.length; i++) {375if (options[i] == null || options[i].isEmpty())376throw new IllegalArgumentException();377}378379this.prompt = prompt;380this.messageType = messageType;381this.optionType = UNSPECIFIED_OPTION;382this.options = options.clone();383this.defaultOption = defaultOption;384}385386/**387* Get the prompt.388*389* @return the prompt, or null if this {@code ConfirmationCallback}390* was instantiated without a {@code prompt}.391*/392public String getPrompt() {393return prompt;394}395396/**397* Get the message type.398*399* @return the message type ({@code INFORMATION},400* {@code WARNING} or {@code ERROR}).401*/402public int getMessageType() {403return messageType;404}405406/**407* Get the option type.408*409* <p> If this method returns {@code UNSPECIFIED_OPTION}, then this410* {@code ConfirmationCallback} was instantiated with411* {@code options} instead of an {@code optionType}.412* In this case, invoke the {@code getOptions} method413* to determine which confirmation options to display.414*415* @return the option type ({@code YES_NO_OPTION},416* {@code YES_NO_CANCEL_OPTION} or417* {@code OK_CANCEL_OPTION}), or418* {@code UNSPECIFIED_OPTION} if this419* {@code ConfirmationCallback} was instantiated with420* {@code options} instead of an {@code optionType}.421*/422public int getOptionType() {423return optionType;424}425426/**427* Get the confirmation options.428*429* @return a copy of the list of confirmation options, or null if this430* {@code ConfirmationCallback} was instantiated with431* an {@code optionType} instead of {@code options}.432*/433public String[] getOptions() {434return options == null ? null : options.clone();435}436437/**438* Get the default option.439*440* @return the default option, represented as441* {@code YES}, {@code NO}, {@code OK} or442* {@code CANCEL} if an {@code optionType}443* was specified to the constructor of this444* {@code ConfirmationCallback}.445* Otherwise, this method returns the default option as446* an index into the447* {@code options} array specified to the constructor448* of this {@code ConfirmationCallback}.449*/450public int getDefaultOption() {451return defaultOption;452}453454/**455* Set the selected confirmation option.456*457* @param selection the selection represented as {@code YES},458* {@code NO}, {@code OK} or {@code CANCEL}459* if an {@code optionType} was specified to the constructor460* of this {@code ConfirmationCallback}.461* Otherwise, the selection represents the index into the462* {@code options} array specified to the constructor463* of this {@code ConfirmationCallback}.464*465* @see #getSelectedIndex466*/467public void setSelectedIndex(int selection) {468this.selection = selection;469}470471/**472* Get the selected confirmation option.473*474* @return the selected confirmation option represented as475* {@code YES}, {@code NO}, {@code OK} or476* {@code CANCEL} if an {@code optionType}477* was specified to the constructor of this478* {@code ConfirmationCallback}.479* Otherwise, this method returns the selected confirmation480* option as an index into the481* {@code options} array specified to the constructor482* of this {@code ConfirmationCallback}.483*484* @see #setSelectedIndex485*/486public int getSelectedIndex() {487return selection;488}489}490491492