Search in sources :

Example 16 with ValidationAware

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);
        }
    }
}
Also used : Collection(java.util.Collection) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 17 with ValidationAware

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);
        }
    }
}
Also used : ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 18 with ValidationAware

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;
    }
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ApplicationMap(org.apache.struts2.dispatcher.ApplicationMap) ActionProxy(com.opensymphony.xwork2.ActionProxy) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMap(org.apache.struts2.dispatcher.RequestMap) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) ServletContext(javax.servlet.ServletContext) SessionMap(org.apache.struts2.dispatcher.SessionMap) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 19 with ValidationAware

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());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ValidateGroupAction(org.apache.struts.beanvalidation.actions.ValidateGroupAction) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 20 with ValidationAware

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);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) FieldAction(org.apache.struts.beanvalidation.actions.FieldAction) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Aggregations

ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)42 ActionProxy (com.opensymphony.xwork2.ActionProxy)38 List (java.util.List)28 HashMap (java.util.HashMap)23 Map (java.util.Map)8 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 Collection (java.util.Collection)5 ModelDrivenAction (org.apache.struts.beanvalidation.actions.ModelDrivenAction)5 ActionContext (com.opensymphony.xwork2.ActionContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ValidateGroupAction (org.apache.struts.beanvalidation.actions.ValidateGroupAction)2 HttpParameters (org.apache.struts2.dispatcher.HttpParameters)2 Parameter (org.apache.struts2.dispatcher.Parameter)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1