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 java.io.IOException;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.jboss.netty.handler.codec.http.HttpRequest;
25
26
27
28
29
30
31
32
33
34 public class DefaultHttpDataFactory implements HttpDataFactory {
35
36
37
38 public static long MINSIZE = 0x4000;
39
40 private final boolean useDisk;
41
42 private final boolean checkSize;
43
44 private long minSize;
45
46
47
48
49 private final ConcurrentHashMap<HttpRequest, List<HttpData>> requestFileDeleteMap =
50 new ConcurrentHashMap<HttpRequest, List<HttpData>>();
51
52
53
54
55 public DefaultHttpDataFactory() {
56 useDisk = false;
57 checkSize = true;
58 minSize = MINSIZE;
59 }
60
61
62
63
64
65 public DefaultHttpDataFactory(boolean useDisk) {
66 this.useDisk = useDisk;
67 checkSize = false;
68 }
69
70
71
72
73
74
75 public DefaultHttpDataFactory(long minSize) {
76 useDisk = false;
77 checkSize = true;
78 this.minSize = minSize;
79 }
80
81
82
83
84
85
86 private List<HttpData> getList(HttpRequest request) {
87 List<HttpData> list = requestFileDeleteMap.get(request);
88 if (list == null) {
89 list = new ArrayList<HttpData>();
90 requestFileDeleteMap.put(request, list);
91 }
92 return list;
93 }
94
95 public Attribute createAttribute(HttpRequest request, String name) {
96 if (useDisk) {
97 Attribute attribute = new DiskAttribute(name);
98 List<HttpData> fileToDelete = getList(request);
99 fileToDelete.add(attribute);
100 return attribute;
101 } else if (checkSize) {
102 Attribute attribute = new MixedAttribute(name, minSize);
103 List<HttpData> fileToDelete = getList(request);
104 fileToDelete.add(attribute);
105 return attribute;
106 }
107 return new MemoryAttribute(name);
108 }
109
110 public Attribute createAttribute(HttpRequest request, String name, String value) {
111 if (useDisk) {
112 Attribute attribute;
113 try {
114 attribute = new DiskAttribute(name, value);
115 } catch (IOException e) {
116
117 attribute = new MixedAttribute(name, value, minSize);
118 }
119 List<HttpData> fileToDelete = getList(request);
120 fileToDelete.add(attribute);
121 return attribute;
122 } else if (checkSize) {
123 Attribute attribute = new MixedAttribute(name, value, minSize);
124 List<HttpData> fileToDelete = getList(request);
125 fileToDelete.add(attribute);
126 return attribute;
127 }
128 try {
129 return new MemoryAttribute(name, value);
130 } catch (IOException e) {
131 throw new IllegalArgumentException(e);
132 }
133 }
134
135 public FileUpload createFileUpload(HttpRequest request, String name, String filename,
136 String contentType, String contentTransferEncoding, Charset charset,
137 long size) {
138 if (useDisk) {
139 FileUpload fileUpload = new DiskFileUpload(name, filename, contentType,
140 contentTransferEncoding, charset, size);
141 List<HttpData> fileToDelete = getList(request);
142 fileToDelete.add(fileUpload);
143 return fileUpload;
144 } else if (checkSize) {
145 FileUpload fileUpload = new MixedFileUpload(name, filename, contentType,
146 contentTransferEncoding, charset, size, minSize);
147 List<HttpData> fileToDelete = getList(request);
148 fileToDelete.add(fileUpload);
149 return fileUpload;
150 }
151 return new MemoryFileUpload(name, filename, contentType,
152 contentTransferEncoding, charset, size);
153 }
154
155 public void removeHttpDataFromClean(HttpRequest request, InterfaceHttpData data) {
156 if (data instanceof HttpData) {
157 List<HttpData> fileToDelete = getList(request);
158 fileToDelete.remove(data);
159 }
160 }
161
162 public void cleanRequestHttpDatas(HttpRequest request) {
163 List<HttpData> fileToDelete = requestFileDeleteMap.remove(request);
164 if (fileToDelete != null) {
165 for (HttpData data: fileToDelete) {
166 data.delete();
167 }
168 fileToDelete.clear();
169 }
170 }
171
172 public void cleanAllHttpDatas() {
173 for (HttpRequest request : requestFileDeleteMap.keySet()) {
174 List<HttpData> fileToDelete = requestFileDeleteMap.get(request);
175 if (fileToDelete != null) {
176 for (HttpData data: fileToDelete) {
177 data.delete();
178 }
179 fileToDelete.clear();
180 }
181 requestFileDeleteMap.remove(request);
182 }
183 }
184 }