2
|
1 |
/*
|
18830
|
2 |
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.security.auth.callback;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* <p> Underlying security services instantiate and pass a
|
18830
|
30 |
* {@code ConfirmationCallback} to the {@code handle}
|
|
31 |
* method of a {@code CallbackHandler} to ask for YES/NO,
|
2
|
32 |
* OK/CANCEL, YES/NO/CANCEL or other similar confirmations.
|
|
33 |
*
|
|
34 |
* @see javax.security.auth.callback.CallbackHandler
|
|
35 |
*/
|
|
36 |
public class ConfirmationCallback implements Callback, java.io.Serializable {
|
|
37 |
|
|
38 |
private static final long serialVersionUID = -9095656433782481624L;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Unspecified option type.
|
|
42 |
*
|
18830
|
43 |
* <p> The {@code getOptionType} method returns this
|
|
44 |
* value if this {@code ConfirmationCallback} was instantiated
|
|
45 |
* with {@code options} instead of an {@code optionType}.
|
2
|
46 |
*/
|
|
47 |
public static final int UNSPECIFIED_OPTION = -1;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* YES/NO confirmation option.
|
|
51 |
*
|
|
52 |
* <p> An underlying security service specifies this as the
|
18830
|
53 |
* {@code optionType} to a {@code ConfirmationCallback}
|
2
|
54 |
* constructor if it requires a confirmation which can be answered
|
18830
|
55 |
* with either {@code YES} or {@code NO}.
|
2
|
56 |
*/
|
|
57 |
public static final int YES_NO_OPTION = 0;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* YES/NO/CANCEL confirmation confirmation option.
|
|
61 |
*
|
|
62 |
* <p> An underlying security service specifies this as the
|
18830
|
63 |
* {@code optionType} to a {@code ConfirmationCallback}
|
2
|
64 |
* constructor if it requires a confirmation which can be answered
|
18830
|
65 |
* with either {@code YES}, {@code NO} or {@code CANCEL}.
|
2
|
66 |
*/
|
|
67 |
public static final int YES_NO_CANCEL_OPTION = 1;
|
|
68 |
|
|
69 |
/**
|
|
70 |
* OK/CANCEL confirmation confirmation option.
|
|
71 |
*
|
|
72 |
* <p> An underlying security service specifies this as the
|
18830
|
73 |
* {@code optionType} to a {@code ConfirmationCallback}
|
2
|
74 |
* constructor if it requires a confirmation which can be answered
|
18830
|
75 |
* with either {@code OK} or {@code CANCEL}.
|
2
|
76 |
*/
|
|
77 |
public static final int OK_CANCEL_OPTION = 2;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* YES option.
|
|
81 |
*
|
18830
|
82 |
* <p> If an {@code optionType} was specified to this
|
|
83 |
* {@code ConfirmationCallback}, this option may be specified as a
|
|
84 |
* {@code defaultOption} or returned as the selected index.
|
2
|
85 |
*/
|
|
86 |
public static final int YES = 0;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* NO option.
|
|
90 |
*
|
18830
|
91 |
* <p> If an {@code optionType} was specified to this
|
|
92 |
* {@code ConfirmationCallback}, this option may be specified as a
|
|
93 |
* {@code defaultOption} or returned as the selected index.
|
2
|
94 |
*/
|
|
95 |
public static final int NO = 1;
|
|
96 |
|
|
97 |
/**
|
|
98 |
* CANCEL option.
|
|
99 |
*
|
18830
|
100 |
* <p> If an {@code optionType} was specified to this
|
|
101 |
* {@code ConfirmationCallback}, this option may be specified as a
|
|
102 |
* {@code defaultOption} or returned as the selected index.
|
2
|
103 |
*/
|
|
104 |
public static final int CANCEL = 2;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* OK option.
|
|
108 |
*
|
18830
|
109 |
* <p> If an {@code optionType} was specified to this
|
|
110 |
* {@code ConfirmationCallback}, this option may be specified as a
|
|
111 |
* {@code defaultOption} or returned as the selected index.
|
2
|
112 |
*/
|
|
113 |
public static final int OK = 3;
|
|
114 |
|
|
115 |
/** INFORMATION message type. */
|
|
116 |
public static final int INFORMATION = 0;
|
|
117 |
|
|
118 |
/** WARNING message type. */
|
|
119 |
public static final int WARNING = 1;
|
|
120 |
|
|
121 |
/** ERROR message type. */
|
|
122 |
public static final int ERROR = 2;
|
|
123 |
/**
|
|
124 |
* @serial
|
|
125 |
* @since 1.4
|
|
126 |
*/
|
|
127 |
private String prompt;
|
|
128 |
/**
|
|
129 |
* @serial
|
|
130 |
* @since 1.4
|
|
131 |
*/
|
|
132 |
private int messageType;
|
|
133 |
/**
|
|
134 |
* @serial
|
|
135 |
* @since 1.4
|
|
136 |
*/
|
|
137 |
private int optionType = UNSPECIFIED_OPTION;
|
|
138 |
/**
|
|
139 |
* @serial
|
|
140 |
* @since 1.4
|
|
141 |
*/
|
|
142 |
private int defaultOption;
|
|
143 |
/**
|
|
144 |
* @serial
|
|
145 |
* @since 1.4
|
|
146 |
*/
|
|
147 |
private String[] options;
|
|
148 |
/**
|
|
149 |
* @serial
|
|
150 |
* @since 1.4
|
|
151 |
*/
|
|
152 |
private int selection;
|
|
153 |
|
|
154 |
/**
|
18830
|
155 |
* Construct a {@code ConfirmationCallback} with a
|
2
|
156 |
* message type, an option type and a default option.
|
|
157 |
*
|
|
158 |
* <p> Underlying security services use this constructor if
|
|
159 |
* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL
|
|
160 |
* confirmation.
|
|
161 |
*
|
|
162 |
* <p>
|
|
163 |
*
|
18830
|
164 |
* @param messageType the message type ({@code INFORMATION},
|
|
165 |
* {@code WARNING} or {@code ERROR}). <p>
|
2
|
166 |
*
|
18830
|
167 |
* @param optionType the option type ({@code YES_NO_OPTION},
|
|
168 |
* {@code YES_NO_CANCEL_OPTION} or
|
|
169 |
* {@code OK_CANCEL_OPTION}). <p>
|
2
|
170 |
*
|
|
171 |
* @param defaultOption the default option
|
18830
|
172 |
* from the provided optionType ({@code YES},
|
|
173 |
* {@code NO}, {@code CANCEL} or
|
|
174 |
* {@code OK}).
|
2
|
175 |
*
|
|
176 |
* @exception IllegalArgumentException if messageType is not either
|
18830
|
177 |
* {@code INFORMATION}, {@code WARNING},
|
|
178 |
* or {@code ERROR}, if optionType is not either
|
|
179 |
* {@code YES_NO_OPTION},
|
|
180 |
* {@code YES_NO_CANCEL_OPTION}, or
|
|
181 |
* {@code OK_CANCEL_OPTION},
|
|
182 |
* or if {@code defaultOption}
|
2
|
183 |
* does not correspond to one of the options in
|
18830
|
184 |
* {@code optionType}.
|
2
|
185 |
*/
|
|
186 |
public ConfirmationCallback(int messageType,
|
|
187 |
int optionType, int defaultOption) {
|
|
188 |
|
|
189 |
if (messageType < INFORMATION || messageType > ERROR ||
|
|
190 |
optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)
|
|
191 |
throw new IllegalArgumentException();
|
|
192 |
|
|
193 |
switch (optionType) {
|
|
194 |
case YES_NO_OPTION:
|
|
195 |
if (defaultOption != YES && defaultOption != NO)
|
|
196 |
throw new IllegalArgumentException();
|
|
197 |
break;
|
|
198 |
case YES_NO_CANCEL_OPTION:
|
|
199 |
if (defaultOption != YES && defaultOption != NO &&
|
|
200 |
defaultOption != CANCEL)
|
|
201 |
throw new IllegalArgumentException();
|
|
202 |
break;
|
|
203 |
case OK_CANCEL_OPTION:
|
|
204 |
if (defaultOption != OK && defaultOption != CANCEL)
|
|
205 |
throw new IllegalArgumentException();
|
|
206 |
break;
|
|
207 |
}
|
|
208 |
|
|
209 |
this.messageType = messageType;
|
|
210 |
this.optionType = optionType;
|
|
211 |
this.defaultOption = defaultOption;
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
18830
|
215 |
* Construct a {@code ConfirmationCallback} with a
|
2
|
216 |
* message type, a list of options and a default option.
|
|
217 |
*
|
|
218 |
* <p> Underlying security services use this constructor if
|
|
219 |
* they require a confirmation different from the available preset
|
|
220 |
* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).
|
18830
|
221 |
* The confirmation options are listed in the {@code options} array,
|
|
222 |
* and are displayed by the {@code CallbackHandler} implementation
|
2
|
223 |
* in a manner consistent with the way preset options are displayed.
|
|
224 |
*
|
|
225 |
* <p>
|
|
226 |
*
|
18830
|
227 |
* @param messageType the message type ({@code INFORMATION},
|
|
228 |
* {@code WARNING} or {@code ERROR}). <p>
|
2
|
229 |
*
|
|
230 |
* @param options the list of confirmation options. <p>
|
|
231 |
*
|
|
232 |
* @param defaultOption the default option, represented as an index
|
18830
|
233 |
* into the {@code options} array.
|
2
|
234 |
*
|
|
235 |
* @exception IllegalArgumentException if messageType is not either
|
18830
|
236 |
* {@code INFORMATION}, {@code WARNING},
|
|
237 |
* or {@code ERROR}, if {@code options} is null,
|
|
238 |
* if {@code options} has a length of 0,
|
|
239 |
* if any element from {@code options} is null,
|
|
240 |
* if any element from {@code options}
|
|
241 |
* has a length of 0, or if {@code defaultOption}
|
2
|
242 |
* does not lie within the array boundaries of
|
18830
|
243 |
* {@code options}.
|
2
|
244 |
*/
|
|
245 |
public ConfirmationCallback(int messageType,
|
|
246 |
String[] options, int defaultOption) {
|
|
247 |
|
|
248 |
if (messageType < INFORMATION || messageType > ERROR ||
|
|
249 |
options == null || options.length == 0 ||
|
|
250 |
defaultOption < 0 || defaultOption >= options.length)
|
|
251 |
throw new IllegalArgumentException();
|
|
252 |
|
|
253 |
for (int i = 0; i < options.length; i++) {
|
|
254 |
if (options[i] == null || options[i].length() == 0)
|
|
255 |
throw new IllegalArgumentException();
|
|
256 |
}
|
|
257 |
|
|
258 |
this.messageType = messageType;
|
|
259 |
this.options = options;
|
|
260 |
this.defaultOption = defaultOption;
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
18830
|
264 |
* Construct a {@code ConfirmationCallback} with a prompt,
|
2
|
265 |
* message type, an option type and a default option.
|
|
266 |
*
|
|
267 |
* <p> Underlying security services use this constructor if
|
|
268 |
* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL
|
|
269 |
* confirmation.
|
|
270 |
*
|
|
271 |
* <p>
|
|
272 |
*
|
|
273 |
* @param prompt the prompt used to describe the list of options. <p>
|
|
274 |
*
|
18830
|
275 |
* @param messageType the message type ({@code INFORMATION},
|
|
276 |
* {@code WARNING} or {@code ERROR}). <p>
|
2
|
277 |
*
|
18830
|
278 |
* @param optionType the option type ({@code YES_NO_OPTION},
|
|
279 |
* {@code YES_NO_CANCEL_OPTION} or
|
|
280 |
* {@code OK_CANCEL_OPTION}). <p>
|
2
|
281 |
*
|
|
282 |
* @param defaultOption the default option
|
18830
|
283 |
* from the provided optionType ({@code YES},
|
|
284 |
* {@code NO}, {@code CANCEL} or
|
|
285 |
* {@code OK}).
|
2
|
286 |
*
|
18830
|
287 |
* @exception IllegalArgumentException if {@code prompt} is null,
|
|
288 |
* if {@code prompt} has a length of 0,
|
2
|
289 |
* if messageType is not either
|
18830
|
290 |
* {@code INFORMATION}, {@code WARNING},
|
|
291 |
* or {@code ERROR}, if optionType is not either
|
|
292 |
* {@code YES_NO_OPTION},
|
|
293 |
* {@code YES_NO_CANCEL_OPTION}, or
|
|
294 |
* {@code OK_CANCEL_OPTION},
|
|
295 |
* or if {@code defaultOption}
|
2
|
296 |
* does not correspond to one of the options in
|
18830
|
297 |
* {@code optionType}.
|
2
|
298 |
*/
|
|
299 |
public ConfirmationCallback(String prompt, int messageType,
|
|
300 |
int optionType, int defaultOption) {
|
|
301 |
|
|
302 |
if (prompt == null || prompt.length() == 0 ||
|
|
303 |
messageType < INFORMATION || messageType > ERROR ||
|
|
304 |
optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION)
|
|
305 |
throw new IllegalArgumentException();
|
|
306 |
|
|
307 |
switch (optionType) {
|
|
308 |
case YES_NO_OPTION:
|
|
309 |
if (defaultOption != YES && defaultOption != NO)
|
|
310 |
throw new IllegalArgumentException();
|
|
311 |
break;
|
|
312 |
case YES_NO_CANCEL_OPTION:
|
|
313 |
if (defaultOption != YES && defaultOption != NO &&
|
|
314 |
defaultOption != CANCEL)
|
|
315 |
throw new IllegalArgumentException();
|
|
316 |
break;
|
|
317 |
case OK_CANCEL_OPTION:
|
|
318 |
if (defaultOption != OK && defaultOption != CANCEL)
|
|
319 |
throw new IllegalArgumentException();
|
|
320 |
break;
|
|
321 |
}
|
|
322 |
|
|
323 |
this.prompt = prompt;
|
|
324 |
this.messageType = messageType;
|
|
325 |
this.optionType = optionType;
|
|
326 |
this.defaultOption = defaultOption;
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
18830
|
330 |
* Construct a {@code ConfirmationCallback} with a prompt,
|
2
|
331 |
* message type, a list of options and a default option.
|
|
332 |
*
|
|
333 |
* <p> Underlying security services use this constructor if
|
|
334 |
* they require a confirmation different from the available preset
|
|
335 |
* confirmations provided (for example, CONTINUE/ABORT or STOP/GO).
|
18830
|
336 |
* The confirmation options are listed in the {@code options} array,
|
|
337 |
* and are displayed by the {@code CallbackHandler} implementation
|
2
|
338 |
* in a manner consistent with the way preset options are displayed.
|
|
339 |
*
|
|
340 |
* <p>
|
|
341 |
*
|
|
342 |
* @param prompt the prompt used to describe the list of options. <p>
|
|
343 |
*
|
18830
|
344 |
* @param messageType the message type ({@code INFORMATION},
|
|
345 |
* {@code WARNING} or {@code ERROR}). <p>
|
2
|
346 |
*
|
|
347 |
* @param options the list of confirmation options. <p>
|
|
348 |
*
|
|
349 |
* @param defaultOption the default option, represented as an index
|
18830
|
350 |
* into the {@code options} array.
|
2
|
351 |
*
|
18830
|
352 |
* @exception IllegalArgumentException if {@code prompt} is null,
|
|
353 |
* if {@code prompt} has a length of 0,
|
2
|
354 |
* if messageType is not either
|
18830
|
355 |
* {@code INFORMATION}, {@code WARNING},
|
|
356 |
* or {@code ERROR}, if {@code options} is null,
|
|
357 |
* if {@code options} has a length of 0,
|
|
358 |
* if any element from {@code options} is null,
|
|
359 |
* if any element from {@code options}
|
|
360 |
* has a length of 0, or if {@code defaultOption}
|
2
|
361 |
* does not lie within the array boundaries of
|
18830
|
362 |
* {@code options}.
|
2
|
363 |
*/
|
|
364 |
public ConfirmationCallback(String prompt, int messageType,
|
|
365 |
String[] options, int defaultOption) {
|
|
366 |
|
|
367 |
if (prompt == null || prompt.length() == 0 ||
|
|
368 |
messageType < INFORMATION || messageType > ERROR ||
|
|
369 |
options == null || options.length == 0 ||
|
|
370 |
defaultOption < 0 || defaultOption >= options.length)
|
|
371 |
throw new IllegalArgumentException();
|
|
372 |
|
|
373 |
for (int i = 0; i < options.length; i++) {
|
|
374 |
if (options[i] == null || options[i].length() == 0)
|
|
375 |
throw new IllegalArgumentException();
|
|
376 |
}
|
|
377 |
|
|
378 |
this.prompt = prompt;
|
|
379 |
this.messageType = messageType;
|
|
380 |
this.options = options;
|
|
381 |
this.defaultOption = defaultOption;
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
|
385 |
* Get the prompt.
|
|
386 |
*
|
|
387 |
* <p>
|
|
388 |
*
|
18830
|
389 |
* @return the prompt, or null if this {@code ConfirmationCallback}
|
|
390 |
* was instantiated without a {@code prompt}.
|
2
|
391 |
*/
|
|
392 |
public String getPrompt() {
|
|
393 |
return prompt;
|
|
394 |
}
|
|
395 |
|
|
396 |
/**
|
|
397 |
* Get the message type.
|
|
398 |
*
|
|
399 |
* <p>
|
|
400 |
*
|
18830
|
401 |
* @return the message type ({@code INFORMATION},
|
|
402 |
* {@code WARNING} or {@code ERROR}).
|
2
|
403 |
*/
|
|
404 |
public int getMessageType() {
|
|
405 |
return messageType;
|
|
406 |
}
|
|
407 |
|
|
408 |
/**
|
|
409 |
* Get the option type.
|
|
410 |
*
|
18830
|
411 |
* <p> If this method returns {@code UNSPECIFIED_OPTION}, then this
|
|
412 |
* {@code ConfirmationCallback} was instantiated with
|
|
413 |
* {@code options} instead of an {@code optionType}.
|
|
414 |
* In this case, invoke the {@code getOptions} method
|
2
|
415 |
* to determine which confirmation options to display.
|
|
416 |
*
|
|
417 |
* <p>
|
|
418 |
*
|
18830
|
419 |
* @return the option type ({@code YES_NO_OPTION},
|
|
420 |
* {@code YES_NO_CANCEL_OPTION} or
|
|
421 |
* {@code OK_CANCEL_OPTION}), or
|
|
422 |
* {@code UNSPECIFIED_OPTION} if this
|
|
423 |
* {@code ConfirmationCallback} was instantiated with
|
|
424 |
* {@code options} instead of an {@code optionType}.
|
2
|
425 |
*/
|
|
426 |
public int getOptionType() {
|
|
427 |
return optionType;
|
|
428 |
}
|
|
429 |
|
|
430 |
/**
|
|
431 |
* Get the confirmation options.
|
|
432 |
*
|
|
433 |
* <p>
|
|
434 |
*
|
|
435 |
* @return the list of confirmation options, or null if this
|
18830
|
436 |
* {@code ConfirmationCallback} was instantiated with
|
|
437 |
* an {@code optionType} instead of {@code options}.
|
2
|
438 |
*/
|
|
439 |
public String[] getOptions() {
|
|
440 |
return options;
|
|
441 |
}
|
|
442 |
|
|
443 |
/**
|
|
444 |
* Get the default option.
|
|
445 |
*
|
|
446 |
* <p>
|
|
447 |
*
|
|
448 |
* @return the default option, represented as
|
18830
|
449 |
* {@code YES}, {@code NO}, {@code OK} or
|
|
450 |
* {@code CANCEL} if an {@code optionType}
|
2
|
451 |
* was specified to the constructor of this
|
18830
|
452 |
* {@code ConfirmationCallback}.
|
2
|
453 |
* Otherwise, this method returns the default option as
|
|
454 |
* an index into the
|
18830
|
455 |
* {@code options} array specified to the constructor
|
|
456 |
* of this {@code ConfirmationCallback}.
|
2
|
457 |
*/
|
|
458 |
public int getDefaultOption() {
|
|
459 |
return defaultOption;
|
|
460 |
}
|
|
461 |
|
|
462 |
/**
|
|
463 |
* Set the selected confirmation option.
|
|
464 |
*
|
|
465 |
* <p>
|
|
466 |
*
|
18830
|
467 |
* @param selection the selection represented as {@code YES},
|
|
468 |
* {@code NO}, {@code OK} or {@code CANCEL}
|
|
469 |
* if an {@code optionType} was specified to the constructor
|
|
470 |
* of this {@code ConfirmationCallback}.
|
2
|
471 |
* Otherwise, the selection represents the index into the
|
18830
|
472 |
* {@code options} array specified to the constructor
|
|
473 |
* of this {@code ConfirmationCallback}.
|
2
|
474 |
*
|
|
475 |
* @see #getSelectedIndex
|
|
476 |
*/
|
|
477 |
public void setSelectedIndex(int selection) {
|
|
478 |
this.selection = selection;
|
|
479 |
}
|
|
480 |
|
|
481 |
/**
|
|
482 |
* Get the selected confirmation option.
|
|
483 |
*
|
|
484 |
* <p>
|
|
485 |
*
|
|
486 |
* @return the selected confirmation option represented as
|
18830
|
487 |
* {@code YES}, {@code NO}, {@code OK} or
|
|
488 |
* {@code CANCEL} if an {@code optionType}
|
2
|
489 |
* was specified to the constructor of this
|
18830
|
490 |
* {@code ConfirmationCallback}.
|
2
|
491 |
* Otherwise, this method returns the selected confirmation
|
|
492 |
* option as an index into the
|
18830
|
493 |
* {@code options} array specified to the constructor
|
|
494 |
* of this {@code ConfirmationCallback}.
|
2
|
495 |
*
|
|
496 |
* @see #setSelectedIndex
|
|
497 |
*/
|
|
498 |
public int getSelectedIndex() {
|
|
499 |
return selection;
|
|
500 |
}
|
|
501 |
}
|