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.HttpHeaderNames;
21  import io.netty.handler.codec.http.HttpHeaderValues;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.charset.Charset;
26  
27  /**
28   * Disk FileUpload implementation that stores file into real files
29   */
30  public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
31      public static String baseDirectory;
32  
33      public static boolean deleteOnExitTemporaryFile = true;
34  
35      public static final String prefix = "FUp_";
36  
37      public static final String postfix = ".tmp";
38  
39      private String filename;
40  
41      private String contentType;
42  
43      private String contentTransferEncoding;
44  
45      public DiskFileUpload(String name, String filename, String contentType,
46              String contentTransferEncoding, Charset charset, long size) {
47          super(name, charset, size);
48          setFilename(filename);
49          setContentType(contentType);
50          setContentTransferEncoding(contentTransferEncoding);
51      }
52  
53      @Override
54      public HttpDataType getHttpDataType() {
55          return HttpDataType.FileUpload;
56      }
57  
58      @Override
59      public String getFilename() {
60          return filename;
61      }
62  
63      @Override
64      public void setFilename(String filename) {
65          if (filename == null) {
66              throw new NullPointerException("filename");
67          }
68          this.filename = filename;
69      }
70  
71      @Override
72      public int hashCode() {
73          return getName().hashCode();
74      }
75  
76      @Override
77      public boolean equals(Object o) {
78          if (!(o instanceof Attribute)) {
79              return false;
80          }
81          Attribute attribute = (Attribute) o;
82          return getName().equalsIgnoreCase(attribute.getName());
83      }
84  
85      @Override
86      public int compareTo(InterfaceHttpData o) {
87          if (!(o instanceof FileUpload)) {
88              throw new ClassCastException("Cannot compare " + getHttpDataType() +
89                      " with " + o.getHttpDataType());
90          }
91          return compareTo((FileUpload) o);
92      }
93  
94      public int compareTo(FileUpload o) {
95          int v;
96          v = getName().compareToIgnoreCase(o.getName());
97          if (v != 0) {
98              return v;
99          }
100         // TODO should we compare size ?
101         return v;
102     }
103 
104     @Override
105     public void setContentType(String contentType) {
106         if (contentType == null) {
107             throw new NullPointerException("contentType");
108         }
109         this.contentType = contentType;
110     }
111 
112     @Override
113     public String getContentType() {
114         return contentType;
115     }
116 
117     @Override
118     public String getContentTransferEncoding() {
119         return contentTransferEncoding;
120     }
121 
122     @Override
123     public void setContentTransferEncoding(String contentTransferEncoding) {
124         this.contentTransferEncoding = contentTransferEncoding;
125     }
126 
127     @Override
128     public String toString() {
129         File file = null;
130         try {
131             file = getFile();
132         } catch (IOException e) {
133             // Should not occur.
134         }
135 
136         return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
137                HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
138                 "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
139                 HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
140                 (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") +
141                 HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
142                 "Completed: " + isCompleted() +
143                 "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " +
144                 (file != null ? file.getAbsolutePath() : "null") + " DefaultDeleteAfter: " +
145                 deleteOnExitTemporaryFile;
146     }
147 
148     @Override
149     protected boolean deleteOnExit() {
150         return deleteOnExitTemporaryFile;
151     }
152 
153     @Override
154     protected String getBaseDirectory() {
155         return baseDirectory;
156     }
157 
158     @Override
159     protected String getDiskFilename() {
160         File file = new File(filename);
161         return file.getName();
162     }
163 
164     @Override
165     protected String getPostfix() {
166         return postfix;
167     }
168 
169     @Override
170     protected String getPrefix() {
171         return prefix;
172     }
173 
174     @Override
175     public FileUpload copy() {
176         DiskFileUpload upload = new DiskFileUpload(getName(),
177                 getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
178         ByteBuf buf = content();
179         if (buf != null) {
180             try {
181                 upload.setContent(buf.copy());
182             } catch (IOException e) {
183                 throw new ChannelException(e);
184             }
185         }
186         return upload;
187     }
188 
189     @Override
190     public FileUpload duplicate() {
191         DiskFileUpload upload = new DiskFileUpload(getName(),
192                 getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
193         ByteBuf buf = content();
194         if (buf != null) {
195             try {
196                 upload.setContent(buf.duplicate());
197             } catch (IOException e) {
198                 throw new ChannelException(e);
199             }
200         }
201         return upload;
202     }
203 
204     @Override
205     public FileUpload retain(int increment) {
206         super.retain(increment);
207         return this;
208     }
209 
210     @Override
211     public FileUpload retain() {
212         super.retain();
213         return this;
214     }
215 
216     @Override
217     public FileUpload touch() {
218         super.touch();
219         return this;
220     }
221 
222     @Override
223     public FileUpload touch(Object hint) {
224         super.touch(hint);
225         return this;
226     }
227 }