use of org.apache.struts2.dispatcher.ApplicationMap in project struts by apache.
the class TagUtils method getStack.
public static ValueStack getStack(PageContext pageContext) {
HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
ValueStack stack = ServletActionContext.getValueStack(req);
if (stack == null) {
HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
Dispatcher du = Dispatcher.getInstance();
if (du == null) {
throw new ConfigurationException("The Struts dispatcher cannot be found. This is usually caused by " + "using Struts tags without the associated filter. Struts tags are only usable when the request " + "has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.");
}
stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
HttpParameters params = HttpParameters.create(req.getParameterMap()).build();
Map<String, Object> extraContext = du.createContextMap(new RequestMap(req), params, new SessionMap<>(req), new ApplicationMap(pageContext.getServletContext()), req, res);
extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
stack.getContext().putAll(extraContext);
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
// also tie this stack/context to the ThreadLocal
ActionContext.of(stack.getContext()).bind();
} else {
// let's make sure that the current page context is in the action context
// TODO: refactor this to stop using put()
Map<String, Object> context = stack.getContext();
context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
AttributeMap attrMap = new AttributeMap(context);
context.put("attr", attrMap);
}
return stack;
}
use of org.apache.struts2.dispatcher.ApplicationMap in project struts by apache.
the class URLTagTest method testEmptyActionCustomMapper.
public void testEmptyActionCustomMapper() throws Exception {
Map<String, String> props = new HashMap<>();
props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
this.tearDown();
Dispatcher du = this.initDispatcher(props);
/**
* create our standard mock objects
*/
action = this.getAction();
stack = ActionContext.getContext().getValueStack();
context = stack.getContext();
stack.push(action);
request = new StrutsMockHttpServletRequest();
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
response = new StrutsMockHttpServletResponse();
request.setSession(new StrutsMockHttpSession());
request.setupGetServletPath("/");
writer = new StringWriter();
servletContext = new StrutsMockServletContext();
servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
servletContext.setServletInfo("Resin");
pageContext = new StrutsMockPageContext();
pageContext.setRequest(request);
pageContext.setResponse(response);
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
session = new SessionMap(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, response);
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
// ... but generally we want to just use no locale (let it stay system default)
extraContext.remove(ActionContext.LOCALE);
stack.getContext().putAll(extraContext);
context.put(ServletActionContext.HTTP_REQUEST, request);
context.put(ServletActionContext.HTTP_RESPONSE, response);
context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
// Make sure we have an action invocation available
ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
apFactory.setContainer(container);
ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
ActionContext.getContext().getActionInvocation().init(ap);
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(80);
tag = new URLTag();
tag.setPageContext(pageContext);
JspWriter jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
request.setRequestURI("/context/someAction.action");
tag.setAction(null);
tag.setValue(null);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
URLTag freshTag = new URLTag();
freshTag.setPageContext(pageContext);
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-blue", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
// URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
use of org.apache.struts2.dispatcher.ApplicationMap in project struts by apache.
the class URLTagTest method testEmptyActionCustomMapper_clearTagStateSet.
public void testEmptyActionCustomMapper_clearTagStateSet() throws Exception {
Map<String, String> props = new HashMap<String, String>();
props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
this.tearDown();
Dispatcher du = this.initDispatcher(props);
action = this.getAction();
stack = ActionContext.getContext().getValueStack();
context = stack.getContext();
stack.push(action);
request = new StrutsMockHttpServletRequest();
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
response = new StrutsMockHttpServletResponse();
request.setSession(new StrutsMockHttpSession());
request.setupGetServletPath("/");
writer = new StringWriter();
servletContext = new StrutsMockServletContext();
servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
servletContext.setServletInfo("Resin");
pageContext = new StrutsMockPageContext();
pageContext.setRequest(request);
pageContext.setResponse(response);
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
session = new SessionMap<>(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, response);
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
// ... but generally we want to just use no locale (let it stay system default)
extraContext = ActionContext.of(extraContext).withLocale(null).getContextMap();
stack.getContext().putAll(extraContext);
ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
// Make sure we have an action invocation available
ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
apFactory.setContainer(container);
ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
ActionContext.getContext().getActionInvocation().init(ap);
request.setScheme("http");
request.setServerName("localhost");
request.setServerPort(80);
tag = new URLTag();
// Explicitly request tag state clearing.
tag.setPerformClearTagStateForTagPoolingServers(true);
tag.setPageContext(pageContext);
JspWriter jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
request.setRequestURI("/context/someAction.action");
tag.setAction(null);
tag.setValue(null);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
URLTag freshTag = new URLTag();
freshTag.setPerformClearTagStateForTagPoolingServers(true);
freshTag.setPageContext(pageContext);
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-blue", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
writer = new StringWriter();
jspWriter = new StrutsMockJspWriter(writer);
pageContext.setJspWriter(jspWriter);
tag.doStartTag();
// Ensure component tag state clearing is set true (to match tag).
setComponentTagClearTagState(tag, true);
tag.doEndTag();
assertEquals("/hello.action-red", writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set. " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
use of org.apache.struts2.dispatcher.ApplicationMap in project struts by apache.
the class AbstractTagTest method createMocks.
protected void createMocks() {
action = this.getAction();
container.inject(action);
stack = ActionContext.getContext().getValueStack();
context = stack.getContext();
stack.push(action);
request = new StrutsMockHttpServletRequest();
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
response = new StrutsMockHttpServletResponse();
request.setSession(new StrutsMockHttpSession());
request.setupGetServletPath("/");
writer = new StringWriter();
JspWriter jspWriter = new StrutsMockJspWriter(writer);
servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
servletContext.setServletInfo("Resin");
pageContext = new StrutsMockPageContext();
pageContext.setRequest(request);
pageContext.setResponse(response);
pageContext.setJspWriter(jspWriter);
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
MockDispatcher du = new MockDispatcher(pageContext.getServletContext(), new HashMap<>(), configurationManager);
du.init();
Dispatcher.setInstance(du);
session = new SessionMap<>(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, response);
// let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
// ... but generally we want to just use no locale (let it stay system default)
extraContext = ActionContext.of(extraContext).withLocale(null).getContextMap();
stack.getContext().putAll(extraContext);
ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
}
use of org.apache.struts2.dispatcher.ApplicationMap in project struts by apache.
the class Jsr168Dispatcher method createContextMap.
/**
* Merges all application and portlet attributes into a single
* <tt>HashMap</tt> to represent the entire <tt>Action</tt> context.
*
* @param requestMap a Map of all request attributes.
* @param parameterMap a Map of all request parameters.
* @param sessionMap a Map of all session attributes.
* @param applicationMap a Map of all servlet context attributes.
* @param request the PortletRequest object.
* @param response the PortletResponse object.
* @param servletRequest the HttpServletRequest object.
* @param servletResponse the HttpServletResponse object.
* @param servletContext the ServletContext object.
* @param portletConfig the PortletConfig object.
* @param phase The portlet phase (render or action, see
* {@link PortletConstants})
* @return a HashMap representing the <tt>Action</tt> context.
* @throws IOException in case of IO errors
*/
public Map<String, Object> createContextMap(Map<String, Object> requestMap, Map<String, String[]> parameterMap, Map<String, Object> sessionMap, Map<String, Object> applicationMap, PortletRequest request, PortletResponse response, HttpServletRequest servletRequest, HttpServletResponse servletResponse, ServletContext servletContext, PortletConfig portletConfig, PortletPhase phase) throws IOException {
// TODO Must put http request/response objects into map for use with
container.inject(servletRequest);
// ServletActionContext
Map<String, Object> extraContext = ActionContext.of(new HashMap<String, Object>()).withServletRequest(servletRequest).withServletResponse(servletResponse).withServletContext(servletContext).withParameters(HttpParameters.create(parameterMap).build()).withSession(sessionMap).withApplication(applicationMap).withLocale(getLocale(request)).with(StrutsStatics.STRUTS_PORTLET_CONTEXT, getPortletContext()).with(REQUEST, request).with(RESPONSE, response).with(PORTLET_CONFIG, portletConfig).with(PORTLET_NAMESPACE, portletNamespace).with(DEFAULT_ACTION_FOR_MODE, actionMap.get(request.getPortletMode())).with("request", requestMap).with("session", sessionMap).with("application", applicationMap).with("parameters", parameterMap).with(MODE_NAMESPACE_MAP, modeMap).with(PortletConstants.DEFAULT_ACTION_MAP, actionMap).with(PortletConstants.PHASE, phase).getContextMap();
AttributeMap attrMap = new AttributeMap(extraContext);
extraContext.put("attr", attrMap);
return extraContext;
}
Aggregations