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