View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.http2;
17  
18  import java.nio.charset.Charset;
19  
20  /**
21   * Default FileUpload implementation that stores file into memory.<br><br>
22   *
23   * Warning: be aware of the memory limitation.
24   *
25   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
26   * @author Andy Taylor (andy.taylor@jboss.org)
27   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
28   * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
29   *
30   */
31  public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUpload {
32  
33      private String filename = null;
34  
35      private String contentType = null;
36  
37      private String contentTransferEncoding = null;
38  
39      public MemoryFileUpload(String name, String filename, String contentType,
40              String contentTransferEncoding, Charset charset, long size)
41              throws NullPointerException, IllegalArgumentException {
42          super(name, charset, size);
43          setFilename(filename);
44          setContentType(contentType);
45          setContentTransferEncoding(contentTransferEncoding);
46      }
47  
48      public HttpDataType getHttpDataType() {
49          return HttpDataType.FileUpload;
50      }
51  
52      public String getFilename() {
53          return filename;
54      }
55  
56      public void setFilename(String filename) {
57          if (filename == null) {
58              throw new NullPointerException("filename");
59          }
60          this.filename = filename;
61      }
62  
63      @Override
64      public int hashCode() {
65          return getName().hashCode();
66      }
67  
68      @Override
69      public boolean equals(Object o) {
70          if (!(o instanceof Attribute)) {
71              return false;
72          }
73          Attribute attribute = (Attribute) o;
74          return getName().equalsIgnoreCase(attribute.getName());
75      }
76  
77      public int compareTo(InterfaceHttpData arg0) {
78          if (!(arg0 instanceof FileUpload)) {
79              throw new ClassCastException("Cannot compare " + getHttpDataType() +
80                      " with " + arg0.getHttpDataType());
81          }
82          return compareTo((FileUpload) arg0);
83      }
84  
85      public int compareTo(FileUpload o) {
86          int v;
87          v = getName().compareToIgnoreCase(o.getName());
88          if (v != 0) {
89              return v;
90          }
91          // TODO should we compare size for instance ?
92          return v;
93      }
94  
95      public void setContentType(String contentType) {
96          if (contentType == null) {
97              throw new NullPointerException("contentType");
98          }
99          this.contentType = contentType;
100     }
101 
102     public String getContentType() {
103         return contentType;
104     }
105 
106     public String getContentTransferEncoding() {
107         return contentTransferEncoding;
108     }
109 
110     public void setContentTransferEncoding(String contentTransferEncoding) {
111         this.contentTransferEncoding = contentTransferEncoding;
112     }
113 
114     @Override
115     public String toString() {
116         return HttpPostBodyUtil.CONTENT_DISPOSITION+": "+
117             HttpPostBodyUtil.FORM_DATA+"; "+HttpPostBodyUtil.NAME+"=\"" + getName() +
118             "\"; "+HttpPostBodyUtil.FILENAME+"=\"" + filename + "\"\r\n" +
119             HttpHeaders.Names.CONTENT_TYPE+": " + contentType +
120             (charset != null? "; "+HttpHeaders.Values.CHARSET+"=" + charset + "\r\n" : "\r\n") +
121             HttpHeaders.Names.CONTENT_LENGTH+": " + length() + "\r\n" +
122             "Completed: " + isCompleted() +
123             "\r\nIsInMemory: " + isInMemory();
124     }
125 }