From cf5ef8cf5e9db0663483f97df81ac83b4fb21cd2 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 20 Nov 2009 04:12:16 +0000 Subject: [PATCH] Resolved issue: NETTY-247 Make HttpMessage, HttpChunk and their default implementation completely mutable --- .../handler/codec/http/DefaultHttpChunk.java | 16 +++++++---- .../codec/http/DefaultHttpChunkTrailer.java | 4 +++ .../codec/http/DefaultHttpMessage.java | 14 ++++++---- .../codec/http/DefaultHttpRequest.java | 28 ++++++++++++------- .../codec/http/DefaultHttpResponse.java | 14 ++++++---- .../netty/handler/codec/http/HttpChunk.java | 10 +++++++ .../netty/handler/codec/http/HttpMessage.java | 5 ++++ .../netty/handler/codec/http/HttpRequest.java | 10 +++++++ .../handler/codec/http/HttpResponse.java | 5 ++++ 9 files changed, 80 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunk.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunk.java index 8d5c09d615..780527cd61 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunk.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunk.java @@ -26,14 +26,22 @@ import org.jboss.netty.buffer.ChannelBuffer; */ public class DefaultHttpChunk implements HttpChunk { - private final ChannelBuffer content; - private final boolean last; + private ChannelBuffer content; + private boolean last; /** * Creates a new instance with the specified chunk content. If an empty * buffer is specified, this chunk becomes the 'end of content' marker. */ public DefaultHttpChunk(ChannelBuffer content) { + setContent(content); + } + + public ChannelBuffer getContent() { + return content; + } + + public void setContent(ChannelBuffer content) { if (content == null) { throw new NullPointerException("content"); } @@ -41,10 +49,6 @@ public class DefaultHttpChunk implements HttpChunk { this.content = content; } - public ChannelBuffer getContent() { - return content; - } - public boolean isLast() { return last; } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunkTrailer.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunkTrailer.java index 450fb72ac2..3b59db8687 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunkTrailer.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpChunkTrailer.java @@ -130,4 +130,8 @@ public class DefaultHttpChunkTrailer implements HttpChunkTrailer { public ChannelBuffer getContent() { return ChannelBuffers.EMPTY_BUFFER; } + + public void setContent(ChannelBuffer content) { + throw new IllegalStateException("read-only"); + } } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java index c00bfa1b70..b5dca47ae6 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpMessage.java @@ -37,8 +37,8 @@ import org.jboss.netty.util.internal.CaseIgnoringComparator; */ public class DefaultHttpMessage implements HttpMessage { - private final HttpVersion version; private final Map> headers = new TreeMap>(CaseIgnoringComparator.INSTANCE); + private HttpVersion version; private ChannelBuffer content = ChannelBuffers.EMPTY_BUFFER; private boolean chunked; @@ -46,10 +46,7 @@ public class DefaultHttpMessage implements HttpMessage { * Creates a new instance. */ protected DefaultHttpMessage(final HttpVersion version) { - if (version == null) { - throw new NullPointerException("version"); - } - this.version = version; + setProtocolVersion(version); } public void addHeader(final String name, final String value) { @@ -190,6 +187,13 @@ public class DefaultHttpMessage implements HttpMessage { return version; } + public void setProtocolVersion(HttpVersion version) { + if (version == null) { + throw new NullPointerException("version"); + } + this.version = version; + } + public ChannelBuffer getContent() { return content; } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java index a7f94b1890..45b97ba7af 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpRequest.java @@ -25,8 +25,8 @@ package org.jboss.netty.handler.codec.http; */ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest { - private final HttpMethod method; - private final String uri; + private HttpMethod method; + private String uri; /** * Creates a new instance. @@ -37,24 +37,32 @@ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpReques */ public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) { super(httpVersion); - if (method == null) { - throw new NullPointerException("method"); - } - if (uri == null) { - throw new NullPointerException("uri"); - } - this.method = method; - this.uri = uri; + setMethod(method); + setUri(uri); } public HttpMethod getMethod() { return method; } + public void setMethod(HttpMethod method) { + if (method == null) { + throw new NullPointerException("method"); + } + this.method = method; + } + public String getUri() { return uri; } + public void setUri(String uri) { + if (uri == null) { + throw new NullPointerException("uri"); + } + this.uri = uri; + } + @Override public String toString() { return getMethod().toString() + ' ' + getUri() + ' ' + getProtocolVersion().getText(); diff --git a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java index 044b18aa10..28e33dcfad 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/DefaultHttpResponse.java @@ -24,7 +24,8 @@ package org.jboss.netty.handler.codec.http; * @version $Rev$, $Date$ */ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpResponse { - private final HttpResponseStatus status; + + private HttpResponseStatus status; /** * Creates a new instance. @@ -34,16 +35,19 @@ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpRespo */ public DefaultHttpResponse(HttpVersion version, HttpResponseStatus status) { super(version); - if (status == null) { - throw new NullPointerException("status"); - } - this.status = status; + setStatus(status); } public HttpResponseStatus getStatus() { return status; } + public void setStatus(HttpResponseStatus status) { + if (status == null) { + throw new NullPointerException("status"); + } + this.status = status; + } @Override public String toString() { diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpChunk.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpChunk.java index 3f813a547e..51d7cb5d96 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpChunk.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpChunk.java @@ -47,6 +47,10 @@ public interface HttpChunk { return ChannelBuffers.EMPTY_BUFFER; } + public void setContent(ChannelBuffer content) { + throw new IllegalStateException("read-only"); + } + public boolean isLast() { return true; } @@ -99,4 +103,10 @@ public interface HttpChunk { * marker, {@link ChannelBuffers#EMPTY_BUFFER} will be returned. */ ChannelBuffer getContent(); + + /** + * Sets the content of this chunk. If an empty buffer is specified, + * this chunk becomes the 'end of content' marker. + */ + void setContent(ChannelBuffer content); } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessage.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessage.java index 0811e7ce46..3f4608c102 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpMessage.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpMessage.java @@ -71,6 +71,11 @@ public interface HttpMessage { */ HttpVersion getProtocolVersion(); + /** + * Sets the protocol version of this message. + */ + void setProtocolVersion(HttpVersion version); + /** * Returns the content of this message. If there is no content or * {@link #isChunked()} returns {@code true}, an diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpRequest.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpRequest.java index 26e7151bfe..9995de9f58 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpRequest.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpRequest.java @@ -41,8 +41,18 @@ public interface HttpRequest extends HttpMessage { */ HttpMethod getMethod(); + /** + * Sets the method of this request. + */ + void setMethod(HttpMethod method); + /** * Returns the URI (or path) of this request. */ String getUri(); + + /** + * Sets the URI (or path) of this request. + */ + void setUri(String uri); } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpResponse.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpResponse.java index 2d43225002..8d7e22e805 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpResponse.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpResponse.java @@ -39,4 +39,9 @@ public interface HttpResponse extends HttpMessage { * Returns the status of this response. */ HttpResponseStatus getStatus(); + + /** + * Sets the status of this response. + */ + void setStatus(HttpResponseStatus status); }