01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.servlet.multipart;
19:
20: import java.io.File;
21: import java.io.IOException;
22: import java.util.Hashtable;
23:
24: import javax.servlet.http.HttpServletRequest;
25:
26: /**
27: * This is the interface of Request Wrapper in Cocoon.
28: *
29: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
30: * @version CVS $Id: RequestFactory.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class RequestFactory {
33:
34: private boolean saveUploadedFilesToDisk;
35:
36: private File uploadDirectory;
37:
38: private boolean allowOverwrite;
39:
40: private boolean silentlyRename;
41:
42: private String defaultCharEncoding;
43:
44: private int maxUploadSize;
45:
46: public RequestFactory(boolean saveUploadedFilesToDisk,
47: File uploadDirectory, boolean allowOverwrite,
48: boolean silentlyRename, int maxUploadSize,
49: String defaultCharEncoding) {
50: this .saveUploadedFilesToDisk = saveUploadedFilesToDisk;
51: this .uploadDirectory = uploadDirectory;
52: this .allowOverwrite = allowOverwrite;
53: this .silentlyRename = silentlyRename;
54: this .maxUploadSize = maxUploadSize;
55: this .defaultCharEncoding = defaultCharEncoding;
56:
57: if (saveUploadedFilesToDisk) {
58: // Empty the contents of the upload directory
59: File[] files = uploadDirectory.listFiles();
60: for (int i = 0; i < files.length; i++) {
61: files[i].delete();
62: }
63: }
64: }
65:
66: /**
67: * If the request includes a "multipart/form-data", then wrap it with
68: * methods that allow easier connection to those objects since the servlet
69: * API doesn't provide those methods directly.
70: */
71: public HttpServletRequest getServletRequest(
72: HttpServletRequest request) throws IOException,
73: MultipartException {
74: HttpServletRequest req = request;
75: String contentType = request.getContentType();
76:
77: if ((contentType != null)
78: && (contentType.toLowerCase().indexOf(
79: "multipart/form-data") > -1)) {
80:
81: String charEncoding = request.getCharacterEncoding();
82: if (charEncoding == null || charEncoding.length() == 0) {
83: charEncoding = this .defaultCharEncoding;
84: }
85:
86: MultipartParser parser = new MultipartParser(
87: this .saveUploadedFilesToDisk, this .uploadDirectory,
88: this .allowOverwrite, this .silentlyRename,
89: this .maxUploadSize, charEncoding);
90:
91: Hashtable parts = parser.getParts(request);
92:
93: req = new MultipartHttpServletRequest(request, parts);
94: }
95:
96: return req;
97: }
98:
99: }
|