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: package org.apache.cocoon.portlet.multipart;
18:
19: import org.apache.cocoon.servlet.multipart.MultipartException;
20: import org.apache.cocoon.servlet.multipart.MultipartParser;
21:
22: import javax.portlet.ActionRequest;
23:
24: import java.io.File;
25: import java.io.IOException;
26: import java.util.Hashtable;
27:
28: /**
29: * This is the interface of Request Wrapper in Cocoon for JSR-168 Portlet environment.
30: *
31: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
32: * @version CVS $Id: RequestFactory.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34: public class RequestFactory {
35:
36: private boolean saveUploadedFilesToDisk;
37: private File uploadDirectory;
38: private boolean allowOverwrite;
39: private boolean silentlyRename;
40: private String defaultCharEncoding;
41: private int maxUploadSize;
42:
43: public RequestFactory(boolean saveUploadedFilesToDisk,
44: File uploadDirectory, boolean allowOverwrite,
45: boolean silentlyRename, int maxUploadSize,
46: String defaultCharEncoding) {
47: this .saveUploadedFilesToDisk = saveUploadedFilesToDisk;
48: this .uploadDirectory = uploadDirectory;
49: this .allowOverwrite = allowOverwrite;
50: this .silentlyRename = silentlyRename;
51: this .maxUploadSize = maxUploadSize;
52: this .defaultCharEncoding = defaultCharEncoding;
53: }
54:
55: /**
56: * If the request includes a "multipart/form-data", then wrap it with
57: * methods that allow easier connection to those objects since the servlet
58: * API doesn't provide those methods directly.
59: */
60: public ActionRequest getServletRequest(ActionRequest request)
61: throws IOException, MultipartException {
62: ActionRequest req = request;
63: String contentType = request.getContentType();
64:
65: if ((contentType != null)
66: && (contentType.toLowerCase().indexOf(
67: "multipart/form-data") > -1)) {
68: if (request.getContentLength() > maxUploadSize) {
69: throw new IOException(
70: "Content length exceeds maximum upload size.");
71: }
72:
73: String charEncoding = request.getCharacterEncoding();
74: if (charEncoding == null || charEncoding.length() == 0) {
75: charEncoding = this .defaultCharEncoding;
76: }
77:
78: MultipartParser parser = new MultipartParser(
79: this .saveUploadedFilesToDisk, this .uploadDirectory,
80: this .allowOverwrite, this .silentlyRename,
81: this .maxUploadSize, charEncoding);
82:
83: Hashtable parts = parser.getParts(request
84: .getContentLength(), request.getContentType(),
85: request.getPortletInputStream());
86:
87: req = new MultipartActionRequest(request, parts);
88: }
89:
90: return req;
91: }
92: }
|