* @see
* com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony
* .xwork2.ActionInvocation)
*/
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) ac
.get(ServletActionContext.HTTP_REQUEST);
if (!(request instanceof GaeMultiPartRequestWrapper)) {
if (LOG.isDebugEnabled()) {
ActionProxy proxy = invocation.getProxy();
LOG.debug(getTextMessage(
"struts.messages.bypass.request",
new Object[] { proxy.getNamespace(),
proxy.getActionName() }, ac.getLocale()));
}
return invocation.invoke();
}
ValidationAware validation = null;
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
validation = (ValidationAware) action;
}
GaeMultiPartRequestWrapper multiWrapper = (GaeMultiPartRequestWrapper) request;
if (multiWrapper.hasErrors()) {
for (String error : multiWrapper.getErrors()) {
if (validation != null) {
validation.addActionError(error);
}
LOG.error(error);
}
}
// bind allowed Files
Enumeration<String> fileParameterNames = multiWrapper
.getFileParameterNames();
while (fileParameterNames != null
&& fileParameterNames.hasMoreElements()) {
// get the value of this input tag
String inputName = fileParameterNames.nextElement();
// get the content type
String[] contentType = multiWrapper.getContentTypes(inputName);
if (isNonEmpty(contentType)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName);
if (isNonEmpty(fileName)) {
String[] fileItemStreams = multiWrapper
.getFileContents(inputName);
// get a File object for the uploaded File
if (fileItemStreams != null && fileItemStreams.length > 0) {
List<String> acceptedFiles = new ArrayList<String>(
fileItemStreams.length);
List<String> acceptedContentTypes = new ArrayList<String>(
fileItemStreams.length);
List<String> acceptedFileNames = new ArrayList<String>(
fileItemStreams.length);
String contentTypeName = inputName + "ContentType";
String fileNameName = inputName + "FileName";
for (int index = 0; index < fileItemStreams.length; index++) {
if (acceptFile(action, fileItemStreams[index],
fileName[index], contentType[index],
inputName, validation, ac.getLocale())) {
acceptedFiles.add((fileItemStreams[index]));
acceptedContentTypes.add(contentType[index]);
acceptedFileNames.add(fileName[index]);
}
}
if (!acceptedFiles.isEmpty()) {
Map<String, Object> params = ac.getParameters();
params.put(inputName, acceptedFiles
.toArray(new String[acceptedFiles.size()]));
params.put(contentTypeName, acceptedContentTypes
.toArray(new String[acceptedContentTypes
.size()]));
params.put(fileNameName, acceptedFileNames
.toArray(new String[acceptedFileNames
.size()]));
}
}
} else {
LOG.error(getTextMessage(action,
"struts.messages.invalid.file",
new Object[] { inputName }, ac.getLocale()));
}
} else {
LOG.error(getTextMessage(action,
"struts.messages.invalid.content.type",
new Object[] { inputName }, ac.getLocale()));
}
}
// invoke action
String result = invocation.invoke();