Search in sources :

Example 1 with ServletActionRedirectResult

use of org.apache.struts2.result.ServletActionRedirectResult in project struts by apache.

the class ServletActionRedirectResultTest method testExpressionParameterInResultWithConditionParseOn.

public void testExpressionParameterInResultWithConditionParseOn() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "${#value1}").addParam("param2", "${#value2}").addParam("param3", "${#value3}").addParam("anchor", "${#fragment}").build();
    ActionContext context = ActionContext.getContext();
    ValueStack stack = context.getValueStack();
    context.getContextMap().put("value1", "value 1");
    context.getContextMap().put("value2", "value 2");
    context.getContextMap().put("value3", "value 3");
    context.getContextMap().put("namespaceName", "${1-1}");
    context.getContextMap().put("actionName", "${1-1}");
    context.getContextMap().put("methodName", "${1-1}");
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    Map<String, ResultConfig> results = new HashMap<>();
    results.put("myResult", resultConfig);
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
    ServletActionRedirectResult result = new ServletActionRedirectResult();
    result.setNamespace("/myNamespace${#namespaceName}");
    result.setActionName("myAction${#actionName}");
    result.setMethod("myMethod${#methodName}");
    result.setParse(true);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setAnchor("fragment");
    result.setUrlHelper(new DefaultUrlHelper());
    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy).anyTimes();
    expect(mockInvocation.getResultCode()).andReturn("myResult").anyTimes();
    expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
    expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
    expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace${1-1}/myAction${1-1}!myMethod${1-1}.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    req = new MockHttpServletRequest();
    res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    result.execute(mockInvocation);
    assertEquals("/myNamespace0/myAction0!myMethod0.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 2 with ServletActionRedirectResult

use of org.apache.struts2.result.ServletActionRedirectResult in project ipt by gbif.

the class RedirectMessageInterceptor method after.

/**
 * If the result is a redirect then store error and messages in the session.
 */
protected void after(ActionInvocation invocation, BaseAction action) throws Exception {
    Result result = invocation.getResult();
    if (result != null && (result instanceof ServletRedirectResult || result instanceof ServletActionRedirectResult)) {
        Map<String, Object> session = invocation.getInvocationContext().getSession();
        Collection<String> actionWarnings = action.getWarnings();
        if (actionWarnings != null && !actionWarnings.isEmpty()) {
            session.put(ACTION_WARNINGS_KEY, actionWarnings);
        }
        Collection<String> actionErrors = action.getActionErrors();
        if (actionErrors != null && !actionErrors.isEmpty()) {
            session.put(ACTION_ERRORS_KEY, actionErrors);
        }
        Collection<String> actionMessages = action.getActionMessages();
        if (actionMessages != null && !actionMessages.isEmpty()) {
            session.put(ACTION_MESSAGES_KEY, actionMessages);
        }
        Map<String, List<String>> fieldErrors = action.getFieldErrors();
        if (fieldErrors != null && !fieldErrors.isEmpty()) {
            session.put(FIELD_ERRORS_KEY, fieldErrors);
        }
    }
}
Also used : ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult) List(java.util.List) ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) Result(com.opensymphony.xwork2.Result) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult)

Example 3 with ServletActionRedirectResult

use of org.apache.struts2.result.ServletActionRedirectResult in project entando-core by entando.

the class RedirectMessageInterceptor method after.

/**
 * If the result is a redirect then store error and messages in the session.
 * @param invocation
 * @param validationAware
 * @throws java.lang.Exception
 */
protected void after(ActionInvocation invocation, ValidationAware validationAware) throws Exception {
    Result result = invocation.getResult();
    if (result != null && (result instanceof ServletRedirectResult || result instanceof ServletActionRedirectResult || result instanceof FrontServletActionRedirectResult)) {
        HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession();
        Collection<String> actionErrors = validationAware.getActionErrors();
        if (actionErrors != null && actionErrors.size() > 0) {
            session.setAttribute(ACTION_ERRORS_KEY, actionErrors);
        }
        Collection<String> actionMessages = validationAware.getActionMessages();
        if (actionMessages != null && actionMessages.size() > 0) {
            session.setAttribute(ACTION_MESSAGES_KEY, actionMessages);
        }
        Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
        if (fieldErrors != null && fieldErrors.size() > 0) {
            session.setAttribute(FIELD_ERRORS_KEY, fieldErrors);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult) HttpSession(javax.servlet.http.HttpSession) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult) List(java.util.List) Result(com.opensymphony.xwork2.Result) ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult)

Example 4 with ServletActionRedirectResult

use of org.apache.struts2.result.ServletActionRedirectResult in project struts by apache.

the class ServletActionRedirectResultTest method testIncludeParameterInResultWithConditionParseOn.

public void testIncludeParameterInResultWithConditionParseOn() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "${#value1}").addParam("param2", "${#value2}").addParam("param3", "${#value3}").addParam("anchor", "${#fragment}").build();
    ActionContext context = ActionContext.getContext();
    ValueStack stack = context.getValueStack();
    context.getContextMap().put("value1", "value 1");
    context.getContextMap().put("value2", "value 2");
    context.getContextMap().put("value3", "value 3");
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    Map<String, ResultConfig> results = new HashMap<>();
    results.put("myResult", resultConfig);
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
    ServletActionRedirectResult result = new ServletActionRedirectResult();
    result.setActionName("myAction${1-1}");
    result.setNamespace("/myNamespace${1-1}");
    result.setParse(true);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setAnchor("fragment");
    result.setUrlHelper(new DefaultUrlHelper());
    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
    expect(mockInvocation.getResultCode()).andReturn("myResult");
    expect(mockActionProxy.getConfig()).andReturn(actionConfig);
    expect(mockInvocation.getInvocationContext()).andReturn(context);
    expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace0/myAction0.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 5 with ServletActionRedirectResult

use of org.apache.struts2.result.ServletActionRedirectResult in project struts by apache.

the class ServletActionRedirectResultTest method testIncludeParameterInResult.

public void testIncludeParameterInResult() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").addParam("anchor", "fragment").build();
    ActionContext context = ActionContext.getContext();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    Map<String, ResultConfig> results = new HashMap<>();
    results.put("myResult", resultConfig);
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
    ServletActionRedirectResult result = new ServletActionRedirectResult();
    result.setActionName("myAction");
    result.setNamespace("/myNamespace");
    result.setParse(false);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setAnchor("fragment");
    result.setUrlHelper(new DefaultUrlHelper());
    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
    expect(mockInvocation.getResultCode()).andReturn("myResult");
    expect(mockActionProxy.getConfig()).andReturn(actionConfig);
    expect(mockInvocation.getInvocationContext()).andReturn(context);
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)4 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)4 ActionProxy (com.opensymphony.xwork2.ActionProxy)4 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)4 HashMap (java.util.HashMap)4 ServletActionContext (org.apache.struts2.ServletActionContext)4 ActionMapper (org.apache.struts2.dispatcher.mapper.ActionMapper)4 DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)4 IMocksControl (org.easymock.IMocksControl)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 Result (com.opensymphony.xwork2.Result)2 List (java.util.List)2 ServletActionRedirectResult (org.apache.struts2.result.ServletActionRedirectResult)2 ServletRedirectResult (org.apache.struts2.result.ServletRedirectResult)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 FrontServletActionRedirectResult (org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult)1