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.http2;
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: 1191 $, $Date: 2012-05-10 20:38:47 +0200 (jeu., 10 mai 2012) $
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         return headers.getHeader(name);
111     }
112 
113     public List<String> getHeaders(final String name) {
114         return headers.getHeaders(name);
115     }
116 
117     public List<Map.Entry<String, String>> getHeaders() {
118         return headers.getHeaders();
119     }
120 
121     public boolean containsHeader(final String name) {
122         return headers.containsHeader(name);
123     }
124 
125     public Set<String> getHeaderNames() {
126         return headers.getHeaderNames();
127     }
128 
129     public HttpVersion getProtocolVersion() {
130         return version;
131     }
132 
133     public void setProtocolVersion(HttpVersion version) {
134         if (version == null) {
135             throw new NullPointerException("version");
136         }
137         this.version = version;
138     }
139 
140     public ChannelBuffer getContent() {
141         return content;
142     }
143 
144     @Override
145     public String toString() {
146         StringBuilder buf = new StringBuilder();
147         buf.append(getClass().getSimpleName());
148         buf.append("(version: ");
149         buf.append(getProtocolVersion().getText());
150         buf.append(", keepAlive: ");
151         buf.append(isKeepAlive());
152         buf.append(", chunked: ");
153         buf.append(isChunked());
154         buf.append(')');
155         buf.append(StringUtil.NEWLINE);
156         appendHeaders(buf);
157 
158         // Remove the last newline.
159         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
160         return buf.toString();
161     }
162 
163     void appendHeaders(StringBuilder buf) {
164         for (Map.Entry<String, String> e: getHeaders()) {
165             buf.append(e.getKey());
166             buf.append(": ");
167             buf.append(e.getValue());
168             buf.append(StringUtil.NEWLINE);
169         }
170     }
171 }