1 package org.apache.fulcrum.upload;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25
26 import javax.portlet.ActionRequest;
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.apache.avalon.framework.activity.Initializable;
30 import org.apache.avalon.framework.configuration.Configurable;
31 import org.apache.avalon.framework.configuration.Configuration;
32 import org.apache.avalon.framework.context.Context;
33 import org.apache.avalon.framework.context.ContextException;
34 import org.apache.avalon.framework.context.Contextualizable;
35 import org.apache.avalon.framework.logger.AbstractLogEnabled;
36 import org.apache.avalon.framework.service.ServiceException;
37 import org.apache.commons.fileupload.FileItem;
38 import org.apache.commons.fileupload.FileItemIterator;
39 import org.apache.commons.fileupload.FileUploadException;
40 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
41 import org.apache.commons.fileupload.portlet.PortletFileUpload;
42 import org.apache.commons.fileupload.servlet.ServletFileUpload;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class DefaultUploadService extends AbstractLogEnabled
68 implements UploadService, Initializable, Configurable, Contextualizable {
69
70 private DiskFileItemFactory itemFactory;
71
72 private int sizeThreshold;
73 private int sizeMax;
74
75 private String repositoryPath;
76 private String headerEncoding;
77
78
79
80
81 private String applicationRoot;
82
83
84
85
86 @Override
87 public long getSizeMax() {
88 return sizeMax;
89 }
90
91
92
93
94 @Override
95 public long getSizeThreshold() {
96 return itemFactory.getSizeThreshold();
97 }
98
99
100
101
102
103 @Override
104 public String getRepository() {
105 return itemFactory.getRepository().getAbsolutePath();
106 }
107
108
109
110
111 @Override
112 public String getHeaderEncoding() {
113 return headerEncoding;
114 }
115
116
117
118
119
120
121
122
123
124
125
126 @Override
127 public List<FileItem> parseRequest(HttpServletRequest req) throws ServiceException {
128 return parseRequest(req, this.sizeMax, this.itemFactory);
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public List<FileItem> parseRequest(HttpServletRequest req, String path) throws ServiceException {
144 return parseRequest(req, this.sizeThreshold, this.sizeMax, path);
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 @Override
162 public List<FileItem> parseRequest(HttpServletRequest req, int sizeThreshold, int sizeMax, String path)
163 throws ServiceException {
164 return parseRequest(req, sizeMax, new DiskFileItemFactory(sizeThreshold, new File(path)));
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 protected List<FileItem> parseRequest(HttpServletRequest req, int sizeMax, DiskFileItemFactory factory)
181 throws ServiceException {
182 try {
183 ServletFileUpload fileUpload = new ServletFileUpload(factory);
184 fileUpload.setSizeMax(sizeMax);
185 fileUpload.setHeaderEncoding(headerEncoding);
186 return fileUpload.parseRequest(req);
187 } catch (FileUploadException e) {
188 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
189 }
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 @Override
207 public FileItemIterator getItemIterator(HttpServletRequest req) throws ServiceException {
208 ServletFileUpload upload = new ServletFileUpload();
209 try {
210 return upload.getItemIterator(req);
211 } catch (FileUploadException e) {
212 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
213 } catch (IOException e) {
214 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
215 }
216 }
217
218
219
220
221
222
223
224
225
226
227
228 @Override
229 public List<FileItem> parseRequest(ActionRequest req) throws ServiceException {
230 return parseRequest(req, this.sizeMax, this.itemFactory);
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244 @Override
245 public List<FileItem> parseRequest(ActionRequest req, String path) throws ServiceException {
246 return parseRequest(req, this.sizeThreshold, this.sizeMax, path);
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 @Override
263 public List<FileItem> parseRequest(ActionRequest req, int sizeThreshold, int sizeMax, String path)
264 throws ServiceException {
265 return parseRequest(req, sizeMax, new DiskFileItemFactory(sizeThreshold, new File(path)));
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 protected List<FileItem> parseRequest(ActionRequest req, int sizeMax, DiskFileItemFactory factory)
282 throws ServiceException {
283 try {
284 PortletFileUpload fileUpload = new PortletFileUpload(factory);
285 fileUpload.setSizeMax(sizeMax);
286 fileUpload.setHeaderEncoding(headerEncoding);
287 return fileUpload.parseRequest(req);
288 } catch (FileUploadException e) {
289 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
290 }
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 @Override
308 public FileItemIterator getItemIterator(ActionRequest req) throws ServiceException {
309 PortletFileUpload upload = new PortletFileUpload();
310 try {
311 return upload.getItemIterator(req);
312 } catch (FileUploadException e) {
313 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
314 } catch (IOException e) {
315 throw new ServiceException(UploadService.ROLE, e.getMessage(), e);
316 }
317 }
318
319
320
321
322
323
324
325
326
327
328 @Override
329 public boolean isMultipart(HttpServletRequest req) {
330 return ServletFileUpload.isMultipartContent(req);
331 }
332
333
334
335
336
337
338
339
340
341
342 @Override
343 public boolean isMultipart(ActionRequest req) {
344 return PortletFileUpload.isMultipartContent(req);
345 }
346
347
348
349
350 private String getRealPath(String path) {
351 String absolutePath = null;
352 if (applicationRoot == null) {
353 absolutePath = new File(path).getAbsolutePath();
354 } else {
355 absolutePath = new File(applicationRoot, path).getAbsolutePath();
356 }
357
358 return absolutePath;
359 }
360
361
362
363
364
365 @Override
366 public void configure(Configuration conf) {
367 repositoryPath = conf.getAttribute(UploadService.REPOSITORY_KEY, UploadService.REPOSITORY_DEFAULT);
368
369 headerEncoding = conf.getAttribute(UploadService.HEADER_ENCODING_KEY, UploadService.HEADER_ENCODING_DEFAULT);
370
371 sizeMax = conf.getAttributeAsInteger(UploadService.SIZE_MAX_KEY, UploadService.SIZE_MAX_DEFAULT);
372
373 sizeThreshold = conf.getAttributeAsInteger(UploadService.SIZE_THRESHOLD_KEY,
374 UploadService.SIZE_THRESHOLD_DEFAULT);
375 }
376
377
378
379
380
381
382
383
384
385 @Override
386 public void initialize() throws Exception {
387
388
389 String testPath = getRealPath(repositoryPath);
390 File testDir = new File(testPath);
391 if (testDir.exists()) {
392 repositoryPath = testPath;
393 }
394
395 getLogger().debug("Upload Service: REPOSITORY_KEY => " + repositoryPath);
396
397 itemFactory = new DiskFileItemFactory(sizeThreshold, new File(repositoryPath));
398 }
399
400
401
402
403 @Override
404 public void contextualize(Context context) throws ContextException {
405 this.applicationRoot = context.get("urn:avalon:home").toString();
406 }
407 }