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