Clover Coverage Report - Atlassian XWork(Aggregated)
Coverage timestamp: Wed Jul 27 2011 23:39:31 CDT
47   153   20   4.27
18   116   0.43   3.67
11     1.82  
3    
 
 
  FileUploadUtils       Line # 11 33 12 0% 0.0
  FileUploadUtils.UploadedFile       Line # 98 6 4 0% 0.0
  FileUploadUtils.FileUploadException       Line # 127 8 4 0% 0.0
 
No Tests
 
1    package com.atlassian.xwork;
2   
3    import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
4    import com.opensymphony.webwork.ServletActionContext;
5   
6    import java.io.File;
7    import java.util.*;
8   
9    import org.apache.log4j.Category;
10   
 
11    public class FileUploadUtils
12    {
13    private static Category log = Category.getInstance(FileUploadUtils.class);
14   
 
15  0 toggle public static File getSingleFile() throws FileUploadException
16    {
17  0 FileUploadUtils.UploadedFile uploadedFile = getSingleUploadedFile();
18  0 return uploadedFile == null ? null : uploadedFile.getFile();
19    }
20   
 
21  0 toggle public static UploadedFile getSingleUploadedFile() throws FileUploadException
22    {
23  0 MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
24   
25  0 FileUploadUtils.UploadedFile[] uploadedFiles = FileUploadUtils.handleFileUpload(multiWrapper, true);
26   
27  0 if (uploadedFiles.length == 0)
28  0 return null;
29   
30  0 return uploadedFiles[0];
31    }
32   
33    /**
34    * The multipart request should always be checked for errors before processing is done on it.
35    * @throws FileUploadException
36    */
 
37  0 toggle public static void checkMultiPartRequestForErrors(MultiPartRequestWrapper multiWrapper) throws FileUploadException
38    {
39  0 if (multiWrapper.hasErrors())
40    {
41  0 FileUploadException fileUploadException = new FileUploadException();
42  0 Collection errors = multiWrapper.getErrors();
43  0 Iterator i = errors.iterator();
44   
45  0 while (i.hasNext())
46    {
47  0 String error = (String)i.next();
48  0 log.error(error);
49  0 fileUploadException.addError(error);
50    }
51   
52  0 throw fileUploadException;
53    }
54    }
55   
56   
 
57  0 toggle public static UploadedFile[] handleFileUpload(MultiPartRequestWrapper multiWrapper, boolean clean)
58    throws FileUploadException
59    {
60  0 checkMultiPartRequestForErrors(multiWrapper);
61   
62  0 Enumeration e = multiWrapper.getFileParameterNames();
63  0 List uploadedFiles = new ArrayList();
64   
65  0 while (e.hasMoreElements())
66    {
67    // get the value of this input tag
68  0 String inputValue = (String)e.nextElement();
69   
70    // Get a File object for the uploaded File
71  0 File[] files = multiWrapper.getFiles(inputValue);
72   
73  0 for (int i=0; i<files.length; i++) // support multiple upload controls with the same name
74    {
75  0 File file = files[i];
76   
77    // If it's null the upload failed
78  0 if (file == null && !clean)
79    {
80  0 FileUploadException fileUploadException = new FileUploadException();
81  0 fileUploadException.addError("Error uploading: " + multiWrapper.getFileSystemNames(inputValue)[i]);
82  0 throw fileUploadException;
83    }
84  0 else if (file == null)
85    {
86  0 continue; //i.e. we simply don't store nulls in thye uploadedFiles array.
87    }
88   
89  0 UploadedFile uploadedFile = new UploadedFile(file, multiWrapper.getFileNames(inputValue)[i],
90    multiWrapper.getContentTypes(inputValue)[i]);
91  0 uploadedFiles.add(uploadedFile);
92    }
93    }
94   
95  0 return (UploadedFile[])uploadedFiles.toArray(new UploadedFile[0]);
96    }
97   
 
98    public static final class UploadedFile
99    {
100    private File file;
101    private String fileName;
102    private String contentType;
103   
 
104  0 toggle public UploadedFile(File file, String fileName, String contentType)
105    {
106  0 this.file = file;
107  0 this.fileName = fileName;
108  0 this.contentType = contentType;
109    }
110   
 
111  0 toggle public File getFile()
112    {
113  0 return file;
114    }
115   
 
116  0 toggle public String getFileName()
117    {
118  0 return fileName;
119    }
120   
 
121  0 toggle public String getContentType()
122    {
123  0 return contentType;
124    }
125    }
126   
 
127    public static final class FileUploadException extends Exception
128    {
129    private List errors = new ArrayList();
130   
 
131  0 toggle public void addError(String error)
132    {
133  0 errors.add(error);
134    }
135   
 
136  0 toggle public String[] getErrors()
137    {
138  0 return (String[])errors.toArray(new String[0]);
139    }
140   
 
141  0 toggle public String getMessage()
142    {
143  0 String s = "";
144  0 String sep = "";
145  0 for (Iterator i = errors.iterator(); i.hasNext();)
146    {
147  0 s += sep + i.next();
148  0 sep = ", ";
149    }
150  0 return s;
151    }
152    }
153    }