KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portlet > multipart > RequestFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portlet.multipart;
17
18 import org.apache.cocoon.servlet.multipart.MultipartException;
19 import org.apache.cocoon.servlet.multipart.MultipartParser;
20
21 import javax.portlet.ActionRequest;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 /**
28  * This is the interface of Request Wrapper in Cocoon for JSR-168 Portlet environment.
29  *
30  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
31  * @version CVS $Id: RequestFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public class RequestFactory {
34
35     private boolean saveUploadedFilesToDisk;
36     private File JavaDoc uploadDirectory;
37     private boolean allowOverwrite;
38     private boolean silentlyRename;
39     private String JavaDoc defaultCharEncoding;
40     private int maxUploadSize;
41
42     public RequestFactory(boolean saveUploadedFilesToDisk,
43                           File JavaDoc uploadDirectory,
44                           boolean allowOverwrite,
45                           boolean silentlyRename,
46                           int maxUploadSize,
47                           String JavaDoc defaultCharEncoding) {
48         this.saveUploadedFilesToDisk = saveUploadedFilesToDisk;
49         this.uploadDirectory = uploadDirectory;
50         this.allowOverwrite = allowOverwrite;
51         this.silentlyRename = silentlyRename;
52         this.maxUploadSize = maxUploadSize;
53         this.defaultCharEncoding = defaultCharEncoding;
54     }
55
56     /**
57      * If the request includes a "multipart/form-data", then wrap it with
58      * methods that allow easier connection to those objects since the servlet
59      * API doesn't provide those methods directly.
60      */

61     public ActionRequest getServletRequest(ActionRequest request)
62     throws IOException JavaDoc, MultipartException {
63         ActionRequest req = request;
64         String JavaDoc contentType = request.getContentType();
65
66         if ((contentType != null) && (contentType.toLowerCase().indexOf("multipart/form-data") > -1)) {
67             if (request.getContentLength() > maxUploadSize) {
68                 throw new IOException JavaDoc("Content length exceeds maximum upload size.");
69             }
70
71             String JavaDoc charEncoding = request.getCharacterEncoding();
72             if (charEncoding == null || charEncoding.equals("")) {
73                 charEncoding = this.defaultCharEncoding;
74             }
75
76             MultipartParser parser =
77                     new MultipartParser(
78                             this.saveUploadedFilesToDisk,
79                             this.uploadDirectory,
80                             this.allowOverwrite,
81                             this.silentlyRename,
82                             this.maxUploadSize,
83                             charEncoding);
84
85             Hashtable JavaDoc parts = parser.getParts(request.getContentLength(),
86                                               request.getContentType(),
87                                               request.getPortletInputStream());
88
89             req = new MultipartActionRequest(request, parts);
90         }
91
92         return req;
93     }
94 }
95
Popular Tags