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.*;
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, Charset charset) {
47          super(name, charset, 0);
48      }
49  
50      public DiskAttribute(String name, String value) throws IOException {
51          this(name, value, HttpConstants.DEFAULT_CHARSET);
52      }
53  
54      public DiskAttribute(String name, String value, Charset charset) throws IOException {
55          super(name, charset, 0); // Attribute have no default size
56          setValue(value);
57      }
58  
59      @Override
60      public HttpDataType getHttpDataType() {
61          return HttpDataType.Attribute;
62      }
63  
64      @Override
65      public String getValue() throws IOException {
66          byte [] bytes = get();
67          return new String(bytes, getCharset());
68      }
69  
70      @Override
71      public void setValue(String value) throws IOException {
72          if (value == null) {
73              throw new NullPointerException("value");
74          }
75          byte [] bytes = value.getBytes(getCharset());
76          checkSize(bytes.length);
77          ByteBuf buffer = wrappedBuffer(bytes);
78          if (definedSize > 0) {
79              definedSize = buffer.readableBytes();
80          }
81          setContent(buffer);
82      }
83  
84      @Override
85      public void addContent(ByteBuf buffer, boolean last) throws IOException {
86          final long newDefinedSize = size + buffer.readableBytes();
87          checkSize(newDefinedSize);
88          if (definedSize > 0 && definedSize < newDefinedSize) {
89              definedSize = newDefinedSize;
90          }
91          super.addContent(buffer, last);
92      }
93  
94      @Override
95      public int hashCode() {
96          return getName().hashCode();
97      }
98  
99      @Override
100     public boolean equals(Object o) {
101         if (!(o instanceof Attribute)) {
102             return false;
103         }
104         Attribute attribute = (Attribute) o;
105         return getName().equalsIgnoreCase(attribute.getName());
106     }
107 
108     @Override
109     public int compareTo(InterfaceHttpData o) {
110         if (!(o instanceof Attribute)) {
111             throw new ClassCastException("Cannot compare " + getHttpDataType() +
112                     " with " + o.getHttpDataType());
113         }
114         return compareTo((Attribute) o);
115     }
116 
117     public int compareTo(Attribute o) {
118         return getName().compareToIgnoreCase(o.getName());
119     }
120 
121     @Override
122     public String toString() {
123         try {
124             return getName() + '=' + getValue();
125         } catch (IOException e) {
126             return getName() + '=' + e;
127         }
128     }
129 
130     @Override
131     protected boolean deleteOnExit() {
132         return deleteOnExitTemporaryFile;
133     }
134 
135     @Override
136     protected String getBaseDirectory() {
137         return baseDirectory;
138     }
139 
140     @Override
141     protected String getDiskFilename() {
142         return getName() + postfix;
143     }
144 
145     @Override
146     protected String getPostfix() {
147         return postfix;
148     }
149 
150     @Override
151     protected String getPrefix() {
152         return prefix;
153     }
154 
155     @Override
156     public Attribute copy() {
157         DiskAttribute attr = new DiskAttribute(getName());
158         attr.setCharset(getCharset());
159         ByteBuf content = content();
160         if (content != null) {
161             try {
162                 attr.setContent(content.copy());
163             } catch (IOException e) {
164                 throw new ChannelException(e);
165             }
166         }
167         return attr;
168     }
169 
170     @Override
171     public Attribute duplicate() {
172         DiskAttribute attr = new DiskAttribute(getName());
173         attr.setCharset(getCharset());
174         ByteBuf content = content();
175         if (content != null) {
176             try {
177                 attr.setContent(content.duplicate());
178             } catch (IOException e) {
179                 throw new ChannelException(e);
180             }
181         }
182         return attr;
183     }
184 
185     @Override
186     public Attribute retain(int increment) {
187         super.retain(increment);
188         return this;
189     }
190 
191     @Override
192     public Attribute retain() {
193         super.retain();
194         return this;
195     }
196 
197     @Override
198     public Attribute touch() {
199         super.touch();
200         return this;
201     }
202 
203     @Override
204     public Attribute touch(Object hint) {
205         super.touch(hint);
206         return this;
207     }
208 }