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