* Renamed Cookie.portList to Cookie.ports and its type from int[] to Set<Integer>

* Renamed CookieHeaderNames.PORTLIST to CookieHeaderNames.PORT
This commit is contained in:
Trustin Lee 2009-03-13 12:32:47 +00:00
parent b8f03d60e4
commit b796a1d97b
7 changed files with 92 additions and 57 deletions

View File

@ -21,6 +21,8 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import java.util.Set;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
@ -46,6 +48,7 @@ public interface Cookie extends Comparable<Cookie> {
void setCommentURL(String commentURL); void setCommentURL(String commentURL);
boolean isDiscard(); boolean isDiscard();
void setDiscard(boolean discard); void setDiscard(boolean discard);
int[] getPortList(); Set<Integer> getPorts();
void setPortList(int... portList); void setPorts(int... ports);
void setPorts(Iterable<Integer> ports);
} }

View File

@ -21,11 +21,11 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import org.jboss.netty.util.CaseIgnoringComparator;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.jboss.netty.util.CaseIgnoringComparator;
/** /**
* @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 Andy Taylor (andy.taylor@jboss.org)
@ -73,17 +73,22 @@ public class CookieDecoder {
int version = 0; int version = 0;
int maxAge = 0; int maxAge = 0;
int[] ports = null; int[] ports = null;
loop:
for (int j = i + 1; j < split.length; j++, i++) { for (int j = i + 1; j < split.length; j++, i++) {
String[] val = split[j].split(EQUALS, 2); String[] val = split[j].split(EQUALS, 2);
if (val != null && val.length == 1) { if (val == null) {
continue;
}
switch (val.length) {
case 1:
if (CookieHeaderNames.DISCARD.equalsIgnoreCase(val[0])) { if (CookieHeaderNames.DISCARD.equalsIgnoreCase(val[0])) {
discard = true; discard = true;
} }
else if (CookieHeaderNames.SECURE.equalsIgnoreCase(val[0])) { else if (CookieHeaderNames.SECURE.equalsIgnoreCase(val[0])) {
secure = true; secure = true;
} }
} break;
else if (val != null && val.length == 2) { case 2:
name = val[0].trim(); name = val[0].trim();
value = val[1].trim(); value = val[1].trim();
if (CookieHeaderNames.COMMENT.equalsIgnoreCase(name)) { if (CookieHeaderNames.COMMENT.equalsIgnoreCase(name)) {
@ -107,17 +112,17 @@ public class CookieDecoder {
else if (CookieHeaderNames.VERSION.equalsIgnoreCase(name)) { else if (CookieHeaderNames.VERSION.equalsIgnoreCase(name)) {
version = Integer.valueOf(value); version = Integer.valueOf(value);
} }
else if (CookieHeaderNames.PORTLIST.equalsIgnoreCase(name)) { else if (CookieHeaderNames.PORT.equalsIgnoreCase(name)) {
String[] portList = value.split(COMMA); String[] portList = value.split(COMMA);
ports = new int[portList.length]; ports = new int[portList.length];
for (int i1 = 0; i1 < portList.length; i1++) { for (int i1 = 0; i1 < portList.length; i1++) {
String s1 = portList[i1]; String s1 = portList[i1];
ports[i1] = Integer.valueOf(s1); ports[i1] = Integer.valueOf(s1);
} }
} else {
break loop;
} }
else { break;
break;
}
} }
} }
theCookie.setVersion(version); theCookie.setVersion(version);
@ -130,11 +135,12 @@ public class CookieDecoder {
} }
if (version > 1) { if (version > 1) {
theCookie.setCommentURL(commentURL); theCookie.setCommentURL(commentURL);
theCookie.setPortList(ports); if (ports != null) {
theCookie.setPorts(ports);
}
theCookie.setDiscard(discard); theCookie.setDiscard(discard);
} }
} }
} }
return cookies; return cookies;
} }

View File

@ -21,12 +21,12 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import org.jboss.netty.util.CaseIgnoringComparator;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.jboss.netty.util.CaseIgnoringComparator;
/** /**
* @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 Andy Taylor (andy.taylor@jboss.org)
@ -106,16 +106,14 @@ public class CookieEncoder {
if (cookie.getCommentURL() != null) { if (cookie.getCommentURL() != null) {
add(sb, CookieHeaderNames.COMMENTURL, QueryStringEncoder.encodeComponent(cookie.getCommentURL(), charset)); add(sb, CookieHeaderNames.COMMENTURL, QueryStringEncoder.encodeComponent(cookie.getCommentURL(), charset));
} }
if(cookie.getPortList() != null && cookie.getPortList().length > 0) { if(!cookie.getPorts().isEmpty()) {
sb.append(CookieHeaderNames.PORTLIST); sb.append(CookieHeaderNames.PORT);
sb.append((char) HttpCodecUtil.EQUALS); sb.append((char) HttpCodecUtil.EQUALS);
for (int i = 0; i < cookie.getPortList().length; i++) { for (int port: cookie.getPorts()) {
int port = cookie.getPortList()[i];
if(i > 0) {
sb.append((char)HttpCodecUtil.COMMA);
}
sb.append(port); sb.append(port);
sb.append((char) HttpCodecUtil.COMMA);
} }
sb.setLength(sb.length() - 1); // Remove the trailing comma.
sb.append((char) HttpCodecUtil.SEMICOLON); sb.append((char) HttpCodecUtil.SEMICOLON);
} }
if (cookie.isDiscard()) { if (cookie.isDiscard()) {

View File

@ -41,7 +41,7 @@ public class CookieHeaderNames {
public static final String DISCARD = "discard"; public static final String DISCARD = "discard";
public static final String PORTLIST = "port"; public static final String PORT = "port";
public static final String VERSION = "version"; public static final String VERSION = "version";

View File

@ -21,6 +21,10 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -37,7 +41,8 @@ public class DefaultCookie implements Cookie {
private String comment; private String comment;
private String commentURL; private String commentURL;
private boolean discard; private boolean discard;
private int[] portList; private Set<Integer> ports = Collections.emptySet();
private Set<Integer> unmodifiablePorts = ports;
private int maxAge; private int maxAge;
private int version; private int version;
private boolean secure; private boolean secure;
@ -127,21 +132,48 @@ public class DefaultCookie implements Cookie {
this.discard = discard; this.discard = discard;
} }
public int[] getPortList() { public Set<Integer> getPorts() {
return portList.clone(); if (unmodifiablePorts == null) {
unmodifiablePorts = Collections.unmodifiableSet(ports);
}
return unmodifiablePorts;
} }
public void setPortList(int... portList) { public void setPorts(int... ports) {
if (portList == null) { if (ports == null) {
throw new NullPointerException("portList"); throw new NullPointerException("ports");
} }
int[] portListCopy = portList.clone();
for (int p: portListCopy) { int[] portsCopy = ports.clone();
if (portsCopy.length == 0) {
unmodifiablePorts = this.ports = Collections.emptySet();
} else {
Set<Integer> newPorts = new TreeSet<Integer>();
for (int p: portsCopy) {
if (p <= 0 || p > 65535) {
throw new IllegalArgumentException("port out of range: " + p);
}
newPorts.add(Integer.valueOf(p));
}
this.ports = newPorts;
unmodifiablePorts = null;
}
}
public void setPorts(Iterable<Integer> ports) {
Set<Integer> newPorts = new TreeSet<Integer>();
for (int p: ports) {
if (p <= 0 || p > 65535) { if (p <= 0 || p > 65535) {
throw new IllegalArgumentException("port out of range: " + p); throw new IllegalArgumentException("port out of range: " + p);
} }
newPorts.add(Integer.valueOf(p));
}
if (newPorts.isEmpty()) {
unmodifiablePorts = this.ports = Collections.emptySet();
} else {
this.ports = newPorts;
unmodifiablePorts = null;
} }
this.portList = portListCopy;
} }
public int getMaxAge() { public int getMaxAge() {

View File

@ -21,15 +21,12 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Map; import java.util.Map;
import org.junit.Test;
/** /**
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a> * @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
*/ */
@ -49,7 +46,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(0, cookie.getVersion()); assertEquals(0, cookie.getVersion());
} }
@ -69,7 +66,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(0, cookie.getVersion()); assertEquals(0, cookie.getVersion());
} }
@ -88,7 +85,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(1, cookie.getVersion()); assertEquals(1, cookie.getVersion());
} }
@ -108,7 +105,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(1, cookie.getVersion()); assertEquals(1, cookie.getVersion());
} }
@ -127,10 +124,9 @@ public class CookieDecoderTest {
assertTrue(cookie.isDiscard()); assertTrue(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNotNull(cookie.getPortList()); assertEquals(2, cookie.getPorts().size());
assertEquals(2, cookie.getPortList().length); assertTrue(cookie.getPorts().contains(80));
assertEquals(80, cookie.getPortList()[0]); assertTrue(cookie.getPorts().contains(8080));
assertEquals(8080, cookie.getPortList()[1]);
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(2, cookie.getVersion()); assertEquals(2, cookie.getVersion());
} }
@ -154,10 +150,9 @@ public class CookieDecoderTest {
assertTrue(cookie.isDiscard()); assertTrue(cookie.isDiscard());
assertEquals(50, cookie.getMaxAge()); assertEquals(50, cookie.getMaxAge());
assertEquals("/apathsomewhere", cookie.getPath()); assertEquals("/apathsomewhere", cookie.getPath());
assertNotNull(cookie.getPortList()); assertEquals(2, cookie.getPorts().size());
assertEquals(2, cookie.getPortList().length); assertTrue(cookie.getPorts().contains(80));
assertEquals(80, cookie.getPortList()[0]); assertTrue(cookie.getPorts().contains(8080));
assertEquals(8080, cookie.getPortList()[1]);
assertTrue(cookie.isSecure()); assertTrue(cookie.isSecure());
assertEquals(2, cookie.getVersion()); assertEquals(2, cookie.getVersion());
cookie = cookieMap.get("MyCookie2"); cookie = cookieMap.get("MyCookie2");
@ -169,7 +164,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(0, cookie.getMaxAge()); assertEquals(0, cookie.getMaxAge());
assertEquals("/anotherpathsomewhere", cookie.getPath()); assertEquals("/anotherpathsomewhere", cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertFalse(cookie.isSecure()); assertFalse(cookie.isSecure());
assertEquals(2, cookie.getVersion()); assertEquals(2, cookie.getVersion());
cookie = cookieMap.get("MyCookie3"); cookie = cookieMap.get("MyCookie3");
@ -181,7 +176,7 @@ public class CookieDecoderTest {
assertFalse(cookie.isDiscard()); assertFalse(cookie.isDiscard());
assertEquals(0, cookie.getMaxAge()); assertEquals(0, cookie.getMaxAge());
assertNull(cookie.getPath()); assertNull(cookie.getPath());
assertNull(cookie.getPortList()); assertTrue(cookie.getPorts().isEmpty());
assertFalse(cookie.isSecure()); assertFalse(cookie.isSecure());
assertEquals(2, cookie.getVersion()); assertEquals(2, cookie.getVersion());
} }

View File

@ -21,7 +21,8 @@
*/ */
package org.jboss.netty.handler.codec.http; package org.jboss.netty.handler.codec.http;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
@ -41,7 +42,7 @@ public class CookieEncoderTest {
cookie.setDiscard(true); cookie.setDiscard(true);
cookie.setMaxAge(50); cookie.setMaxAge(50);
cookie.setPath("/apathsomewhere"); cookie.setPath("/apathsomewhere");
cookie.setPortList(new int[]{80, 8080}); cookie.setPorts(80, 8080);
cookie.setSecure(true); cookie.setSecure(true);
String encodedCookie = encoder.encode(); String encodedCookie = encoder.encode();
assertEquals(result, encodedCookie); assertEquals(result, encodedCookie);
@ -58,7 +59,7 @@ public class CookieEncoderTest {
cookie.setDiscard(true); cookie.setDiscard(true);
cookie.setMaxAge(50); cookie.setMaxAge(50);
cookie.setPath("/apathsomewhere"); cookie.setPath("/apathsomewhere");
cookie.setPortList(new int[]{80, 8080}); cookie.setPorts(80, 8080);
cookie.setSecure(true); cookie.setSecure(true);
String encodedCookie = encoder.encode(); String encodedCookie = encoder.encode();
assertEquals(result, encodedCookie); assertEquals(result, encodedCookie);
@ -75,7 +76,7 @@ public class CookieEncoderTest {
cookie.setDiscard(true); cookie.setDiscard(true);
cookie.setMaxAge(50); cookie.setMaxAge(50);
cookie.setPath("/apathsomewhere"); cookie.setPath("/apathsomewhere");
cookie.setPortList(new int[]{80, 8080}); cookie.setPorts(80, 8080);
cookie.setSecure(true); cookie.setSecure(true);
String encodedCookie = encoder.encode(); String encodedCookie = encoder.encode();
assertEquals(result, encodedCookie); assertEquals(result, encodedCookie);
@ -94,7 +95,7 @@ public class CookieEncoderTest {
cookie.setDiscard(true); cookie.setDiscard(true);
cookie.setMaxAge(50); cookie.setMaxAge(50);
cookie.setPath("/apathsomewhere"); cookie.setPath("/apathsomewhere");
cookie.setPortList(new int[]{80, 8080}); cookie.setPorts(80, 8080);
cookie.setSecure(true); cookie.setSecure(true);
encoder.addCookie(cookie); encoder.addCookie(cookie);
Cookie cookie2 = new DefaultCookie("myCookie2", "myValue2"); Cookie cookie2 = new DefaultCookie("myCookie2", "myValue2");