1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpec.java,v 1.11 2004/09/14 20:11:31 olegk Exp $
  3. * $Revision: 1.11 $
  4. * $Date: 2004/09/14 20:11:31 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.cookie;
  30. import java.util.Collection;
  31. import org.apache.commons.httpclient.Header;
  32. import org.apache.commons.httpclient.NameValuePair;
  33. import org.apache.commons.httpclient.Cookie;
  34. /**
  35. * Defines the cookie management specification.
  36. * <p>Cookie management specification must define
  37. * <ul>
  38. * <li> rules of parsing "Set-Cookie" header
  39. * <li> rules of validation of parsed cookies
  40. * <li> formatting of "Cookie" header
  41. * </ul>
  42. * for a given host, port and path of origin
  43. *
  44. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  45. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  46. *
  47. * @since 2.0
  48. */
  49. public interface CookieSpec {
  50. /** Path delimiter */
  51. static final String PATH_DELIM = "/";
  52. /** Path delimiting charachter */
  53. static final char PATH_DELIM_CHAR = PATH_DELIM.charAt(0);
  54. /**
  55. * Parse the <tt>"Set-Cookie"</tt> header value into Cookie array.
  56. *
  57. * @param host the host which sent the <tt>Set-Cookie</tt> header
  58. * @param port the port which sent the <tt>Set-Cookie</tt> header
  59. * @param path the path which sent the <tt>Set-Cookie</tt> header
  60. * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
  61. * was received over secure conection
  62. * @param header the <tt>Set-Cookie</tt> received from the server
  63. * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
  64. * @throws MalformedCookieException if an exception occurs during parsing
  65. * @throws IllegalArgumentException if an input parameter is illegal
  66. */
  67. Cookie[] parse(String host, int port, String path, boolean secure,
  68. final String header)
  69. throws MalformedCookieException, IllegalArgumentException;
  70. /**
  71. * Parse the <tt>"Set-Cookie"</tt> Header into an array of Cookies.
  72. *
  73. * @param host the host which sent the <tt>Set-Cookie</tt> header
  74. * @param port the port which sent the <tt>Set-Cookie</tt> header
  75. * @param path the path which sent the <tt>Set-Cookie</tt> header
  76. * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
  77. * was received over secure conection
  78. * @param header the <tt>Set-Cookie</tt> received from the server
  79. * @return an array of <tt>Cookie</tt>s parsed from the header
  80. * @throws MalformedCookieException if an exception occurs during parsing
  81. * @throws IllegalArgumentException if an input parameter is illegal
  82. */
  83. Cookie[] parse(String host, int port, String path, boolean secure,
  84. final Header header)
  85. throws MalformedCookieException, IllegalArgumentException;
  86. /**
  87. * Parse the cookie attribute and update the corresponsing Cookie
  88. * properties.
  89. *
  90. * @param attribute cookie attribute from the <tt>Set-Cookie</tt>
  91. * @param cookie the to be updated
  92. * @throws MalformedCookieException if an exception occurs during parsing
  93. * @throws IllegalArgumentException if an input parameter is illegal
  94. */
  95. void parseAttribute(NameValuePair attribute, Cookie cookie)
  96. throws MalformedCookieException, IllegalArgumentException;
  97. /**
  98. * Validate the cookie according to validation rules defined by the
  99. * cookie specification.
  100. *
  101. * @param host the host from which the {@link Cookie} was received
  102. * @param port the port from which the {@link Cookie} was received
  103. * @param path the path from which the {@link Cookie} was received
  104. * @param secure <tt>true</tt> when the {@link Cookie} was received
  105. * using a secure connection
  106. * @param cookie the Cookie to validate
  107. * @throws MalformedCookieException if the cookie is invalid
  108. * @throws IllegalArgumentException if an input parameter is illegal
  109. */
  110. void validate(String host, int port, String path, boolean secure,
  111. final Cookie cookie)
  112. throws MalformedCookieException, IllegalArgumentException;
  113. /**
  114. * Sets the {@link Collection} of date patterns used for parsing. The String patterns must be
  115. * compatible with {@link java.text.SimpleDateFormat}.
  116. *
  117. * @param datepatterns collection of date patterns
  118. */
  119. void setValidDateFormats(Collection datepatterns);
  120. /**
  121. * Returns the {@link Collection} of date patterns used for parsing. The String patterns are compatible
  122. * with the {@link java.text.SimpleDateFormat}.
  123. *
  124. * @return collection of date patterns
  125. */
  126. Collection getValidDateFormats();
  127. /**
  128. * Determines if a Cookie matches a location.
  129. *
  130. * @param host the host to which the request is being submitted
  131. * @param port the port to which the request is being submitted
  132. * @param path the path to which the request is being submitted
  133. * @param secure <tt>true</tt> if the request is using a secure connection
  134. * @param cookie the Cookie to be matched
  135. *
  136. * @return <tt>true</tt> if the cookie should be submitted with a request
  137. * with given attributes, <tt>false</tt> otherwise.
  138. */
  139. boolean match(String host, int port, String path, boolean secure,
  140. final Cookie cookie);
  141. /**
  142. * Determines which of an array of Cookies matches a location.
  143. *
  144. * @param host the host to which the request is being submitted
  145. * @param port the port to which the request is being submitted
  146. * (currenlty ignored)
  147. * @param path the path to which the request is being submitted
  148. * @param secure <tt>true</tt> if the request is using a secure protocol
  149. * @param cookies an array of <tt>Cookie</tt>s to be matched
  150. *
  151. * @return <tt>true</tt> if the cookie should be submitted with a request
  152. * with given attributes, <tt>false</tt> otherwise.
  153. */
  154. Cookie[] match(String host, int port, String path, boolean secure,
  155. final Cookie cookies[]);
  156. /**
  157. * Performs domain-match as defined by the cookie specification.
  158. * @param host The target host.
  159. * @param domain The cookie domain attribute.
  160. * @return true if the specified host matches the given domain.
  161. *
  162. * @since 3.0
  163. */
  164. boolean domainMatch(String host, String domain);
  165. /**
  166. * Performs path-match as defined by the cookie specification.
  167. * @param path The target path.
  168. * @param topmostPath The cookie path attribute.
  169. * @return true if the paths match
  170. *
  171. * @since 3.0
  172. */
  173. boolean pathMatch(String path, String topmostPath);
  174. /**
  175. * Create a <tt>"Cookie"</tt> header value for an array of cookies.
  176. *
  177. * @param cookie the cookie to be formatted as string
  178. * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
  179. */
  180. String formatCookie(Cookie cookie);
  181. /**
  182. * Create a <tt>"Cookie"</tt> header value for an array of cookies.
  183. *
  184. * @param cookies the Cookies to be formatted
  185. * @return a string suitable for sending in a Cookie header.
  186. * @throws IllegalArgumentException if an input parameter is illegal
  187. */
  188. String formatCookies(Cookie[] cookies) throws IllegalArgumentException;
  189. /**
  190. * Create a <tt>"Cookie"</tt> Header for an array of Cookies.
  191. *
  192. * @param cookies the Cookies format into a Cookie header
  193. * @return a Header for the given Cookies.
  194. * @throws IllegalArgumentException if an input parameter is illegal
  195. */
  196. Header formatCookieHeader(Cookie[] cookies) throws IllegalArgumentException;
  197. /**
  198. * Create a <tt>"Cookie"</tt> Header for single Cookie.
  199. *
  200. * @param cookie the Cookie format as a <tt>Cookie</tt> header
  201. * @return a Cookie header.
  202. * @throws IllegalArgumentException if an input parameter is illegal
  203. */
  204. Header formatCookieHeader(Cookie cookie) throws IllegalArgumentException;
  205. }