Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/callback/ConfirmationCallback.java
38918 views
/*1* Copyright (c) 1999, 2013, 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* @see javax.security.auth.callback.CallbackHandler34*/35public class ConfirmationCallback implements Callback, java.io.Serializable {3637private static final long serialVersionUID = -9095656433782481624L;3839/**40* Unspecified option type.41*42* <p> The {@code getOptionType} method returns this43* value if this {@code ConfirmationCallback} was instantiated44* with {@code options} instead of an {@code optionType}.45*/46public static final int UNSPECIFIED_OPTION = -1;4748/**49* YES/NO confirmation option.50*51* <p> An underlying security service specifies this as the52* {@code optionType} to a {@code ConfirmationCallback}53* constructor if it requires a confirmation which can be answered54* with either {@code YES} or {@code NO}.55*/56public static final int YES_NO_OPTION = 0;5758/**59* YES/NO/CANCEL confirmation confirmation option.60*61* <p> An underlying security service specifies this as the62* {@code optionType} to a {@code ConfirmationCallback}63* constructor if it requires a confirmation which can be answered64* with either {@code YES}, {@code NO} or {@code CANCEL}.65*/66public static final int YES_NO_CANCEL_OPTION = 1;6768/**69* OK/CANCEL confirmation confirmation option.70*71* <p> An underlying security service specifies this as the72* {@code optionType} to a {@code ConfirmationCallback}73* constructor if it requires a confirmation which can be answered74* with either {@code OK} or {@code CANCEL}.75*/76public static final int OK_CANCEL_OPTION = 2;7778/**79* YES option.80*81* <p> If an {@code optionType} was specified to this82* {@code ConfirmationCallback}, this option may be specified as a83* {@code defaultOption} or returned as the selected index.84*/85public static final int YES = 0;8687/**88* NO option.89*90* <p> If an {@code optionType} was specified to this91* {@code ConfirmationCallback}, this option may be specified as a92* {@code defaultOption} or returned as the selected index.93*/94public static final int NO = 1;9596/**97* CANCEL option.98*99* <p> If an {@code optionType} was specified to this100* {@code ConfirmationCallback}, this option may be specified as a101* {@code defaultOption} or returned as the selected index.102*/103public static final int CANCEL = 2;104105/**106* OK option.107*108* <p> If an {@code optionType} was specified to this109* {@code ConfirmationCallback}, this option may be specified as a110* {@code defaultOption} or returned as the selected index.111*/112public static final int OK = 3;113114/** INFORMATION message type. */115public static final int INFORMATION = 0;116117/** WARNING message type. */118public static final int WARNING = 1;119120/** ERROR message type. */121public static final int ERROR = 2;122/**123* @serial124* @since 1.4125*/126private String prompt;127/**128* @serial129* @since 1.4130*/131private int messageType;132/**133* @serial134* @since 1.4135*/136private int optionType = UNSPECIFIED_OPTION;137/**138* @serial139* @since 1.4140*/141private int defaultOption;142/**143* @serial144* @since 1.4145*/146private String[] options;147/**148* @serial149* @since 1.4150*/151private int selection;152153/**154* Construct a {@code ConfirmationCallback} with a155* message type, an option type and a default option.156*157* <p> Underlying security services use this constructor if158* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL159* confirmation.160*161* <p>162*163* @param messageType the message type ({@code INFORMATION},164* {@code WARNING} or {@code ERROR}). <p>165*166* @param optionType the option type ({@code YES_NO_OPTION},167* {@code YES_NO_CANCEL_OPTION} or168* {@code OK_CANCEL_OPTION}). <p>169*170* @param defaultOption the default option171* from the provided optionType ({@code YES},172* {@code NO}, {@code CANCEL} or173* {@code OK}).174*175* @exception IllegalArgumentException if messageType is not either176* {@code INFORMATION}, {@code WARNING},177* or {@code ERROR}, if optionType is not either178* {@code YES_NO_OPTION},179* {@code YES_NO_CANCEL_OPTION}, or180* {@code OK_CANCEL_OPTION},181* or if {@code defaultOption}182* does not correspond to one of the options in183* {@code optionType}.184*/185public ConfirmationCallback(int messageType,186int optionType, int defaultOption) {187188if (messageType < INFORMATION || messageType > ERROR ||189optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)190throw new IllegalArgumentException();191192switch (optionType) {193case YES_NO_OPTION:194if (defaultOption != YES && defaultOption != NO)195throw new IllegalArgumentException();196break;197case YES_NO_CANCEL_OPTION:198if (defaultOption != YES && defaultOption != NO &&199defaultOption != CANCEL)200throw new IllegalArgumentException();201break;202case OK_CANCEL_OPTION:203if (defaultOption != OK && defaultOption != CANCEL)204throw new IllegalArgumentException();205break;206}207208this.messageType = messageType;209this.optionType = optionType;210this.defaultOption = defaultOption;211}212213/**214* Construct a {@code ConfirmationCallback} with a215* message type, a list of options and a default option.216*217* <p> Underlying security services use this constructor if218* they require a confirmation different from the available preset219* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).220* The confirmation options are listed in the {@code options} array,221* and are displayed by the {@code CallbackHandler} implementation222* in a manner consistent with the way preset options are displayed.223*224* <p>225*226* @param messageType the message type ({@code INFORMATION},227* {@code WARNING} or {@code ERROR}). <p>228*229* @param options the list of confirmation options. <p>230*231* @param defaultOption the default option, represented as an index232* into the {@code options} array.233*234* @exception IllegalArgumentException if messageType is not either235* {@code INFORMATION}, {@code WARNING},236* or {@code ERROR}, if {@code options} is null,237* if {@code options} has a length of 0,238* if any element from {@code options} is null,239* if any element from {@code options}240* has a length of 0, or if {@code defaultOption}241* does not lie within the array boundaries of242* {@code options}.243*/244public ConfirmationCallback(int messageType,245String[] options, int defaultOption) {246247if (messageType < INFORMATION || messageType > ERROR ||248options == null || options.length == 0 ||249defaultOption < 0 || defaultOption >= options.length)250throw new IllegalArgumentException();251252for (int i = 0; i < options.length; i++) {253if (options[i] == null || options[i].length() == 0)254throw new IllegalArgumentException();255}256257this.messageType = messageType;258this.options = options;259this.defaultOption = defaultOption;260}261262/**263* Construct a {@code ConfirmationCallback} with a prompt,264* message type, an option type and a default option.265*266* <p> Underlying security services use this constructor if267* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL268* confirmation.269*270* <p>271*272* @param prompt the prompt used to describe the list of options. <p>273*274* @param messageType the message type ({@code INFORMATION},275* {@code WARNING} or {@code ERROR}). <p>276*277* @param optionType the option type ({@code YES_NO_OPTION},278* {@code YES_NO_CANCEL_OPTION} or279* {@code OK_CANCEL_OPTION}). <p>280*281* @param defaultOption the default option282* from the provided optionType ({@code YES},283* {@code NO}, {@code CANCEL} or284* {@code OK}).285*286* @exception IllegalArgumentException if {@code prompt} is null,287* if {@code prompt} has a length of 0,288* if messageType is not either289* {@code INFORMATION}, {@code WARNING},290* or {@code ERROR}, if optionType is not either291* {@code YES_NO_OPTION},292* {@code YES_NO_CANCEL_OPTION}, or293* {@code OK_CANCEL_OPTION},294* or if {@code defaultOption}295* does not correspond to one of the options in296* {@code optionType}.297*/298public ConfirmationCallback(String prompt, int messageType,299int optionType, int defaultOption) {300301if (prompt == null || prompt.length() == 0 ||302messageType < INFORMATION || messageType > ERROR ||303optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)304throw new IllegalArgumentException();305306switch (optionType) {307case YES_NO_OPTION:308if (defaultOption != YES && defaultOption != NO)309throw new IllegalArgumentException();310break;311case YES_NO_CANCEL_OPTION:312if (defaultOption != YES && defaultOption != NO &&313defaultOption != CANCEL)314throw new IllegalArgumentException();315break;316case OK_CANCEL_OPTION:317if (defaultOption != OK && defaultOption != CANCEL)318throw new IllegalArgumentException();319break;320}321322this.prompt = prompt;323this.messageType = messageType;324this.optionType = optionType;325this.defaultOption = defaultOption;326}327328/**329* Construct a {@code ConfirmationCallback} with a prompt,330* message type, a list of options and a default option.331*332* <p> Underlying security services use this constructor if333* they require a confirmation different from the available preset334* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).335* The confirmation options are listed in the {@code options} array,336* and are displayed by the {@code CallbackHandler} implementation337* in a manner consistent with the way preset options are displayed.338*339* <p>340*341* @param prompt the prompt used to describe the list of options. <p>342*343* @param messageType the message type ({@code INFORMATION},344* {@code WARNING} or {@code ERROR}). <p>345*346* @param options the list of confirmation options. <p>347*348* @param defaultOption the default option, represented as an index349* into the {@code options} array.350*351* @exception IllegalArgumentException if {@code prompt} is null,352* if {@code prompt} has a length of 0,353* if messageType is not either354* {@code INFORMATION}, {@code WARNING},355* or {@code ERROR}, if {@code options} is null,356* if {@code options} has a length of 0,357* if any element from {@code options} is null,358* if any element from {@code options}359* has a length of 0, or if {@code defaultOption}360* does not lie within the array boundaries of361* {@code options}.362*/363public ConfirmationCallback(String prompt, int messageType,364String[] options, int defaultOption) {365366if (prompt == null || prompt.length() == 0 ||367messageType < INFORMATION || messageType > ERROR ||368options == null || options.length == 0 ||369defaultOption < 0 || defaultOption >= options.length)370throw new IllegalArgumentException();371372for (int i = 0; i < options.length; i++) {373if (options[i] == null || options[i].length() == 0)374throw new IllegalArgumentException();375}376377this.prompt = prompt;378this.messageType = messageType;379this.options = options;380this.defaultOption = defaultOption;381}382383/**384* Get the prompt.385*386* <p>387*388* @return the prompt, or null if this {@code ConfirmationCallback}389* was instantiated without a {@code prompt}.390*/391public String getPrompt() {392return prompt;393}394395/**396* Get the message type.397*398* <p>399*400* @return the message type ({@code INFORMATION},401* {@code WARNING} or {@code ERROR}).402*/403public int getMessageType() {404return messageType;405}406407/**408* Get the option type.409*410* <p> If this method returns {@code UNSPECIFIED_OPTION}, then this411* {@code ConfirmationCallback} was instantiated with412* {@code options} instead of an {@code optionType}.413* In this case, invoke the {@code getOptions} method414* to determine which confirmation options to display.415*416* <p>417*418* @return the option type ({@code YES_NO_OPTION},419* {@code YES_NO_CANCEL_OPTION} or420* {@code OK_CANCEL_OPTION}), or421* {@code UNSPECIFIED_OPTION} if this422* {@code ConfirmationCallback} was instantiated with423* {@code options} instead of an {@code optionType}.424*/425public int getOptionType() {426return optionType;427}428429/**430* Get the confirmation options.431*432* <p>433*434* @return the list of confirmation options, or null if this435* {@code ConfirmationCallback} was instantiated with436* an {@code optionType} instead of {@code options}.437*/438public String[] getOptions() {439return options;440}441442/**443* Get the default option.444*445* <p>446*447* @return the default option, represented as448* {@code YES}, {@code NO}, {@code OK} or449* {@code CANCEL} if an {@code optionType}450* was specified to the constructor of this451* {@code ConfirmationCallback}.452* Otherwise, this method returns the default option as453* an index into the454* {@code options} array specified to the constructor455* of this {@code ConfirmationCallback}.456*/457public int getDefaultOption() {458return defaultOption;459}460461/**462* Set the selected confirmation option.463*464* <p>465*466* @param selection the selection represented as {@code YES},467* {@code NO}, {@code OK} or {@code CANCEL}468* if an {@code optionType} was specified to the constructor469* of this {@code ConfirmationCallback}.470* Otherwise, the selection represents the index into the471* {@code options} array specified to the constructor472* of this {@code ConfirmationCallback}.473*474* @see #getSelectedIndex475*/476public void setSelectedIndex(int selection) {477this.selection = selection;478}479480/**481* Get the selected confirmation option.482*483* <p>484*485* @return the selected confirmation option represented as486* {@code YES}, {@code NO}, {@code OK} or487* {@code CANCEL} if an {@code optionType}488* was specified to the constructor of this489* {@code ConfirmationCallback}.490* Otherwise, this method returns the selected confirmation491* option as an index into the492* {@code options} array specified to the constructor493* of this {@code ConfirmationCallback}.494*495* @see #setSelectedIndex496*/497public int getSelectedIndex() {498return selection;499}500}501502503