From b8bc78a7d089a27d30af68aef4efd4d2eb2a2308 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 26 Feb 2009 07:59:37 +0000 Subject: [PATCH] * 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 --- .../http/{HttpCookie.java => Cookie.java} | 65 ++----- ...pCookieDecoder.java => CookieDecoder.java} | 8 +- ...pCookieEncoder.java => CookieEncoder.java} | 8 +- .../handler/codec/http/DefaultCookie.java | 163 ++++++++++++++++++ 4 files changed, 187 insertions(+), 57 deletions(-) rename src/main/java/org/jboss/netty/handler/codec/http/{HttpCookie.java => Cookie.java} (50%) rename src/main/java/org/jboss/netty/handler/codec/http/{HttpCookieDecoder.java => CookieDecoder.java} (87%) rename src/main/java/org/jboss/netty/handler/codec/http/{HttpCookieEncoder.java => CookieEncoder.java} (88%) create mode 100644 src/main/java/org/jboss/netty/handler/codec/http/DefaultCookie.java diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookie.java b/src/main/java/org/jboss/netty/handler/codec/http/Cookie.java similarity index 50% rename from src/main/java/org/jboss/netty/handler/codec/http/HttpCookie.java rename to src/main/java/org/jboss/netty/handler/codec/http/Cookie.java index e056afd806..ec08ef3305 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookie.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/Cookie.java @@ -23,56 +23,23 @@ 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 HttpCookie implements Comparable { - - // TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?) - private final String name; - private final String value; - - public HttpCookie(String name, String value) { - if (name == null) { - throw new NullPointerException("name"); - } - if (value == null) { - throw new NullPointerException("value"); - } - - this.name = name; - this.value = value; - } - - 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(); - } +public interface Cookie extends Comparable { + String getName(); + String getValue(); + void setValue(String value); + String getDomain(); + void setDomain(String domain); + String getPath(); + void setPath(String path); + String getComment(); + void setComment(String comment); + int getMaxAge(); + void setMaxAge(int maxAge); + int getVersion(); + void setVersion(int version); + boolean isSecure(); + void setSecure(boolean secure); } \ No newline at end of file diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookieDecoder.java b/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java similarity index 87% rename from src/main/java/org/jboss/netty/handler/codec/http/HttpCookieDecoder.java rename to src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java index 75c6b8945f..7ab9ac8455 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookieDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java @@ -31,21 +31,21 @@ import org.jboss.netty.util.CaseIgnoringComparator; * @author Andy Taylor (andy.taylor@jboss.org) * @version $Rev$, $Date$ */ -public class HttpCookieDecoder { +public class CookieDecoder { // TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?) private final static String semicolon = ";"; private final static String equals = "="; - public Map decode(String header) { - Map cookies = new TreeMap(CaseIgnoringComparator.INSTANCE); + public Map decode(String header) { + Map cookies = new TreeMap(CaseIgnoringComparator.INSTANCE); String[] split = header.split(semicolon); for (String s : split) { String[] cookie = s.split(equals); if(cookie != null && cookie.length == 2) { String name = cookie[0].trim(); String value = cookie[1].trim(); - cookies.put(name, new HttpCookie(name, value)); + cookies.put(name, new DefaultCookie(name, value)); } } return cookies; diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookieEncoder.java b/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java similarity index 88% rename from src/main/java/org/jboss/netty/handler/codec/http/HttpCookieEncoder.java rename to src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java index b856db224e..0971c4e92d 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpCookieEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java @@ -32,16 +32,16 @@ import org.jboss.netty.util.CaseIgnoringComparator; * @author Andy Taylor (andy.taylor@jboss.org) * @version $Rev$, $Date$ */ -public class HttpCookieEncoder { +public class CookieEncoder { // TODO: Add domain, path, maxAge, and version (and perhaps secure and comment?) - private final Map cookies = new TreeMap(CaseIgnoringComparator.INSTANCE); + private final Map cookies = new TreeMap(CaseIgnoringComparator.INSTANCE); 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); } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultCookie.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultCookie.java new file mode 100644 index 0000000000..7d8930ab45 --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultCookie.java @@ -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(); + } +} \ No newline at end of file