public static ActionContext getActionContext(HttpServletRequest req) { OgnlValueStack vs = getValueStack(req); if (vs != null) { return new ActionContext(vs.getContext()); } else { return null; } }
@Deprecated public static synchronized ActionContext getContext() { final String message = "Detected a call to obsolete XWork1 ActionContext. Set log level to DEBUG on " + ActionContext.class.getCanonicalName() + " to find out where the call came from"; if (log.isDebugEnabled()) { log.debug(message, new Throwable()); } else { log.info(message); } final com.opensymphony.xwork2.ActionContext superContext = getSuperContext(); if (superContext==null) { actionContextThreadLocal.remove(); return null; } ActionContext actionContext = actionContextThreadLocal.get(); if (actionContext==null) { ValueStack vs = superContext.getValueStack(); actionContext = new ActionContext(vs.getContext()); actionContextThreadLocal.set(actionContext); } return actionContext; }
public static OgnlValueStack getStack(PageContext pageContext) { HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); OgnlValueStack stack = (OgnlValueStack) req.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY); if (stack == null) { stack = new OgnlValueStack(); HttpServletResponse res = (HttpServletResponse) pageContext.getResponse(); DispatcherUtils.initialize(pageContext.getServletContext()); DispatcherUtils du = DispatcherUtils.getInstance(); Map extraContext = du.createContextMap(new RequestMap(req), req.getParameterMap(), new SessionMap(req), new ApplicationMap(pageContext.getServletContext()), req, res, pageContext.getServletContext()); extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext); stack.getContext().putAll(extraContext); req.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack); // also tie this stack/context to the ThreadLocal ActionContext.setContext(new ActionContext(stack.getContext())); } else { // let's make sure that the current page context is in the action context Map context = stack.getContext(); context.put(ServletActionContext.PAGE_CONTEXT, pageContext); AttributeMap attrMap = new AttributeMap(context); context.put("attr", attrMap); } return stack; }
private Map createExtraContext() { Map parentParams = null; if (!ignoreContextParams) { parentParams = new ActionContext(getStack().getContext()).getParameters(); } Map newParams = (parentParams != null) ? new HashMap(parentParams) : new HashMap(); if (parameters != null) { newParams.putAll(parameters); } ActionContext ctx = new ActionContext(stack.getContext()); ServletContext servletContext = (ServletContext) ctx.get(ServletActionContext.SERVLET_CONTEXT); PageContext pageContext = (PageContext) ctx.get(ServletActionContext.PAGE_CONTEXT); Map session = ctx.getSession(); Map application = ctx.getApplication(); DispatcherUtils.initialize(servletContext); DispatcherUtils du = DispatcherUtils.getInstance(); Map extraContext = du.createContextMap(new RequestMap(req), newParams, session, application, req, res, servletContext); OgnlValueStack newStack = new OgnlValueStack(stack); extraContext.put(ActionContext.VALUE_STACK, newStack); // add page context, such that ServletDispatcherResult will do an include extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext); return extraContext; }
public static String buildNamespace(OgnlValueStack stack, HttpServletRequest request) { ActionContext context = new ActionContext(stack.getContext()); ActionInvocation invocation = context.getActionInvocation(); if (invocation == null) { ActionMapper mapper = ActionMapperFactory.getMapper(); ActionMapping mapping = mapper.getMapping(request); if (mapping != null) { return mapping.getNamespace(); } else { // well, if the ActionMapper can't tell us, and there is no existing action invocation, // let's just go with a default guess that the namespace is the last the path minus the // last part (/foo/bar/baz.xyz -> /foo/bar) String path = RequestUtils.getServletPath(request); return path.substring(0, path.lastIndexOf("/")); } } else { return invocation.getProxy().getNamespace(); } } }
/** * Applies the decorator, creating the relevent contexts and delegating to * the extended applyDecorator(). * * @param page The page * @param decorator The decorator * @param req The servlet request * @param res The servlet response */ protected void applyDecorator(Page page, Decorator decorator, HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletContext servletContext = filterConfig.getServletContext(); ActionContext ctx = ServletActionContext.getActionContext(req); if (ctx == null) { // ok, one isn't associated with the request, so let's get a ThreadLocal one (which will create one if needed) OgnlValueStack vs = new OgnlValueStack(); vs.getContext().putAll(DispatcherUtils.getInstance().createContextMap(req, res, null, servletContext)); ctx = new ActionContext(vs.getContext()); if (ctx.getActionInvocation() == null) { // put in a dummy ActionSupport so basic functionality still works ActionSupport action = new ActionSupport(); vs.push(action); ctx.setActionInvocation(new DummyActionInvocation(action)); } } // delegate to the actual page decorator applyDecorator(page, decorator, req, res, servletContext, ctx); }