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());
}
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());
}
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¶m2=value+2¶m3=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¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
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();
}
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¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
Aggregations