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