Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't pass ServletContext #1

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@

package org.apache.struts2.components;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsException;
import org.apache.struts2.StrutsStatics;
Expand All @@ -43,15 +41,14 @@
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.jsp.TagUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* <!-- START SNIPPET: javadoc -->
Expand Down Expand Up @@ -188,7 +185,6 @@ protected Map createExtraContext() {
Map newParams = createParametersForContext();

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();
Expand All @@ -199,8 +195,7 @@ protected Map createExtraContext() {
session,
application,
req,
res,
servletContext);
res);

ValueStack newStack = valueStackFactory.createValueStack(stack);
extraContext.put(ActionContext.VALUE_STACK, newStack);
Expand Down
77 changes: 61 additions & 16 deletions core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ protected ConfigurationManager createConfigurationManager(String name) {
return new ConfigurationManager(name);
}

/**
* @deprecated use version without ServletContext param
*/
@Deprecated
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {

serviceAction(request, response, mapping);
}

/**
* Load Action class for mapping and invoke the appropriate Action method, or go directly to the Result.
* <p/>
Expand All @@ -509,12 +519,13 @@ protected ConfigurationManager createConfigurationManager(String name) {
* @param mapping the action mapping object
* @throws ServletException when an unknown error occurs (not a 404, but typically something that
* would end up as a 5xx by the servlet container)
* @param context Our ServletContext object
*
* @since 2.3.17
*/
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
throws ServletException {

Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
Map<String, Object> extraContext = createContextMap(request, response, mapping);

// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
Expand Down Expand Up @@ -586,17 +597,28 @@ protected void logConfigurationException(HttpServletRequest request, Configurati
}
}

/**
* @deprecated use version without servletContext param
*/
@Deprecated
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
ActionMapping mapping, ServletContext context) {

return createContextMap(request, response, mapping);
}

/**
* Create a context map containing all the wrapped request objects
*
* @param request The servlet request
* @param response The servlet response
* @param mapping The action mapping
* @param context The servlet context
* @return A map of context objects
*
* @since 2.3.17
*/
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
ActionMapping mapping, ServletContext context) {
ActionMapping mapping) {

// request map wrapping the http request objects
Map requestMap = new RequestMap(request);
Expand All @@ -608,16 +630,31 @@ public Map<String,Object> createContextMap(HttpServletRequest request, HttpServl
Map session = new SessionMap(request);

// application map wrapping the ServletContext
Map application = new ApplicationMap(context);
Map application = new ApplicationMap(servletContext);

Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response);

if (mapping != null) {
extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
}
return extraContext;
}

/**
* @deprecated use version without ServletContext param
*/
@Deprecated
public HashMap<String,Object> createContextMap(Map requestMap,
Map parameterMap,
Map sessionMap,
Map applicationMap,
HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {

return createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response);
}

