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  
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          this.filename = FileUploadUtil.validateFileNameForMultiPart(filename);
60      }
61  
62      @Override
63      public int hashCode() {
64          return FileUploadUtil.hashCode(this);
65      }
66  
67      @Override
68      public boolean equals(Object o) {
69          return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
70      }
71  
72      @Override
73      public int compareTo(InterfaceHttpData o) {
74          if (!(o instanceof FileUpload)) {
75              throw new ClassCastException("Cannot compare " + getHttpDataType() +
76                      " with " + o.getHttpDataType());
77          }
78          return compareTo((FileUpload) o);
79      }
80  
81      public int compareTo(FileUpload o) {
82          return FileUploadUtil.compareTo(this, o);
83      }
84  
85      @Override
86      public void setContentType(String contentType) {
87          this.contentType = FileUploadUtil.validateContentTypeForMultiPart(contentType);
88      }
89  
90      @Override
91      public String getContentType() {
92          return contentType;
93      }
94  
95      @Override
96      public String getContentTransferEncoding() {
97          return contentTransferEncoding;
98      }
99  
100     @Override
101     public void setContentTransferEncoding(String contentTransferEncoding) {
102         this.contentTransferEncoding = contentTransferEncoding;
103     }
104 
105     @Override
106     public String toString() {
107         return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
108                HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
109             "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
110             HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
111             (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset().name() + "\r\n" : "\r\n") +
112             HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
113             "Completed: " + isCompleted() +
114             "\r\nIsInMemory: " + isInMemory();
115     }
116 
117     @Override
118     public FileUpload copy() {
119         final ByteBuf content = content();
120         return replace(content != null ? content.copy() : content);
121     }
122 
123     @Override
124     public FileUpload duplicate() {
125         final ByteBuf content = content();
126         return replace(content != null ? content.duplicate() : content);
127     }
128 
129     @Override
130     public FileUpload retainedDuplicate() {
131         ByteBuf content = content();
132         if (content != null) {
133             content = content.retainedDuplicate();
134             boolean success = false;
135             try {
136                 FileUpload duplicate = replace(content);
137                 success = true;
138                 return duplicate;
139             } finally {
140                 if (!success) {
141                     content.release();
142                 }
143             }
144         } else {
145             return replace(null);
146         }
147     }
148 
149     @Override
150     public FileUpload replace(ByteBuf content) {
151         MemoryFileUpload upload = new MemoryFileUpload(
152                 getName(), getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
153         if (content != null) {
154             try {
155                 upload.setContent(content);
156             } catch (IOException e) {
157                 throw new ChannelException(e);
158             }
159         }
160         upload.setCompleted(isCompleted());
161         return upload;
162     }
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 }