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 io.netty.handler.codec.http.multipart;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelException;
20  import io.netty.handler.codec.http.HttpConstants;
21  import io.netty.util.AbstractReferenceCounted;
22  
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  import java.util.regex.Pattern;
26  
27  /**
28   * Abstract HttpData implementation
29   */
30  public abstract class AbstractHttpData extends AbstractReferenceCounted implements HttpData {
31  
32      private static final Pattern STRIP_PATTERN = Pattern.compile("(?:^\\s+|\\s+$|\\n)");
33      private static final Pattern REPLACE_PATTERN = Pattern.compile("[\\r\\t]");
34  
35      private final String name;
36      protected long definedSize;
37      protected long size;
38      private Charset charset = HttpConstants.DEFAULT_CHARSET;
39      private boolean completed;
40      private long maxSize = DefaultHttpDataFactory.MAXSIZE;
41  
42      protected AbstractHttpData(String name, Charset charset, long size) {
43          if (name == null) {
44              throw new NullPointerException("name");
45          }
46  
47          name = REPLACE_PATTERN.matcher(name).replaceAll(" ");
48          name = STRIP_PATTERN.matcher(name).replaceAll("");
49  
50          if (name.isEmpty()) {
51              throw new IllegalArgumentException("empty name");
52          }
53  
54          this.name = name;
55          if (charset != null) {
56              setCharset(charset);
57          }
58          definedSize = size;
59      }
60  
61      @Override
62      public long getMaxSize() { return maxSize; }
63  
64      @Override
65      public void setMaxSize(long maxSize) {
66          this.maxSize = maxSize;
67      }
68  
69      @Override
70      public void checkSize(long newSize) throws IOException {
71          if (maxSize >= 0 && newSize > maxSize) {
72              throw new IOException("Size exceed allowed maximum capacity");
73          }
74      }
75  
76      @Override
77      public String getName() {
78          return name;
79      }
80  
81      @Override
82      public boolean isCompleted() {
83          return completed;
84      }
85  
86      protected void setCompleted() {
87          completed = true;
88      }
89  
90      @Override
91      public Charset getCharset() {
92          return charset;
93      }
94  
95      @Override
96      public void setCharset(Charset charset) {
97          if (charset == null) {
98              throw new NullPointerException("charset");
99          }
100         this.charset = charset;
101     }
102 
103     @Override
104     public long length() {
105         return size;
106     }
107 
108     @Override
109     public long definedLength() {
110         return definedSize;
111     }
112 
113     @Override
114     public ByteBuf content() {
115         try {
116             return getByteBuf();
117         } catch (IOException e) {
118             throw new ChannelException(e);
119         }
120     }
121 
122     @Override
123     protected void deallocate() {
124         delete();
125     }
126 
127     @Override
128     public HttpData retain() {
129         super.retain();
130         return this;
131     }
132 
133     @Override
134     public HttpData retain(int increment) {
135         super.retain(increment);
136         return this;
137     }
138 
139     @Override
140     public abstract HttpData touch();
141 
142     @Override
143     public abstract HttpData touch(Object hint);
144 }