use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class MessageStoreInterceptor method before.
/**
* Handle the retrieving of field errors / action messages / field errors, which is
* done before action invocation, and the <code>operationMode</code> is 'RETRIEVE'.
*
* @param invocation the action invocation
* @throws Exception in case of any error
*/
protected void before(ActionInvocation invocation) throws Exception {
String reqOperationMode = getRequestOperationMode(invocation);
if (RETRIEVE_MODE.equalsIgnoreCase(reqOperationMode) || RETRIEVE_MODE.equalsIgnoreCase(operationMode) || AUTOMATIC_MODE.equalsIgnoreCase(operationMode)) {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
// retrieve error / message from session
Map<String, Object> session = invocation.getInvocationContext().getSession();
if (session == null) {
LOG.debug("Session is not open, no errors / messages could be retrieve for action [{}]", action);
return;
}
ValidationAware validationAwareAction = (ValidationAware) action;
LOG.debug("Retrieve error / message from session to populate into action [{}]", action);
Collection actionErrors = (Collection) session.get(actionErrorsSessionKey);
Collection actionMessages = (Collection) session.get(actionMessagesSessionKey);
Map fieldErrors = (Map) session.get(fieldErrorsSessionKey);
if (actionErrors != null && actionErrors.size() > 0) {
Collection mergedActionErrors = mergeCollection(validationAwareAction.getActionErrors(), actionErrors);
validationAwareAction.setActionErrors(mergedActionErrors);
}
if (actionMessages != null && actionMessages.size() > 0) {
Collection mergedActionMessages = mergeCollection(validationAwareAction.getActionMessages(), actionMessages);
validationAwareAction.setActionMessages(mergedActionMessages);
}
if (fieldErrors != null && fieldErrors.size() > 0) {
Map mergedFieldErrors = mergeMap(validationAwareAction.getFieldErrors(), fieldErrors);
validationAwareAction.setFieldErrors(mergedFieldErrors);
}
session.remove(actionErrorsSessionKey);
session.remove(actionMessagesSessionKey);
session.remove(fieldErrorsSessionKey);
}
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class MessageStorePreResultListener method beforeResult.
@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
boolean isCommitted = isCommitted();
if (isCommitted) {
LOG.trace("Response was already committed, cannot store messages!");
return;
}
boolean isInvalidated = isInvalidated();
if (isInvalidated) {
LOG.trace("Session was invalidated or never created, cannot store messages!");
return;
}
Map<String, Object> session = invocation.getInvocationContext().getSession();
if (session == null) {
LOG.trace("Could not store action [{}] error/messages into session, because session hasn't been opened yet.", invocation.getAction());
return;
}
String reqOperationMode = interceptor.getRequestOperationMode(invocation);
boolean isRedirect = isRedirect(invocation, resultCode);
if (MessageStoreInterceptor.STORE_MODE.equalsIgnoreCase(reqOperationMode) || MessageStoreInterceptor.STORE_MODE.equalsIgnoreCase(interceptor.getOperationModel()) || (MessageStoreInterceptor.AUTOMATIC_MODE.equalsIgnoreCase(interceptor.getOperationModel()) && isRedirect)) {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
LOG.debug("Storing action [{}] error/messages into session ", action);
ValidationAware validationAwareAction = (ValidationAware) action;
session.put(MessageStoreInterceptor.actionErrorsSessionKey, validationAwareAction.getActionErrors());
session.put(MessageStoreInterceptor.actionMessagesSessionKey, validationAwareAction.getActionMessages());
session.put(MessageStoreInterceptor.fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
} else {
LOG.debug("Action [{}] is not ValidationAware, no message / error that are storeable", action);
}
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class DWRValidator method doPost.
public ValidationAwareSupport doPost(String namespace, String actionName, Map params) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
ServletContext servletContext = WebContextFactory.get().getServletContext();
HttpServletResponse res = WebContextFactory.get().getHttpServletResponse();
HttpParameters.Builder requestParams = HttpParameters.create(req.getParameterMap());
if (params != null) {
requestParams = requestParams.withExtraParams(params);
}
Map<String, Object> requestMap = new RequestMap(req);
Map<String, Object> session = new SessionMap<>(req);
Map<String, Object> application = new ApplicationMap(servletContext);
Dispatcher du = Dispatcher.getInstance();
Map<String, Object> ctx = du.createContextMap(requestMap, requestParams.build(), session, application, req, res);
try {
ActionProxyFactory actionProxyFactory = du.getContainer().getInstance(ActionProxyFactory.class);
ActionProxy proxy = actionProxyFactory.createActionProxy(namespace, actionName, null, ctx, true, true);
proxy.execute();
Object action = proxy.getAction();
if (action instanceof ValidationAware) {
ValidationAware aware = (ValidationAware) action;
ValidationAwareSupport vas = new ValidationAwareSupport();
vas.setActionErrors(aware.getActionErrors());
vas.setActionMessages(aware.getActionMessages());
vas.setFieldErrors(aware.getFieldErrors());
return vas;
} else {
return null;
}
} catch (Exception e) {
LOG.error("Error while trying to validate", e);
return null;
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class BeanValidationInterceptorTest method testValidationGroupActionDefault.
public void testValidationGroupActionDefault() throws Exception {
ActionProxy baseActionProxy = getValidateGroupAction("actionDefault");
ValidateGroupAction action = (ValidateGroupAction) baseActionProxy.getAction();
action.getModel().setName(null);
action.getModel().setEmail(null);
action.getModel().getAddress().setStreet(null);
baseActionProxy.execute();
assertEquals("every properties not valid", 3, ((ValidationAware) baseActionProxy.getAction()).getFieldErrors().size());
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class BeanValidationInterceptorTest method testFieldAction.
public void testFieldAction() throws Exception {
ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "fieldAction", null, null);
FieldAction action = (FieldAction) baseActionProxy.getAction();
action.setTest(" ");
baseActionProxy.execute();
Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
assertNotNull(fieldErrors);
assertEquals(1, fieldErrors.size());
assertTrue(fieldErrors.get("test").size() > 0);
}
Aggregations