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.nio.charset.Charset;
19  
20  /**
21   * Abstract HttpData implementation
22   *
23   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
24   * @author Andy Taylor (andy.taylor@jboss.org)
25   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
26   * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
27   *
28   */
29  public abstract class AbstractHttpData implements HttpData {
30  
31      protected final String name;
32  
33      protected long definedSize = 0;
34  
35      protected long size = 0;
36  
37      protected Charset charset = HttpCodecUtil.DEFAULT_CHARSET;
38  
39      protected boolean completed = false;
40  
41      public AbstractHttpData(String name, Charset charset, long size)
42      throws NullPointerException, IllegalArgumentException {
43          if (name == null) {
44              throw new NullPointerException("name");
45          }
46          name = name.trim();
47          if (name.length() == 0) {
48              throw new IllegalArgumentException("empty name");
49          }
50  
51          for (int i = 0; i < name.length(); i ++) {
52              char c = name.charAt(i);
53              if (c > 127) {
54                  throw new IllegalArgumentException(
55                          "name contains non-ascii character: " + name);
56              }
57  
58              // Check prohibited characters.
59              switch (c) {
60              case '=':
61              case ',':
62              case ';':
63              case ' ':
64              case '\t':
65              case '\r':
66              case '\n':
67              case '\f':
68              case 0x0b: // Vertical tab
69                  throw new IllegalArgumentException(
70                          "name contains one of the following prohibited characters: " +
71                          "=,; \\t\\r\\n\\v\\f: " + name);
72              }
73          }
74          this.name = name;
75          if (charset != null) {
76              setCharset(charset);
77          }
78          definedSize = size;
79      }
80  
81      public String getName() {
82          return name;
83      }
84  
85      public boolean isCompleted() {
86          return completed;
87      }
88  
89      public Charset getCharset() {
90          return charset;
91      }
92  
93      public void setCharset(Charset charset) {
94          if (charset == null) {
95              throw new NullPointerException("charset");
96          }
97          this.charset = charset;
98      }
99  
100     public long length() {
101         return size;
102     }
103 }