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