1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.util.internal.StringUtil;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26
27
28
29 public class DefaultHttpMessage implements HttpMessage {
30
31 private final HttpHeaders headers = new DefaultHttpHeaders(true);
32 private HttpVersion version;
33 private ChannelBuffer content = ChannelBuffers.EMPTY_BUFFER;
34 private boolean chunked;
35
36
37
38
39 protected DefaultHttpMessage(final HttpVersion version) {
40 setProtocolVersion(version);
41 }
42
43 public HttpHeaders headers() {
44 return headers;
45 }
46
47 public boolean isChunked() {
48 if (chunked) {
49 return true;
50 } else {
51 return HttpCodecUtil.isTransferEncodingChunked(this);
52 }
53 }
54
55 public void setChunked(boolean chunked) {
56 this.chunked = chunked;
57 if (chunked) {
58 setContent(ChannelBuffers.EMPTY_BUFFER);
59 }
60 }
61
62 public void setContent(ChannelBuffer content) {
63 if (content == null) {
64 content = ChannelBuffers.EMPTY_BUFFER;
65 }
66 if (content.readable() && isChunked()) {
67 throw new IllegalArgumentException(
68 "non-empty content disallowed if this.chunked == true");
69 }
70 this.content = content;
71 }
72
73 public HttpVersion getProtocolVersion() {
74 return version;
75 }
76
77 public void setProtocolVersion(HttpVersion version) {
78 if (version == null) {
79 throw new NullPointerException("version");
80 }
81 this.version = version;
82 }
83
84 public ChannelBuffer getContent() {
85 return content;
86 }
87
88 @Override
89 public String toString() {
90 StringBuilder buf = new StringBuilder();
91 buf.append(getClass().getSimpleName());
92 buf.append("(version: ");
93 buf.append(getProtocolVersion().getText());
94 buf.append(", keepAlive: ");
95 buf.append(HttpHeaders.isKeepAlive(this));
96 buf.append(", chunked: ");
97 buf.append(isChunked());
98 buf.append(')');
99 buf.append(StringUtil.NEWLINE);
100 appendHeaders(buf);
101
102
103 buf.setLength(buf.length() - StringUtil.NEWLINE.length());
104 return buf.toString();
105 }
106
107 void appendHeaders(StringBuilder buf) {
108 for (Map.Entry<String, String> e: headers()) {
109 buf.append(e.getKey());
110 buf.append(": ");
111 buf.append(e.getValue());
112 buf.append(StringUtil.NEWLINE);
113 }
114 }
115 }