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