1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.multipart;
17
18 import org.jboss.netty.handler.codec.http.HttpRequest;
19
20 import java.io.IOException;
21 import java.nio.charset.Charset;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26
27
28
29
30
31
32
33
34
35 public class DefaultHttpDataFactory implements HttpDataFactory {
36
37
38
39 public static final long MINSIZE = 0x4000;
40
41
42
43 public static final long MAXSIZE = -1;
44
45 private final boolean useDisk;
46
47 private final boolean checkSize;
48
49 private long minSize;
50
51 private long maxSize = MAXSIZE;
52
53
54
55
56 private final Map<HttpRequest, List<HttpData>> requestFileDeleteMap =
57 new ConcurrentHashMap<HttpRequest, List<HttpData>>();
58
59
60
61
62 public DefaultHttpDataFactory() {
63 useDisk = false;
64 checkSize = true;
65 minSize = MINSIZE;
66 }
67
68
69
70
71
72 public DefaultHttpDataFactory(boolean useDisk) {
73 this.useDisk = useDisk;
74 checkSize = false;
75 }
76
77
78
79
80
81 public DefaultHttpDataFactory(long minSize) {
82 useDisk = false;
83 checkSize = true;
84 this.minSize = minSize;
85 }
86
87 public void setMaxLimit(long max) {
88 this.maxSize = max;
89 }
90
91
92
93
94 private List<HttpData> getList(HttpRequest request) {
95 List<HttpData> list = requestFileDeleteMap.get(request);
96 if (list == null) {
97 list = new ArrayList<HttpData>();
98 requestFileDeleteMap.put(request, list);
99 }
100 return list;
101 }
102
103 public Attribute createAttribute(HttpRequest request, String name) {
104 if (useDisk) {
105 Attribute attribute = new DiskAttribute(name);
106 attribute.setMaxSize(maxSize);
107 List<HttpData> fileToDelete = getList(request);
108 fileToDelete.add(attribute);
109 return attribute;
110 }
111 if (checkSize) {
112 Attribute attribute = new MixedAttribute(name, minSize);
113 attribute.setMaxSize(maxSize);
114 List<HttpData> fileToDelete = getList(request);
115 fileToDelete.add(attribute);
116 return attribute;
117 }
118 MemoryAttribute attribute = new MemoryAttribute(name);
119 attribute.setMaxSize(maxSize);
120 return attribute;
121 }
122
123
124
125
126
127 private void checkHttpDataSize(HttpData data) {
128 try {
129 data.checkSize(data.length());
130 } catch (IOException e) {
131 throw new IllegalArgumentException("Attribute bigger than maxSize allowed");
132 }
133 }
134
135 public Attribute createAttribute(HttpRequest request, String name, String value) {
136 if (useDisk) {
137 Attribute attribute;
138 try {
139 attribute = new DiskAttribute(name, value);
140 attribute.setMaxSize(maxSize);
141 } catch (IOException e) {
142
143 attribute = new MixedAttribute(name, value, minSize);
144 attribute.setMaxSize(maxSize);
145 }
146 checkHttpDataSize(attribute);
147 List<HttpData> fileToDelete = getList(request);
148 fileToDelete.add(attribute);
149 return attribute;
150 }
151 if (checkSize) {
152 Attribute attribute = new MixedAttribute(name, value, minSize);
153 attribute.setMaxSize(maxSize);
154 checkHttpDataSize(attribute);
155 List<HttpData> fileToDelete = getList(request);
156 fileToDelete.add(attribute);
157 return attribute;
158 }
159 try {
160 MemoryAttribute attribute = new MemoryAttribute(name, value);
161 attribute.setMaxSize(maxSize);
162 checkHttpDataSize(attribute);
163 return attribute;
164 } catch (IOException e) {
165 throw new IllegalArgumentException(e);
166 }
167 }
168
169 public FileUpload createFileUpload(HttpRequest request, String name, String filename,
170 String contentType, String contentTransferEncoding, Charset charset,
171 long size) {
172 if (useDisk) {
173 FileUpload fileUpload = new DiskFileUpload(name, filename, contentType,
174 contentTransferEncoding, charset, size);
175 fileUpload.setMaxSize(maxSize);
176 checkHttpDataSize(fileUpload);
177 List<HttpData> fileToDelete = getList(request);
178 fileToDelete.add(fileUpload);
179 return fileUpload;
180 }
181 if (checkSize) {
182 FileUpload fileUpload = new MixedFileUpload(name, filename, contentType,
183 contentTransferEncoding, charset, size, minSize);
184 fileUpload.setMaxSize(maxSize);
185 checkHttpDataSize(fileUpload);
186 List<HttpData> fileToDelete = getList(request);
187 fileToDelete.add(fileUpload);
188 return fileUpload;
189 }
190 MemoryFileUpload fileUpload = new MemoryFileUpload(name, filename, contentType,
191 contentTransferEncoding, charset, size);
192 fileUpload.setMaxSize(maxSize);
193 checkHttpDataSize(fileUpload);
194 return fileUpload;
195 }
196
197 public void removeHttpDataFromClean(HttpRequest request, InterfaceHttpData data) {
198 if (data instanceof HttpData) {
199 List<HttpData> fileToDelete = getList(request);
200 fileToDelete.remove(data);
201 }
202 }
203
204 public void cleanRequestHttpDatas(HttpRequest request) {
205 List<HttpData> fileToDelete = requestFileDeleteMap.remove(request);
206 if (fileToDelete != null) {
207 for (HttpData data: fileToDelete) {
208 data.delete();
209 }
210 fileToDelete.clear();
211 }
212 }
213
214 public void cleanAllHttpDatas() {
215 for (HttpRequest request : requestFileDeleteMap.keySet()) {
216 List<HttpData> fileToDelete = requestFileDeleteMap.get(request);
217 if (fileToDelete != null) {
218 for (HttpData data: fileToDelete) {
219 data.delete();
220 }
221 fileToDelete.clear();
222 }
223 requestFileDeleteMap.remove(request);
224 }
225 }
226 }