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