/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet/org/restlet/data/CookieSetting.java

https://bitbucket.org/haris_peco/debrief · Java · 318 lines · 90 code · 27 blank · 201 comment · 12 complexity · 92a144305810958068934ad8bb5fe764 MD5 · raw file

  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.data;
  31. import org.restlet.Response;
  32. import org.restlet.engine.util.SystemUtils;
  33. /**
  34. * Cookie setting provided by a server. This allows a server side application to
  35. * add, modify or remove a cookie on the client.<br>
  36. * <br>
  37. * Note that when used with HTTP connectors, this class maps to the "Set-Cookie"
  38. * and "Set-Cookie2" headers.
  39. *
  40. * @see Response#getCookieSettings()
  41. * @author Jerome Louvel
  42. */
  43. public final class CookieSetting extends Cookie {
  44. /**
  45. * Indicates whether to restrict cookie access to untrusted parties.
  46. * Currently this toggles the non-standard but widely supported HttpOnly
  47. * cookie parameter.
  48. */
  49. private volatile boolean accessRestricted;
  50. /** The user's comment. */
  51. private volatile String comment;
  52. /**
  53. * The maximum age in seconds. Use 0 to discard an existing cookie.
  54. */
  55. private volatile int maxAge;
  56. /** Indicates if cookie should only be transmitted by secure means. */
  57. private volatile boolean secure;
  58. /**
  59. * Default constructor.
  60. */
  61. public CookieSetting() {
  62. this(0, null, null);
  63. }
  64. /**
  65. * Constructor.
  66. *
  67. * @param version
  68. * The cookie's version.
  69. * @param name
  70. * The cookie's name.
  71. * @param value
  72. * The cookie's value.
  73. */
  74. public CookieSetting(int version, String name, String value) {
  75. this(version, name, value, null, null);
  76. }
  77. /**
  78. * Constructor.
  79. *
  80. * @param version
  81. * The cookie's version.
  82. * @param name
  83. * The cookie's name.
  84. * @param value
  85. * The cookie's value.
  86. * @param path
  87. * The cookie's path.
  88. * @param domain
  89. * The cookie's domain name.
  90. */
  91. public CookieSetting(int version, String name, String value, String path,
  92. String domain) {
  93. this(version, name, value, path, domain, null, -1, false, false);
  94. }
  95. /**
  96. * Constructor.
  97. *
  98. * @param version
  99. * The cookie's version.
  100. * @param name
  101. * The cookie's name.
  102. * @param value
  103. * The cookie's value.
  104. * @param path
  105. * The cookie's path.
  106. * @param domain
  107. * The cookie's domain name.
  108. * @param comment
  109. * The cookie's comment.
  110. * @param maxAge
  111. * Sets the maximum age in seconds.<br>
  112. * Use 0 to immediately discard an existing cookie.<br>
  113. * Use -1 to discard the cookie at the end of the session
  114. * (default).
  115. * @param secure
  116. * Indicates if cookie should only be transmitted by secure
  117. * means.
  118. */
  119. public CookieSetting(int version, String name, String value, String path,
  120. String domain, String comment, int maxAge, boolean secure) {
  121. this(version, name, value, path, domain, comment, maxAge, secure, false);
  122. }
  123. /**
  124. * Constructor.
  125. *
  126. * @param version
  127. * The cookie's version.
  128. * @param name
  129. * The cookie's name.
  130. * @param value
  131. * The cookie's value.
  132. * @param path
  133. * The cookie's path.
  134. * @param domain
  135. * The cookie's domain name.
  136. * @param comment
  137. * The cookie's comment.
  138. * @param maxAge
  139. * Sets the maximum age in seconds.<br>
  140. * Use 0 to immediately discard an existing cookie.<br>
  141. * Use -1 to discard the cookie at the end of the session
  142. * (default).
  143. * @param secure
  144. * Indicates if cookie should only be transmitted by secure
  145. * means.
  146. * @param accessRestricted
  147. * Indicates whether to restrict cookie access to untrusted
  148. * parties. Currently this toggles the non-standard but widely
  149. * supported HttpOnly cookie parameter.
  150. */
  151. public CookieSetting(int version, String name, String value, String path,
  152. String domain, String comment, int maxAge, boolean secure,
  153. boolean accessRestricted) {
  154. super(version, name, value, path, domain);
  155. this.comment = comment;
  156. this.maxAge = maxAge;
  157. this.secure = secure;
  158. this.accessRestricted = accessRestricted;
  159. }
  160. /**
  161. * Preferred constructor.
  162. *
  163. * @param name
  164. * The cookie's name.
  165. * @param value
  166. * The cookie's value.
  167. */
  168. public CookieSetting(String name, String value) {
  169. this(0, name, value, null, null);
  170. }
  171. /** {@inheritDoc} */
  172. @Override
  173. public boolean equals(Object obj) {
  174. boolean result = (obj == this);
  175. // if obj == this no need to go further
  176. if (!result) {
  177. // test for equality at Cookie level i.e. name and value.
  178. if (super.equals(obj)) {
  179. // if obj isn't a cookie setting or is null don't evaluate
  180. // further
  181. if (obj instanceof CookieSetting) {
  182. final CookieSetting that = (CookieSetting) obj;
  183. result = (this.maxAge == that.maxAge)
  184. && (this.secure == that.secure);
  185. if (result) // if "maxAge" and "secure" properties are equal
  186. // test comments
  187. {
  188. if (!(this.comment == null)) // compare comments
  189. // taking care of nulls
  190. {
  191. result = (this.comment.equals(that.comment));
  192. } else {
  193. result = (that.comment == null);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. return result;
  200. }
  201. /**
  202. * Returns the comment for the user.
  203. *
  204. * @return The comment for the user.
  205. */
  206. public String getComment() {
  207. return this.comment;
  208. }
  209. /**
  210. * Returns the description of this REST element.
  211. *
  212. * @return The description of this REST element.
  213. */
  214. public String getDescription() {
  215. return "Cookie setting";
  216. }
  217. /**
  218. * Returns the maximum age in seconds.<br>
  219. * Use 0 to immediately discard an existing cookie.<br>
  220. * Use -1 to discard the cookie at the end of the session (default).
  221. *
  222. * @return The maximum age in seconds.
  223. */
  224. public int getMaxAge() {
  225. return this.maxAge;
  226. }
  227. /** {@inheritDoc} */
  228. @Override
  229. public int hashCode() {
  230. return SystemUtils.hashCode(super.hashCode(), getComment(),
  231. getMaxAge(), isSecure());
  232. }
  233. /**
  234. * Indicates if cookie access is restricted for untrusted parties. Currently
  235. * this toggles the non-standard but widely supported HttpOnly cookie
  236. * parameter.
  237. *
  238. * @return accessRestricted True if cookie access should be restricted
  239. */
  240. public boolean isAccessRestricted() {
  241. return this.accessRestricted;
  242. }
  243. /**
  244. * Indicates if cookie should only be transmitted by secure means.
  245. *
  246. * @return True if cookie should only be transmitted by secure means.
  247. */
  248. public boolean isSecure() {
  249. return this.secure;
  250. }
  251. /**
  252. * Indicates whether to restrict cookie access to untrusted parties.
  253. * Currently this toggles the non-standard but widely supported HttpOnly
  254. * cookie parameter.
  255. *
  256. * @param accessRestricted
  257. * True if cookie access should be restricted
  258. */
  259. public void setAccessRestricted(boolean accessRestricted) {
  260. this.accessRestricted = accessRestricted;
  261. }
  262. /**
  263. * Sets the comment for the user.
  264. *
  265. * @param comment
  266. * The comment for the user.
  267. */
  268. public void setComment(String comment) {
  269. this.comment = comment;
  270. }
  271. /**
  272. * Sets the maximum age in seconds.<br>
  273. * Use 0 to immediately discard an existing cookie.<br>
  274. * Use -1 to discard the cookie at the end of the session (default).
  275. *
  276. * @param maxAge
  277. * The maximum age in seconds.
  278. */
  279. public void setMaxAge(int maxAge) {
  280. this.maxAge = maxAge;
  281. }
  282. /**
  283. * Indicates if cookie should only be transmitted by secure means.
  284. *
  285. * @param secure
  286. * True if cookie should only be transmitted by secure means.
  287. */
  288. public void setSecure(boolean secure) {
  289. this.secure = secure;
  290. }
  291. }