* Made CaseIgnoringComparator a singleton
* Changed HttpCookieDecoder to return a Map instead of a Set * Optimized imports
This commit is contained in:
parent
fea082e689
commit
df05ba3733
@ -23,7 +23,6 @@ package org.jboss.netty.example.http;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
@ -36,15 +35,15 @@ import org.jboss.netty.channel.MessageEvent;
|
|||||||
import org.jboss.netty.channel.SimpleChannelHandler;
|
import org.jboss.netty.channel.SimpleChannelHandler;
|
||||||
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
|
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
|
||||||
import org.jboss.netty.handler.codec.http.HttpChunk;
|
import org.jboss.netty.handler.codec.http.HttpChunk;
|
||||||
|
import org.jboss.netty.handler.codec.http.HttpCookie;
|
||||||
|
import org.jboss.netty.handler.codec.http.HttpCookieDecoder;
|
||||||
|
import org.jboss.netty.handler.codec.http.HttpCookieEncoder;
|
||||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
||||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||||
import org.jboss.netty.handler.codec.http.HttpResponse;
|
import org.jboss.netty.handler.codec.http.HttpResponse;
|
||||||
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
|
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
|
||||||
import org.jboss.netty.handler.codec.http.HttpVersion;
|
import org.jboss.netty.handler.codec.http.HttpVersion;
|
||||||
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
|
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
|
||||||
import org.jboss.netty.handler.codec.http.HttpCookieDecoder;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpCookie;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpCookieEncoder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
@ -131,16 +130,18 @@ public class HttpRequestHandler extends SimpleChannelHandler {
|
|||||||
response.setContent(buf);
|
response.setContent(buf);
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
||||||
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
|
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
|
||||||
|
|
||||||
HttpCookieDecoder cookieDecoder = new HttpCookieDecoder();
|
HttpCookieDecoder cookieDecoder = new HttpCookieDecoder();
|
||||||
Set<HttpCookie> cookieSet = cookieDecoder.decode(request.getHeader(HttpHeaders.Names.COOKIE));
|
Map<String, HttpCookie> cookies = cookieDecoder.decode(request.getHeader(HttpHeaders.Names.COOKIE));
|
||||||
if(cookieSet != null) {
|
if(!cookies.isEmpty()) {
|
||||||
//lets reset the cookies
|
// Reset the cookies if necessary.
|
||||||
HttpCookieEncoder cookieEncoder = new HttpCookieEncoder();
|
HttpCookieEncoder cookieEncoder = new HttpCookieEncoder();
|
||||||
for (HttpCookie cookie : cookieSet) {
|
for (HttpCookie cookie : cookies.values()) {
|
||||||
cookieEncoder.addCookie(cookie);
|
cookieEncoder.addCookie(cookie);
|
||||||
}
|
}
|
||||||
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
|
response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the response.
|
// Write the response.
|
||||||
ChannelFuture future = e.getChannel().write(response);
|
ChannelFuture future = e.getChannel().write(response);
|
||||||
|
|
||||||
|
@ -21,14 +21,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.handler.codec.http;
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
import org.jboss.netty.util.CaseIgnoringComparator;
|
import org.jboss.netty.util.CaseIgnoringComparator;
|
||||||
@ -42,10 +39,9 @@ import org.jboss.netty.util.CaseIgnoringComparator;
|
|||||||
* @version $Rev$, $Date$
|
* @version $Rev$, $Date$
|
||||||
*/
|
*/
|
||||||
public class DefaultHttpMessage implements HttpMessage {
|
public class DefaultHttpMessage implements HttpMessage {
|
||||||
private final static Comparator<String> caseIgnoringComparator = new CaseIgnoringComparator();
|
|
||||||
|
|
||||||
private final HttpVersion version;
|
private final HttpVersion version;
|
||||||
private final Map<String, List<String>> headers = new TreeMap<String, List<String>>(caseIgnoringComparator);
|
private final Map<String, List<String>> headers = new TreeMap<String, List<String>>(CaseIgnoringComparator.INSTANCE);
|
||||||
private ChannelBuffer content;
|
private ChannelBuffer content;
|
||||||
|
|
||||||
protected DefaultHttpMessage(final HttpVersion version) {
|
protected DefaultHttpMessage(final HttpVersion version) {
|
||||||
|
@ -21,8 +21,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.handler.codec.http;
|
package org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
import java.util.HashSet;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import org.jboss.netty.util.CaseIgnoringComparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||||
@ -32,13 +34,15 @@ public class HttpCookieDecoder {
|
|||||||
|
|
||||||
private final static String equals = "=";
|
private final static String equals = "=";
|
||||||
|
|
||||||
public Set<HttpCookie> decode(String header) {
|
public Map<String, HttpCookie> decode(String header) {
|
||||||
Set<HttpCookie> cookies = new HashSet<HttpCookie>();
|
Map<String, HttpCookie> cookies = new TreeMap<String, HttpCookie>(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) {
|
||||||
cookies.add(new HttpCookie(cookie[0].trim(), cookie[1].trim()));
|
String name = cookie[0].trim();
|
||||||
|
String value = cookie[1].trim();
|
||||||
|
cookies.put(name, new HttpCookie(name, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cookies;
|
return cookies;
|
||||||
|
@ -21,29 +21,18 @@
|
|||||||
*/
|
*/
|
||||||
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 static org.jboss.netty.handler.codec.http.HttpCodecUtil.COLON;
|
|
||||||
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.SP;
|
|
||||||
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.EQUALS;
|
|
||||||
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.SEMICOLON;
|
|
||||||
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.CRLF;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Collection;
|
import org.jboss.netty.util.CaseIgnoringComparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||||
*/
|
*/
|
||||||
public class HttpCookieEncoder {
|
public class HttpCookieEncoder {
|
||||||
private final static String semicolon = ";";
|
|
||||||
|
|
||||||
private final static String equals = "=";
|
private final Map<String, HttpCookie> cookies = new TreeMap<String, HttpCookie>(CaseIgnoringComparator.INSTANCE);
|
||||||
|
|
||||||
private final static Comparator<String> caseIgnoringComparator = new CaseIgnoringComparator();
|
|
||||||
|
|
||||||
private final Map<String, HttpCookie> cookies = new TreeMap<String, HttpCookie>(caseIgnoringComparator);
|
|
||||||
|
|
||||||
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 HttpCookie(name, val));
|
||||||
@ -61,9 +50,9 @@ public class HttpCookieEncoder {
|
|||||||
}
|
}
|
||||||
for (String cookieName : cookieNames) {
|
for (String cookieName : cookieNames) {
|
||||||
sb.append(cookieName);
|
sb.append(cookieName);
|
||||||
sb.append(equals);
|
sb.append((char) HttpCodecUtil.EQUALS);
|
||||||
sb.append(cookies.get(cookieName).getValue());
|
sb.append(cookies.get(cookieName).getValue());
|
||||||
sb.append(semicolon);
|
sb.append((char) HttpCodecUtil.SEMICOLON);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ package org.jboss.netty.handler.codec.http;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ package org.jboss.netty.handler.codec.http;
|
|||||||
import static org.jboss.netty.buffer.ChannelBuffers.*;
|
import static org.jboss.netty.buffer.ChannelBuffers.*;
|
||||||
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.*;
|
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.*;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -26,16 +26,23 @@ import java.util.Comparator;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||||
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
*/
|
*/
|
||||||
public class CaseIgnoringComparator implements Comparator<String>, Serializable {
|
public final class CaseIgnoringComparator implements Comparator<String>, Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 4582133183775373862L;
|
private static final long serialVersionUID = 4582133183775373862L;
|
||||||
|
|
||||||
public CaseIgnoringComparator() {
|
public static final CaseIgnoringComparator INSTANCE = new CaseIgnoringComparator();
|
||||||
|
|
||||||
|
private CaseIgnoringComparator() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compare(String o1, String o2) {
|
public int compare(String o1, String o2) {
|
||||||
return o1.compareToIgnoreCase(o2);
|
return o1.compareToIgnoreCase(o2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object readResolve() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user