Brought back the encode() method of CookieEncoder
This commit is contained in:
parent
b6cfc24127
commit
efd5af9313
@ -91,7 +91,7 @@ public class HttpClient {
|
||||
HttpRequest request = new DefaultHttpRequest(
|
||||
HttpVersion.HTTP_1_0, HttpMethod.GET, uri.toASCIIString());
|
||||
request.addHeader(HttpHeaders.Names.HOST, host);
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder();
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder(false);
|
||||
httpCookieEncoder.addCookie("my-cookie", "foo");
|
||||
httpCookieEncoder.addCookie("another-cookie", "bar");
|
||||
request.addHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
|
||||
|
@ -136,7 +136,7 @@ public class HttpRequestHandler extends SimpleChannelHandler {
|
||||
Set<Cookie> cookies = cookieDecoder.decode(cookieString);
|
||||
if(!cookies.isEmpty()) {
|
||||
// Reset the cookies if necessary.
|
||||
CookieEncoder cookieEncoder = new CookieEncoder();
|
||||
CookieEncoder cookieEncoder = new CookieEncoder(true);
|
||||
for (Cookie cookie : cookies) {
|
||||
cookieEncoder.addCookie(cookie);
|
||||
}
|
||||
|
@ -34,17 +34,18 @@ import java.util.TreeSet;
|
||||
public class CookieEncoder {
|
||||
|
||||
private final Set<Cookie> cookies = new TreeSet<Cookie>();
|
||||
|
||||
private final boolean server;
|
||||
private final String charset;
|
||||
|
||||
public CookieEncoder() {
|
||||
this(QueryStringDecoder.DEFAULT_CHARSET);
|
||||
public CookieEncoder(boolean server) {
|
||||
this(server, QueryStringDecoder.DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
public CookieEncoder(String charset) {
|
||||
public CookieEncoder(boolean server, String charset) {
|
||||
if (charset == null) {
|
||||
throw new NullPointerException("charset");
|
||||
}
|
||||
this.server = server;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
@ -57,7 +58,15 @@ public class CookieEncoder {
|
||||
}
|
||||
|
||||
public String encode() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (server) {
|
||||
return encodeServerSide();
|
||||
} else {
|
||||
return encodeClientSide();
|
||||
}
|
||||
}
|
||||
|
||||
private String encodeServerSide() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (Cookie cookie: cookies) {
|
||||
add(sb, cookie.getName(), QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
|
||||
@ -115,14 +124,50 @@ public class CookieEncoder {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void add(StringBuffer sb, String name, String val) {
|
||||
private String encodeClientSide() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (Cookie cookie: cookies) {
|
||||
if (cookie.getVersion() >= 1) {
|
||||
add(sb, '$' + CookieHeaderNames.VERSION, 1);
|
||||
}
|
||||
|
||||
add(sb, cookie.getName(), QueryStringEncoder.encodeComponent(cookie.getValue(), charset));
|
||||
|
||||
if (cookie.getPath() != null) {
|
||||
add(sb, '$' + CookieHeaderNames.PATH, cookie.getPath());
|
||||
}
|
||||
|
||||
if (cookie.getDomain() != null) {
|
||||
add(sb, '$' + CookieHeaderNames.DOMAIN, cookie.getDomain());
|
||||
}
|
||||
|
||||
if (cookie.getVersion() >= 1) {
|
||||
if(!cookie.getPorts().isEmpty()) {
|
||||
sb.append('$');
|
||||
sb.append(CookieHeaderNames.PORT);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append((char) HttpCodecUtil.DOUBLE_QUOTE);
|
||||
for (int port: cookie.getPorts()) {
|
||||
sb.append(port);
|
||||
sb.append((char) HttpCodecUtil.COMMA);
|
||||
}
|
||||
sb.setCharAt(sb.length() - 1, (char) HttpCodecUtil.DOUBLE_QUOTE);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void add(StringBuilder sb, String name, String val) {
|
||||
sb.append(name);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append(val);
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
|
||||
private void addQuoted(StringBuffer sb, String name, String val) {
|
||||
private static void addQuoted(StringBuilder sb, String name, String val) {
|
||||
sb.append(name);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append((char) HttpCodecUtil.DOUBLE_QUOTE);
|
||||
@ -131,7 +176,7 @@ public class CookieEncoder {
|
||||
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||
}
|
||||
|
||||
private void add(StringBuffer sb, String name, int val) {
|
||||
private static void add(StringBuilder sb, String name, int val) {
|
||||
sb.append(name);
|
||||
sb.append((char) HttpCodecUtil.EQUALS);
|
||||
sb.append(val);
|
||||
|
@ -39,7 +39,7 @@ public class CookieEncoderTest {
|
||||
public void testEncodingSingleCookieV0() {
|
||||
String result = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
CookieEncoder encoder = new CookieEncoder(true);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setComment("this is a comment");
|
||||
cookie.setCommentUrl("http://aurl.com");
|
||||
@ -57,7 +57,7 @@ public class CookieEncoderTest {
|
||||
public void testEncodingSingleCookieV1() {
|
||||
String result = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
CookieEncoder encoder = new CookieEncoder(true);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
@ -72,7 +72,7 @@ public class CookieEncoderTest {
|
||||
public void testEncodingSingleCookieV2() {
|
||||
String result = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;commentURL=\"http://aurl.com\";port=\"80,8080\";discard;";
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
CookieEncoder encoder = new CookieEncoder(true);
|
||||
encoder.addCookie(cookie);
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
@ -92,7 +92,7 @@ public class CookieEncoderTest {
|
||||
String c1 = "myCookie=myValue;max-age=50;path=/apathsomewhere;domain=.adomainsomewhere;secure;comment=this is a comment;version=1;commentURL=\"http://aurl.com\";port=\"80,8080\";discard;";
|
||||
String c2 = "myCookie2=myValue2;path=/anotherpathsomewhere;domain=.anotherdomainsomewhere;comment=this is another comment;version=1;commentURL=\"http://anotherurl.com\";";
|
||||
String c3 = "myCookie3=myValue3;version=1;";
|
||||
CookieEncoder encoder = new CookieEncoder();
|
||||
CookieEncoder encoder = new CookieEncoder(true);
|
||||
Cookie cookie = new DefaultCookie("myCookie", "myValue");
|
||||
cookie.setVersion(1);
|
||||
cookie.setComment("this is a comment");
|
||||
|
Loading…
Reference in New Issue
Block a user