/**
* Merge all application and servlet attributes into a single <tt>HashMap</tt> to represent the entire
* <tt>Action</tt> context.
Expand All @@ -628,16 +665,16 @@ public Map<String,Object> createContextMap(HttpServletRequest request, HttpServl
* @param applicationMap a Map of all servlet context attributes.
* @param request the HttpServletRequest object.
* @param response the HttpServletResponse object.
* @param servletContext the ServletContextmapping object.
* @return a HashMap representing the <tt>Action</tt> context.
*
* @since 2.3.17
*/
public HashMap<String,Object> createContextMap(Map requestMap,
Map parameterMap,
Map sessionMap,
Map applicationMap,
HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
HttpServletResponse response) {
HashMap<String,Object> extraContext = new HashMap<String,Object>();
extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
extraContext.put(ActionContext.SESSION, sessionMap);
Expand Down Expand Up @@ -672,9 +709,8 @@ public HashMap<String,Object> createContextMap(Map requestMap,
* Return the path to save uploaded files to (this is configurable).
*
* @return the path to save uploaded files to
* @param servletContext Our ServletContext
*/
private String getSaveDir(ServletContext servletContext) {
private String getSaveDir() {
String saveDir = multipartSaveDir.trim();

if (saveDir.equals("")) {
Expand Down Expand Up @@ -762,6 +798,14 @@ private void applyEncoding(HttpServletRequest request, String encoding) {
}
}

/**
* @deprecated use version without ServletContext param
*/
@Deprecated
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
return wrapRequest(request);
}

/**
* Wrap and return the given request or return the original request object.
* </p>
Expand All @@ -771,12 +815,13 @@ private void applyEncoding(HttpServletRequest request, String encoding) {
* flexible - look first to that object before overriding this method to handle multipart data.
*
* @param request the HttpServletRequest object.
* @param servletContext Our ServletContext object
* @return a wrapped request or original request.
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper
* @throws java.io.IOException on any error.
*
* @since 2.3.17
*/
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException {
// don't wrap more than once
if (request instanceof StrutsRequestWrapper) {
return request;
Expand All @@ -786,7 +831,7 @@ public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext
if (content_type != null && content_type.contains("multipart/form-data")) {
MultiPartRequest mpr = getMultiPartRequest();
LocaleProvider provider = getContainer().getInstance(LocaleProvider.class);
request = new MultiPartRequestWrapper(mpr, request, getSaveDir(servletContext), provider);
request = new MultiPartRequestWrapper(mpr, request, getSaveDir(), provider);
} else {
request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ protected HttpServletRequest prepareDispatcherAndWrapRequest(HttpServletRequest
try {
// Wrap request first, just in case it is multipart/form-data
// parameters might not be accessible through before encoding (ww-1278)
request = dispatcher.wrapRequest(request, getServletContext());
request = dispatcher.wrapRequest(request);
} catch (IOException e) {
String message = "Could not wrap servlet request with MultipartRequestWrapper!";
log.error(message, e);
Expand Down Expand Up @@ -431,7 +431,7 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
return;
}

dispatcher.serviceAction(request, response, servletContext, mapping);
dispatcher.serviceAction(request, response, mapping);

} finally {
dispatcher.cleanUpRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@
* Contains execution operations for filters
*/
public class ExecuteOperations {
private ServletContext servletContext;

private Dispatcher dispatcher;

@Deprecated
public ExecuteOperations(ServletContext servletContext, Dispatcher dispatcher) {
this.dispatcher = dispatcher;
this.servletContext = servletContext;
}

public ExecuteOperations(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}

/**
Expand Down Expand Up @@ -74,6 +78,6 @@ public boolean executeStaticResourceRequest(HttpServletRequest request, HttpServ
* @throws ServletException
*/
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
dispatcher.serviceAction(request, response, servletContext, mapping);
dispatcher.serviceAction(request, response, mapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ public class PrepareOperations {

private static final Logger LOG = LoggerFactory.getLogger(PrepareOperations.class);

private ServletContext servletContext;
private Dispatcher dispatcher;
private static final String STRUTS_ACTION_MAPPING_KEY = "struts.actionMapping";
public static final String CLEANUP_RECURSION_COUNTER = "__cleanup_recursion_counter";
private Logger log = LoggerFactory.getLogger(PrepareOperations.class);

@Deprecated
public PrepareOperations(ServletContext servletContext, Dispatcher dispatcher) {
this.dispatcher = dispatcher;
this.servletContext = servletContext;
}

public PrepareOperations(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}

/**
Expand All @@ -75,7 +78,7 @@ public ActionContext createActionContext(HttpServletRequest request, HttpServlet
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
stack.getContext().putAll(dispatcher.createContextMap(request, response, null));
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
Expand Down Expand Up @@ -131,7 +134,7 @@ public HttpServletRequest wrapRequest(HttpServletRequest oldRequest) throws Serv
try {
// Wrap request first, just in case it is multipart/form-data
// parameters might not be accessible through before encoding (ww-1278)
request = dispatcher.wrapRequest(request, servletContext);
request = dispatcher.wrapRequest(request);
} catch (IOException e) {
throw new ServletException("Could not wrap servlet request with MultipartRequestWrapper!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected synchronized void lazyInit() {
Dispatcher dispatcher = init.findDispatcherOnThread();
init.initStaticContentLoader(new FilterHostConfig(filterConfig), dispatcher);

prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
prepare = new PrepareOperations(dispatcher);
execute = new ExecuteOperations(dispatcher);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void init(FilterConfig filterConfig) throws ServletException {
dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);

prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
prepare = new PrepareOperations(dispatcher);
execute = new ExecuteOperations(dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

postInit(dispatcher, filterConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
init.initLogging(config);
dispatcher = init.initDispatcher(config);

prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
prepare = new PrepareOperations(dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

postInit(dispatcher, filterConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void contextInitialized(ServletContextEvent sce) {
dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);

prepare = new PrepareOperations(config.getServletContext(), dispatcher);
prepare = new PrepareOperations(dispatcher);
sce.getServletContext().setAttribute(StrutsStatics.SERVLET_DISPATCHER, dispatcher);
} finally {
if (dispatcher != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void init(ServletConfig filterConfig) throws ServletException {
dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);

prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
prepare = new PrepareOperations(dispatcher);
execute = new ExecuteOperations(dispatcher);
} finally {
if (dispatcher != null) {
dispatcher.cleanUpAfterInit();
Expand Down
Loading