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 org.jboss.netty.handler.codec.http.multipart;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.nio.charset.Charset;
22  
23  import org.jboss.netty.buffer.ChannelBuffer;
24  
25  /**
26   * Mixed implementation using both in Memory and in File with a limit of size
27   */
28  public class MixedFileUpload implements FileUpload {
29      private FileUpload fileUpload;
30  
31      private final long limitSize;
32  
33      private final long definedSize;
34  
35      public MixedFileUpload(String name, String filename, String contentType,
36              String contentTransferEncoding, Charset charset, long size,
37              long limitSize) {
38          this.limitSize = limitSize;
39          if (size > this.limitSize) {
40              fileUpload = new DiskFileUpload(name, filename, contentType,
41                      contentTransferEncoding, charset, size);
42          } else {
43              fileUpload = new MemoryFileUpload(name, filename, contentType,
44                      contentTransferEncoding, charset, size);
45          }
46          definedSize = size;
47      }
48  
49      public void addContent(ChannelBuffer buffer, boolean last)
50              throws IOException {
51          if (fileUpload instanceof MemoryFileUpload) {
52              if (fileUpload.length() + buffer.readableBytes() > limitSize) {
53                  DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload
54                          .getName(), fileUpload.getFilename(), fileUpload
55                          .getContentType(), fileUpload
56                          .getContentTransferEncoding(), fileUpload.getCharset(),
57                          definedSize);
58                  if (((MemoryFileUpload) fileUpload).getChannelBuffer() != null) {
59                      diskFileUpload.addContent(((MemoryFileUpload) fileUpload)
60                          .getChannelBuffer(), false);
61                  }
62                  fileUpload = diskFileUpload;
63              }
64          }
65          fileUpload.addContent(buffer, last);
66      }
67  
68      public void delete() {
69          fileUpload.delete();
70      }
71  
72      public byte[] get() throws IOException {
73          return fileUpload.get();
74      }
75  
76      public ChannelBuffer getChannelBuffer() throws IOException {
77          return fileUpload.getChannelBuffer();
78      }
79  
80      public Charset getCharset() {
81          return fileUpload.getCharset();
82      }
83  
84      public String getContentType() {
85          return fileUpload.getContentType();
86      }
87  
88      public String getContentTransferEncoding() {
89          return fileUpload.getContentTransferEncoding();
90      }
91  
92      public String getFilename() {
93          return fileUpload.getFilename();
94      }
95  
96      public String getString() throws IOException {
97          return fileUpload.getString();
98      }
99  
100     public String getString(Charset encoding) throws IOException {
101         return fileUpload.getString(encoding);
102     }
103 
104     public boolean isCompleted() {
105         return fileUpload.isCompleted();
106     }
107 
108     public boolean isInMemory() {
109         return fileUpload.isInMemory();
110     }
111 
112     public long length() {
113         return fileUpload.length();
114     }
115 
116     public boolean renameTo(File dest) throws IOException {
117         return fileUpload.renameTo(dest);
118     }
119 
120     public void setCharset(Charset charset) {
121         fileUpload.setCharset(charset);
122     }
123 
124     public void setContent(ChannelBuffer buffer) throws IOException {
125         if (buffer.readableBytes() > limitSize) {
126             if (fileUpload instanceof MemoryFileUpload) {
127                 // change to Disk
128                 fileUpload = new DiskFileUpload(fileUpload
129                         .getName(), fileUpload.getFilename(), fileUpload
130                         .getContentType(), fileUpload
131                         .getContentTransferEncoding(), fileUpload.getCharset(),
132                         definedSize);
133             }
134         }
135         fileUpload.setContent(buffer);
136     }
137 
138     public void setContent(File file) throws IOException {
139         if (file.length() > limitSize) {
140             if (fileUpload instanceof MemoryFileUpload) {
141                 // change to Disk
142                 fileUpload = new DiskFileUpload(fileUpload
143                         .getName(), fileUpload.getFilename(), fileUpload
144                         .getContentType(), fileUpload
145                         .getContentTransferEncoding(), fileUpload.getCharset(),
146                         definedSize);
147             }
148         }
149         fileUpload.setContent(file);
150     }
151 
152     public void setContent(InputStream inputStream) throws IOException {
153         if (fileUpload instanceof MemoryFileUpload) {
154             // change to Disk
155             fileUpload = new DiskFileUpload(fileUpload
156                     .getName(), fileUpload.getFilename(), fileUpload
157                     .getContentType(), fileUpload
158                     .getContentTransferEncoding(), fileUpload.getCharset(),
159                     definedSize);
160         }
161         fileUpload.setContent(inputStream);
162     }
163 
164     public void setContentType(String contentType) {
165         fileUpload.setContentType(contentType);
166     }
167 
168     public void setContentTransferEncoding(String contentTransferEncoding) {
169         fileUpload.setContentTransferEncoding(contentTransferEncoding);
170     }
171 
172     public void setFilename(String filename) {
173         fileUpload.setFilename(filename);
174     }
175 
176     public HttpDataType getHttpDataType() {
177         return fileUpload.getHttpDataType();
178     }
179 
180     public String getName() {
181         return fileUpload.getName();
182     }
183 
184     public int compareTo(InterfaceHttpData o) {
185         return fileUpload.compareTo(o);
186     }
187 
188     @Override
189     public String toString() {
190         return "Mixed: " + fileUpload.toString();
191     }
192 
193     public ChannelBuffer getChunk(int length) throws IOException {
194         return fileUpload.getChunk(length);
195     }
196 
197     public File getFile() throws IOException {
198         return fileUpload.getFile();
199     }
200 
201 }