Clover Coverage Report - Atlassian XWork(Aggregated)
Coverage timestamp: Wed Jul 27 2011 23:39:31 CDT
28   127   16   2.8
8   71   0.57   10
10     1.6  
1    
 
 
  PellMultiPartRequest       Line # 22 28 16 0% 0.0
 
No Tests
 
1    package com.atlassian.xwork;
2   
3    import com.opensymphony.webwork.config.Configuration;
4    import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest;
5    import http.utils.multipartrequest.ServletMultipartRequest;
6   
7    import javax.servlet.http.HttpServletRequest;
8    import java.io.File;
9    import java.io.IOException;
10    import java.io.UnsupportedEncodingException;
11    import java.util.ArrayList;
12    import java.util.Enumeration;
13    import java.util.List;
14   
15    /**
16    * Patched version of {@link com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest}.
17    * The original implementation would throw a {@link NullPointerException} if getFileNames() was called for
18    * a non-existent fieldname.
19    *
20    * The entire class had to be duplicated because of the private ServletMultipartRequest
21    */
 
22    public class PellMultiPartRequest extends MultiPartRequest
23    {
24    //~ Instance fields ////////////////////////////////////////////////////////
25   
26    private ServletMultipartRequest multi;
27   
28    //~ Constructors ///////////////////////////////////////////////////////////
29   
30    /**
31    * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
32    * multipart classes (see class description).
33    *
34    * @param maxSize maximum size post allowed
35    * @param saveDir the directory to save off the file
36    * @param servletRequest the request containing the multipart
37    */
 
38  0 toggle public PellMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize) throws IOException {
39    //this needs to be synchronised, as we should not change the encoding at the same time as
40    //calling the constructor. See javadoc for MultipartRequest.setEncoding().
41  0 synchronized (this) {
42  0 setEncoding();
43  0 multi = new ServletMultipartRequest(servletRequest, saveDir, maxSize);
44    }
45    }
46   
47    //~ Methods ////////////////////////////////////////////////////////////////
48   
 
49  0 toggle public Enumeration getFileParameterNames() {
50  0 return multi.getFileParameterNames();
51    }
52   
 
53  0 toggle public String[] getContentType(String fieldName) {
54  0 return new String[]{multi.getContentType(fieldName)};
55    }
56   
 
57  0 toggle public File[] getFile(String fieldName) {
58  0 return new File[]{multi.getFile(fieldName)};
59    }
60   
 
61  0 toggle public String[] getFileNames(String fieldName) {
62    // This is a bug fix from the com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
63    // The original implementation did not check for a null return from multi.getFile()
64  0 File file = multi.getFile(fieldName);
65  0 if (file == null)
66  0 return null;
67   
68  0 return new String[]{file.getName()};
69    }
70   
 
71  0 toggle public String[] getFilesystemName(String fieldName) {
72  0 return new String[]{multi.getFileSystemName(fieldName)};
73    }
74   
 
75  0 toggle public String getParameter(String name) {
76  0 return multi.getURLParameter(name);
77    }
78   
 
79  0 toggle public Enumeration getParameterNames() {
80  0 return multi.getParameterNames();
81    }
82   
 
83  0 toggle public String[] getParameterValues(String name) {
84  0 Enumeration enumeration = multi.getURLParameters(name);
85   
86  0 if (!enumeration.hasMoreElements()) {
87  0 return null;
88    }
89   
90  0 List values = new ArrayList();
91   
92  0 while (enumeration.hasMoreElements()) {
93  0 values.add(enumeration.nextElement());
94    }
95   
96  0 return (String[]) values.toArray(new String[values.size()]);
97    }
98   
99    /**
100    * Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than
101    * ASCII.
102    * <p/>
103    * The encoding is looked up from the configuration setting 'webwork.i18n.encoding'. This is usually set in
104    * default.properties & webwork.properties.
105    */
 
106  0 toggle private static void setEncoding() {
107  0 String encoding = null;
108   
109  0 try {
110  0 encoding = Configuration.getString("webwork.i18n.encoding");
111   
112  0 if (encoding != null) {
113    //NB: This should never be called at the same time as the constructor for
114    //ServletMultiPartRequest, as it can cause problems.
115    //See javadoc for MultipartRequest.setEncoding()
116  0 http.utils.multipartrequest.MultipartRequest.setEncoding(encoding);
117    } else {
118  0 http.utils.multipartrequest.MultipartRequest.setEncoding("UTF-8");
119    }
120    } catch (IllegalArgumentException e) {
121  0 log.info("Could not get encoding property 'webwork.i18n.encoding' for file upload. Using system default");
122    } catch (UnsupportedEncodingException e) {
123  0 log.error("Encoding " + encoding + " is not a valid encoding. Please check your webwork.properties file.");
124    }
125    }
126    }
127