1. /*
  2. * @(#)ResponseCache.java 1.1 03/09/22
  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.io.IOException;
  9. import java.util.Map;
  10. import java.util.List;
  11. import sun.security.util.SecurityConstants;
  12. /**
  13. * Represents implementations of URLConnection caches. An instance of
  14. * such a class can be registered with the system by doing
  15. * ResponseCache.setDefault(ResponseCache), and the system will call
  16. * this object in order to:
  17. *
  18. * <ul><li>store resource data which has been retrieved from an
  19. * external source into the cache</li>
  20. * <li>try to fetch a requested resource that may have been
  21. * stored in the cache</li>
  22. * </ul>
  23. *
  24. * The ResponseCache implementation decides which resources
  25. * should be cached, and for how long they should be cached. If a
  26. * request resource cannot be retrieved from the cache, then the
  27. * protocol handlers will fetch the resource from its original
  28. * location.
  29. *
  30. * The settings for URLConnection#useCaches controls whether the
  31. * protocol is allowed to use a cached response.
  32. *
  33. * For more information on HTTP caching, see <a
  34. * href="http://www.ietf.org/rfc/rfc2616.txt""><i>RFC 2616: Hypertext
  35. * Transfer Protocol -- HTTP/1.1</i></a>
  36. *
  37. * @version 1.1, 03/09/22
  38. * @author Yingxian Wang
  39. * @since 1.5
  40. */
  41. public abstract class ResponseCache {
  42. /**
  43. * The system wide cache that provides access to a url
  44. * caching mechanism.
  45. *
  46. * @see #setDefault(ResponseCache)
  47. * @see #getDefault()
  48. */
  49. private static ResponseCache theResponseCache;
  50. /**
  51. * Gets the system-wide response cache.
  52. *
  53. * @throws SecurityException
  54. * If a security manager has been installed and it denies
  55. * {@link NetPermission}<tt>("getResponseCache")</tt>
  56. *
  57. * @see #setDefault(ResponseCache)
  58. * @return the system-wide <code>ResponseCache</code>
  59. * @since 1.5
  60. */
  61. public synchronized static ResponseCache getDefault() {
  62. SecurityManager sm = System.getSecurityManager();
  63. if (sm != null) {
  64. sm.checkPermission(SecurityConstants.GET_RESPONSECACHE_PERMISSION);
  65. }
  66. return theResponseCache;
  67. }
  68. /**
  69. * Sets (or unsets) the system-wide cache.
  70. *
  71. * Note: non-standard procotol handlers may ignore this setting.
  72. *
  73. * @param responseCache The response cache, or
  74. * <code>null</code> to unset the cache.
  75. *
  76. * @throws SecurityException
  77. * If a security manager has been installed and it denies
  78. * {@link NetPermission}<tt>("setResponseCache")</tt>
  79. *
  80. * @see #getDefault()
  81. * @since 1.5
  82. */
  83. public synchronized static void setDefault(ResponseCache responseCache) {
  84. SecurityManager sm = System.getSecurityManager();
  85. if (sm != null) {
  86. sm.checkPermission(SecurityConstants.SET_RESPONSECACHE_PERMISSION);
  87. }
  88. theResponseCache = responseCache;
  89. }
  90. /**
  91. * Retrieve the cached response based on the requesting uri,
  92. * request method and request headers. Typically this method is
  93. * called by the protocol handler before it sends out the request
  94. * to get the network resource. If a cached response is returned,
  95. * that resource is used instead.
  96. *
  97. * @param uri a <code>URI</code> used to reference the requested
  98. * network resource
  99. * @param rqstMethod a <code>String</code> representing the request
  100. * method
  101. * @param rqstHeaders - a Map from request header
  102. * field names to lists of field values representing
  103. * the current request headers
  104. * @return a <code>CacheResponse</code> instance if available
  105. * from cache, or null otherwise
  106. * @throws IOException if an I/O error occurs
  107. * @throws IllegalArgumentException if any one of the arguments is null
  108. *
  109. * @see java.net.URLConnection#setUseCaches(boolean)
  110. * @see java.net.URLConnection#getUseCaches()
  111. * @see java.net.URLConnection#setDefaultUseCaches(boolean)
  112. * @see java.net.URLConnection#getDefaultUseCaches()
  113. */
  114. public abstract CacheResponse
  115. get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)
  116. throws IOException;
  117. /**
  118. * The protocol handler calls this method after a resource has
  119. * been retrieved, and the ResponseCache must decide whether or
  120. * not to store the resource in its cache. If the resource is to
  121. * be cached, then put() must return a CacheRequest object which
  122. * contains a WriteableByteChannel that the protocol handler will
  123. * use to write the resource into the cache. If the resource is
  124. * not to be cached, then put must return null.
  125. *
  126. * @param uri a <code>URI</code> used to reference the requested
  127. * network resource
  128. * @param conn - a URLConnection instance that is used to fetch
  129. * the response to be cached
  130. * @return a <code>CacheRequest</code> for recording the
  131. * response to be cached. Null return indicates that
  132. * the caller does not intend to cache the response.
  133. * @throws IOException if an I/O error occurs
  134. * @throws IllegalArgumentException if any one of the arguments is
  135. * null
  136. */
  137. public abstract CacheRequest put(URI uri, URLConnection conn) throws IOException;
  138. }