1. /*
  2. * @(#)CookieHandler.java 1.4 03/08/09
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.util.Map;
  9. import java.util.List;
  10. import java.io.IOException;
  11. import sun.security.util.SecurityConstants;
  12. /**
  13. * A CookieHandler object provides a callback mechanism to hook up a
  14. * HTTP state management policy implementation into the HTTP protocol
  15. * handler. The HTTP state management mechanism specifies a way to
  16. * create a stateful session with HTTP requests and responses.
  17. *
  18. * <p>A system-wide CookieHandler that to used by the HTTP protocol
  19. * handler can be registered by doing a
  20. * CookieHandler.setDefault(CookieHandler). The currently registered
  21. * CookieHandler can be retrieved by calling
  22. * CookieHandler.getDefault().
  23. *
  24. * For more information on HTTP state management, see <a
  25. * href="http://www.ietf.org/rfc/rfc2965.txt""><i>RFC 2965: HTTP
  26. * State Management Mechanism</i></a>
  27. *
  28. * @version 1.4, 03/08/09
  29. * @author Yingxian Wang
  30. * @since 1.5
  31. */
  32. public abstract class CookieHandler {
  33. /**
  34. * The system-wide cookie handler that will apply cookies to the
  35. * request headers and manage cookies from the response headers.
  36. *
  37. * @see setDefault(CookieHandler)
  38. * @see getDefault()
  39. */
  40. private static CookieHandler cookieHandler;
  41. /**
  42. * Gets the system-wide cookie handler.
  43. *
  44. * @return the system-wide cookie handler; A null return means
  45. * there is no system-wide cookie handler currently set.
  46. * @throws SecurityException
  47. * If a security manager has been installed and it denies
  48. * {@link NetPermission}<tt>("getCookieHandler")</tt>
  49. * @see #setDefault(CookieHandler)
  50. */
  51. public synchronized static CookieHandler getDefault() {
  52. SecurityManager sm = System.getSecurityManager();
  53. if (sm != null) {
  54. sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);
  55. }
  56. return cookieHandler;
  57. }
  58. /**
  59. * Sets (or unsets) the system-wide cookie handler.
  60. *
  61. * Note: non-standard http protocol handlers may ignore this setting.
  62. *
  63. * @param cHandler The HTTP cookie handler, or
  64. * <code>null</code> to unset.
  65. * @throws SecurityException
  66. * If a security manager has been installed and it denies
  67. * {@link NetPermission}<tt>("setCookieHandler")</tt>
  68. * @see #getDefault()
  69. */
  70. public synchronized static void setDefault(CookieHandler cHandler) {
  71. SecurityManager sm = System.getSecurityManager();
  72. if (sm != null) {
  73. sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);
  74. }
  75. cookieHandler = cHandler;
  76. }
  77. /**
  78. * Gets all the applicable cookies from a cookie cache for the
  79. * specified uri in the request header.
  80. *
  81. * HTTP protocol implementers should make sure that this method is
  82. * called after all request headers related to choosing cookies
  83. * are added, and before the request is sent.
  84. *
  85. * @param uri a <code>URI</code> to send cookies to in a request
  86. * @param requestHeaders - a Map from request header
  87. * field names to lists of field values representing
  88. * the current request headers
  89. * @return an immutable map from state management headers, with
  90. * field names "Cookie" or "Cookie2" to a list of
  91. * cookies containing state information
  92. *
  93. * @throws IOException if an I/O * error occurs
  94. * @throws IllegalArgumentException if either argument is null
  95. * @see #put(URI, Map)
  96. */
  97. public abstract Map<String, List<String>>
  98. get(URI uri, Map<String, List<String>> requestHeaders)
  99. throws IOException;
  100. /**
  101. * Sets all the applicable cookies, examples are response header
  102. * fields that are named Set-Cookie2, present in the response
  103. * headers into a cookie cache.
  104. *
  105. * @param uri a <code>URI</code> where the cookies come from
  106. * @param responseHeaders an immutable map from field names to
  107. * lists of field values representing the response
  108. * header fields returned
  109. * @throws IOException if an I/O error occurs
  110. * @throws IllegalArgumentException if either argument is null
  111. * @see #get(URI, Map)
  112. */
  113. public abstract void
  114. put(URI uri, Map<String, List<String>> responseHeaders)
  115. throws IOException;
  116. }