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 org.jboss.netty.handler.codec.http.multipart;
17  
18  import org.jboss.netty.handler.codec.http.HttpHeaders;
19  
20  import java.io.File;
21  import java.nio.charset.Charset;
22  
23  /**
24   * Disk FileUpload implementation that stores file into real files
25   */
26  public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
27      public static String baseDirectory;
28  
29      public static boolean deleteOnExitTemporaryFile = true;
30  
31      public static final String prefix = "FUp_";
32  
33      public static final String postfix = ".tmp";
34  
35      private String filename;
36  
37      private String contentType;
38  
39      private String contentTransferEncoding;
40  
41      public DiskFileUpload(String name, String filename, String contentType,
42              String contentTransferEncoding, Charset charset, long size) {
43          super(name, charset, size);
44          setFilename(filename);
45          setContentType(contentType);
46          setContentTransferEncoding(contentTransferEncoding);
47      }
48  
49      public HttpDataType getHttpDataType() {
50          return HttpDataType.FileUpload;
51      }
52  
53      public String getFilename() {
54          return filename;
55      }
56  
57      public void setFilename(String filename) {
58          if (filename == null) {
59              throw new NullPointerException("filename");
60          }
61          this.filename = filename;
62      }
63  
64      @Override
65      public int hashCode() {
66          return getName().hashCode();
67      }
68  
69      @Override
70      public boolean equals(Object o) {
71          if (!(o instanceof Attribute)) {
72              return false;
73          }
74          Attribute attribute = (Attribute) o;
75          return getName().equalsIgnoreCase(attribute.getName());
76      }
77  
78      public int compareTo(InterfaceHttpData o) {
79          if (!(o instanceof FileUpload)) {
80              throw new ClassCastException("Cannot compare " + getHttpDataType() +
81                      " with " + o.getHttpDataType());
82          }
83          return compareTo((FileUpload) o);
84      }
85  
86      public int compareTo(FileUpload o) {
87          int v;
88          v = getName().compareToIgnoreCase(o.getName());
89          if (v != 0) {
90              return v;
91          }
92          // TODO should we compare size ?
93          return v;
94      }
95  
96      public void setContentType(String contentType) {
97          if (contentType == null) {
98              throw new NullPointerException("contentType");
99          }
100         this.contentType = contentType;
101     }
102 
103     public String getContentType() {
104         return contentType;
105     }
106 
107     public String getContentTransferEncoding() {
108         return contentTransferEncoding;
109     }
110 
111     public void setContentTransferEncoding(String contentTransferEncoding) {
112         this.contentTransferEncoding = contentTransferEncoding;
113     }
114 
115     @Override
116     public String toString() {
117         return HttpPostBodyUtil.CONTENT_DISPOSITION + ": " +
118             HttpPostBodyUtil.FORM_DATA + "; " + HttpPostBodyUtil.NAME + "=\"" + getName() +
119                 "\"; " + HttpPostBodyUtil.FILENAME + "=\"" + filename + "\"\r\n" +
120                 HttpHeaders.Names.CONTENT_TYPE + ": " + contentType +
121                 (charset != null? "; " + HttpHeaders.Values.CHARSET + '=' + charset.name() + "\r\n" : "\r\n") +
122                 HttpHeaders.Names.CONTENT_LENGTH + ": " + length() + "\r\n" +
123                 "Completed: " + isCompleted() +
124                 "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " +
125                 file.getAbsolutePath() + " DefaultDeleteAfter: " +
126                 deleteOnExitTemporaryFile;
127     }
128 
129     @Override
130     protected boolean deleteOnExit() {
131         return deleteOnExitTemporaryFile;
132     }
133 
134     @Override
135     protected String getBaseDirectory() {
136         return baseDirectory;
137     }
138 
139     @Override
140     protected String getDiskFilename() {
141         File file = new File(filename);
142         return file.getName();
143     }
144 
145     @Override
146     protected String getPostfix() {
147         return postfix;
148     }
149 
150     @Override
151     protected String getPrefix() {
152         return prefix;
153     }
154 }