View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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 java.util.List;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.jboss.netty.buffer.ChannelBuffer;
23  import org.jboss.netty.buffer.ChannelBuffers;
24  import org.jboss.netty.util.internal.StringUtil;
25  
26  /**
27   * The default {@link HttpMessage} implementation.
28   *
29   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
30   * @author Andy Taylor (andy.taylor@jboss.org)
31   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
32   * @version $Rev: 2088 $, $Date: 2010-01-27 11:38:17 +0900 (Wed, 27 Jan 2010) $
33   */
34  public class DefaultHttpMessage implements HttpMessage {
35  
36      private final HttpHeaders headers = new HttpHeaders();
37      private HttpVersion version;
38      private ChannelBuffer content = ChannelBuffers.EMPTY_BUFFER;
39      private boolean chunked;
40  
41      /**
42       * Creates a new instance.
43       */
44      protected DefaultHttpMessage(final HttpVersion version) {
45          setProtocolVersion(version);
46      }
47  
48      public void addHeader(final String name, final Object value) {
49          headers.addHeader(name, value);
50      }
51  
52      public void setHeader(final String name, final Object value) {
53          headers.setHeader(name, value);
54      }
55  
56      public void setHeader(final String name, final Iterable<?> values) {
57          headers.setHeader(name, values);
58      }
59  
60      public void removeHeader(final String name) {
61          headers.removeHeader(name);
62      }
63  
64      @Deprecated
65      public long getContentLength() {
66          return HttpHeaders.getContentLength(this);
67      }
68  
69      @Deprecated
70      public long getContentLength(long defaultValue) {
71          return HttpHeaders.getContentLength(this, defaultValue);
72      }
73  
74      public boolean isChunked() {
75          if (chunked) {
76              return true;
77          } else {
78              return HttpCodecUtil.isTransferEncodingChunked(this);
79          }
80      }
81  
82      public void setChunked(boolean chunked) {
83          this.chunked = chunked;
84          if (chunked) {
85              setContent(ChannelBuffers.EMPTY_BUFFER);
86          }
87      }
88  
89      @Deprecated
90      public boolean isKeepAlive() {
91          return HttpHeaders.isKeepAlive(this);
92      }
93  
94      public void clearHeaders() {
95          headers.clearHeaders();
96      }
97  
98      public void setContent(ChannelBuffer content) {
99          if (content == null) {
100             content = ChannelBuffers.EMPTY_BUFFER;
101         }
102         if (content.readable() && isChunked()) {
103             throw new IllegalArgumentException(
104                     "non-empty content disallowed if this.chunked == true");
105         }
106         this.content = content;
107     }
108 
109     public String getHeader(final String name) {
110         List<String> values = getHeaders(name);
111         return values.size() > 0 ? values.get(0) : null;
112     }
113 
114     public List<String> getHeaders(final String name) {
115         return headers.getHeaders(name);
116     }
117 
118     public List<Map.Entry<String, String>> getHeaders() {
119         return headers.getHeaders();
120     }
121 
122     public boolean containsHeader(final String name) {
123         return headers.containsHeader(name);
124     }
125 
126     public Set<String> getHeaderNames() {
127         return headers.getHeaderNames();
128     }
129 
130     public HttpVersion getProtocolVersion() {
131         return version;
132     }
133 
134     public void setProtocolVersion(HttpVersion version) {
135         if (version == null) {
136             throw new NullPointerException("version");
137         }
138         this.version = version;
139     }
140 
141     public ChannelBuffer getContent() {
142         return content;
143     }
144 
145     @Override
146     public String toString() {
147         StringBuilder buf = new StringBuilder();
148         buf.append(getClass().getSimpleName());
149         buf.append("(version: ");
150         buf.append(getProtocolVersion().getText());
151         buf.append(", keepAlive: ");
152         buf.append(isKeepAlive());
153         buf.append(", chunked: ");
154         buf.append(isChunked());
155         buf.append(')');
156         buf.append(StringUtil.NEWLINE);
157         appendHeaders(buf);
158 
159         // Remove the last newline.
160         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
161         return buf.toString();
162     }
163 
164     void appendHeaders(StringBuilder buf) {
165         for (Map.Entry<String, String> e: getHeaders()) {
166             buf.append(e.getKey());
167             buf.append(": ");
168             buf.append(e.getValue());
169             buf.append(StringUtil.NEWLINE);
170         }
171     }
172 }