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