KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > multipart > CosMultiPartRequest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher.multipart;
6
7 import com.oreilly.servlet.MultipartRequest;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.List JavaDoc;
15
16
17 /**
18  * Multipart form data request adapter for Jason Hunter's
19  * <a HREF="http://www.servlets.com/cos/index.html" target="_blank">multipart utils</a>
20  * (COS == com.oreilly.servlet).
21  *
22  * @author <a HREF="mailto:matt@smallleap.com">Matt Baldree</a> (modified for WW's use)
23  * @author <a HREF="mailto:scott@atlassian.com">Scott Farquhar</a> (added i18n handling (WW-109))
24  */

25 public class CosMultiPartRequest extends MultiPartRequest {
26     //~ Instance fields ////////////////////////////////////////////////////////
27

28     private MultipartRequest multi;
29
30     //~ Constructors ///////////////////////////////////////////////////////////
31

32     /**
33      * Creates a new request wrapper to handle multi-part data using methods adapted from the COS
34      * multipart classes (see class description).
35      *
36      * @param maxSize maximum size post allowed
37      * @param saveDir the directory to save off the file
38      * @param servletRequest the request containing the multipart
39      */

40     public CosMultiPartRequest(HttpServletRequest JavaDoc servletRequest, String JavaDoc saveDir, int maxSize) throws IOException JavaDoc {
41         String JavaDoc encoding = getEncoding();
42
43         if (encoding != null) {
44             multi = new MultipartRequest(servletRequest, saveDir, maxSize, encoding);
45         } else {
46             multi = new MultipartRequest(servletRequest, saveDir, maxSize);
47         }
48     }
49
50     //~ Methods ////////////////////////////////////////////////////////////////
51

52     public Enumeration JavaDoc getFileParameterNames() {
53         return multi.getFileNames();
54     }
55
56     public String JavaDoc[] getContentType(String JavaDoc fieldName) {
57         return new String JavaDoc[]{multi.getContentType(fieldName)};
58     }
59
60     public File JavaDoc[] getFile(String JavaDoc fieldName) {
61         return new File JavaDoc[]{multi.getFile(fieldName)};
62     }
63
64     public String JavaDoc[] getFileNames(String JavaDoc fieldName) {
65         return new String JavaDoc[]{multi.getFile(fieldName).getName()};
66     }
67
68     public String JavaDoc[] getFilesystemName(String JavaDoc name) {
69         return new String JavaDoc[]{multi.getFilesystemName(name)};
70     }
71
72     public String JavaDoc getParameter(String JavaDoc name) {
73         return multi.getParameter(name);
74     }
75
76     public Enumeration JavaDoc getParameterNames() {
77         return multi.getParameterNames();
78     }
79
80     public String JavaDoc[] getParameterValues(String JavaDoc name) {
81         return multi.getParameterValues(name);
82     }
83
84     public List JavaDoc getErrors() {
85         return Collections.EMPTY_LIST;
86     }
87
88     /**
89      * Set the encoding for the uploaded parameters. This needs to be set if you are using character sets
90      * other than ASCII.<p>
91      * <p/>
92      * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in
93      * default.properties and webwork.properties.
94      */

95     private static String JavaDoc getEncoding() {
96         return "utf-8";
97
98         //todo: configuration in xwork needs to support non-action level config
99

100         /*
101         String encoding = null;
102         try {
103
104             //encoding = Configuration.getString("webwork.i18n.encoding");
105         } catch (IllegalArgumentException e) {
106             LogFactory.getLog(PellMultiPartRequest.class).info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default");
107         }
108         return encoding;
109         */

110     }
111 }
112
Popular Tags