KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > 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
17 package org.apache.cocoon.servlet.multipart;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 /**
26  * This is the interface of Request Wrapper in Cocoon.
27  *
28  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
29  * @version CVS $Id: RequestFactory.java 280876 2005-09-14 15:44:29Z sylvain $
30  */

31 public class RequestFactory {
32
33     private boolean saveUploadedFilesToDisk;
34
35     private File JavaDoc uploadDirectory;
36
37     private boolean allowOverwrite;
38
39     private boolean silentlyRename;
40
41     private String JavaDoc defaultCharEncoding;
42     
43     private int maxUploadSize;
44     
45     public RequestFactory (boolean saveUploadedFilesToDisk,
46                            File JavaDoc uploadDirectory,
47                            boolean allowOverwrite,
48                            boolean silentlyRename,
49                            int maxUploadSize,
50                            String JavaDoc defaultCharEncoding) {
51        this.saveUploadedFilesToDisk = saveUploadedFilesToDisk;
52        this.uploadDirectory = uploadDirectory;
53        this.allowOverwrite = allowOverwrite;
54        this.silentlyRename = silentlyRename;
55        this.maxUploadSize = maxUploadSize;
56        this.defaultCharEncoding = defaultCharEncoding;
57        
58        if (saveUploadedFilesToDisk) {
59            // Empty the contents of the upload directory
60
File JavaDoc[] files = uploadDirectory.listFiles();
61            for (int i = 0; i < files.length; i++) {
62                files[i].delete();
63            }
64        }
65     }
66
67     /**
68      * If the request includes a "multipart/form-data", then wrap it with
69      * methods that allow easier connection to those objects since the servlet
70      * API doesn't provide those methods directly.
71      */

72     public HttpServletRequest JavaDoc getServletRequest(HttpServletRequest JavaDoc request) throws IOException JavaDoc, MultipartException {
73         HttpServletRequest JavaDoc req = request;
74         String JavaDoc contentType = request.getContentType();
75         
76         if ((contentType != null) && (contentType.toLowerCase().indexOf("multipart/form-data") > -1)) {
77  
78             String JavaDoc charEncoding = request.getCharacterEncoding();
79             if (charEncoding == null || charEncoding.equals("")) {
80                 charEncoding = this.defaultCharEncoding;
81             }
82             
83             MultipartParser parser = new MultipartParser(
84                     this.saveUploadedFilesToDisk,
85                     this.uploadDirectory,
86                     this.allowOverwrite,
87                     this.silentlyRename,
88                     this.maxUploadSize,
89                     charEncoding);
90                     
91             Hashtable JavaDoc parts = parser.getParts(request);
92             
93             req = new MultipartHttpServletRequest(request,parts);
94         }
95
96         return req;
97     }
98     
99 }
Popular Tags