SVN copied from Commons HttpClient with some old (and unusable) bits of code removed
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@404838 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
63f3ec6457
commit
6d220e3263
|
@ -0,0 +1,455 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/Cookie.java,v 1.44 2004/06/05 16:49:20 olegk Exp $
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 1999-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.cookie;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.util.LangUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* HTTP "magic-cookie" represents a piece of state information
|
||||
* that the HTTP agent and the target server can exchange to maintain
|
||||
* a session.
|
||||
* </p>
|
||||
*
|
||||
* @author B.C. Holmes
|
||||
* @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
|
||||
* @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
|
||||
* @author Rod Waldhoff
|
||||
* @author dIon Gillard
|
||||
* @author Sean C. Sullivan
|
||||
* @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
|
||||
* @author Marc A. Saegesser
|
||||
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
|
||||
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Cookie extends NameValuePair {
|
||||
|
||||
// ----------------------------------------------------------- Constructors
|
||||
|
||||
/**
|
||||
* Default constructor. Creates a blank cookie
|
||||
*/
|
||||
|
||||
public Cookie() {
|
||||
this(null, "noname", null, null, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a cookie with the given name, value and domain attribute.
|
||||
*
|
||||
* @param name the cookie name
|
||||
* @param value the cookie value
|
||||
* @param domain the domain this cookie can be sent to
|
||||
*/
|
||||
public Cookie(String domain, String name, String value) {
|
||||
this(domain, name, value, null, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a cookie with the given name, value, domain attribute,
|
||||
* path attribute, expiration attribute, and secure attribute
|
||||
*
|
||||
* @param name the cookie name
|
||||
* @param value the cookie value
|
||||
* @param domain the domain this cookie can be sent to
|
||||
* @param path the path prefix for which this cookie can be sent
|
||||
* @param expires the {@link Date} at which this cookie expires,
|
||||
* or <tt>null</tt> if the cookie expires at the end
|
||||
* of the session
|
||||
* @param secure if true this cookie can only be sent over secure
|
||||
* connections
|
||||
* @throws IllegalArgumentException If cookie name is null or blank,
|
||||
* cookie name contains a blank, or cookie name starts with character $
|
||||
*
|
||||
*/
|
||||
public Cookie(String domain, String name, String value,
|
||||
String path, Date expires, boolean secure) {
|
||||
|
||||
super(name, value);
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("Cookie name may not be null");
|
||||
}
|
||||
if (name.trim().equals("")) {
|
||||
throw new IllegalArgumentException("Cookie name may not be blank");
|
||||
}
|
||||
this.setPath(path);
|
||||
this.setDomain(domain);
|
||||
this.setExpiryDate(expires);
|
||||
this.setSecure(secure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a cookie with the given name, value, domain attribute,
|
||||
* path attribute, maximum age attribute, and secure attribute
|
||||
*
|
||||
* @param name the cookie name
|
||||
* @param value the cookie value
|
||||
* @param domain the domain this cookie can be sent to
|
||||
* @param path the path prefix for which this cookie can be sent
|
||||
* @param maxAge the number of seconds for which this cookie is valid.
|
||||
* maxAge is expected to be a non-negative number.
|
||||
* <tt>-1</tt> signifies that the cookie should never expire.
|
||||
* @param secure if <tt>true</tt> this cookie can only be sent over secure
|
||||
* connections
|
||||
*/
|
||||
public Cookie(String domain, String name, String value, String path,
|
||||
int maxAge, boolean secure) {
|
||||
|
||||
this(domain, name, value, path, null, secure);
|
||||
if (maxAge < -1) {
|
||||
throw new IllegalArgumentException("Invalid max age: " + Integer.toString(maxAge));
|
||||
}
|
||||
if (maxAge >= 0) {
|
||||
setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the comment describing the purpose of this cookie, or
|
||||
* <tt>null</tt> if no such comment has been defined.
|
||||
*
|
||||
* @return comment
|
||||
*
|
||||
* @see #setComment(String)
|
||||
*/
|
||||
public String getComment() {
|
||||
return cookieComment;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a user agent (web browser) presents this cookie to a user, the
|
||||
* cookie's purpose will be described using this comment.
|
||||
*
|
||||
* @param comment
|
||||
*
|
||||
* @see #getComment()
|
||||
*/
|
||||
public void setComment(String comment) {
|
||||
cookieComment = comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the expiration {@link Date} of the cookie, or <tt>null</tt>
|
||||
* if none exists.
|
||||
* <p><strong>Note:</strong> the object returned by this method is
|
||||
* considered immutable. Changing it (e.g. using setTime()) could result
|
||||
* in undefined behaviour. Do so at your peril. </p>
|
||||
* @return Expiration {@link Date}, or <tt>null</tt>.
|
||||
*
|
||||
* @see #setExpiryDate(java.util.Date)
|
||||
*
|
||||
*/
|
||||
public Date getExpiryDate() {
|
||||
return cookieExpiryDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets expiration date.
|
||||
* <p><strong>Note:</strong> the object returned by this method is considered
|
||||
* immutable. Changing it (e.g. using setTime()) could result in undefined
|
||||
* behaviour. Do so at your peril.</p>
|
||||
*
|
||||
* @param expiryDate the {@link Date} after which this cookie is no longer valid.
|
||||
*
|
||||
* @see #getExpiryDate
|
||||
*
|
||||
*/
|
||||
public void setExpiryDate (Date expiryDate) {
|
||||
cookieExpiryDate = expiryDate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns <tt>false</tt> if the cookie should be discarded at the end
|
||||
* of the "session"; <tt>true</tt> otherwise.
|
||||
*
|
||||
* @return <tt>false</tt> if the cookie should be discarded at the end
|
||||
* of the "session"; <tt>true</tt> otherwise
|
||||
*/
|
||||
public boolean isPersistent() {
|
||||
return (null != cookieExpiryDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns domain attribute of the cookie.
|
||||
*
|
||||
* @return the value of the domain attribute
|
||||
*
|
||||
* @see #setDomain(java.lang.String)
|
||||
*/
|
||||
public String getDomain() {
|
||||
return cookieDomain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the domain attribute.
|
||||
*
|
||||
* @param domain The value of the domain attribute
|
||||
*
|
||||
* @see #getDomain
|
||||
*/
|
||||
public void setDomain(String domain) {
|
||||
if (domain != null) {
|
||||
int ndx = domain.indexOf(":");
|
||||
if (ndx != -1) {
|
||||
domain = domain.substring(0, ndx);
|
||||
}
|
||||
cookieDomain = domain.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the path attribute of the cookie
|
||||
*
|
||||
* @return The value of the path attribute.
|
||||
*
|
||||
* @see #setPath(java.lang.String)
|
||||
*/
|
||||
public String getPath() {
|
||||
return cookiePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path attribute.
|
||||
*
|
||||
* @param path The value of the path attribute
|
||||
*
|
||||
* @see #getPath
|
||||
*
|
||||
*/
|
||||
public void setPath(String path) {
|
||||
cookiePath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return <code>true</code> if this cookie should only be sent over secure connections.
|
||||
* @see #setSecure(boolean)
|
||||
*/
|
||||
public boolean getSecure() {
|
||||
return isSecure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the secure attribute of the cookie.
|
||||
* <p>
|
||||
* When <tt>true</tt> the cookie should only be sent
|
||||
* using a secure protocol (https). This should only be set when
|
||||
* the cookie's originating server used a secure protocol to set the
|
||||
* cookie's value.
|
||||
*
|
||||
* @param secure The value of the secure attribute
|
||||
*
|
||||
* @see #getSecure()
|
||||
*/
|
||||
public void setSecure (boolean secure) {
|
||||
isSecure = secure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version of the cookie specification to which this
|
||||
* cookie conforms.
|
||||
*
|
||||
* @return the version of the cookie.
|
||||
*
|
||||
* @see #setVersion(int)
|
||||
*
|
||||
*/
|
||||
public int getVersion() {
|
||||
return cookieVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the version of the cookie specification to which this
|
||||
* cookie conforms.
|
||||
*
|
||||
* @param version the version of the cookie.
|
||||
*
|
||||
* @see #getVersion
|
||||
*/
|
||||
public void setVersion(int version) {
|
||||
cookieVersion = version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this cookie has expired.
|
||||
*
|
||||
* @return <tt>true</tt> if the cookie has expired.
|
||||
*/
|
||||
public boolean isExpired() {
|
||||
return (cookieExpiryDate != null
|
||||
&& cookieExpiryDate.getTime() <= System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this cookie has expired according to the time passed in.
|
||||
*
|
||||
* @param now The current time.
|
||||
*
|
||||
* @return <tt>true</tt> if the cookie expired.
|
||||
*/
|
||||
public boolean isExpired(Date now) {
|
||||
return (cookieExpiryDate != null
|
||||
&& cookieExpiryDate.getTime() <= now.getTime());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Indicates whether the cookie had a path specified in a
|
||||
* path attribute of the <tt>Set-Cookie</tt> header. This value
|
||||
* is important for generating the <tt>Cookie</tt> header because
|
||||
* some cookie specifications require that the <tt>Cookie</tt> header
|
||||
* should only include a path attribute if the cookie's path
|
||||
* was specified in the <tt>Set-Cookie</tt> header.
|
||||
*
|
||||
* @param value <tt>true</tt> if the cookie's path was explicitly
|
||||
* set, <tt>false</tt> otherwise.
|
||||
*
|
||||
* @see #isPathAttributeSpecified
|
||||
*/
|
||||
public void setPathAttributeSpecified(boolean value) {
|
||||
hasPathAttribute = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if cookie's path was set via a path attribute
|
||||
* in the <tt>Set-Cookie</tt> header.
|
||||
*
|
||||
* @return value <tt>true</tt> if the cookie's path was explicitly
|
||||
* set, <tt>false</tt> otherwise.
|
||||
*
|
||||
* @see #setPathAttributeSpecified
|
||||
*/
|
||||
public boolean isPathAttributeSpecified() {
|
||||
return hasPathAttribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the cookie had a domain specified in a
|
||||
* domain attribute of the <tt>Set-Cookie</tt> header. This value
|
||||
* is important for generating the <tt>Cookie</tt> header because
|
||||
* some cookie specifications require that the <tt>Cookie</tt> header
|
||||
* should only include a domain attribute if the cookie's domain
|
||||
* was specified in the <tt>Set-Cookie</tt> header.
|
||||
*
|
||||
* @param value <tt>true</tt> if the cookie's domain was explicitly
|
||||
* set, <tt>false</tt> otherwise.
|
||||
*
|
||||
* @see #isDomainAttributeSpecified
|
||||
*/
|
||||
public void setDomainAttributeSpecified(boolean value) {
|
||||
hasDomainAttribute = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if cookie's domain was set via a domain
|
||||
* attribute in the <tt>Set-Cookie</tt> header.
|
||||
*
|
||||
* @return value <tt>true</tt> if the cookie's domain was explicitly
|
||||
* set, <tt>false</tt> otherwise.
|
||||
*
|
||||
* @see #setDomainAttributeSpecified
|
||||
*/
|
||||
public boolean isDomainAttributeSpecified() {
|
||||
return hasDomainAttribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hash code in keeping with the
|
||||
* {@link Object#hashCode} general hashCode contract.
|
||||
* @return A hash code
|
||||
*/
|
||||
public int hashCode() {
|
||||
int hash = LangUtils.HASH_SEED;
|
||||
hash = LangUtils.hashCode(hash, this.getName());
|
||||
hash = LangUtils.hashCode(hash, this.cookieDomain);
|
||||
hash = LangUtils.hashCode(hash, this.cookiePath);
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Two cookies are equal if the name, path and domain match.
|
||||
* @param obj The object to compare against.
|
||||
* @return true if the two objects are equal.
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
if (this == obj) return true;
|
||||
if (obj instanceof Cookie) {
|
||||
Cookie that = (Cookie) obj;
|
||||
return LangUtils.equals(this.getName(), that.getName())
|
||||
&& LangUtils.equals(this.cookieDomain, that.cookieDomain)
|
||||
&& LangUtils.equals(this.cookiePath, that.cookiePath);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- Instance Variables
|
||||
|
||||
/** Comment attribute. */
|
||||
private String cookieComment;
|
||||
|
||||
/** Domain attribute. */
|
||||
private String cookieDomain;
|
||||
|
||||
/** Expiration {@link Date}. */
|
||||
private Date cookieExpiryDate;
|
||||
|
||||
/** Path attribute. */
|
||||
private String cookiePath;
|
||||
|
||||
/** My secure flag. */
|
||||
private boolean isSecure;
|
||||
|
||||
/**
|
||||
* Specifies if the set-cookie header included a Path attribute for this
|
||||
* cookie
|
||||
*/
|
||||
private boolean hasPathAttribute = false;
|
||||
|
||||
/**
|
||||
* Specifies if the set-cookie header included a Domain attribute for this
|
||||
* cookie
|
||||
*/
|
||||
private boolean hasDomainAttribute = false;
|
||||
|
||||
/** The version of the cookie specification I was created from. */
|
||||
private int cookieVersion = 0;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 1999-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.cookie;
|
||||
|
||||
/**
|
||||
* Ths interface represents a cookie attribute handler responsible
|
||||
* for parsing, validating, matching and formatting a specific
|
||||
* cookie attribute, such as path, domain, port, etc.
|
||||
*
|
||||
* Different cookie specifications can provide a specific
|
||||
* implementation for this class based on their cookie handling
|
||||
* rules.
|
||||
*
|
||||
* @author jain.samit@gmail.com (Samit Jain)
|
||||
*/
|
||||
public interface CookieAttributeHandler {
|
||||
|
||||
/**
|
||||
* Parse the given cookie attribute value and update the corresponding
|
||||
* {@link org.apache.commons.httpclient.Cookie} property.
|
||||
*
|
||||
* @param cookie {@link org.apache.commons.httpclient.Cookie} to be updated
|
||||
* @param value cookie attribute value from the cookie response header
|
||||
*/
|
||||
void parse(Cookie cookie, String value)
|
||||
throws MalformedCookieException;
|
||||
|
||||
/**
|
||||
* Peforms cookie validation for the given attribute value.
|
||||
*
|
||||
* @param cookie {@link org.apache.commons.httpclient.Cookie} to validate
|
||||
* @param origin the cookie source to validate against
|
||||
* @throws MalformedCookieException if cookie validation fails for this attribute
|
||||
*/
|
||||
void validate(Cookie cookie, CookieOrigin origin)
|
||||
throws MalformedCookieException;
|
||||
|
||||
/**
|
||||
* Matches the given value (property of the destination host where request is being
|
||||
* submitted) with the corresponding cookie attribute.
|
||||
*
|
||||
* @param cookie {@link org.apache.commons.httpclient.Cookie} to match
|
||||
* @param origin the cookie source to match against
|
||||
* @return <tt>true</tt> if the match is successful; <tt>false</tt> otherwise
|
||||
*/
|
||||
boolean match(Cookie cookie, CookieOrigin origin);
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 1999-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.cookie;
|
||||
|
||||
public final class CookieOrigin {
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final String path;
|
||||
private final boolean secure;
|
||||
|
||||
public CookieOrigin(final String host, int port, final String path, boolean secure) {
|
||||
super();
|
||||
if (host == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Host of origin may not be null");
|
||||
}
|
||||
if (host.trim().equals("")) {
|
||||
throw new IllegalArgumentException(
|
||||
"Host of origin may not be blank");
|
||||
}
|
||||
if (port < 0) {
|
||||
throw new IllegalArgumentException("Invalid port: " + port);
|
||||
}
|
||||
if (path == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Path of origin may not be null.");
|
||||
}
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
this.secure = secure;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public boolean isSecure() {
|
||||
return this.secure;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 1999-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.cookie;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* This cookie comparator ensures that multiple cookies satisfying
|
||||
* a common criteria are ordered in the <tt>Cookie</tt> header such
|
||||
* that those with more specific Path attributes precede those with
|
||||
* less specific.
|
||||
*
|
||||
* <p>
|
||||
* This comparator assumes that Path attributes of two cookies
|
||||
* path-match a commmon request-URI. Otherwise, the result of the
|
||||
* comparison is undefined.
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
||||
*/
|
||||
public class CookiePathComparator implements Comparator {
|
||||
|
||||
private String normalizePath(final Cookie cookie) {
|
||||
String path = cookie.getPath();
|
||||
if (path == null) {
|
||||
path = "/";
|
||||
}
|
||||
if (!path.endsWith("/")) {
|
||||
path = path + "/";
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public int compare(final Object o1, final Object o2) {
|
||||
Cookie c1 = (Cookie) o1;
|
||||
Cookie c2 = (Cookie) o2;
|
||||
String path1 = normalizePath(c1);
|
||||
String path2 = normalizePath(c2);
|
||||
if (path1.equals(path2)) {
|
||||
return 0;
|
||||
} else if (path1.startsWith(path2)) {
|
||||
return -1;
|
||||
} else if (path2.startsWith(path1)) {
|
||||
return 1;
|
||||
} else {
|
||||
// Does not really matter
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,203 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 2002-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.cookie;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.http.Header;
|
||||
|
||||
/**
|
||||
* Defines the cookie management specification.
|
||||
* <p>Cookie management specification must define
|
||||
* <ul>
|
||||
* <li> rules of parsing "Set-Cookie" header
|
||||
* <li> rules of validation of parsed cookies
|
||||
* <li> formatting of "Cookie" header
|
||||
* </ul>
|
||||
* for a given host, port and path of origin
|
||||
*
|
||||
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
|
||||
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface CookieSpec {
|
||||
|
||||
/** Path delimiter */
|
||||
static final String PATH_DELIM = "/";
|
||||
|
||||
/** Path delimiting charachter */
|
||||
static final char PATH_DELIM_CHAR = PATH_DELIM.charAt(0);
|
||||
|
||||
/**
|
||||
* Parse the <tt>"Set-Cookie"</tt> header value into Cookie array.
|
||||
*
|
||||
* <p>This method will not perform the validation of the resultant
|
||||
* {@link Cookie}s</p>
|
||||
*
|
||||
* @see #validate(String, int, String, boolean, Cookie)
|
||||
*
|
||||
* @param host the host which sent the <tt>Set-Cookie</tt> header
|
||||
* @param port the port which sent the <tt>Set-Cookie</tt> header
|
||||
* @param path the path which sent the <tt>Set-Cookie</tt> header
|
||||
* @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
|
||||
* was received over secure conection
|
||||
* @param header the <tt>Set-Cookie</tt> received from the server
|
||||
* @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
|
||||
* @throws MalformedCookieException if an exception occurs during parsing
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
Cookie[] parse(String host, int port, String path, boolean secure,
|
||||
final String header)
|
||||
throws MalformedCookieException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Parse the <tt>"Set-Cookie"</tt> Header into an array of Cookies.
|
||||
*
|
||||
* <p>This method will not perform the validation of the resultant
|
||||
* {@link Cookie}s</p>
|
||||
*
|
||||
* @see #validate(String, int, String, boolean, Cookie)
|
||||
*
|
||||
* @param host the host which sent the <tt>Set-Cookie</tt> header
|
||||
* @param port the port which sent the <tt>Set-Cookie</tt> header
|
||||
* @param path the path which sent the <tt>Set-Cookie</tt> header
|
||||
* @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
|
||||
* was received over secure conection
|
||||
* @param header the <tt>Set-Cookie</tt> received from the server
|
||||
* @return an array of <tt>Cookie</tt>s parsed from the header
|
||||
* @throws MalformedCookieException if an exception occurs during parsing
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
Cookie[] parse(String host, int port, String path, boolean secure,
|
||||
final Header header)
|
||||
throws MalformedCookieException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Validate the cookie according to validation rules defined by the
|
||||
* cookie specification.
|
||||
*
|
||||
* @param host the host from which the {@link Cookie} was received
|
||||
* @param port the port from which the {@link Cookie} was received
|
||||
* @param path the path from which the {@link Cookie} was received
|
||||
* @param secure <tt>true</tt> when the {@link Cookie} was received
|
||||
* using a secure connection
|
||||
* @param cookie the Cookie to validate
|
||||
* @throws MalformedCookieException if the cookie is invalid
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
void validate(String host, int port, String path, boolean secure,
|
||||
final Cookie cookie)
|
||||
throws MalformedCookieException, IllegalArgumentException;
|
||||
|
||||
|
||||
/**
|
||||
* Sets the {@link Collection} of date patterns used for parsing. The String patterns must be
|
||||
* compatible with {@link java.text.SimpleDateFormat}.
|
||||
*
|
||||
* @param datepatterns collection of date patterns
|
||||
*/
|
||||
void setValidDateFormats(Collection datepatterns);
|
||||
|
||||
/**
|
||||
* Returns the {@link Collection} of date patterns used for parsing. The String patterns are compatible
|
||||
* with the {@link java.text.SimpleDateFormat}.
|
||||
*
|
||||
* @return collection of date patterns
|
||||
*/
|
||||
Collection getValidDateFormats();
|
||||
|
||||
/**
|
||||
* Determines if a Cookie matches a location.
|
||||
*
|
||||
* @param host the host to which the request is being submitted
|
||||
* @param port the port to which the request is being submitted
|
||||
* @param path the path to which the request is being submitted
|
||||
* @param secure <tt>true</tt> if the request is using a secure connection
|
||||
* @param cookie the Cookie to be matched
|
||||
*
|
||||
* @return <tt>true</tt> if the cookie should be submitted with a request
|
||||
* with given attributes, <tt>false</tt> otherwise.
|
||||
*/
|
||||
boolean match(String host, int port, String path, boolean secure,
|
||||
final Cookie cookie);
|
||||
|
||||
/**
|
||||
* Determines which of an array of Cookies matches a location.
|
||||
*
|
||||
* @param host the host to which the request is being submitted
|
||||
* @param port the port to which the request is being submitted
|
||||
* (currenlty ignored)
|
||||
* @param path the path to which the request is being submitted
|
||||
* @param secure <tt>true</tt> if the request is using a secure protocol
|
||||
* @param cookies an array of <tt>Cookie</tt>s to be matched
|
||||
*
|
||||
* @return <tt>true</tt> if the cookie should be submitted with a request
|
||||
* with given attributes, <tt>false</tt> otherwise.
|
||||
*/
|
||||
Cookie[] match(String host, int port, String path, boolean secure,
|
||||
final Cookie cookies[]);
|
||||
|
||||
/**
|
||||
* Create a <tt>"Cookie"</tt> header value for an array of cookies.
|
||||
*
|
||||
* @param cookie the cookie to be formatted as string
|
||||
* @return a string suitable for sending in a <tt>"Cookie"</tt> header.
|
||||
*/
|
||||
String formatCookie(Cookie cookie);
|
||||
|
||||
/**
|
||||
* Create a <tt>"Cookie"</tt> header value for an array of cookies.
|
||||
*
|
||||
* @param cookies the Cookies to be formatted
|
||||
* @return a string suitable for sending in a Cookie header.
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
String formatCookies(Cookie[] cookies) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Create a <tt>"Cookie"</tt> Header for an array of Cookies.
|
||||
*
|
||||
* @param cookies the Cookies format into a Cookie header
|
||||
* @return a Header for the given Cookies.
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
Header formatCookieHeader(Cookie[] cookies) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Create a <tt>"Cookie"</tt> Header for single Cookie.
|
||||
*
|
||||
* @param cookie the Cookie format as a <tt>Cookie</tt> header
|
||||
* @return a Cookie header.
|
||||
* @throws IllegalArgumentException if an input parameter is illegal
|
||||
*/
|
||||
Header formatCookieHeader(Cookie cookie) throws IllegalArgumentException;
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/cookie/MalformedCookieException.java,v 1.8 2004/05/13 04:02:00 mbecke Exp $
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Copyright 2002-2004 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.cookie;
|
||||
|
||||
import org.apache.http.ProtocolException;
|
||||
|
||||
/**
|
||||
* Signals that a cookie is in some way invalid or illegal in a given
|
||||
* context
|
||||
*
|
||||
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MalformedCookieException extends ProtocolException {
|
||||
|
||||
private static final long serialVersionUID = -6695462944287282185L;
|
||||
|
||||
/**
|
||||
* Creates a new MalformedCookieException with a <tt>null</tt> detail message.
|
||||
*/
|
||||
public MalformedCookieException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new MalformedCookieException with a specified message string.
|
||||
*
|
||||
* @param message The exception detail message
|
||||
*/
|
||||
public MalformedCookieException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new MalformedCookieException with the specified detail message and cause.
|
||||
*
|
||||
* @param message the exception detail message
|
||||
* @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
|
||||
* if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public MalformedCookieException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue