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