PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jsr-311-1.1.1/src/javax/ws/rs/core/CacheControl.java

#
Java | 370 lines | 160 code | 31 blank | 179 comment | 44 complexity | fb5200d96e78f93356d9a7fc401b2dad MD5 | raw file
  1. /*
  2. * The contents of this file are subject to the terms
  3. * of the Common Development and Distribution License
  4. * (the "License"). You may not use this file except
  5. * in compliance with the License.
  6. *
  7. * You can obtain a copy of the license at
  8. * http://www.opensource.org/licenses/cddl1.php
  9. * See the License for the specific language governing
  10. * permissions and limitations under the License.
  11. */
  12. /*
  13. * CacheControl.java
  14. *
  15. * Created on March 5, 2007, 3:36 PM
  16. */
  17. package javax.ws.rs.core;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import javax.ws.rs.ext.RuntimeDelegate;
  23. import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
  24. /**
  25. * An abstraction for the value of a HTTP Cache-Control response header.
  26. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">HTTP/1.1 section 14.9</a>
  27. */
  28. public class CacheControl {
  29. private boolean _private;
  30. private List<String> privateFields;
  31. private boolean noCache;
  32. private List<String> noCacheFields;
  33. private boolean noStore;
  34. private boolean noTransform;
  35. private boolean mustRevalidate;
  36. private boolean proxyRevalidate;
  37. private int maxAge = -1;
  38. private int sMaxAge = -1;
  39. private Map<String, String> cacheExtension;
  40. private static final HeaderDelegate<CacheControl> delegate =
  41. RuntimeDelegate.getInstance().createHeaderDelegate(CacheControl.class);
  42. /**
  43. * Create a new instance of CacheControl. The new instance will have the
  44. * following default settings:
  45. *
  46. * <ul>
  47. * <li>private = false</li>
  48. * <li>noCache = false</li>
  49. * <li>noStore = false</li>
  50. * <li>noTransform = true</li>
  51. * <li>mustRevalidate = false</li>
  52. * <li>proxyRevalidate = false</li>
  53. * <li>An empty list of private fields</li>
  54. * <li>An empty list of no-cache fields</li>
  55. * <li>An empty map of cache extensions</li>
  56. * </ul>
  57. */
  58. public CacheControl() {
  59. _private = false;
  60. noCache = false;
  61. noStore = false;
  62. noTransform = true;
  63. mustRevalidate = false;
  64. proxyRevalidate = false;
  65. }
  66. /**
  67. * Creates a new instance of CacheControl by parsing the supplied string.
  68. * @param value the cache control string
  69. * @return the newly created CacheControl
  70. * @throws IllegalArgumentException if the supplied string cannot be parsed
  71. * or is null
  72. */
  73. public static CacheControl valueOf(String value) throws IllegalArgumentException {
  74. return delegate.fromString(value);
  75. }
  76. /**
  77. * Corresponds to the must-revalidate cache control directive.
  78. * @return true if the must-revalidate cache control directive will be included in the
  79. * response, false otherwise.
  80. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
  81. */
  82. public boolean isMustRevalidate() {
  83. return mustRevalidate;
  84. }
  85. /**
  86. * Corresponds to the must-revalidate cache control directive.
  87. * @param mustRevalidate true if the must-revalidate cache control directive should be included in the
  88. * response, false otherwise.
  89. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
  90. */
  91. public void setMustRevalidate(boolean mustRevalidate) {
  92. this.mustRevalidate = mustRevalidate;
  93. }
  94. /**
  95. * Corresponds to the proxy-revalidate cache control directive.
  96. * @return true if the proxy-revalidate cache control directive will be included in the
  97. * response, false otherwise.
  98. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
  99. */
  100. public boolean isProxyRevalidate() {
  101. return proxyRevalidate;
  102. }
  103. /**
  104. * Corresponds to the must-revalidate cache control directive.
  105. * @param proxyRevalidate true if the proxy-revalidate cache control directive should be included in the
  106. * response, false otherwise.
  107. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
  108. */
  109. public void setProxyRevalidate(boolean proxyRevalidate) {
  110. this.proxyRevalidate = proxyRevalidate;
  111. }
  112. /**
  113. * Corresponds to the max-age cache control directive.
  114. * @return the value of the max-age cache control directive, -1 if the directive is disabled.
  115. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
  116. */
  117. public int getMaxAge() {
  118. return maxAge;
  119. }
  120. /**
  121. * Corresponds to the max-age cache control directive.
  122. * @param maxAge the value of the max-age cache control directive, a value of -1 will disable the directive.
  123. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
  124. */
  125. public void setMaxAge(int maxAge) {
  126. this.maxAge = maxAge;
  127. }
  128. /**
  129. * Corresponds to the s-maxage cache control directive.
  130. * @return the value of the s-maxage cache control directive, -1 if the directive is disabled.
  131. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
  132. */
  133. public int getSMaxAge() {
  134. return sMaxAge;
  135. }
  136. /**
  137. * Corresponds to the s-maxage cache control directive.
  138. * @param sMaxAge the value of the s-maxage cache control directive, a value of -1 will disable the directive.
  139. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
  140. */
  141. public void setSMaxAge(int sMaxAge) {
  142. this.sMaxAge = sMaxAge;
  143. }
  144. /**
  145. * Corresponds to the value of the no-cache cache control directive.
  146. * @return a mutable list of field-names that will form the value of the no-cache cache control directive.
  147. * An empty list results in a bare no-cache directive.
  148. * @see #isNoCache
  149. * @see #setNoCache
  150. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  151. */
  152. public List<String> getNoCacheFields() {
  153. if (noCacheFields == null)
  154. noCacheFields = new ArrayList<String>();
  155. return noCacheFields;
  156. }
  157. /**
  158. * Corresponds to the no-cache cache control directive.
  159. * @param noCache true if the no-cache cache control directive should be included in the
  160. * response, false otherwise.
  161. * @see #getNoCacheFields
  162. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  163. */
  164. public void setNoCache(boolean noCache) {
  165. this.noCache = noCache;
  166. }
  167. /**
  168. * Corresponds to the no-cache cache control directive.
  169. * @return true if the no-cache cache control directive will be included in the
  170. * response, false otherwise.
  171. * @see #getNoCacheFields
  172. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  173. */
  174. public boolean isNoCache() {
  175. return noCache;
  176. }
  177. /**
  178. * Corresponds to the private cache control directive.
  179. * @return true if the private cache control directive will be included in the
  180. * response, false otherwise.
  181. * @see #getPrivateFields
  182. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  183. */
  184. public boolean isPrivate() {
  185. return _private;
  186. }
  187. /**
  188. * Corresponds to the value of the private cache control directive.
  189. * @return a mutable list of field-names that will form the value of the private cache control directive.
  190. * An empty list results in a bare no-cache directive.
  191. * @see #isPrivate
  192. * @see #setPrivate
  193. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  194. */
  195. public List<String> getPrivateFields() {
  196. if (privateFields == null)
  197. privateFields = new ArrayList<String>();
  198. return privateFields;
  199. }
  200. /**
  201. * Corresponds to the private cache control directive.
  202. * @param _private true if the private cache control directive should be included in the
  203. * response, false otherwise.
  204. * @see #getPrivateFields
  205. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
  206. */
  207. public void setPrivate(boolean _private) {
  208. this._private = _private;
  209. }
  210. /**
  211. * Corresponds to the no-transform cache control directive.
  212. * @return true if the no-transform cache control directive will be included in the
  213. * response, false otherwise.
  214. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.5">HTTP/1.1 section 14.9.5</a>
  215. */
  216. public boolean isNoTransform() {
  217. return noTransform;
  218. }
  219. /**
  220. * Corresponds to the no-transform cache control directive.
  221. * @param noTransform true if the no-transform cache control directive should be included in the
  222. * response, false otherwise.
  223. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.5">HTTP/1.1 section 14.9.5</a>
  224. */
  225. public void setNoTransform(boolean noTransform) {
  226. this.noTransform = noTransform;
  227. }
  228. /**
  229. * Corresponds to the no-store cache control directive.
  230. * @return true if the no-store cache control directive will be included in the
  231. * response, false otherwise.
  232. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2">HTTP/1.1 section 14.9.2</a>
  233. */
  234. public boolean isNoStore() {
  235. return noStore;
  236. }
  237. /**
  238. * Corresponds to the no-store cache control directive.
  239. * @param noStore true if the no-store cache control directive should be included in the
  240. * response, false otherwise.
  241. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2">HTTP/1.1 section 14.9.2</a>
  242. */
  243. public void setNoStore(boolean noStore) {
  244. this.noStore = noStore;
  245. }
  246. /**
  247. * Corresponds to a set of extension cache control directives.
  248. * @return a mutable map of cache control extension names and their values.
  249. * If a key has a null value, it will appear as a bare directive. If a key has
  250. * a value that contains no whitespace then the directive will appear as
  251. * a simple name=value pair. If a key has a value that contains whitespace
  252. * then the directive will appear as a quoted name="value" pair.
  253. * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.6">HTTP/1.1 section 14.9.6</a>
  254. */
  255. public Map<String, String> getCacheExtension() {
  256. if (cacheExtension == null)
  257. cacheExtension = new HashMap<String, String>();
  258. return cacheExtension;
  259. }
  260. /**
  261. * Convert the cache control to a string suitable for use as the value of the
  262. * corresponding HTTP header.
  263. * @return a stringified cache control
  264. */
  265. @Override
  266. public String toString() {
  267. return delegate.toString(this);
  268. }
  269. /**
  270. * Generate hash code from cache control properties.
  271. * @return the hashCode
  272. */
  273. @Override
  274. public int hashCode() {
  275. int hash = 7;
  276. hash = 41 * hash + (this._private ? 1 : 0);
  277. hash = 41 * hash + (this.privateFields != null ? this.privateFields.hashCode() : 0);
  278. hash = 41 * hash + (this.noCache ? 1 : 0);
  279. hash = 41 * hash + (this.noCacheFields != null ? this.noCacheFields.hashCode() : 0);
  280. hash = 41 * hash + (this.noStore ? 1 : 0);
  281. hash = 41 * hash + (this.noTransform ? 1 : 0);
  282. hash = 41 * hash + (this.mustRevalidate ? 1 : 0);
  283. hash = 41 * hash + (this.proxyRevalidate ? 1 : 0);
  284. hash = 41 * hash + this.maxAge;
  285. hash = 41 * hash + this.sMaxAge;
  286. hash = 41 * hash + (this.cacheExtension != null ? this.cacheExtension.hashCode() : 0);
  287. return hash;
  288. }
  289. /**
  290. * Compares obj to this cache control to see if they are the same
  291. * considering all property values.
  292. * @param obj the object to compare to
  293. * @return true if the two cache controls are the same, false otherwise.
  294. */
  295. @Override
  296. public boolean equals(Object obj) {
  297. if (obj == null) {
  298. return false;
  299. }
  300. if (getClass() != obj.getClass()) {
  301. return false;
  302. }
  303. final CacheControl other = (CacheControl) obj;
  304. if (this._private != other._private) {
  305. return false;
  306. }
  307. if (this.privateFields != other.privateFields && (this.privateFields == null || !this.privateFields.equals(other.privateFields))) {
  308. return false;
  309. }
  310. if (this.noCache != other.noCache) {
  311. return false;
  312. }
  313. if (this.noCacheFields != other.noCacheFields && (this.noCacheFields == null || !this.noCacheFields.equals(other.noCacheFields))) {
  314. return false;
  315. }
  316. if (this.noStore != other.noStore) {
  317. return false;
  318. }
  319. if (this.noTransform != other.noTransform) {
  320. return false;
  321. }
  322. if (this.mustRevalidate != other.mustRevalidate) {
  323. return false;
  324. }
  325. if (this.proxyRevalidate != other.proxyRevalidate) {
  326. return false;
  327. }
  328. if (this.maxAge != other.maxAge) {
  329. return false;
  330. }
  331. if (this.sMaxAge != other.sMaxAge) {
  332. return false;
  333. }
  334. if (this.cacheExtension != other.cacheExtension && (this.cacheExtension == null || !this.cacheExtension.equals(other.cacheExtension))) {
  335. return false;
  336. }
  337. return true;
  338. }
  339. }