use of org.apache.struts2.dispatcher.LocalizedMessage in project struts by apache.
the class FileUploadInterceptor method intercept.
/* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac = invocation.getInvocationContext();
HttpServletRequest request = ac.getServletRequest();
if (!(request instanceof MultiPartRequestWrapper)) {
if (LOG.isDebugEnabled()) {
ActionProxy proxy = invocation.getProxy();
LOG.debug(getTextMessage("struts.messages.bypass.request", new String[] { proxy.getNamespace(), proxy.getActionName() }));
}
return invocation.invoke();
}
ValidationAware validation = null;
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
validation = (ValidationAware) action;
}
MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
if (multiWrapper.hasErrors() && validation != null) {
TextProvider textProvider = getTextProvider(action);
for (LocalizedMessage error : multiWrapper.getErrors()) {
String errorMessage;
if (textProvider.hasKey(error.getTextKey())) {
errorMessage = textProvider.getText(error.getTextKey(), Arrays.asList(error.getArgs()));
} else {
errorMessage = textProvider.getText("struts.messages.error.uploading", error.getDefaultMessage());
}
validation.addActionError(errorMessage);
}
}
// bind allowed Files
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
// get the value of this input tag
String inputName = (String) 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)) {
// get a File object for the uploaded File
UploadedFile[] files = multiWrapper.getFiles(inputName);
if (files != null && files.length > 0) {
List<UploadedFile> acceptedFiles = new ArrayList<>(files.length);
List<String> acceptedContentTypes = new ArrayList<>(files.length);
List<String> acceptedFileNames = new ArrayList<>(files.length);
String contentTypeName = inputName + "ContentType";
String fileNameName = inputName + "FileName";
for (int index = 0; index < files.length; index++) {
if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation)) {
acceptedFiles.add(files[index]);
acceptedContentTypes.add(contentType[index]);
acceptedFileNames.add(fileName[index]);
}
}
if (!acceptedFiles.isEmpty()) {
Map<String, Parameter> newParams = new HashMap<>();
newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new UploadedFile[acceptedFiles.size()])));
newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])));
newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])));
ac.getParameters().appendAll(newParams);
}
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn(getTextMessage(action, "struts.messages.invalid.file", new String[] { inputName }));
}
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn(getTextMessage(action, "struts.messages.invalid.content.type", new String[] { inputName }));
}
}
}
// invoke action
return invocation.invoke();
}
use of org.apache.struts2.dispatcher.LocalizedMessage in project struts by apache.
the class JakartaStreamMultiPartRequest method addFileSkippedError.
/**
* Add a file skipped message notification for action messages.
*
* @param fileName file name
* @param request the servlet request
*/
protected void addFileSkippedError(String fileName, HttpServletRequest request) {
String exceptionMessage = "Skipped file " + fileName + "; request size limit exceeded.";
FileSizeLimitExceededException exception = new FileUploadBase.FileSizeLimitExceededException(exceptionMessage, getRequestSize(request), maxSize);
LocalizedMessage message = buildErrorMessage(exception, new Object[] { fileName, getRequestSize(request), maxSize });
if (!errors.contains(message)) {
errors.add(message);
}
}
use of org.apache.struts2.dispatcher.LocalizedMessage in project struts by apache.
the class JakartaStreamMultiPartRequestTest method unknownContentLength.
/**
* Number of bytes in files greater than 2GB overflow the {@code int} primative.
* The {@link HttpServletRequest#getContentLength()} returns {@literal -1}
* when the header is not present or the size is greater than {@link Integer#MAX_VALUE}.
* @throws IOException
*/
@Test
public void unknownContentLength() throws IOException {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getContentType()).thenReturn("multipart/form-data; charset=utf-8; boundary=__X_BOUNDARY__");
Mockito.when(request.getMethod()).thenReturn("POST");
Mockito.when(request.getContentLength()).thenReturn(Integer.valueOf(-1));
StringBuilder entity = new StringBuilder();
entity.append("\r\n--__X_BOUNDARY__\r\n");
entity.append("Content-Disposition: form-data; name=\"upload\"; filename=\"test.csv\"\r\n");
entity.append("Content-Type: text/csv\r\n\r\n1,2\r\n\r\n");
entity.append("--__X_BOUNDARY__\r\n");
entity.append("Content-Disposition: form-data; name=\"upload2\"; filename=\"test2.csv\"\r\n");
entity.append("Content-Type: text/csv\r\n\r\n3,4\r\n\r\n");
entity.append("--__X_BOUNDARY__--\r\n");
Mockito.when(request.getInputStream()).thenReturn(new DelegatingServletInputStream(new ByteArrayInputStream(entity.toString().getBytes(StandardCharsets.UTF_8))));
multiPart.setMaxSize("4");
multiPart.parse(request, tempDir.toString());
LocalizedMessage next = multiPart.getErrors().iterator().next();
Assert.assertEquals(next.getTextKey(), "struts.messages.upload.error.SizeLimitExceededException");
}
Aggregations