Search in sources :

Example 1 with DefaultUrlHelper

use of org.apache.struts2.views.util.DefaultUrlHelper in project struts by apache.

the class JSONActionRedirectResultTest method testJsonRedirect.

public void testJsonRedirect() throws Exception {
    JSONActionRedirectResult result = new JSONActionRedirectResult();
    result.setActionName("targetAction");
    result.setActionMapper(new DefaultActionMapper());
    result.setUrlHelper(new DefaultUrlHelper());
    request.setParameter("struts.enableJSONValidation", "true");
    request.setParameter("struts.validateOnly", "false");
    Object action = new Object();
    stack.push(action);
    this.invocation.setAction(action);
    result.execute(this.invocation);
    String content = response.getContentAsString();
    assertEquals("{\"location\": \"/targetAction.action\"}", content);
    assertEquals(200, response.getStatus());
}
Also used : DefaultActionMapper(org.apache.struts2.dispatcher.mapper.DefaultActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper)

Example 2 with DefaultUrlHelper

use of org.apache.struts2.views.util.DefaultUrlHelper in project struts by apache.

the class JSONActionRedirectResultTest method testValidateOnlyFalse.

public void testValidateOnlyFalse() throws Exception {
    JSONActionRedirectResult result = new JSONActionRedirectResult();
    result.setActionName("targetAction");
    result.setActionMapper(new DefaultActionMapper());
    result.setUrlHelper(new DefaultUrlHelper());
    request.setParameter("struts.enableJSONValidation", "true");
    request.setParameter("struts.validateOnly", "true");
    Object action = new Object();
    stack.push(action);
    this.invocation.setAction(action);
    result.execute(this.invocation);
    String content = response.getContentAsString();
    assertEquals("", content);
    String location = response.getHeader("Location");
    assertEquals("/targetAction.action", location);
    assertEquals(302, response.getStatus());
}
Also used : DefaultActionMapper(org.apache.struts2.dispatcher.mapper.DefaultActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper)

Example 3 with DefaultUrlHelper

use of org.apache.struts2.views.util.DefaultUrlHelper 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 4 with DefaultUrlHelper

use of org.apache.struts2.views.util.DefaultUrlHelper in project struts by apache.

the class JSPRuntime method handle.

public static void handle(String location, boolean flush) throws Exception {
    final HttpServletResponse response = ServletActionContext.getResponse();
    final HttpServletRequest request = ServletActionContext.getRequest();
    int i = location.indexOf("?");
    if (i > 0) {
        // extract params from the url and add them to the request
        final UrlHelper urlHelperGetInstance = ServletActionContext.getContext().getInstance(UrlHelper.class);
        final UrlHelper contextUrlHelper = (urlHelperGetInstance != null ? urlHelperGetInstance : (UrlHelper) ActionContext.getContext().get(StrutsConstants.STRUTS_URL_HELPER));
        final UrlHelper urlHelper = (contextUrlHelper != null ? contextUrlHelper : new DefaultUrlHelper());
        String query = location.substring(i + 1);
        Map<String, Object> queryParams = urlHelper.parseQueryString(query, true);
        if (queryParams != null && !queryParams.isEmpty()) {
            Map<String, Parameter> newParams = new HashMap<>();
            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
                newParams.put(entry.getKey(), new Parameter.Request(entry.getKey(), entry.getValue()));
            }
            ActionContext.getContext().getParameters().appendAll(newParams);
        }
        location = location.substring(0, i);
    }
    Servlet servlet = servletCache.get(location);
    HttpJspPage page = (HttpJspPage) servlet;
    page._jspService(request, response);
    if (flush)
        response.flushBuffer();
}
Also used : DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) UrlHelper(org.apache.struts2.views.util.UrlHelper) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpJspPage(javax.servlet.jsp.HttpJspPage) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) Parameter(org.apache.struts2.dispatcher.Parameter) Servlet(javax.servlet.Servlet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with DefaultUrlHelper

use of org.apache.struts2.views.util.DefaultUrlHelper 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)

Aggregations

DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)11 HashMap (java.util.HashMap)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 ActionContext (com.opensymphony.xwork2.ActionContext)6 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)6 ActionProxy (com.opensymphony.xwork2.ActionProxy)6 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)6 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)6 ServletActionContext (org.apache.struts2.ServletActionContext)6 ActionMapper (org.apache.struts2.dispatcher.mapper.ActionMapper)6 IMocksControl (org.easymock.IMocksControl)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 DefaultActionMapper (org.apache.struts2.dispatcher.mapper.DefaultActionMapper)3 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 UrlHelper (org.apache.struts2.views.util.UrlHelper)2 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1