Fixed NPE in the HTTP example

This commit is contained in:
Trustin Lee 2009-03-09 08:18:52 +00:00
parent f3149fafc1
commit 7985fa94a9

View File

@ -33,11 +33,11 @@ import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage; import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.MessageEvent; 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.HttpChunk;
import org.jboss.netty.handler.codec.http.Cookie; import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.CookieDecoder; import org.jboss.netty.handler.codec.http.CookieDecoder;
import org.jboss.netty.handler.codec.http.CookieEncoder; import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunk;
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;
@ -131,8 +131,10 @@ public class HttpRequestHandler extends SimpleChannelHandler {
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()));
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
if (cookieString != null) {
CookieDecoder cookieDecoder = new CookieDecoder(); CookieDecoder cookieDecoder = new CookieDecoder();
Map<String, Cookie> cookies = cookieDecoder.decode(request.getHeader(HttpHeaders.Names.COOKIE)); Map<String, Cookie> cookies = cookieDecoder.decode(cookieString);
if(!cookies.isEmpty()) { if(!cookies.isEmpty()) {
// Reset the cookies if necessary. // Reset the cookies if necessary.
CookieEncoder cookieEncoder = new CookieEncoder(); CookieEncoder cookieEncoder = new CookieEncoder();
@ -141,6 +143,7 @@ public class HttpRequestHandler extends SimpleChannelHandler {
} }
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);