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    *   https://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.internal.ObjectUtil;
22  
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  
26  import static io.netty.buffer.Unpooled.wrappedBuffer;
27  
28  /**
29   * Disk implementation of Attributes
30   */
31  public class DiskAttribute extends AbstractDiskHttpData implements Attribute {
32      public static String baseDirectory;
33  
34      public static boolean deleteOnExitTemporaryFile = true;
35  
36      public static final String prefix = "Attr_";
37  
38      public static final String postfix = ".att";
39  
40      private String baseDir;
41  
42      private boolean deleteOnExit;
43  
44      /**
45       * Constructor used for huge Attribute
46       */
47      public DiskAttribute(String name) {
48          this(name, HttpConstants.DEFAULT_CHARSET);
49      }
50  
51      public DiskAttribute(String name, String baseDir, boolean deleteOnExit) {
52          this(name, HttpConstants.DEFAULT_CHARSET);
53          this.baseDir = baseDir == null ? baseDirectory : baseDir;
54          this.deleteOnExit = deleteOnExit;
55      }
56  
57      public DiskAttribute(String name, long definedSize) {
58          this(name, definedSize, HttpConstants.DEFAULT_CHARSET, baseDirectory, deleteOnExitTemporaryFile);
59      }
60  
61      public DiskAttribute(String name, long definedSize, String baseDir, boolean deleteOnExit) {
62          this(name, definedSize, HttpConstants.DEFAULT_CHARSET);
63          this.baseDir = baseDir == null ? baseDirectory : baseDir;
64          this.deleteOnExit = deleteOnExit;
65      }
66  
67      public DiskAttribute(String name, Charset charset) {
68          this(name, charset, baseDirectory, deleteOnExitTemporaryFile);
69      }
70  
71      public DiskAttribute(String name, Charset charset, String baseDir, boolean deleteOnExit) {
72          super(name, charset, 0);
73          this.baseDir = baseDir == null ? baseDirectory : baseDir;
74          this.deleteOnExit = deleteOnExit;
75      }
76  
77      public DiskAttribute(String name, long definedSize, Charset charset) {
78          this(name, definedSize, charset, baseDirectory, deleteOnExitTemporaryFile);
79      }
80  
81      public DiskAttribute(String name, long definedSize, Charset charset, String baseDir, boolean deleteOnExit) {
82          super(name, charset, definedSize);
83          this.baseDir = baseDir == null ? baseDirectory : baseDir;
84          this.deleteOnExit = deleteOnExit;
85      }
86  
87      public DiskAttribute(String name, String value) throws IOException {
88          this(name, value, HttpConstants.DEFAULT_CHARSET);
89      }
90  
91      public DiskAttribute(String name, String value, Charset charset) throws IOException {
92          this(name, value, charset, baseDirectory, deleteOnExitTemporaryFile);
93      }
94  
95      public DiskAttribute(String name, String value, Charset charset,
96                           String baseDir, boolean deleteOnExit) throws IOException {
97          super(name, charset, 0); // Attribute have no default size
98          setValue(value);
99          this.baseDir = baseDir == null ? baseDirectory : baseDir;
100         this.deleteOnExit = deleteOnExit;
101     }
102 
103     @Override
104     public HttpDataType getHttpDataType() {
105         return HttpDataType.Attribute;
106     }
107 
108     @Override
109     public String getValue() throws IOException {
110         byte [] bytes = get();
111         return new String(bytes, getCharset());
112     }
113 
114     @Override
115     public void setValue(String value) throws IOException {
116         ObjectUtil.checkNotNull(value, "value");
117         byte [] bytes = value.getBytes(getCharset());
118         checkSize(bytes.length);
119         ByteBuf buffer = wrappedBuffer(bytes);
120         if (definedSize > 0) {
121             definedSize = buffer.readableBytes();
122         }
123         setContent(buffer);
124     }
125 
126     @Override
127     public void addContent(ByteBuf buffer, boolean last) throws IOException {
128         final long newDefinedSize = size + buffer.readableBytes();
129         try {
130             checkSize(newDefinedSize);
131         } catch (IOException e) {
132             buffer.release();
133             throw e;
134         }
135         if (definedSize > 0 && definedSize < newDefinedSize) {
136             definedSize = newDefinedSize;
137         }
138         super.addContent(buffer, last);
139     }
140 
141     @Override
142     public int hashCode() {
143         return getName().hashCode();
144     }
145 
146     @Override
147     public boolean equals(Object o) {
148         if (!(o instanceof Attribute)) {
149             return false;
150         }
151         Attribute attribute = (Attribute) o;
152         return getName().equalsIgnoreCase(attribute.getName());
153     }
154 
155     @Override
156     public int compareTo(InterfaceHttpData o) {
157         if (!(o instanceof Attribute)) {
158             throw new ClassCastException("Cannot compare " + getHttpDataType() +
159                     " with " + o.getHttpDataType());
160         }
161         return compareTo((Attribute) o);
162     }
163 
164     public int compareTo(Attribute o) {
165         return getName().compareToIgnoreCase(o.getName());
166     }
167 
168     @Override
169     public String toString() {
170         try {
171             return getName() + '=' + getValue();
172         } catch (IOException e) {
173             return getName() + '=' + e;
174         }
175     }
176 
177     @Override
178     protected boolean deleteOnExit() {
179         return deleteOnExit;
180     }
181 
182     @Override
183     protected String getBaseDirectory() {
184         return baseDir;
185     }
186 
187     @Override
188     protected String getDiskFilename() {
189         return getName() + postfix;
190     }
191 
192     @Override
193     protected String getPostfix() {
194         return postfix;
195     }
196 
197     @Override
198     protected String getPrefix() {
199         return prefix;
200     }
201 
202     @Override
203     public Attribute copy() {
204         final ByteBuf content = content();
205         return replace(content != null ? content.copy() : null);
206     }
207 
208     @Override
209     public Attribute duplicate() {
210         final ByteBuf content = content();
211         return replace(content != null ? content.duplicate() : null);
212     }
213 
214     @Override
215     public Attribute retainedDuplicate() {
216         ByteBuf content = content();
217         if (content != null) {
218             content = content.retainedDuplicate();
219             boolean success = false;
220             try {
221                 Attribute duplicate = replace(content);
222                 success = true;
223                 return duplicate;
224             } finally {
225                 if (!success) {
226                     content.release();
227                 }
228             }
229         } else {
230             return replace(null);
231         }
232     }
233 
234     @Override
235     public Attribute replace(ByteBuf content) {
236         DiskAttribute attr = new DiskAttribute(getName(), baseDir, deleteOnExit);
237         attr.setCharset(getCharset());
238         if (content != null) {
239             try {
240                 attr.setContent(content);
241             } catch (IOException e) {
242                 throw new ChannelException(e);
243             }
244         }
245         attr.setCompleted(isCompleted());
246         return attr;
247     }
248 
249     @Override
250     public Attribute retain(int increment) {
251         super.retain(increment);
252         return this;
253     }
254 
255     @Override
256     public Attribute retain() {
257         super.retain();
258         return this;
259     }
260 
261     @Override
262     public Attribute touch() {
263         super.touch();
264         return this;
265     }
266 
267     @Override
268     public Attribute touch(Object hint) {
269         super.touch(hint);
270         return this;
271     }
272 }