* Renamed HttpCookie to Cookie
* Split Cookie into an interface and its implementation (DefaultCookie) * Renamed HttpCookieEncoder/Decoder to CookieEncoderDecoder * Added all optional fields to Cookie * Made Cookie.value mutable * Revised DefaultCookie.toString
This commit is contained in:
parent
29b0af4f07
commit
b8bc78a7d0
@ -23,56 +23,23 @@ package org.jboss.netty.handler.codec.http;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public class HttpCookie implements Comparable<HttpCookie> {
|
public interface Cookie extends Comparable<Cookie> {
|
||||||
|
String getName();
|
||||||
// TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?)
|
String getValue();
|
||||||
private final String name;
|
void setValue(String value);
|
||||||
private final String value;
|
String getDomain();
|
||||||
|
void setDomain(String domain);
|
||||||
public HttpCookie(String name, String value) {
|
String getPath();
|
||||||
if (name == null) {
|
void setPath(String path);
|
||||||
throw new NullPointerException("name");
|
String getComment();
|
||||||
}
|
void setComment(String comment);
|
||||||
if (value == null) {
|
int getMaxAge();
|
||||||
throw new NullPointerException("value");
|
void setMaxAge(int maxAge);
|
||||||
}
|
int getVersion();
|
||||||
|
void setVersion(int version);
|
||||||
this.name = name;
|
boolean isSecure();
|
||||||
this.value = value;
|
void setSecure(boolean secure);
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return getName().hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (!(o instanceof HttpCookie)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return getName().equalsIgnoreCase(((HttpCookie) o).getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public int compareTo(HttpCookie c) {
|
|
||||||
return getName().compareToIgnoreCase(c.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return getName() + " = " + getValue();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -31,21 +31,21 @@ import org.jboss.netty.util.CaseIgnoringComparator;
|
|||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public class HttpCookieDecoder {
|
public class CookieDecoder {
|
||||||
|
|
||||||
// TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?)
|
// TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?)
|
||||||
private final static String semicolon = ";";
|
private final static String semicolon = ";";
|
||||||
private final static String equals = "=";
|
private final static String equals = "=";
|
||||||
|
|
||||||
public Map<String, HttpCookie> decode(String header) {
|
public Map<String, Cookie> decode(String header) {
|
||||||
Map<String, HttpCookie> cookies = new TreeMap<String, HttpCookie>(CaseIgnoringComparator.INSTANCE);
|
Map<String, Cookie> cookies = new TreeMap<String, Cookie>(CaseIgnoringComparator.INSTANCE);
|
||||||
String[] split = header.split(semicolon);
|
String[] split = header.split(semicolon);
|
||||||
for (String s : split) {
|
for (String s : split) {
|
||||||
String[] cookie = s.split(equals);
|
String[] cookie = s.split(equals);
|
||||||
if(cookie != null && cookie.length == 2) {
|
if(cookie != null && cookie.length == 2) {
|
||||||
String name = cookie[0].trim();
|
String name = cookie[0].trim();
|
||||||
String value = cookie[1].trim();
|
String value = cookie[1].trim();
|
||||||
cookies.put(name, new HttpCookie(name, value));
|
cookies.put(name, new DefaultCookie(name, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cookies;
|
return cookies;
|
@ -32,16 +32,16 @@ import org.jboss.netty.util.CaseIgnoringComparator;
|
|||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public class HttpCookieEncoder {
|
public class CookieEncoder {
|
||||||
|
|
||||||
// TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?)
|
// TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?)
|
||||||
private final Map<String, HttpCookie> cookies = new TreeMap<String, HttpCookie>(CaseIgnoringComparator.INSTANCE);
|
private final Map<String, Cookie> cookies = new TreeMap<String, Cookie>(CaseIgnoringComparator.INSTANCE);
|
||||||
|
|
||||||
public void addCookie(String name, String val) {
|
public void addCookie(String name, String val) {
|
||||||
cookies.put(name, new HttpCookie(name, val));
|
cookies.put(name, new DefaultCookie(name, val));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCookie(HttpCookie cookie) {
|
public void addCookie(Cookie cookie) {
|
||||||
cookies.put(cookie.getName(), cookie);
|
cookies.put(cookie.getName(), cookie);
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* JBoss, Home of Professional Open Source
|
||||||
|
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
|
||||||
|
* by the @authors tag. See the copyright.txt in the distribution for a
|
||||||
|
* full listing of individual contributors.
|
||||||
|
*
|
||||||
|
* This is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Lesser General Public License as
|
||||||
|
* published by the Free Software Foundation; either version 2.1 of
|
||||||
|
* the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This software is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this software; if not, write to the Free
|
||||||
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||||
|
*/
|
||||||
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
|
* @version $Rev$, $Date$
|
||||||
|
*/
|
||||||
|
public class DefaultCookie implements Cookie {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private String value;
|
||||||
|
private String domain;
|
||||||
|
private String path;
|
||||||
|
private String comment;
|
||||||
|
private int maxAge;
|
||||||
|
private int version;
|
||||||
|
private boolean secure;
|
||||||
|
|
||||||
|
public DefaultCookie(String name, String value) {
|
||||||
|
if (name == null) {
|
||||||
|
throw new NullPointerException("name");
|
||||||
|
}
|
||||||
|
this.name = name;
|
||||||
|
setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
if (value == null) {
|
||||||
|
throw new NullPointerException("value");
|
||||||
|
}
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment() {
|
||||||
|
return comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComment(String comment) {
|
||||||
|
this.comment = comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxAge() {
|
||||||
|
return maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxAge(int maxAge) {
|
||||||
|
if (maxAge < -1) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"maxAge must be either -1, 0, or a positive integer: " +
|
||||||
|
maxAge);
|
||||||
|
}
|
||||||
|
this.maxAge = maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(int version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSecure() {
|
||||||
|
return secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecure(boolean secure) {
|
||||||
|
this.secure = secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return getName().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof Cookie)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getName().equalsIgnoreCase(((Cookie) o).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compareTo(Cookie c) {
|
||||||
|
return getName().compareToIgnoreCase(c.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
buf.append(getName());
|
||||||
|
buf.append('=');
|
||||||
|
buf.append(getValue());
|
||||||
|
if (getDomain() != null) {
|
||||||
|
buf.append(", domain=");
|
||||||
|
buf.append(getDomain());
|
||||||
|
}
|
||||||
|
if (getPath() != null) {
|
||||||
|
buf.append(", path=");
|
||||||
|
buf.append(getPath());
|
||||||
|
}
|
||||||
|
if (getComment() != null) {
|
||||||
|
buf.append(", comment=");
|
||||||
|
buf.append(getComment());
|
||||||
|
}
|
||||||
|
if (getMaxAge() >= 0) {
|
||||||
|
buf.append(", maxAge=");
|
||||||
|
buf.append(getMaxAge());
|
||||||
|
buf.append('s');
|
||||||
|
}
|
||||||
|
if (isSecure()) {
|
||||||
|
buf.append(", secure");
|
||||||
|
}
|
||||||
|
return buf.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user