View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * The default {@link HttpMessage} implementation.
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       * Creates a new instance.
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         // Remove the last newline.
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 }