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 FileUploadUtil.hashCode(this);
74      }
75  
76      @Override
77      public boolean equals(Object o) {
78          return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
79      }
80  
81      @Override
82      public int compareTo(InterfaceHttpData o) {
83          if (!(o instanceof FileUpload)) {
84              throw new ClassCastException("Cannot compare " + getHttpDataType() +
85                      " with " + o.getHttpDataType());
86          }
87          return compareTo((FileUpload) o);
88      }
89  
90      public int compareTo(FileUpload o) {
91          return FileUploadUtil.compareTo(this, o);
92      }
93  
94      @Override
95      public void setContentType(String contentType) {
96          if (contentType == null) {
97              throw new NullPointerException("contentType");
98          }
99          this.contentType = contentType;
100     }
101 
102     @Override
103     public String getContentType() {
104         return contentType;
105     }
106 
107     @Override
108     public String getContentTransferEncoding() {
109         return contentTransferEncoding;
110     }
111 
112     @Override
113     public void setContentTransferEncoding(String contentTransferEncoding) {
114         this.contentTransferEncoding = contentTransferEncoding;
115     }
116 
117     @Override
118     public String toString() {
119         File file = null;
120         try {
121             file = getFile();
122         } catch (IOException e) {
123             // Should not occur.
124         }
125 
126         return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
127                HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
128                 "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
129                 HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
130                 (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset().name() + "\r\n" : "\r\n") +
131                 HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
132                 "Completed: " + isCompleted() +
133                 "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " +
134                 (file != null ? file.getAbsolutePath() : "null") + " DefaultDeleteAfter: " +
135                 deleteOnExitTemporaryFile;
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 "upload";
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 FileUpload copy() {
165         final ByteBuf content = content();
166         return replace(content != null ? content.copy() : null);
167     }
168 
169     @Override
170     public FileUpload duplicate() {
171         final ByteBuf content = content();
172         return replace(content != null ? content.duplicate() : null);
173     }
174 
175     @Override
176     public FileUpload retainedDuplicate() {
177         ByteBuf content = content();
178         if (content != null) {
179             content = content.retainedDuplicate();
180             boolean success = false;
181             try {
182                 FileUpload 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 FileUpload replace(ByteBuf content) {
197         DiskFileUpload upload = new DiskFileUpload(
198                 getName(), getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
199         if (content != null) {
200             try {
201                 upload.setContent(content);
202             } catch (IOException e) {
203                 throw new ChannelException(e);
204             }
205         }
206         return upload;
207     }
208 
209     @Override
210     public FileUpload retain(int increment) {
211         super.retain(increment);
212         return this;
213     }
214 
215     @Override
216     public FileUpload retain() {
217         super.retain();
218         return this;
219     }
220 
221     @Override
222     public FileUpload touch() {
223         super.touch();
224         return this;
225     }
226 
227     @Override
228     public FileUpload touch(Object hint) {
229         super.touch(hint);
230         return this;
231     }
232 }