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.IOException;
24  import java.nio.charset.Charset;
25  
26  /**
27   * Default FileUpload implementation that stores file into memory.<br><br>
28   *
29   * Warning: be aware of the memory limitation.
30   */
31  public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUpload {
32  
33      private String filename;
34  
35      private String contentType;
36  
37      private String contentTransferEncoding;
38  
39      public MemoryFileUpload(String name, String filename, String contentType,
40              String contentTransferEncoding, Charset charset, long size) {
41          super(name, charset, size);
42          setFilename(filename);
43          setContentType(contentType);
44          setContentTransferEncoding(contentTransferEncoding);
45      }
46  
47      @Override
48      public HttpDataType getHttpDataType() {
49          return HttpDataType.FileUpload;
50      }
51  
52      @Override
53      public String getFilename() {
54          return filename;
55      }
56  
57      @Override
58      public void setFilename(String filename) {
59          if (filename == null) {
60              throw new NullPointerException("filename");
61          }
62          this.filename = filename;
63      }
64  
65      @Override
66      public int hashCode() {
67          return getName().hashCode();
68      }
69  
70      @Override
71      public boolean equals(Object o) {
72          if (!(o instanceof Attribute)) {
73              return false;
74          }
75          Attribute attribute = (Attribute) o;
76          return getName().equalsIgnoreCase(attribute.getName());
77      }
78  
79      @Override
80      public int compareTo(InterfaceHttpData o) {
81          if (!(o instanceof FileUpload)) {
82              throw new ClassCastException("Cannot compare " + getHttpDataType() +
83                      " with " + o.getHttpDataType());
84          }
85          return compareTo((FileUpload) o);
86      }
87  
88      public int compareTo(FileUpload o) {
89          int v;
90          v = getName().compareToIgnoreCase(o.getName());
91          if (v != 0) {
92              return v;
93          }
94          // TODO should we compare size for instance ?
95          return v;
96      }
97  
98      @Override
99      public void setContentType(String contentType) {
100         if (contentType == null) {
101             throw new NullPointerException("contentType");
102         }
103         this.contentType = contentType;
104     }
105 
106     @Override
107     public String getContentType() {
108         return contentType;
109     }
110 
111     @Override
112     public String getContentTransferEncoding() {
113         return contentTransferEncoding;
114     }
115 
116     @Override
117     public void setContentTransferEncoding(String contentTransferEncoding) {
118         this.contentTransferEncoding = contentTransferEncoding;
119     }
120 
121     @Override
122     public String toString() {
123         return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
124                HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
125             "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
126             HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
127             (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset() + "\r\n" : "\r\n") +
128             HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
129             "Completed: " + isCompleted() +
130             "\r\nIsInMemory: " + isInMemory();
131     }
132 
133     @Override
134     public FileUpload copy() {
135         MemoryFileUpload upload = new MemoryFileUpload(getName(), getFilename(), getContentType(),
136                 getContentTransferEncoding(), getCharset(), size);
137         ByteBuf buf = content();
138         if (buf != null) {
139             try {
140                 upload.setContent(buf.copy());
141                 return upload;
142             } catch (IOException e) {
143                 throw new ChannelException(e);
144             }
145         }
146         return upload;
147     }
148 
149     @Override
150     public FileUpload duplicate() {
151         MemoryFileUpload upload = new MemoryFileUpload(getName(), getFilename(), getContentType(),
152                 getContentTransferEncoding(), getCharset(), size);
153         ByteBuf buf = content();
154         if (buf != null) {
155             try {
156                 upload.setContent(buf.duplicate());
157                 return upload;
158             } catch (IOException e) {
159                 throw new ChannelException(e);
160             }
161         }
162         return upload;
163     }
164     @Override
165     public FileUpload retain() {
166         super.retain();
167         return this;
168     }
169 
170     @Override
171     public FileUpload retain(int increment) {
172         super.retain(increment);
173         return this;
174     }
175 
176     @Override
177     public FileUpload touch() {
178         super.touch();
179         return this;
180     }
181 
182     @Override
183     public FileUpload touch(Object hint) {
184         super.touch(hint);
185         return this;
186     }
187 }