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.nio.charset.Charset;
21  
22  /**
23   * Default FileUpload implementation that stores file into memory.<br><br>
24   *
25   * Warning: be aware of the memory limitation.
26   */
27  public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUpload {
28  
29      private String filename;
30  
31      private String contentType;
32  
33      private String contentTransferEncoding;
34  
35      public MemoryFileUpload(String name, String filename, String contentType,
36              String contentTransferEncoding, Charset charset, long size) {
37          super(name, charset, size);
38          setFilename(filename);
39          setContentType(contentType);
40          setContentTransferEncoding(contentTransferEncoding);
41      }
42  
43      public HttpDataType getHttpDataType() {
44          return HttpDataType.FileUpload;
45      }
46  
47      public String getFilename() {
48          return filename;
49      }
50  
51      public void setFilename(String filename) {
52          if (filename == null) {
53              throw new NullPointerException("filename");
54          }
55          this.filename = filename;
56      }
57  
58      @Override
59      public int hashCode() {
60          return getName().hashCode();
61      }
62  
63      @Override
64      public boolean equals(Object o) {
65          if (!(o instanceof Attribute)) {
66              return false;
67          }
68          Attribute attribute = (Attribute) o;
69          return getName().equalsIgnoreCase(attribute.getName());
70      }
71  
72      public int compareTo(InterfaceHttpData o) {
73          if (!(o instanceof FileUpload)) {
74              throw new ClassCastException("Cannot compare " + getHttpDataType() +
75                      " with " + o.getHttpDataType());
76          }
77          return compareTo((FileUpload) o);
78      }
79  
80      public int compareTo(FileUpload o) {
81          int v;
82          v = getName().compareToIgnoreCase(o.getName());
83          if (v != 0) {
84              return v;
85          }
86          // TODO should we compare size for instance ?
87          return v;
88      }
89  
90      public void setContentType(String contentType) {
91          if (contentType == null) {
92              throw new NullPointerException("contentType");
93          }
94          this.contentType = contentType;
95      }
96  
97      public String getContentType() {
98          return contentType;
99      }
100 
101     public String getContentTransferEncoding() {
102         return contentTransferEncoding;
103     }
104 
105     public void setContentTransferEncoding(String contentTransferEncoding) {
106         this.contentTransferEncoding = contentTransferEncoding;
107     }
108 
109     @Override
110     public String toString() {
111         return HttpPostBodyUtil.CONTENT_DISPOSITION + ": " +
112             HttpPostBodyUtil.FORM_DATA + "; " + HttpPostBodyUtil.NAME + "=\"" + getName() +
113             "\"; " + HttpPostBodyUtil.FILENAME + "=\"" + filename + "\"\r\n" +
114             HttpHeaders.Names.CONTENT_TYPE + ": " + contentType +
115             (charset != null? "; " + HttpHeaders.Values.CHARSET + '=' + charset.name() + "\r\n" : "\r\n") +
116             HttpHeaders.Names.CONTENT_LENGTH + ": " + length() + "\r\n" +
117             "Completed: " + isCompleted() +
118             "\r\nIsInMemory: " + isInMemory();
119     }
120 }