use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class RestWorkflowInterceptor method doIntercept.
/**
* Intercept {@link ActionInvocation} and processes the errors using the {@link org.apache.struts2.rest.handler.ContentTypeHandler}
* appropriate for the request.
*
* @return String result name
*/
protected String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
ValidationAware validationAwareAction = (ValidationAware) action;
if (validationAwareAction.hasErrors()) {
LOG.debug("Errors on action {}, returning result name 'input'", validationAwareAction);
ActionMapping mapping = ActionContext.getContext().getActionMapping();
String method = inputResultName;
if (postMethodName.equals(mapping.getMethod())) {
method = newMethodName;
} else if (putMethodName.equals(mapping.getMethod())) {
method = editMethodName;
}
HttpHeaders info = new DefaultHttpHeaders().disableCaching().renderResult(method).withStatus(validationFailureStatusCode);
Map<String, Object> errors = new HashMap<>();
errors.put("actionErrors", validationAwareAction.getActionErrors());
errors.put("fieldErrors", validationAwareAction.getFieldErrors());
return manager.handleResult(invocation, info, errors);
}
}
return invocation.invoke();
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testSimpleFieldsJPAAnnotations.
public void testSimpleFieldsJPAAnnotations() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsJPA", null, null);
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(1, fieldErrors.size());
assertValue(fieldErrors, "firstName", Collections.singletonList("firstName cannot be null"));
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testSimpleFieldsInheritedXML.
public void testSimpleFieldsInheritedXML() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsXMLChild", null, null);
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(3, fieldErrors.size());
assertValue(fieldErrors, "firstName", Collections.singletonList("firstName cannot be null"));
assertValue(fieldErrors, "lastName", Collections.singletonList("lastName cannot be null"));
assertValue(fieldErrors, "middleName", Collections.singletonList("middleName cannot be null"));
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testProgrammaticValidation.
public void testProgrammaticValidation() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleField", 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"));
assertTrue(action.isValidateCalled());
assertTrue(action.isValidateExecuteCalled());
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class OValValidationInterceptorTest method testModelDrivenAction.
public void testModelDrivenAction() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "modelDrivenAction", null, null);
ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
action.getModel().setName(null);
action.getModel().setEmail(null);
action.getModel().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"));
}
Aggregations