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      @Deprecated
48      public void addHeader(final String name, final Object value) {
49          headers.add(name, value);
50      }
51  
52      @Deprecated
53      public void setHeader(final String name, final Object value) {
54          headers.set(name, value);
55      }
56  
57      @Deprecated
58      public void setHeader(final String name, final Iterable<?> values) {
59          headers.set(name, values);
60      }
61  
62      @Deprecated
63      public void removeHeader(final String name) {
64          headers.remove(name);
65      }
66  
67      public boolean isChunked() {
68          if (chunked) {
69              return true;
70          } else {
71              return HttpCodecUtil.isTransferEncodingChunked(this);
72          }
73      }
74  
75      public void setChunked(boolean chunked) {
76          this.chunked = chunked;
77          if (chunked) {
78              setContent(ChannelBuffers.EMPTY_BUFFER);
79          }
80      }
81  
82      @Deprecated
83      public void clearHeaders() {
84          headers.clear();
85      }
86  
87      public void setContent(ChannelBuffer content) {
88          if (content == null) {
89              content = ChannelBuffers.EMPTY_BUFFER;
90          }
91          if (content.readable() && isChunked()) {
92              throw new IllegalArgumentException(
93                      "non-empty content disallowed if this.chunked == true");
94          }
95          this.content = content;
96      }
97  
98      @Deprecated
99      public String getHeader(final String name) {
100         return headers.get(name);
101     }
102 
103     @Deprecated
104     public List<String> getHeaders(final String name) {
105         return headers.getAll(name);
106     }
107 
108     @Deprecated
109     public List<Map.Entry<String, String>> getHeaders() {
110         return headers.entries();
111     }
112 
113     @Deprecated
114     public boolean containsHeader(final String name) {
115         return headers.contains(name);
116     }
117 
118     @Deprecated
119     public Set<String> getHeaderNames() {
120         return headers.names();
121     }
122 
123     public HttpVersion getProtocolVersion() {
124         return version;
125     }
126 
127     public void setProtocolVersion(HttpVersion version) {
128         if (version == null) {
129             throw new NullPointerException("version");
130         }
131         this.version = version;
132     }
133 
134     public ChannelBuffer getContent() {
135         return content;
136     }
137 
138     @Override
139     public String toString() {
140         StringBuilder buf = new StringBuilder();
141         buf.append(getClass().getSimpleName());
142         buf.append("(version: ");
143         buf.append(getProtocolVersion().getText());
144         buf.append(", keepAlive: ");
145         buf.append(HttpHeaders.isKeepAlive(this));
146         buf.append(", chunked: ");
147         buf.append(isChunked());
148         buf.append(')');
149         buf.append(StringUtil.NEWLINE);
150         appendHeaders(buf);
151 
152         // Remove the last newline.
153         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
154         return buf.toString();
155     }
156 
157     void appendHeaders(StringBuilder buf) {
158         for (Map.Entry<String, String> e: headers()) {
159             buf.append(e.getKey());
160             buf.append(": ");
161             buf.append(e.getValue());
162             buf.append(StringUtil.NEWLINE);
163         }
164     }
165 }