author | avstepan |
Mon, 16 Mar 2015 19:09:13 +0400 | |
changeset 29492 | a4bf9a570035 |
parent 25859 | 3317bb8137f4 |
child 45434 | 4582657c7260 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1999, 2015, 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 |
* |
|
18830 | 162 |
* @param messageType the message type ({@code INFORMATION}, |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
163 |
* {@code WARNING} or {@code ERROR}). |
2 | 164 |
* |
18830 | 165 |
* @param optionType the option type ({@code YES_NO_OPTION}, |
166 |
* {@code YES_NO_CANCEL_OPTION} or |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
167 |
* {@code OK_CANCEL_OPTION}). |
2 | 168 |
* |
169 |
* @param defaultOption the default option |
|
18830 | 170 |
* from the provided optionType ({@code YES}, |
171 |
* {@code NO}, {@code CANCEL} or |
|
172 |
* {@code OK}). |
|
2 | 173 |
* |
174 |
* @exception IllegalArgumentException if messageType is not either |
|
18830 | 175 |
* {@code INFORMATION}, {@code WARNING}, |
176 |
* or {@code ERROR}, if optionType is not either |
|
177 |
* {@code YES_NO_OPTION}, |
|
178 |
* {@code YES_NO_CANCEL_OPTION}, or |
|
179 |
* {@code OK_CANCEL_OPTION}, |
|
180 |
* or if {@code defaultOption} |
|
2 | 181 |
* does not correspond to one of the options in |
18830 | 182 |
* {@code optionType}. |
2 | 183 |
*/ |
184 |
public ConfirmationCallback(int messageType, |
|
185 |
int optionType, int defaultOption) { |
|
186 |
||
187 |
if (messageType < INFORMATION || messageType > ERROR || |
|
188 |
optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION) |
|
189 |
throw new IllegalArgumentException(); |
|
190 |
||
191 |
switch (optionType) { |
|
192 |
case YES_NO_OPTION: |
|
193 |
if (defaultOption != YES && defaultOption != NO) |
|
194 |
throw new IllegalArgumentException(); |
|
195 |
break; |
|
196 |
case YES_NO_CANCEL_OPTION: |
|
197 |
if (defaultOption != YES && defaultOption != NO && |
|
198 |
defaultOption != CANCEL) |
|
199 |
throw new IllegalArgumentException(); |
|
200 |
break; |
|
201 |
case OK_CANCEL_OPTION: |
|
202 |
if (defaultOption != OK && defaultOption != CANCEL) |
|
203 |
throw new IllegalArgumentException(); |
|
204 |
break; |
|
205 |
} |
|
206 |
||
207 |
this.messageType = messageType; |
|
208 |
this.optionType = optionType; |
|
209 |
this.defaultOption = defaultOption; |
|
210 |
} |
|
211 |
||
212 |
/** |
|
18830 | 213 |
* Construct a {@code ConfirmationCallback} with a |
2 | 214 |
* message type, a list of options and a default option. |
215 |
* |
|
216 |
* <p> Underlying security services use this constructor if |
|
217 |
* they require a confirmation different from the available preset |
|
218 |
* confirmations provided (for example, CONTINUE/ABORT or STOP/GO). |
|
18830 | 219 |
* The confirmation options are listed in the {@code options} array, |
220 |
* and are displayed by the {@code CallbackHandler} implementation |
|
2 | 221 |
* in a manner consistent with the way preset options are displayed. |
222 |
* |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
223 |
* @param messageType the message type ({@code INFORMATION}, |
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
224 |
* {@code WARNING} or {@code ERROR}). |
2 | 225 |
* |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
226 |
* @param options the list of confirmation options. |
2 | 227 |
* |
228 |
* @param defaultOption the default option, represented as an index |
|
18830 | 229 |
* into the {@code options} array. |
2 | 230 |
* |
231 |
* @exception IllegalArgumentException if messageType is not either |
|
18830 | 232 |
* {@code INFORMATION}, {@code WARNING}, |
233 |
* or {@code ERROR}, if {@code options} is null, |
|
234 |
* if {@code options} has a length of 0, |
|
235 |
* if any element from {@code options} is null, |
|
236 |
* if any element from {@code options} |
|
237 |
* has a length of 0, or if {@code defaultOption} |
|
2 | 238 |
* does not lie within the array boundaries of |
18830 | 239 |
* {@code options}. |
2 | 240 |
*/ |
241 |
public ConfirmationCallback(int messageType, |
|
242 |
String[] options, int defaultOption) { |
|
243 |
||
244 |
if (messageType < INFORMATION || messageType > ERROR || |
|
245 |
options == null || options.length == 0 || |
|
246 |
defaultOption < 0 || defaultOption >= options.length) |
|
247 |
throw new IllegalArgumentException(); |
|
248 |
||
249 |
for (int i = 0; i < options.length; i++) { |
|
250 |
if (options[i] == null || options[i].length() == 0) |
|
251 |
throw new IllegalArgumentException(); |
|
252 |
} |
|
253 |
||
254 |
this.messageType = messageType; |
|
255 |
this.options = options; |
|
256 |
this.defaultOption = defaultOption; |
|
257 |
} |
|
258 |
||
259 |
/** |
|
18830 | 260 |
* Construct a {@code ConfirmationCallback} with a prompt, |
2 | 261 |
* message type, an option type and a default option. |
262 |
* |
|
263 |
* <p> Underlying security services use this constructor if |
|
264 |
* they require either a YES/NO, YES/NO/CANCEL or OK/CANCEL |
|
265 |
* confirmation. |
|
266 |
* |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
267 |
* @param prompt the prompt used to describe the list of options. |
2 | 268 |
* |
18830 | 269 |
* @param messageType the message type ({@code INFORMATION}, |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
270 |
* {@code WARNING} or {@code ERROR}). |
2 | 271 |
* |
18830 | 272 |
* @param optionType the option type ({@code YES_NO_OPTION}, |
273 |
* {@code YES_NO_CANCEL_OPTION} or |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
274 |
* {@code OK_CANCEL_OPTION}). |
2 | 275 |
* |
276 |
* @param defaultOption the default option |
|
18830 | 277 |
* from the provided optionType ({@code YES}, |
278 |
* {@code NO}, {@code CANCEL} or |
|
279 |
* {@code OK}). |
|
2 | 280 |
* |
18830 | 281 |
* @exception IllegalArgumentException if {@code prompt} is null, |
282 |
* if {@code prompt} has a length of 0, |
|
2 | 283 |
* if messageType is not either |
18830 | 284 |
* {@code INFORMATION}, {@code WARNING}, |
285 |
* or {@code ERROR}, if optionType is not either |
|
286 |
* {@code YES_NO_OPTION}, |
|
287 |
* {@code YES_NO_CANCEL_OPTION}, or |
|
288 |
* {@code OK_CANCEL_OPTION}, |
|
289 |
* or if {@code defaultOption} |
|
2 | 290 |
* does not correspond to one of the options in |
18830 | 291 |
* {@code optionType}. |
2 | 292 |
*/ |
293 |
public ConfirmationCallback(String prompt, int messageType, |
|
294 |
int optionType, int defaultOption) { |
|
295 |
||
296 |
if (prompt == null || prompt.length() == 0 || |
|
297 |
messageType < INFORMATION || messageType > ERROR || |
|
298 |
optionType < YES_NO_OPTION || optionType > OK_CANCEL_OPTION) |
|
299 |
throw new IllegalArgumentException(); |
|
300 |
||
301 |
switch (optionType) { |
|
302 |
case YES_NO_OPTION: |
|
303 |
if (defaultOption != YES && defaultOption != NO) |
|
304 |
throw new IllegalArgumentException(); |
|
305 |
break; |
|
306 |
case YES_NO_CANCEL_OPTION: |
|
307 |
if (defaultOption != YES && defaultOption != NO && |
|
308 |
defaultOption != CANCEL) |
|
309 |
throw new IllegalArgumentException(); |
|
310 |
break; |
|
311 |
case OK_CANCEL_OPTION: |
|
312 |
if (defaultOption != OK && defaultOption != CANCEL) |
|
313 |
throw new IllegalArgumentException(); |
|
314 |
break; |
|
315 |
} |
|
316 |
||
317 |
this.prompt = prompt; |
|
318 |
this.messageType = messageType; |
|
319 |
this.optionType = optionType; |
|
320 |
this.defaultOption = defaultOption; |
|
321 |
} |
|
322 |
||
323 |
/** |
|
18830 | 324 |
* Construct a {@code ConfirmationCallback} with a prompt, |
2 | 325 |
* message type, a list of options and a default option. |
326 |
* |
|
327 |
* <p> Underlying security services use this constructor if |
|
328 |
* they require a confirmation different from the available preset |
|
329 |
* confirmations provided (for example, CONTINUE/ABORT or STOP/GO). |
|
18830 | 330 |
* The confirmation options are listed in the {@code options} array, |
331 |
* and are displayed by the {@code CallbackHandler} implementation |
|
2 | 332 |
* in a manner consistent with the way preset options are displayed. |
333 |
* |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
334 |
* @param prompt the prompt used to describe the list of options. |
2 | 335 |
* |
18830 | 336 |
* @param messageType the message type ({@code INFORMATION}, |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
337 |
* {@code WARNING} or {@code ERROR}). |
2 | 338 |
* |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
339 |
* @param options the list of confirmation options. |
2 | 340 |
* |
341 |
* @param defaultOption the default option, represented as an index |
|
18830 | 342 |
* into the {@code options} array. |
2 | 343 |
* |
18830 | 344 |
* @exception IllegalArgumentException if {@code prompt} is null, |
345 |
* if {@code prompt} has a length of 0, |
|
2 | 346 |
* if messageType is not either |
18830 | 347 |
* {@code INFORMATION}, {@code WARNING}, |
348 |
* or {@code ERROR}, if {@code options} is null, |
|
349 |
* if {@code options} has a length of 0, |
|
350 |
* if any element from {@code options} is null, |
|
351 |
* if any element from {@code options} |
|
352 |
* has a length of 0, or if {@code defaultOption} |
|
2 | 353 |
* does not lie within the array boundaries of |
18830 | 354 |
* {@code options}. |
2 | 355 |
*/ |
356 |
public ConfirmationCallback(String prompt, int messageType, |
|
357 |
String[] options, int defaultOption) { |
|
358 |
||
359 |
if (prompt == null || prompt.length() == 0 || |
|
360 |
messageType < INFORMATION || messageType > ERROR || |
|
361 |
options == null || options.length == 0 || |
|
362 |
defaultOption < 0 || defaultOption >= options.length) |
|
363 |
throw new IllegalArgumentException(); |
|
364 |
||
365 |
for (int i = 0; i < options.length; i++) { |
|
366 |
if (options[i] == null || options[i].length() == 0) |
|
367 |
throw new IllegalArgumentException(); |
|
368 |
} |
|
369 |
||
370 |
this.prompt = prompt; |
|
371 |
this.messageType = messageType; |
|
372 |
this.options = options; |
|
373 |
this.defaultOption = defaultOption; |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Get the prompt. |
|
378 |
* |
|
18830 | 379 |
* @return the prompt, or null if this {@code ConfirmationCallback} |
380 |
* was instantiated without a {@code prompt}. |
|
2 | 381 |
*/ |
382 |
public String getPrompt() { |
|
383 |
return prompt; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Get the message type. |
|
388 |
* |
|
18830 | 389 |
* @return the message type ({@code INFORMATION}, |
390 |
* {@code WARNING} or {@code ERROR}). |
|
2 | 391 |
*/ |
392 |
public int getMessageType() { |
|
393 |
return messageType; |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Get the option type. |
|
398 |
* |
|
18830 | 399 |
* <p> If this method returns {@code UNSPECIFIED_OPTION}, then this |
400 |
* {@code ConfirmationCallback} was instantiated with |
|
401 |
* {@code options} instead of an {@code optionType}. |
|
402 |
* In this case, invoke the {@code getOptions} method |
|
2 | 403 |
* to determine which confirmation options to display. |
404 |
* |
|
18830 | 405 |
* @return the option type ({@code YES_NO_OPTION}, |
406 |
* {@code YES_NO_CANCEL_OPTION} or |
|
407 |
* {@code OK_CANCEL_OPTION}), or |
|
408 |
* {@code UNSPECIFIED_OPTION} if this |
|
409 |
* {@code ConfirmationCallback} was instantiated with |
|
410 |
* {@code options} instead of an {@code optionType}. |
|
2 | 411 |
*/ |
412 |
public int getOptionType() { |
|
413 |
return optionType; |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Get the confirmation options. |
|
418 |
* |
|
419 |
* @return the list of confirmation options, or null if this |
|
18830 | 420 |
* {@code ConfirmationCallback} was instantiated with |
421 |
* an {@code optionType} instead of {@code options}. |
|
2 | 422 |
*/ |
423 |
public String[] getOptions() { |
|
424 |
return options; |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Get the default option. |
|
429 |
* |
|
430 |
* @return the default option, represented as |
|
18830 | 431 |
* {@code YES}, {@code NO}, {@code OK} or |
432 |
* {@code CANCEL} if an {@code optionType} |
|
2 | 433 |
* was specified to the constructor of this |
18830 | 434 |
* {@code ConfirmationCallback}. |
2 | 435 |
* Otherwise, this method returns the default option as |
436 |
* an index into the |
|
18830 | 437 |
* {@code options} array specified to the constructor |
438 |
* of this {@code ConfirmationCallback}. |
|
2 | 439 |
*/ |
440 |
public int getDefaultOption() { |
|
441 |
return defaultOption; |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Set the selected confirmation option. |
|
446 |
* |
|
18830 | 447 |
* @param selection the selection represented as {@code YES}, |
448 |
* {@code NO}, {@code OK} or {@code CANCEL} |
|
449 |
* if an {@code optionType} was specified to the constructor |
|
450 |
* of this {@code ConfirmationCallback}. |
|
2 | 451 |
* Otherwise, the selection represents the index into the |
18830 | 452 |
* {@code options} array specified to the constructor |
453 |
* of this {@code ConfirmationCallback}. |
|
2 | 454 |
* |
455 |
* @see #getSelectedIndex |
|
456 |
*/ |
|
457 |
public void setSelectedIndex(int selection) { |
|
458 |
this.selection = selection; |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* Get the selected confirmation option. |
|
463 |
* |
|
464 |
* @return the selected confirmation option represented as |
|
18830 | 465 |
* {@code YES}, {@code NO}, {@code OK} or |
466 |
* {@code CANCEL} if an {@code optionType} |
|
2 | 467 |
* was specified to the constructor of this |
18830 | 468 |
* {@code ConfirmationCallback}. |
2 | 469 |
* Otherwise, this method returns the selected confirmation |
470 |
* option as an index into the |
|
18830 | 471 |
* {@code options} array specified to the constructor |
472 |
* of this {@code ConfirmationCallback}. |
|
2 | 473 |
* |
474 |
* @see #setSelectedIndex |
|
475 |
*/ |
|
476 |
public int getSelectedIndex() { |
|
477 |
return selection; |
|
478 |
} |
|
479 |
} |