use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testSimpleField.
public void testSimpleField() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleField", null, null);
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(1, fieldErrors.size());
assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testMemberObject.
public void testMemberObject() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "memberObject", null, null);
MemberObject action = (MemberObject) baseActionProxy.getAction();
action.getPerson().setName(null);
action.getPerson().setEmail(null);
action.getPerson().getAddress().setStreet("short");
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(3, fieldErrors.size());
assertValue(fieldErrors, "person.name", Collections.singletonList("person.name cannot be null"));
assertValue(fieldErrors, "person.email", Collections.singletonList("person.email cannot be null"));
assertValue(fieldErrors, "person.address.street", Collections.singletonList("person.address.street cannot be shorter than 7 characters"));
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testProgrammaticValidationDontInvokeValidate.
public void testProgrammaticValidationDontInvokeValidate() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldNoValidate", null, null);
SimpleField action = (SimpleField) baseActionProxy.getAction();
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(1, fieldErrors.size());
assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
assertFalse(action.isValidateCalled());
assertTrue(action.isValidateExecuteCalled());
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testValidationInMethods.
public void testValidationInMethods() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "validationInMethods", null, null);
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(4, fieldErrors.size());
assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
assertValue(fieldErrors, "SisyphusHasTheAnswer", Collections.singletonList("SisyphusHasTheAnswer cannot be null"));
assertValue(fieldErrors, "thereAnyMeaningInLife", Collections.singletonList("thereAnyMeaningInLife cannot be null"));
assertValue(fieldErrors, "theMeaningOfLife", Collections.singletonList("theMeaningOfLife cannot be null"));
}
use of com.opensymphony.xwork2.interceptor.ValidationAware 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();
}
Aggregations