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 HttpChunkTrailer} implementation.
28   */
29  public class DefaultHttpChunkTrailer implements HttpChunkTrailer {
30  
31      private final HttpHeaders trailingHeaders = new TrailingHeaders(true);
32  
33      public boolean isLast() {
34          return true;
35      }
36  
37      public ChannelBuffer getContent() {
38          return ChannelBuffers.EMPTY_BUFFER;
39      }
40  
41      public void setContent(ChannelBuffer content) {
42          throw new IllegalStateException("read-only");
43      }
44  
45      public HttpHeaders trailingHeaders() {
46          return trailingHeaders;
47      }
48  
49      @Override
50      public String toString() {
51          StringBuilder buf = new StringBuilder(super.toString());
52          buf.append(StringUtil.NEWLINE);
53          appendHeaders(buf);
54  
55          // Remove the last newline.
56          buf.setLength(buf.length() - StringUtil.NEWLINE.length());
57          return buf.toString();
58      }
59  
60      private void appendHeaders(StringBuilder buf) {
61          for (Map.Entry<String, String> e: trailingHeaders()) {
62              buf.append(e.getKey());
63              buf.append(": ");
64              buf.append(e.getValue());
65              buf.append(StringUtil.NEWLINE);
66          }
67      }
68  
69      private static final class TrailingHeaders extends DefaultHttpHeaders {
70  
71          TrailingHeaders(boolean validateHeaders) {
72              super(validateHeaders);
73          }
74  
75          @Override
76          public HttpHeaders add(String name, Object value) {
77              if (validate) {
78                  validateName(name);
79              }
80              return super.add(name, value);
81          }
82  
83          @Override
84          public HttpHeaders add(String name, Iterable<?> values) {
85              if (validate) {
86                  validateName(name);
87              }
88              return super.add(name, values);
89          }
90  
91          @Override
92          public HttpHeaders set(String name, Iterable<?> values) {
93              if (validate) {
94                  validateName(name);
95              }
96              return super.set(name, values);
97          }
98  
99          @Override
100         public HttpHeaders set(String name, Object value) {
101             if (validate) {
102                 validateName(name);
103             }
104             return super.set(name, value);
105         }
106 
107         private static void validateName(String name) {
108             if (name.equalsIgnoreCase(HttpHeaders.Names.CONTENT_LENGTH) ||
109                     name.equalsIgnoreCase(HttpHeaders.Names.TRANSFER_ENCODING) ||
110                     name.equalsIgnoreCase(HttpHeaders.Names.TRAILER)) {
111                 throw new IllegalArgumentException(
112                         "prohibited trailing header: " + name);
113             }
114         }
115     }
116 }