1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.security.auth.callback;
  6. /**
  7. * <p> An application implements a <code>CallbackHandler</code> and passes
  8. * it to underlying security services so that they may interact with
  9. * the application to retrieve specific authentication data,
  10. * such as usernames and passwords, or to display certain information,
  11. * such as error and warning messages.
  12. *
  13. * <p> CallbackHandlers are implemented in an application-dependent fashion.
  14. * For example, implementations for an application with a graphical user
  15. * interface (GUI) may pop up windows to prompt for requested information
  16. * or to display error messages. An implementation may also choose to obtain
  17. * requested information from an alternate source without asking the end user.
  18. *
  19. * <p> Underlying security services make requests for different types
  20. * of information by passing individual Callbacks to the
  21. * <code>CallbackHandler</code>. The <code>CallbackHandler</code>
  22. * implementation decides how to retrieve and display information
  23. * depending on the Callbacks passed to it. For example,
  24. * if the underlying service needs a username and password to
  25. * authenticate a user, it uses a <code>NameCallback</code> and
  26. * <code>PasswordCallback</code>. The <code>CallbackHandler</code>
  27. * can then choose to prompt for a username and password serially,
  28. * or to prompt for both in a single window.
  29. *
  30. * @version 1.9, 01/11/00
  31. */
  32. public interface CallbackHandler {
  33. /**
  34. * <p> Retrieve or display the information requested in the
  35. * provided Callbacks.
  36. *
  37. * <p> The <code>handle</code> method implementation checks the
  38. * instance(s) of the <code>Callback</code> object(s) passed in
  39. * to retrieve or display the requested information.
  40. * The following example is provided to help demonstrate what an
  41. * <code>handle</code> method implementation might look like.
  42. * This example code is for guidance only. Many details,
  43. * including proper error handling, are left out for simplicity.
  44. *
  45. * <pre>
  46. * public void handle(Callback[] callbacks)
  47. * throws IOException, UnsupportedCallbackException {
  48. *
  49. * for (int i = 0; i < callbacks.length; i++) {
  50. * if (callbacks[i] instanceof TextOutputCallback) {
  51. *
  52. * // display the message according to the specified type
  53. * TextOutputCallback toc = (TextOutputCallback)callbacks[i];
  54. * switch (toc.getMessageType()) {
  55. * case TextOutputCallback.INFORMATION:
  56. * System.out.println(td.getMessage());
  57. * break;
  58. * case TextOutputCallback.ERROR:
  59. * System.out.println("ERROR: " + td.getMessage());
  60. * break;
  61. * case TextOutputCallback.WARNING:
  62. * System.out.println("WARNING: " + td.getMessage());
  63. * break;
  64. * default:
  65. * throw new IOException("Unsupported message type: " +
  66. * td.getMessageType());
  67. * }
  68. *
  69. * } else if (callbacks[i] instanceof NameCallback) {
  70. *
  71. * // prompt the user for a username
  72. * NameCallback nc = (NameCallback)callbacks[i];
  73. *
  74. * // ignore the provided defaultName
  75. * System.err.print(nc.getPrompt());
  76. * System.err.flush();
  77. * nc.setName((new BufferedReader
  78. * (new InputStreamReader(System.in))).readLine());
  79. *
  80. * } else if (callbacks[i] instanceof PasswordCallback) {
  81. *
  82. * // prompt the user for sensitive information
  83. * PasswordCallback pc = (PasswordCallback)callbacks[i];
  84. * System.err.print(pc.getPrompt());
  85. * System.err.flush();
  86. * pc.setPassword(readPassword(System.in));
  87. *
  88. * } else {
  89. * throw new UnsupportedCallbackException
  90. * (callbacks[i], "Unrecognized Callback");
  91. * }
  92. * }
  93. * }
  94. *
  95. * // Reads user password from given input stream.
  96. * private char[] readPassword(InputStream in) throws IOException {
  97. * // insert code to read a user password from the input stream
  98. * }
  99. * </pre>
  100. *
  101. * @param callbacks an array of <code>Callback</code> objects provided
  102. * by an underlying security service which contains
  103. * the information requested to be retrieved or displayed.
  104. *
  105. * @exception java.io.IOException if an input or output error occurs. <p>
  106. *
  107. * @exception UnsupportedCallbackException if the implementation of this
  108. * method does not support one or more of the Callbacks
  109. * specified in the <code>callbacks</code> parameter.
  110. */
  111. void handle(Callback[] callbacks)
  112. throws java.io.IOException, UnsupportedCallbackException;
  113. }