| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 53 (53) |
Complexity: 12 |
Complexity Density: 0.36 |
|
| 11 |
|
public class FileUploadUtils |
| 12 |
|
{ |
| 13 |
|
private static Category log = Category.getInstance(FileUploadUtils.class); |
| 14 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 15 |
0
|
public static File getSingleFile() throws FileUploadException... |
| 16 |
|
{ |
| 17 |
0
|
FileUploadUtils.UploadedFile uploadedFile = getSingleUploadedFile(); |
| 18 |
0
|
return uploadedFile == null ? null : uploadedFile.getFile(); |
| 19 |
|
} |
| 20 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 21 |
0
|
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 |
|
|
| 35 |
|
@throws |
| 36 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 37 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 57 |
0
|
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 |
|
|
| 68 |
0
|
String inputValue = (String)e.nextElement(); |
| 69 |
|
|
| 70 |
|
|
| 71 |
0
|
File[] files = multiWrapper.getFiles(inputValue); |
| 72 |
|
|
| 73 |
0
|
for (int i=0; i<files.length; i++) |
| 74 |
|
{ |
| 75 |
0
|
File file = files[i]; |
| 76 |
|
|
| 77 |
|
|
| 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; |
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
| 98 |
|
public static final class UploadedFile |
| 99 |
|
{ |
| 100 |
|
private File file; |
| 101 |
|
private String fileName; |
| 102 |
|
private String contentType; |
| 103 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 104 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 111 |
0
|
public File getFile()... |
| 112 |
|
{ |
| 113 |
0
|
return file; |
| 114 |
|
} |
| 115 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 116 |
0
|
public String getFileName()... |
| 117 |
|
{ |
| 118 |
0
|
return fileName; |
| 119 |
|
} |
| 120 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 121 |
0
|
public String getContentType()... |
| 122 |
|
{ |
| 123 |
0
|
return contentType; |
| 124 |
|
} |
| 125 |
|
} |
| 126 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 127 |
|
public static final class FileUploadException extends Exception |
| 128 |
|
{ |
| 129 |
|
private List errors = new ArrayList(); |
| 130 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 131 |
0
|
public void addError(String error)... |
| 132 |
|
{ |
| 133 |
0
|
errors.add(error); |
| 134 |
|
} |
| 135 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 136 |
0
|
public String[] getErrors()... |
| 137 |
|
{ |
| 138 |
0
|
return (String[])errors.toArray(new String[0]); |
| 139 |
|
} |
| 140 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 141 |
0
|
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 |
|
} |