CookieEncoder should not use a Map but use a Set because cookie name might be duplicate

This commit is contained in:
Trustin Lee 2009-03-13 15:17:11 +00:00
parent e3f3e8ce5a
commit 510a270cb4

View File

@ -21,12 +21,9 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Set;
import java.util.TreeMap; import java.util.TreeSet;
import org.jboss.netty.util.CaseIgnoringComparator;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -36,7 +33,7 @@ import org.jboss.netty.util.CaseIgnoringComparator;
*/ */
public class CookieEncoder { public class CookieEncoder {
private final Map<String, Cookie> cookies = new TreeMap<String, Cookie>(CaseIgnoringComparator.INSTANCE); private final Set<Cookie> cookies = new TreeSet<Cookie>();
private final String charset; private final String charset;
@ -51,23 +48,19 @@ public class CookieEncoder {
this.charset = charset; this.charset = charset;
} }
public void addCookie(String name, String val) { public void addCookie(String name, String value) {
cookies.put(name, new DefaultCookie(name, val)); cookies.add(new DefaultCookie(name, value));
} }
public void addCookie(Cookie cookie) { public void addCookie(Cookie cookie) {
cookies.put(cookie.getName(), cookie); cookies.add(cookie);
} }
public String encode() { public String encode() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
Collection<String> cookieNames = cookies.keySet();
if (cookieNames.isEmpty()) { for (Cookie cookie: cookies) {
return null; add(sb, cookie.getName(), QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
}
for (String cookieName : cookieNames) {
Cookie cookie = cookies.get(cookieName);
add(sb, cookieName, QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
if (cookie.getMaxAge() >= 0) { if (cookie.getMaxAge() >= 0) {
if (cookie.getVersion() == 0) { if (cookie.getVersion() == 0) {