Merge branch 'kusal-depr-apis-3.5' into 7.0.x/merge-master-2024-11-01

# Conflicts:
#	core/src/main/java/com/opensymphony/xwork2/Action.java
#	core/src/main/java/com/opensymphony/xwork2/ActionContext.java
#	core/src/main/java/com/opensymphony/xwork2/factory/DefaultInterceptorFactory.java
#	core/src/main/java/com/opensymphony/xwork2/interceptor/Interceptor.java
#	jakarta/pom.xml
#	plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/FreemarkerDecoratorServlet.java
#	plugins/sitemesh/src/main/java/org/apache/struts2/sitemesh/VelocityDecoratorServlet.java
This commit is contained in:
Kusal Kithul-Godage
2024-11-01 15:21:57 +11:00
21 changed files with 1571 additions and 829 deletions
@@ -19,70 +19,10 @@
package com.opensymphony.xwork2;
/**
* All actions <b>may</b> implement this interface, which exposes the <code>execute()</code> method.
* <p>
* However, as of XWork 1.1, this is <b>not</b> required and is only here to assist users. You are free to create POJOs
* that honor the same contract defined by this interface without actually implementing the interface.
* </p>
* {@inheritDoc}
*
* @deprecated since 6.7.0, use {@link org.apache.struts2.Action} instead.
*/
public interface Action {
/**
* The action execution was successful. Show result
* view to the end user.
*/
String SUCCESS = "success";
/**
* The action execution was successful but do not
* show a view. This is useful for actions that are
* handling the view in another fashion like redirect.
*/
String NONE = "none";
/**
* The action execution was a failure.
* Show an error view, possibly asking the
* user to retry entering data.
*/
String ERROR = "error";
/**
* <p>
* The action execution require more input
* in order to succeed.
* This result is typically used if a form
* handling action has been executed so as
* to provide defaults for a form. The
* form associated with the handler should be
* shown to the end user.
* </p>
*
* <p>
* This result is also used if the given input
* params are invalid, meaning the user
* should try providing input again.
* </p>
*/
String INPUT = "input";
/**
* The action could not execute, since the
* user most was not logged in. The login view
* should be shown.
*/
String LOGIN = "login";
/**
* Where the logic of the action is executed.
*
* @return a string representing the logical result of the execution.
* See constants in this interface for a list of standard result values.
* @throws Exception thrown if a system level exception occurs.
* <b>Note:</b> Application level exceptions should be handled by returning
* an error value, such as <code>Action.ERROR</code>.
*/
String execute() throws Exception;
@Deprecated
public interface Action extends org.apache.struts2.Action {
}
@@ -21,8 +21,6 @@ package com.opensymphony.xwork2;
import com.opensymphony.xwork2.conversion.impl.ConversionData;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsException;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
@@ -30,515 +28,242 @@ import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.jsp.PageContext;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* <p>
* The ActionContext is the context in which an {@link Action} is executed. Each context is basically a
* container of objects an action needs for execution like the session, parameters, locale, etc.
* </p>
* {@inheritDoc}
*
* <p>
* The ActionContext is thread local which means that values stored in the ActionContext are
* unique per thread. See the {@link ThreadLocal} class for more information. The benefit of
* this is you don't need to worry about a user specific action context, you just get it:
* </p>
*
* <code>ActionContext context = ActionContext.getContext();</code>
*
* <p>
* Finally, because of the thread local usage you don't need to worry about making your actions thread safe.
* </p>
*
* @author Patrick Lightbody
* @author Bill Lynch (docs)
* @deprecated since 6.7.0, use {@link org.apache.struts2.ActionContext} instead.
*/
public class ActionContext implements Serializable {
@Deprecated
public class ActionContext extends org.apache.struts2.ActionContext {
private static final ThreadLocal<ActionContext> actionContext = new ThreadLocal<>();
/**
* Constant for the name of the action being executed.
*/
private static final String ACTION_NAME = "org.apache.struts2.ActionContext.name";
/**
* Constant for the {@link com.opensymphony.xwork2.util.ValueStack OGNL value stack}.
*/
private static final String VALUE_STACK = ValueStack.VALUE_STACK;
/**
* Constant for the action's session.
*/
private static final String SESSION = "org.apache.struts2.ActionContext.session";
/**
* Constant for the action's application context.
*/
private static final String APPLICATION = "org.apache.struts2.ActionContext.application";
/**
* Constant for the action's parameters.
*/
private static final String PARAMETERS = "org.apache.struts2.ActionContext.parameters";
/**
* Constant for the action's locale.
*/
private static final String LOCALE = "org.apache.struts2.ActionContext.locale";
/**
* Constant for the action's {@link com.opensymphony.xwork2.ActionInvocation invocation} context.
*/
private static final String ACTION_INVOCATION = "org.apache.struts2.ActionContext.actionInvocation";
/**
* Constant for the map of type conversion errors.
*/
private static final String CONVERSION_ERRORS = "org.apache.struts2.ActionContext.conversionErrors";
/**
* Constant for the container
*/
private static final String CONTAINER = "org.apache.struts2.ActionContext.container";
private final Map<String, Object> context;
/**
* Creates a new ActionContext initialized with another context.
*
* @param context a context map.
*/
protected ActionContext(Map<String, Object> context) {
this.context = context;
private ActionContext(org.apache.struts2.ActionContext actualContext) {
super(actualContext.getContextMap());
}
static ActionContext adapt(org.apache.struts2.ActionContext actualContext) {
return actualContext != null ? new ActionContext(actualContext) : null;
}
/**
* Creates a new ActionContext based on passed in Map
*
* @param context a map with context values
* @return new ActionContext
*/
public static ActionContext of(Map<String, Object> context) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null!");
}
return new ActionContext(context);
return adapt(org.apache.struts2.ActionContext.of(context));
}
/**
* Creates a new ActionContext based on empty Map
*
* @return new ActionContext
*/
public static ActionContext of() {
return of(new HashMap<>());
return adapt(org.apache.struts2.ActionContext.of());
}
/**
* Binds the provided context with the current thread
*
* @param actionContext context to bind to the thread
* @return context which was bound to the thread
*/
public static ActionContext bind(ActionContext actionContext) {
ActionContext.setContext(actionContext);
return ActionContext.getContext();
return adapt(org.apache.struts2.ActionContext.bind(actionContext));
}
public static boolean containsValueStack(Map<String, Object> context) {
return context != null && context.containsKey(VALUE_STACK);
return org.apache.struts2.ActionContext.containsValueStack(context);
}
/**
* Binds this context with the current thread
*
* @return this context which was bound to the thread
*/
public ActionContext bind() {
ActionContext.setContext(this);
return ActionContext.getContext();
}
/**
* Wipes out current ActionContext, use wisely!
*/
public static void clear() {
actionContext.remove();
org.apache.struts2.ActionContext.clear();
}
/**
* Sets the action context for the current thread.
*
* @param context the action context.
*/
private static void setContext(ActionContext context) {
actionContext.set(context);
}
/**
* Returns the ActionContext specific to the current thread.
*
* @return the ActionContext for the current thread, is never <tt>null</tt>.
*/
public static ActionContext getContext() {
return actionContext.get();
return adapt(org.apache.struts2.ActionContext.getContext());
}
@Override
public ActionContext bind() {
super.bind();
return this;
}
/**
* Sets the action invocation (the execution state).
*
* @param actionInvocation the action execution state.
*/
public ActionContext withActionInvocation(ActionInvocation actionInvocation) {
put(ACTION_INVOCATION, actionInvocation);
return withActionInvocation((org.apache.struts2.ActionInvocation) actionInvocation);
}
@Override
public ActionContext withActionInvocation(org.apache.struts2.ActionInvocation actionInvocation) {
super.withActionInvocation(actionInvocation);
return this;
}
/**
* Gets the action invocation (the execution state).
*
* @return the action invocation (the execution state).
*/
@Override
public ActionInvocation getActionInvocation() {
return (ActionInvocation) get(ACTION_INVOCATION);
return ActionInvocation.adapt(super.getActionInvocation());
}
/**
* Sets the action's application context.
*
* @param application the action's application context.
*/
@Override
public ActionContext withApplication(Map<String, Object> application) {
put(APPLICATION, application);
super.withApplication(application);
return this;
}
/**
* Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
*
* @return a Map of ServletContext or generic application level Map
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getApplication() {
return (Map<String, Object>) get(APPLICATION);
return super.getApplication();
}
/**
* Gets the context map.
*
* @return the context map.
*/
@Override
public Map<String, Object> getContextMap() {
return context;
return super.getContextMap();
}
/**
* Sets conversion errors which occurred when executing the action.
*
* @param conversionErrors a Map of errors which occurred when executing the action.
*/
@Override
public ActionContext withConversionErrors(Map<String, ConversionData> conversionErrors) {
put(CONVERSION_ERRORS, conversionErrors);
super.withConversionErrors(conversionErrors);
return this;
}
/**
* Gets the map of conversion errors which occurred when executing the action.
*
* @return the map of conversion errors which occurred when executing the action or an empty map if
* there were no errors.
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, ConversionData> getConversionErrors() {
Map<String, ConversionData> errors = (Map<String, ConversionData>) get(CONVERSION_ERRORS);
if (errors == null) {
errors = withConversionErrors(new HashMap<>()).getConversionErrors();
}
return errors;
return super.getConversionErrors();
}
/**
* Sets the Locale for the current action.
*
* @param locale the Locale for the current action.
*/
@Override
public ActionContext withLocale(Locale locale) {
put(LOCALE, locale);
super.withLocale(locale);
return this;
}
/**
* Gets the Locale of the current action. If no locale was ever specified the platform's
* {@link java.util.Locale#getDefault() default locale} is used.
*
* @return the Locale of the current action.
*/
@Override
public Locale getLocale() {
Locale locale = (Locale) get(LOCALE);
if (locale == null) {
locale = Locale.getDefault();
withLocale(locale);
}
return locale;
return super.getLocale();
}
/**
* Sets the name of the current Action in the ActionContext.
*
* @param actionName the name of the current action.
*/
@Override
public ActionContext withActionName(String actionName) {
put(ACTION_NAME, actionName);
super.withActionName(actionName);
return this;
}
/**
* Gets the name of the current Action.
*
* @return the name of the current action.
*/
@Override
public String getActionName() {
return (String) get(ACTION_NAME);
return super.getActionName();
}
/**
* Sets the action parameters.
*
* @param parameters the parameters for the current action.
*/
@Override
public ActionContext withParameters(HttpParameters parameters) {
put(PARAMETERS, parameters);
super.withParameters(parameters);
return this;
}
/**
* Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of
* parameters otherwise.
*
* @return a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a
* generic Map of parameters otherwise.
*/
@Override
public HttpParameters getParameters() {
return (HttpParameters) get(PARAMETERS);
return super.getParameters();
}
/**
* Sets a map of action session values.
*
* @param session the session values.
*/
@Override
public ActionContext withSession(Map<String, Object> session) {
put(SESSION, session);
super.withSession(session);
return this;
}
/**
* Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
*
* @return the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getSession() {
return (Map<String, Object>) get(SESSION);
return super.getSession();
}
/**
* Sets the OGNL value stack.
*
* @param valueStack the OGNL value stack.
*/
@Override
public ActionContext withValueStack(ValueStack valueStack) {
put(VALUE_STACK, valueStack);
super.withValueStack(valueStack);
return this;
}
/**
* Gets the OGNL value stack.
*
* @return the OGNL value stack.
*/
@Override
public ValueStack getValueStack() {
return (ValueStack) get(VALUE_STACK);
return super.getValueStack();
}
/**
* Gets the container for this request
*
* @param container The container
*/
@Override
public ActionContext withContainer(Container container) {
put(CONTAINER, container);
super.withContainer(container);
return this;
}
/**
* Sets the container for this request
*
* @return The container
*/
@Override
public Container getContainer() {
return (Container) get(CONTAINER);
return super.getContainer();
}
@Override
public <T> T getInstance(Class<T> type) {
Container cont = getContainer();
if (cont != null) {
return cont.getInstance(type);
} else {
throw new StrutsException("Cannot find an initialized container for this request.");
}
return super.getInstance(type);
}
/**
* Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
*
* @param key the key used to find the value.
* @return the value that was found using the key or <tt>null</tt> if the key was not found.
*/
@Override
public Object get(String key) {
return context.get(key);
return super.get(key);
}
/**
* Stores a value in the current ActionContext. The value can be looked up using the key.
*
* @param key the key of the value.
* @param value the value to be stored.
*/
@Override
public void put(String key, Object value) {
context.put(key, value);
super.put(key, value);
}
/**
* Gets ServletContext associated with current action
*
* @return current ServletContext
*/
@Override
public ServletContext getServletContext() {
return (ServletContext) get(StrutsStatics.SERVLET_CONTEXT);
return super.getServletContext();
}
/**
* Assigns ServletContext to action context
*
* @param servletContext associated with current request
* @return ActionContext
*/
@Override
public ActionContext withServletContext(ServletContext servletContext) {
put(StrutsStatics.SERVLET_CONTEXT, servletContext);
super.withServletContext(servletContext);
return this;
}
/**
* Gets ServletRequest associated with current action
*
* @return current ServletRequest
*/
@Override
public HttpServletRequest getServletRequest() {
return (HttpServletRequest) get(StrutsStatics.HTTP_REQUEST);
return super.getServletRequest();
}
/**
* Assigns ServletRequest to action context
*
* @param request associated with current request
* @return ActionContext
*/
@Override
public ActionContext withServletRequest(HttpServletRequest request) {
put(StrutsStatics.HTTP_REQUEST, request);
super.withServletRequest(request);
return this;
}
/**
* Gets ServletResponse associated with current action
*
* @return current ServletResponse
*/
@Override
public HttpServletResponse getServletResponse() {
return (HttpServletResponse) get(StrutsStatics.HTTP_RESPONSE);
return super.getServletResponse();
}
/**
* Assigns ServletResponse to action context
*
* @param response associated with current request
* @return ActionContext
*/
@Override
public ActionContext withServletResponse(HttpServletResponse response) {
put(StrutsStatics.HTTP_RESPONSE, response);
super.withServletResponse(response);
return this;
}
/**
* Gets PageContext associated with current action
*
* @return current PageContext
*/
@Override
public PageContext getPageContext() {
return (PageContext) get(StrutsStatics.PAGE_CONTEXT);
return super.getPageContext();
}
/**
* Assigns PageContext to action context
*
* @param pageContext associated with current request
* @return ActionContext
*/
@Override
public ActionContext withPageContext(PageContext pageContext) {
put(StrutsStatics.PAGE_CONTEXT, pageContext);
super.withPageContext(pageContext);
return this;
}
/**
* Gets ActionMapping associated with current action
*
* @return current ActionMapping
*/
@Override
public ActionMapping getActionMapping() {
return (ActionMapping) get(StrutsStatics.ACTION_MAPPING);
return super.getActionMapping();
}
/**
* Assigns ActionMapping to action context
*
* @param actionMapping associated with current request
* @return ActionContext
*/
@Override
public ActionContext withActionMapping(ActionMapping actionMapping) {
put(StrutsStatics.ACTION_MAPPING, actionMapping);
super.withActionMapping(actionMapping);
return this;
}
/**
* Assigns an extra context map to action context
*
* @param extraContext to add to the current action context
* @return ActionContext
*/
@Override
public ActionContext withExtraContext(Map<String, Object> extraContext) {
if (extraContext != null) {
context.putAll(extraContext);
}
super.withExtraContext(extraContext);
return this;
}
/**
* Adds arbitrary key to action context
*
* @param key a string
* @param value an object
* @return ActionContext
*/
@Override
public ActionContext with(String key, Object value) {
put(key, value);
super.with(key, value);
return this;
}
}
@@ -22,158 +22,102 @@ import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.ValueStack;
/**
* An {@link ActionInvocation} represents the execution state of an {@link Action}. It holds the Interceptors and the Action instance.
* By repeated re-entrant execution of the <code>invoke()</code> method, initially by the {@link ActionProxy}, then by the Interceptors, the
* Interceptors are all executed, and then the {@link Action} and the {@link Result}.
* {@inheritDoc}
*
* @author Jason Carreira
* @see com.opensymphony.xwork2.ActionProxy
* @deprecated since 6.7.0, use {@link org.apache.struts2.ActionInvocation} instead.
*/
public interface ActionInvocation {
@Deprecated
public interface ActionInvocation extends org.apache.struts2.ActionInvocation {
/**
* Get the Action associated with this ActionInvocation.
*
* @return the Action
*/
Object getAction();
/**
* Gets whether this ActionInvocation has executed before.
* This will be set after the Action and the Result have executed.
*
* @return <tt>true</tt> if this ActionInvocation has executed before.
*/
boolean isExecuted();
/**
* Gets the ActionContext associated with this ActionInvocation. The ActionProxy is
* responsible for setting this ActionContext onto the ThreadLocal before invoking
* the ActionInvocation and resetting the old ActionContext afterwards.
*
* @return the ActionContext.
*/
@Override
ActionContext getInvocationContext();
/**
* Get the ActionProxy holding this ActionInvocation.
*
* @return the ActionProxy.
*/
ActionProxy getProxy();
/**
* If the ActionInvocation has been executed before and the Result is an instance of {@link ActionChainResult}, this method
* will walk down the chain of <code>ActionChainResult</code>s until it finds a non-chain result, which will be returned. If the
* ActionInvocation's result has not been executed before, the Result instance will be created and populated with
* the result params.
*
* @return the result.
* @throws Exception can be thrown.
*/
@Override
Result getResult() throws Exception;
/**
* Gets the result code returned from this ActionInvocation.
*
* @return the result code
*/
String getResultCode();
@Override
default void addPreResultListener(org.apache.struts2.interceptor.PreResultListener listener) {
addPreResultListener(PreResultListener.adapt(listener));
}
/**
* Sets the result code, possibly overriding the one returned by the
* action.
*
* <p>
* The "intended" purpose of this method is to allow PreResultListeners to
* override the result code returned by the Action.
* </p>
*
* <p>
* If this method is used before the Action executes, the Action's returned
* result code will override what was set. However the Action could (if
* specifically coded to do so) inspect the ActionInvocation to see that
* someone "upstream" (e.g. an Interceptor) had suggested a value as the
* result, and it could therefore return the same value itself.
* </p>
*
* <p>
* If this method is called between the Action execution and the Result
* execution, then the value set here will override the result code the
* action had returned. Creating an Interceptor that implements
* {@link PreResultListener} will give you this opportunity.
* </p>
*
* <p>
* If this method is called after the Result has been executed, it will
* have the effect of raising an IllegalStateException.
* </p>
*
* @param resultCode the result code.
* @throws IllegalStateException if called after the Result has been executed.
* @see #isExecuted()
*/
void setResultCode(String resultCode);
/**
* Gets the ValueStack associated with this ActionInvocation.
*
* @return the ValueStack
*/
ValueStack getStack();
/**
* Register a {@link PreResultListener} to be notified after the Action is executed and
* before the Result is executed.
*
* <p>
* The ActionInvocation implementation must guarantee that listeners will be called in
* the order in which they are registered.
* </p>
*
* <p>
* Listener registration and execution does not need to be thread-safe.
* </p>
*
* @param listener the listener to add.
*/
void addPreResultListener(PreResultListener listener);
/**
* Invokes the next step in processing this ActionInvocation.
*
* <p>
* If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit
* ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor
* to execute. If there are no more Interceptors to be applied, the Action is executed.
* If the {@link ActionProxy#getExecuteResult()} method returns <tt>true</tt>, the Result is also executed.
* </p>
*
* @throws Exception can be thrown.
* @return the return code.
*/
String invoke() throws Exception;
static ActionInvocation adapt(org.apache.struts2.ActionInvocation actualInvocation) {
return actualInvocation != null ? new LegacyAdapter(actualInvocation) : null;
}
/**
* Invokes only the Action (not Interceptors or Results).
*
* <p>
* This is useful in rare situations where advanced usage with the interceptor/action/result workflow is
* being manipulated for certain functionality.
* </p>
*
* @return the return code.
* @throws Exception can be thrown.
*/
String invokeActionOnly() throws Exception;
class LegacyAdapter implements ActionInvocation {
/**
* Sets the action event listener to respond to key action events.
*
* @param listener the listener.
*/
void setActionEventListener(ActionEventListener listener);
private final org.apache.struts2.ActionInvocation adaptee;
void init(ActionProxy proxy) ;
private LegacyAdapter(org.apache.struts2.ActionInvocation adaptee) {
this.adaptee = adaptee;
}
@Override
public Object getAction() {
return adaptee.getAction();
}
@Override
public boolean isExecuted() {
return adaptee.isExecuted();
}
@Override
public ActionContext getInvocationContext() {
return ActionContext.adapt(adaptee.getInvocationContext());
}
@Override
public ActionProxy getProxy() {
return adaptee.getProxy();
}
@Override
public Result getResult() throws Exception {
return Result.adapt(adaptee.getResult());
}
@Override
public String getResultCode() {
return adaptee.getResultCode();
}
@Override
public void setResultCode(String resultCode) {
adaptee.setResultCode(resultCode);
}
@Override
public ValueStack getStack() {
return adaptee.getStack();
}
@Override
public void addPreResultListener(PreResultListener listener) {
adaptee.addPreResultListener(listener);
}
@Override
public String invoke() throws Exception {
return adaptee.invoke();
}
@Override
public String invokeActionOnly() throws Exception {
return adaptee.invokeActionOnly();
}
@Override
public void setActionEventListener(ActionEventListener listener) {
adaptee.setActionEventListener(listener);
}
@Override
public void init(ActionProxy proxy) {
adaptee.init(proxy);
}
}
}
@@ -258,6 +258,9 @@ public class DefaultActionInvocation implements ActionInvocation {
Interceptor interceptor = interceptorMapping.getInterceptor();
if (interceptor instanceof WithLazyParams) {
interceptor = lazyParamInjector.injectParams(interceptor, interceptorMapping.getParams(), invocationContext);
} else if (interceptor instanceof Interceptor.LegacyAdapter && ((Interceptor.LegacyAdapter) interceptor).getAdaptee() instanceof WithLazyParams) {
org.apache.struts2.interceptor.Interceptor adaptee = ((Interceptor.LegacyAdapter) interceptor).getAdaptee();
lazyParamInjector.injectParams(adaptee, interceptorMapping.getParams(), invocationContext);
}
if (interceptor instanceof ConditionalInterceptor) {
resultCode = executeConditional((ConditionalInterceptor) interceptor);
@@ -18,33 +18,36 @@
*/
package com.opensymphony.xwork2;
import java.io.Serializable;
/**
* All results (except for <code>Action.NONE</code>) of an {@link Action} are mapped to a View implementation.
* {@inheritDoc}
*
* <p>
* Examples of Views might be:
* </p>
*
* <ul>
* <li>SwingPanelView - pops up a new Swing panel</li>
* <li>ActionChainView - executes another action</li>
* <li>SerlvetRedirectView - redirects the HTTP response to a URL</li>
* <li>ServletDispatcherView - dispatches the HTTP response to a URL</li>
* </ul>
*
* @author plightbo
* @deprecated since 6.7.0, use {@link org.apache.struts2.Result} instead.
*/
public interface Result extends Serializable {
@Deprecated
public interface Result extends org.apache.struts2.Result {
@Override
default void execute(org.apache.struts2.ActionInvocation invocation) throws Exception {
execute(ActionInvocation.adapt(invocation));
}
/**
* Represents a generic interface for all action execution results.
* Whether that be displaying a webpage, generating an email, sending a JMS message, etc.
*
* @param invocation the invocation context.
* @throws Exception can be thrown.
*/
void execute(ActionInvocation invocation) throws Exception;
static Result adapt(org.apache.struts2.Result actualResult) {
return actualResult != null ? new LegacyAdapter(actualResult) : null;
}
class LegacyAdapter implements Result {
private final org.apache.struts2.Result adaptee;
private LegacyAdapter(org.apache.struts2.Result adaptee) {
this.adaptee = adaptee;
}
@Override
public void execute(ActionInvocation invocation) throws Exception {
adaptee.execute(ActionInvocation.adapt(invocation));
}
}
}
@@ -70,12 +70,18 @@ public class DefaultInterceptorFactory implements InterceptorFactory {
reflectionProvider.setProperties(params, o);
}
if (o instanceof Interceptor interceptor) {
interceptor.init();
return interceptor;
Interceptor interceptor = null;
if (o instanceof Interceptor) {
interceptor = (Interceptor) o;
} else if (o instanceof org.apache.struts2.interceptor.Interceptor) {
interceptor = Interceptor.adapt((org.apache.struts2.interceptor.Interceptor) o);
}
throw new ConfigurationException("Class [" + interceptorClassName + "] does not implement Interceptor", interceptorConfig);
if (interceptor == null) {
throw new ConfigurationException("Class [" + interceptorClassName + "] does not implement Interceptor", interceptorConfig);
}
interceptor.init();
return interceptor;
} catch (InstantiationException e) {
cause = e;
message = "Unable to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
@@ -20,6 +20,7 @@ package com.opensymphony.xwork2.factory;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.reflection.ReflectionException;
@@ -51,19 +52,29 @@ public class DefaultResultFactory implements ResultFactory {
Result result = null;
if (resultClassName != null) {
result = (Result) objectFactory.buildBean(resultClassName, extraContext);
Object o = objectFactory.buildBean(resultClassName, extraContext);
Map<String, String> params = resultConfig.getParams();
if (params != null) {
for (Map.Entry<String, String> paramEntry : params.entrySet()) {
try {
reflectionProvider.setProperty(paramEntry.getKey(), paramEntry.getValue(), result, extraContext, true);
reflectionProvider.setProperty(paramEntry.getKey(), paramEntry.getValue(), o, extraContext, true);
} catch (ReflectionException ex) {
if (result instanceof ReflectionExceptionHandler) {
((ReflectionExceptionHandler) result).handle(ex);
if (o instanceof ReflectionExceptionHandler) {
((ReflectionExceptionHandler) o).handle(ex);
}
}
}
}
if (o instanceof Result) {
result = (Result) o;
} else if (o instanceof org.apache.struts2.Result) {
result = Result.adapt((org.apache.struts2.Result) o);
}
if (result == null) {
throw new ConfigurationException("Class [" + resultClassName + "] does not implement Result", resultConfig);
}
}
return result;
@@ -21,19 +21,39 @@ package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
/**
* A marking interface, when implemented allows to conditionally execute a given interceptor
* within the current action invocation.
* {@inheritDoc}
*
* @since Struts 6.1.1
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.Interceptor} instead.
*/
public interface ConditionalInterceptor extends Interceptor {
@Deprecated
public interface ConditionalInterceptor extends org.apache.struts2.interceptor.ConditionalInterceptor, Interceptor {
@Override
default boolean shouldIntercept(org.apache.struts2.ActionInvocation invocation) {
return shouldIntercept(ActionInvocation.adapt(invocation));
}
/**
* Determines if a given interceptor should be executed in the current processing of action invocation.
*
* @param invocation current {@link ActionInvocation} to determine if the interceptor should be executed
* @return true if the given interceptor should be included in the current action invocation
* @since 6.1.1
*/
boolean shouldIntercept(ActionInvocation invocation);
static ConditionalInterceptor adapt(org.apache.struts2.interceptor.ConditionalInterceptor actualInterceptor) {
if (actualInterceptor instanceof ConditionalInterceptor) {
return (ConditionalInterceptor) actualInterceptor;
}
return actualInterceptor != null ? new LegacyAdapter(actualInterceptor) : null;
}
class LegacyAdapter extends Interceptor.LegacyAdapter implements ConditionalInterceptor {
private final org.apache.struts2.interceptor.ConditionalInterceptor adaptee;
private LegacyAdapter(org.apache.struts2.interceptor.ConditionalInterceptor adaptee) {
super(adaptee);
this.adaptee = adaptee;
}
@Override
public boolean shouldIntercept(ActionInvocation invocation) {
return adaptee.shouldIntercept(invocation);
}
}
}
@@ -20,203 +20,56 @@ package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import java.io.Serializable;
/**
* <!-- START SNIPPET: introduction -->
* {@inheritDoc}
*
* <p>
* An interceptor is a stateless class that follows the interceptor pattern, as
* found in {@link jakarta.servlet.Filter} and in AOP languages.
* </p>
*
* <p>
* Interceptors are objects that dynamically intercept Action invocations.
* They provide the developer with the opportunity to define code that can be executed
* before and/or after the execution of an action. They also have the ability
* to prevent an action from executing. Interceptors provide developers a way to
* encapsulate common functionality in a re-usable form that can be applied to
* one or more Actions.
* </p>
*
* <p>
* Interceptors <b>must</b> be stateless and not assume that a new instance will be created for each request or Action.
* Interceptors may choose to either short-circuit the {@link ActionInvocation} execution and return a return code
* (such as {@link com.opensymphony.xwork2.Action#SUCCESS}), or it may choose to do some processing before
* and/or after delegating the rest of the procesing using {@link ActionInvocation#invoke()}.
* </p>
* <!-- END SNIPPET: introduction -->
*
* <!-- START SNIPPET: parameterOverriding -->
* <p>
* Interceptor's parameter could be overridden through the following ways :-
* </p>
* <b>Method 1:</b>
* <pre>
* &lt;action name=&quot;myAction&quot; class=&quot;myActionClass&quot;&gt;
* &lt;interceptor-ref name=&quot;exception&quot;/&gt;
* &lt;interceptor-ref name=&quot;alias&quot;/&gt;
* &lt;interceptor-ref name=&quot;params&quot;/&gt;
* &lt;interceptor-ref name=&quot;servletConfig&quot;/&gt;
* &lt;interceptor-ref name=&quot;prepare&quot;/&gt;
* &lt;interceptor-ref name=&quot;i18n&quot;/&gt;
* &lt;interceptor-ref name=&quot;chain&quot;/&gt;
* &lt;interceptor-ref name=&quot;modelDriven&quot;/&gt;
* &lt;interceptor-ref name=&quot;fileUpload&quot;/&gt;
* &lt;interceptor-ref name=&quot;staticParams&quot;/&gt;
* &lt;interceptor-ref name=&quot;params&quot;/&gt;
* &lt;interceptor-ref name=&quot;conversionError&quot;/&gt;
* &lt;interceptor-ref name=&quot;validation&quot;&gt;
* &lt;param name=&quot;excludeMethods&quot;&gt;myValidationExcudeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;interceptor-ref name=&quot;workflow&quot;&gt;
* &lt;param name=&quot;excludeMethods&quot;&gt;myWorkflowExcludeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <b>Method 2:</b>
* <pre>
* &lt;action name=&quot;myAction&quot; class=&quot;myActionClass&quot;&gt;
* &lt;interceptor-ref name=&quot;defaultStack&quot;&gt;
* &lt;param name=&quot;validation.excludeMethods&quot;&gt;myValidationExcludeMethod&lt;/param&gt;
* &lt;param name=&quot;workflow.excludeMethods&quot;&gt;myWorkflowExcludeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <p>
* In the first method, the whole default stack is copied and the parameter then
* changed accordingly.
* </p>
*
* <p>
* In the second method, the 'interceptor-ref' refer to an existing
* interceptor-stack, namely defaultStack in this example, and override the validator
* and workflow interceptor excludeMethods typically in this case. Note that in the
* 'param' tag, the name attribute contains a dot (.) the word before the dot(.)
* specifies the interceptor name whose parameter is to be overridden and the word after
* the dot (.) specifies the parameter itself. Essetially it is as follows :-
* </p>
*
* <pre>
* &lt;interceptor-name&gt;.&lt;parameter-name&gt;
* </pre>
* <p>
* <b>Note</b> also that in this case the 'interceptor-ref' name attribute
* is used to indicate an interceptor stack which makes sense as if it is referring
* to the interceptor itself it would be just using Method 1 describe above.
* </p>
* <!-- END SNIPPET: parameterOverriding -->
*
* <p>
* <b>Nested Interceptor param overriding</b>
* </p>
*
* <!-- START SNIPPET: nestedParameterOverriding -->
* <p>
* Interceptor stack parameter overriding could be nested into as many level as possible, though it would
* be advisable not to nest it too deep as to avoid confusion, For example,
* </p>
* <pre>
* &lt;interceptor name=&quot;interceptor1&quot; class=&quot;foo.bar.Interceptor1&quot; /&gt;
* &lt;interceptor name=&quot;interceptor2&quot; class=&quot;foo.bar.Interceptor2&quot; /&gt;
* &lt;interceptor name=&quot;interceptor3&quot; class=&quot;foo.bar.Interceptor3&quot; /&gt;
* &lt;interceptor name=&quot;interceptor4&quot; class=&quot;foo.bar.Interceptor4&quot; /&gt;
* &lt;interceptor-stack name=&quot;stack1&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor1&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack2&quot;&gt;
* &lt;interceptor-ref name=&quot;intercetor2&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack1&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack3&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor3&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack2&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack4&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor4&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack3&quot; /&gt;
* &lt;/interceptor-stack&gt;
* </pre>
*
* <p>
* Assuming the interceptor has the following properties
* </p>
*
* <table border="1" summary="">
* <tr>
* <td>Interceptor</td>
* <td>property</td>
* </tr>
* <tr>
* <td>Interceptor1</td>
* <td>param1</td>
* </tr>
* <tr>
* <td>Interceptor2</td>
* <td>param2</td>
* </tr>
* <tr>
* <td>Interceptor3</td>
* <td>param3</td>
* </tr>
* <tr>
* <td>Interceptor4</td>
* <td>param4</td>
* </tr>
* </table>
*
* <p>
* We could override them as follows :
* </p>
*
* <pre>
* &lt;action ... &gt;
* &lt;!-- to override parameters of interceptor located directly in the stack --&gt;
* &lt;interceptor-ref name=&quot;stack4&quot;&gt;
* &lt;param name=&quot;interceptor4.param4&quot;&gt; ... &lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
*
* &lt;action ... &gt;
* &lt;!-- to override parameters of interceptor located under nested stack --&gt;
* &lt;interceptor-ref name=&quot;stack4&quot;&gt;
* &lt;param name=&quot;stack3.interceptor3.param3&quot;&gt; ... &lt;/param&gt;
* &lt;param name=&quot;stack3.stack2.interceptor2.param2&quot;&gt; ... &lt;/param&gt;
* &lt;param name=&quot;stack3.stack2.stack1.interceptor1.param1&quot;&gt; ... &lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <!-- END SNIPPET: nestedParameterOverriding -->
*
* @author Jason Carreira
* @author tmjee
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.Interceptor} instead.
*/
public interface Interceptor extends Serializable {
@Deprecated
public interface Interceptor extends org.apache.struts2.interceptor.Interceptor {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
@Override
default String intercept(org.apache.struts2.ActionInvocation invocation) throws Exception {
return intercept(ActionInvocation.adapt(invocation));
}
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
static Interceptor adapt(org.apache.struts2.interceptor.Interceptor actualInterceptor) {
if (actualInterceptor instanceof org.apache.struts2.interceptor.ConditionalInterceptor) {
return ConditionalInterceptor.adapt((org.apache.struts2.interceptor.ConditionalInterceptor) actualInterceptor);
}
if (actualInterceptor instanceof Interceptor) {
return (Interceptor) actualInterceptor;
}
return actualInterceptor != null ? new LegacyAdapter(actualInterceptor) : null;
}
class LegacyAdapter implements Interceptor {
private final org.apache.struts2.interceptor.Interceptor adaptee;
protected LegacyAdapter(org.apache.struts2.interceptor.Interceptor adaptee) {
this.adaptee = adaptee;
}
public org.apache.struts2.interceptor.Interceptor getAdaptee() {
return adaptee;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
return adaptee.intercept(invocation);
}
@Override
public void destroy() {
adaptee.destroy();
}
@Override
public void init() {
adaptee.init();
}
}
}
@@ -21,21 +21,35 @@ package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
/**
* PreResultListeners may be registered with an {@link ActionInvocation} to get a callback after the
* {@link com.opensymphony.xwork2.Action} has been executed but before the {@link com.opensymphony.xwork2.Result}
* is executed.
* {@inheritDoc}
*
* @author Jason Carreira
* @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.PreResultListener} instead.
*/
public interface PreResultListener {
@Deprecated
public interface PreResultListener extends org.apache.struts2.interceptor.PreResultListener {
@Override
default void beforeResult(org.apache.struts2.ActionInvocation invocation, String resultCode) {
beforeResult(ActionInvocation.adapt(invocation), resultCode);
}
/**
* This callback method will be called after the {@link com.opensymphony.xwork2.Action} execution and
* before the {@link com.opensymphony.xwork2.Result} execution.
*
* @param invocation the action invocation
* @param resultCode the result code returned by the action (eg. <code>success</code>).
*/
void beforeResult(ActionInvocation invocation, String resultCode);
static PreResultListener adapt(org.apache.struts2.interceptor.PreResultListener actualListener) {
return actualListener != null ? new LegacyAdapter(actualListener) : null;
}
class LegacyAdapter implements PreResultListener {
private final org.apache.struts2.interceptor.PreResultListener adaptee;
private LegacyAdapter(org.apache.struts2.interceptor.PreResultListener adaptee) {
this.adaptee = adaptee;
}
@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
adaptee.beforeResult(invocation, resultCode);
}
}
}
@@ -67,11 +67,14 @@ public interface WithLazyParams {
}
public Interceptor injectParams(Interceptor interceptor, Map<String, String> params, ActionContext invocationContext) {
return (Interceptor) injectParams((org.apache.struts2.interceptor.Interceptor) interceptor, params, invocationContext);
}
public org.apache.struts2.interceptor.Interceptor injectParams(org.apache.struts2.interceptor.Interceptor interceptor, Map<String, String> params, ActionContext invocationContext) {
for (Map.Entry<String, String> entry : params.entrySet()) {
Object paramValue = textParser.evaluate(new char[]{ '$' }, entry.getValue(), valueEvaluator, TextParser.DEFAULT_LOOP_COUNT);
ognlUtil.setProperty(entry.getKey(), paramValue, interceptor, invocationContext.getContextMap());
}
return interceptor;
}
}
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2;
/**
* All actions <b>may</b> implement this interface, which exposes the <code>execute()</code> method.
* <p>
* However, as of XWork 1.1, this is <b>not</b> required and is only here to assist users. You are free to create POJOs
* that honor the same contract defined by this interface without actually implementing the interface.
* </p>
*/
public interface Action {
/**
* The action execution was successful. Show result
* view to the end user.
*/
String SUCCESS = "success";
/**
* The action execution was successful but do not
* show a view. This is useful for actions that are
* handling the view in another fashion like redirect.
*/
String NONE = "none";
/**
* The action execution was a failure.
* Show an error view, possibly asking the
* user to retry entering data.
*/
String ERROR = "error";
/**
* <p>
* The action execution require more input
* in order to succeed.
* This result is typically used if a form
* handling action has been executed so as
* to provide defaults for a form. The
* form associated with the handler should be
* shown to the end user.
* </p>
*
* <p>
* This result is also used if the given input
* params are invalid, meaning the user
* should try providing input again.
* </p>
*/
String INPUT = "input";
/**
* The action could not execute, since the
* user most was not logged in. The login view
* should be shown.
*/
String LOGIN = "login";
/**
* Where the logic of the action is executed.
*
* @return a string representing the logical result of the execution.
* See constants in this interface for a list of standard result values.
* @throws Exception thrown if a system level exception occurs.
* <b>Note:</b> Application level exceptions should be handled by returning
* an error value, such as <code>Action.ERROR</code>.
*/
String execute() throws Exception;
}
@@ -0,0 +1,552 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2;
import com.opensymphony.xwork2.conversion.impl.ConversionData;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
/**
* <p>
* The ActionContext is the context in which an {@link Action} is executed. Each context is basically a
* container of objects an action needs for execution like the session, parameters, locale, etc.
* </p>
*
* <p>
* The ActionContext is thread local which means that values stored in the ActionContext are
* unique per thread. See the {@link ThreadLocal} class for more information. The benefit of
* this is you don't need to worry about a user specific action context, you just get it:
* </p>
*
* <code>ActionContext context = ActionContext.getContext();</code>
*
* <p>
* Finally, because of the thread local usage you don't need to worry about making your actions thread safe.
* </p>
*
* @author Patrick Lightbody
* @author Bill Lynch (docs)
*/
public class ActionContext implements Serializable {
private static final ThreadLocal<ActionContext> actionContext = new ThreadLocal<>();
/**
* Constant for the name of the action being executed.
*/
private static final String ACTION_NAME = "org.apache.struts2.ActionContext.name";
/**
* Constant for the {@link ValueStack OGNL value stack}.
*/
private static final String VALUE_STACK = ValueStack.VALUE_STACK;
/**
* Constant for the action's session.
*/
private static final String SESSION = "org.apache.struts2.ActionContext.session";
/**
* Constant for the action's application context.
*/
private static final String APPLICATION = "org.apache.struts2.ActionContext.application";
/**
* Constant for the action's parameters.
*/
private static final String PARAMETERS = "org.apache.struts2.ActionContext.parameters";
/**
* Constant for the action's locale.
*/
private static final String LOCALE = "org.apache.struts2.ActionContext.locale";
/**
* Constant for the action's {@link ActionInvocation invocation} context.
*/
private static final String ACTION_INVOCATION = "org.apache.struts2.ActionContext.actionInvocation";
/**
* Constant for the map of type conversion errors.
*/
private static final String CONVERSION_ERRORS = "org.apache.struts2.ActionContext.conversionErrors";
/**
* Constant for the container
*/
private static final String CONTAINER = "org.apache.struts2.ActionContext.container";
private final Map<String, Object> context;
/**
* Creates a new ActionContext initialized with another context.
*
* @param context a context map.
*/
protected ActionContext(Map<String, Object> context) {
this.context = context;
}
/**
* Creates a new ActionContext based on passed in Map
*
* @param context a map with context values
* @return new ActionContext
*/
public static ActionContext of(Map<String, Object> context) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null!");
}
return new ActionContext(context);
}
/**
* Creates a new ActionContext based on empty Map
*
* @return new ActionContext
*/
public static ActionContext of() {
return of(new HashMap<>());
}
/**
* Binds the provided context with the current thread
*
* @param actionContext context to bind to the thread
* @return context which was bound to the thread
*/
public static ActionContext bind(ActionContext actionContext) {
ActionContext.setContext(actionContext);
return ActionContext.getContext();
}
public static boolean containsValueStack(Map<String, Object> context) {
return context != null && context.containsKey(VALUE_STACK);
}
/**
* Binds this context with the current thread
*
* @return this context which was bound to the thread
*/
public ActionContext bind() {
ActionContext.setContext(this);
return ActionContext.getContext();
}
/**
* Wipes out current ActionContext, use wisely!
*/
public static void clear() {
actionContext.remove();
}
/**
* Sets the action context for the current thread.
*
* @param context the action context.
*/
private static void setContext(ActionContext context) {
actionContext.set(context);
}
/**
* Returns the ActionContext specific to the current thread.
*
* @return the ActionContext for the current thread, is never <tt>null</tt>.
*/
public static ActionContext getContext() {
return actionContext.get();
}
/**
* Sets the action invocation (the execution state).
*
* @param actionInvocation the action execution state.
*/
public ActionContext withActionInvocation(ActionInvocation actionInvocation) {
put(ACTION_INVOCATION, actionInvocation);
return this;
}
/**
* Gets the action invocation (the execution state).
*
* @return the action invocation (the execution state).
*/
public ActionInvocation getActionInvocation() {
return (ActionInvocation) get(ACTION_INVOCATION);
}
/**
* Sets the action's application context.
*
* @param application the action's application context.
*/
public ActionContext withApplication(Map<String, Object> application) {
put(APPLICATION, application);
return this;
}
/**
* Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
*
* @return a Map of ServletContext or generic application level Map
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getApplication() {
return (Map<String, Object>) get(APPLICATION);
}
/**
* Gets the context map.
*
* @return the context map.
*/
public Map<String, Object> getContextMap() {
return context;
}
/**
* Sets conversion errors which occurred when executing the action.
*
* @param conversionErrors a Map of errors which occurred when executing the action.
*/
public ActionContext withConversionErrors(Map<String, ConversionData> conversionErrors) {
put(CONVERSION_ERRORS, conversionErrors);
return this;
}
/**
* Gets the map of conversion errors which occurred when executing the action.
*
* @return the map of conversion errors which occurred when executing the action or an empty map if
* there were no errors.
*/
@SuppressWarnings("unchecked")
public Map<String, ConversionData> getConversionErrors() {
Map<String, ConversionData> errors = (Map<String, ConversionData>) get(CONVERSION_ERRORS);
if (errors == null) {
errors = withConversionErrors(new HashMap<>()).getConversionErrors();
}
return errors;
}
/**
* Sets the Locale for the current action.
*
* @param locale the Locale for the current action.
*/
public ActionContext withLocale(Locale locale) {
put(LOCALE, locale);
return this;
}
/**
* Gets the Locale of the current action. If no locale was ever specified the platform's
* {@link Locale#getDefault() default locale} is used.
*
* @return the Locale of the current action.
*/
public Locale getLocale() {
Locale locale = (Locale) get(LOCALE);
if (locale == null) {
locale = Locale.getDefault();
withLocale(locale);
}
return locale;
}
/**
* Sets the name of the current Action in the ActionContext.
*
* @param actionName the name of the current action.
*/
public ActionContext withActionName(String actionName) {
put(ACTION_NAME, actionName);
return this;
}
/**
* Gets the name of the current Action.
*
* @return the name of the current action.
*/
public String getActionName() {
return (String) get(ACTION_NAME);
}
/**
* Sets the action parameters.
*
* @param parameters the parameters for the current action.
*/
public ActionContext withParameters(HttpParameters parameters) {
put(PARAMETERS, parameters);
return this;
}
/**
* Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of
* parameters otherwise.
*
* @return a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a
* generic Map of parameters otherwise.
*/
public HttpParameters getParameters() {
return (HttpParameters) get(PARAMETERS);
}
/**
* Sets a map of action session values.
*
* @param session the session values.
*/
public ActionContext withSession(Map<String, Object> session) {
put(SESSION, session);
return this;
}
/**
* Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
*
* @return the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getSession() {
return (Map<String, Object>) get(SESSION);
}
/**
* Sets the OGNL value stack.
*
* @param valueStack the OGNL value stack.
*/
public ActionContext withValueStack(ValueStack valueStack) {
put(VALUE_STACK, valueStack);
return this;
}
/**
* Gets the OGNL value stack.
*
* @return the OGNL value stack.
*/
public ValueStack getValueStack() {
return (ValueStack) get(VALUE_STACK);
}
/**
* Gets the container for this request
*
* @param container The container
*/
public ActionContext withContainer(Container container) {
put(CONTAINER, container);
return this;
}
/**
* Sets the container for this request
*
* @return The container
*/
public Container getContainer() {
return (Container) get(CONTAINER);
}
public <T> T getInstance(Class<T> type) {
Container cont = getContainer();
if (cont != null) {
return cont.getInstance(type);
} else {
throw new StrutsException("Cannot find an initialized container for this request.");
}
}
/**
* Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
*
* @param key the key used to find the value.
* @return the value that was found using the key or <tt>null</tt> if the key was not found.
*/
public Object get(String key) {
return context.get(key);
}
/**
* Stores a value in the current ActionContext. The value can be looked up using the key.
*
* @param key the key of the value.
* @param value the value to be stored.
*/
public void put(String key, Object value) {
context.put(key, value);
}
/**
* Gets ServletContext associated with current action
*
* @return current ServletContext
*/
public ServletContext getServletContext() {
return (ServletContext) get(StrutsStatics.SERVLET_CONTEXT);
}
/**
* Assigns ServletContext to action context
*
* @param servletContext associated with current request
* @return ActionContext
*/
public ActionContext withServletContext(ServletContext servletContext) {
put(StrutsStatics.SERVLET_CONTEXT, servletContext);
return this;
}
/**
* Gets ServletRequest associated with current action
*
* @return current ServletRequest
*/
public HttpServletRequest getServletRequest() {
return (HttpServletRequest) get(StrutsStatics.HTTP_REQUEST);
}
/**
* Assigns ServletRequest to action context
*
* @param request associated with current request
* @return ActionContext
*/
public ActionContext withServletRequest(HttpServletRequest request) {
put(StrutsStatics.HTTP_REQUEST, request);
return this;
}
/**
* Gets ServletResponse associated with current action
*
* @return current ServletResponse
*/
public HttpServletResponse getServletResponse() {
return (HttpServletResponse) get(StrutsStatics.HTTP_RESPONSE);
}
/**
* Assigns ServletResponse to action context
*
* @param response associated with current request
* @return ActionContext
*/
public ActionContext withServletResponse(HttpServletResponse response) {
put(StrutsStatics.HTTP_RESPONSE, response);
return this;
}
/**
* Gets PageContext associated with current action
*
* @return current PageContext
*/
public PageContext getPageContext() {
return (PageContext) get(StrutsStatics.PAGE_CONTEXT);
}
/**
* Assigns PageContext to action context
*
* @param pageContext associated with current request
* @return ActionContext
*/
public ActionContext withPageContext(PageContext pageContext) {
put(StrutsStatics.PAGE_CONTEXT, pageContext);
return this;
}
/**
* Gets ActionMapping associated with current action
*
* @return current ActionMapping
*/
public ActionMapping getActionMapping() {
return (ActionMapping) get(StrutsStatics.ACTION_MAPPING);
}
/**
* Assigns ActionMapping to action context
*
* @param actionMapping associated with current request
* @return ActionContext
*/
public ActionContext withActionMapping(ActionMapping actionMapping) {
put(StrutsStatics.ACTION_MAPPING, actionMapping);
return this;
}
/**
* Assigns an extra context map to action context
*
* @param extraContext to add to the current action context
* @return ActionContext
*/
public ActionContext withExtraContext(Map<String, Object> extraContext) {
if (extraContext != null) {
context.putAll(extraContext);
}
return this;
}
/**
* Adds arbitrary key to action context
*
* @param key a string
* @param value an object
* @return ActionContext
*/
public ActionContext with(String key, Object value) {
put(key, value);
return this;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ActionContext)) {
return false;
}
ActionContext other = (ActionContext) obj;
return Objects.equals(getContextMap(), other.getContextMap());
}
}
@@ -0,0 +1,182 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2;
import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionEventListener;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.interceptor.PreResultListener;
/**
* An {@link ActionInvocation} represents the execution state of an {@link Action}. It holds the Interceptors and the Action instance.
* By repeated re-entrant execution of the <code>invoke()</code> method, initially by the {@link ActionProxy}, then by the Interceptors, the
* Interceptors are all executed, and then the {@link Action} and the {@link Result}.
*
* @author Jason Carreira
* @see ActionProxy
*/
public interface ActionInvocation {
/**
* Get the Action associated with this ActionInvocation.
*
* @return the Action
*/
Object getAction();
/**
* Gets whether this ActionInvocation has executed before.
* This will be set after the Action and the Result have executed.
*
* @return <tt>true</tt> if this ActionInvocation has executed before.
*/
boolean isExecuted();
/**
* Gets the ActionContext associated with this ActionInvocation. The ActionProxy is
* responsible for setting this ActionContext onto the ThreadLocal before invoking
* the ActionInvocation and resetting the old ActionContext afterwards.
*
* @return the ActionContext.
*/
ActionContext getInvocationContext();
/**
* Get the ActionProxy holding this ActionInvocation.
*
* @return the ActionProxy.
*/
ActionProxy getProxy();
/**
* If the ActionInvocation has been executed before and the Result is an instance of {@link ActionChainResult}, this method
* will walk down the chain of <code>ActionChainResult</code>s until it finds a non-chain result, which will be returned. If the
* ActionInvocation's result has not been executed before, the Result instance will be created and populated with
* the result params.
*
* @return the result.
* @throws Exception can be thrown.
*/
Result getResult() throws Exception;
/**
* Gets the result code returned from this ActionInvocation.
*
* @return the result code
*/
String getResultCode();
/**
* Sets the result code, possibly overriding the one returned by the
* action.
*
* <p>
* The "intended" purpose of this method is to allow PreResultListeners to
* override the result code returned by the Action.
* </p>
*
* <p>
* If this method is used before the Action executes, the Action's returned
* result code will override what was set. However the Action could (if
* specifically coded to do so) inspect the ActionInvocation to see that
* someone "upstream" (e.g. an Interceptor) had suggested a value as the
* result, and it could therefore return the same value itself.
* </p>
*
* <p>
* If this method is called between the Action execution and the Result
* execution, then the value set here will override the result code the
* action had returned. Creating an Interceptor that implements
* {@link PreResultListener} will give you this opportunity.
* </p>
*
* <p>
* If this method is called after the Result has been executed, it will
* have the effect of raising an IllegalStateException.
* </p>
*
* @param resultCode the result code.
* @throws IllegalStateException if called after the Result has been executed.
* @see #isExecuted()
*/
void setResultCode(String resultCode);
/**
* Gets the ValueStack associated with this ActionInvocation.
*
* @return the ValueStack
*/
ValueStack getStack();
/**
* Register a {@link PreResultListener} to be notified after the Action is executed and
* before the Result is executed.
*
* <p>
* The ActionInvocation implementation must guarantee that listeners will be called in
* the order in which they are registered.
* </p>
*
* <p>
* Listener registration and execution does not need to be thread-safe.
* </p>
*
* @param listener the listener to add.
*/
void addPreResultListener(PreResultListener listener);
/**
* Invokes the next step in processing this ActionInvocation.
*
* <p>
* If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit
* ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor
* to execute. If there are no more Interceptors to be applied, the Action is executed.
* If the {@link ActionProxy#getExecuteResult()} method returns <tt>true</tt>, the Result is also executed.
* </p>
*
* @throws Exception can be thrown.
* @return the return code.
*/
String invoke() throws Exception;
/**
* Invokes only the Action (not Interceptors or Results).
*
* <p>
* This is useful in rare situations where advanced usage with the interceptor/action/result workflow is
* being manipulated for certain functionality.
* </p>
*
* @return the return code.
* @throws Exception can be thrown.
*/
String invokeActionOnly() throws Exception;
/**
* Sets the action event listener to respond to key action events.
*
* @param listener the listener.
*/
void setActionEventListener(ActionEventListener listener);
void init(ActionProxy proxy) ;
}
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2;
import java.io.Serializable;
/**
* All results (except for <code>Action.NONE</code>) of an {@link Action} are mapped to a View implementation.
*
* <p>
* Examples of Views might be:
* </p>
*
* <ul>
* <li>SwingPanelView - pops up a new Swing panel</li>
* <li>ActionChainView - executes another action</li>
* <li>SerlvetRedirectView - redirects the HTTP response to a URL</li>
* <li>ServletDispatcherView - dispatches the HTTP response to a URL</li>
* </ul>
*
* @author plightbo
*/
public interface Result extends Serializable {
/**
* Represents a generic interface for all action execution results.
* Whether that be displaying a webpage, generating an email, sending a JMS message, etc.
*
* @param invocation the invocation context.
* @throws Exception can be thrown.
*/
void execute(ActionInvocation invocation) throws Exception;
}
@@ -20,6 +20,7 @@ package org.apache.struts2.factory;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.factory.ResultFactory;
import com.opensymphony.xwork2.inject.Inject;
@@ -54,16 +55,36 @@ public class StrutsResultFactory implements ResultFactory {
Result result = null;
if (resultClassName != null) {
result = (Result) objectFactory.buildBean(resultClassName, extraContext);
Object o = objectFactory.buildBean(resultClassName, extraContext);
Map<String, String> params = resultConfig.getParams();
if (params != null) {
setParameters(extraContext, result, params);
setParameters(extraContext, o, params);
}
if (o instanceof Result) {
result = (Result) o;
} else if (o instanceof org.apache.struts2.Result) {
result = Result.adapt((org.apache.struts2.Result) o);
}
if (result == null) {
throw new ConfigurationException("Class [" + resultClassName + "] does not implement Result", resultConfig);
}
}
return result;
}
protected void setParameters(Map<String, Object> extraContext, Result result, Map<String, String> params) {
setParametersHelper(extraContext, result, params);
}
protected void setParameters(Map<String, Object> extraContext, Object result, Map<String, String> params) {
if (result instanceof Result) {
setParameters(extraContext, (Result) result, params);
} else {
setParametersHelper(extraContext, result, params);
}
}
private void setParametersHelper(Map<String, Object> extraContext, Object result, Map<String, String> params) {
for (Map.Entry<String, String> paramEntry : params.entrySet()) {
try {
String name = paramEntry.getKey();
@@ -78,6 +99,18 @@ public class StrutsResultFactory implements ResultFactory {
}
protected void setParameter(Result result, String name, String value, Map<String, Object> extraContext) {
setParameterHelper(result, name, value, extraContext);
}
private void setParameter(Object result, String name, String value, Map<String, Object> extraContext) {
if (result instanceof Result) {
setParameter((Result) result, name, value, extraContext);
} else {
setParameterHelper(result, name, value, extraContext);
}
}
private void setParameterHelper(Object result, String name, String value, Map<String, Object> extraContext) {
if (result instanceof ParamNameAwareResult) {
if (((ParamNameAwareResult) result).acceptableParameterName(name, value)) {
reflectionProvider.setProperty(name, value, result, extraContext, true);
@@ -86,5 +119,4 @@ public class StrutsResultFactory implements ResultFactory {
reflectionProvider.setProperty(name, value, result, extraContext, true);
}
}
}
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.interceptor;
import org.apache.struts2.ActionInvocation;
/**
* A marking interface, when implemented allows to conditionally execute a given interceptor
* within the current action invocation.
*
* @since Struts 6.1.1
*/
public interface ConditionalInterceptor extends Interceptor {
/**
* Determines if a given interceptor should be executed in the current processing of action invocation.
*
* @param invocation current {@link ActionInvocation} to determine if the interceptor should be executed
* @return true if the given interceptor should be included in the current action invocation
* @since 6.1.1
*/
boolean shouldIntercept(ActionInvocation invocation);
}
@@ -0,0 +1,222 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.interceptor;
import org.apache.struts2.ActionInvocation;
import java.io.Serializable;
/**
* <!-- START SNIPPET: introduction -->
*
* <p>
* An interceptor is a stateless class that follows the interceptor pattern, as
* found in {@link jakarta.servlet.Filter} and in AOP languages.
* </p>
*
* <p>
* Interceptors are objects that dynamically intercept Action invocations.
* They provide the developer with the opportunity to define code that can be executed
* before and/or after the execution of an action. They also have the ability
* to prevent an action from executing. Interceptors provide developers a way to
* encapsulate common functionality in a re-usable form that can be applied to
* one or more Actions.
* </p>
*
* <p>
* Interceptors <b>must</b> be stateless and not assume that a new instance will be created for each request or Action.
* Interceptors may choose to either short-circuit the {@link ActionInvocation} execution and return a return code
* (such as {@link org.apache.struts2.Action#SUCCESS}), or it may choose to do some processing before
* and/or after delegating the rest of the procesing using {@link ActionInvocation#invoke()}.
* </p>
* <!-- END SNIPPET: introduction -->
*
* <!-- START SNIPPET: parameterOverriding -->
* <p>
* Interceptor's parameter could be overridden through the following ways :-
* </p>
* <b>Method 1:</b>
* <pre>
* &lt;action name=&quot;myAction&quot; class=&quot;myActionClass&quot;&gt;
* &lt;interceptor-ref name=&quot;exception&quot;/&gt;
* &lt;interceptor-ref name=&quot;alias&quot;/&gt;
* &lt;interceptor-ref name=&quot;params&quot;/&gt;
* &lt;interceptor-ref name=&quot;servletConfig&quot;/&gt;
* &lt;interceptor-ref name=&quot;prepare&quot;/&gt;
* &lt;interceptor-ref name=&quot;i18n&quot;/&gt;
* &lt;interceptor-ref name=&quot;chain&quot;/&gt;
* &lt;interceptor-ref name=&quot;modelDriven&quot;/&gt;
* &lt;interceptor-ref name=&quot;fileUpload&quot;/&gt;
* &lt;interceptor-ref name=&quot;staticParams&quot;/&gt;
* &lt;interceptor-ref name=&quot;params&quot;/&gt;
* &lt;interceptor-ref name=&quot;conversionError&quot;/&gt;
* &lt;interceptor-ref name=&quot;validation&quot;&gt;
* &lt;param name=&quot;excludeMethods&quot;&gt;myValidationExcudeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;interceptor-ref name=&quot;workflow&quot;&gt;
* &lt;param name=&quot;excludeMethods&quot;&gt;myWorkflowExcludeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <b>Method 2:</b>
* <pre>
* &lt;action name=&quot;myAction&quot; class=&quot;myActionClass&quot;&gt;
* &lt;interceptor-ref name=&quot;defaultStack&quot;&gt;
* &lt;param name=&quot;validation.excludeMethods&quot;&gt;myValidationExcludeMethod&lt;/param&gt;
* &lt;param name=&quot;workflow.excludeMethods&quot;&gt;myWorkflowExcludeMethod&lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <p>
* In the first method, the whole default stack is copied and the parameter then
* changed accordingly.
* </p>
*
* <p>
* In the second method, the 'interceptor-ref' refer to an existing
* interceptor-stack, namely defaultStack in this example, and override the validator
* and workflow interceptor excludeMethods typically in this case. Note that in the
* 'param' tag, the name attribute contains a dot (.) the word before the dot(.)
* specifies the interceptor name whose parameter is to be overridden and the word after
* the dot (.) specifies the parameter itself. Essetially it is as follows :-
* </p>
*
* <pre>
* &lt;interceptor-name&gt;.&lt;parameter-name&gt;
* </pre>
* <p>
* <b>Note</b> also that in this case the 'interceptor-ref' name attribute
* is used to indicate an interceptor stack which makes sense as if it is referring
* to the interceptor itself it would be just using Method 1 describe above.
* </p>
* <!-- END SNIPPET: parameterOverriding -->
*
* <p>
* <b>Nested Interceptor param overriding</b>
* </p>
*
* <!-- START SNIPPET: nestedParameterOverriding -->
* <p>
* Interceptor stack parameter overriding could be nested into as many level as possible, though it would
* be advisable not to nest it too deep as to avoid confusion, For example,
* </p>
* <pre>
* &lt;interceptor name=&quot;interceptor1&quot; class=&quot;foo.bar.Interceptor1&quot; /&gt;
* &lt;interceptor name=&quot;interceptor2&quot; class=&quot;foo.bar.Interceptor2&quot; /&gt;
* &lt;interceptor name=&quot;interceptor3&quot; class=&quot;foo.bar.Interceptor3&quot; /&gt;
* &lt;interceptor name=&quot;interceptor4&quot; class=&quot;foo.bar.Interceptor4&quot; /&gt;
* &lt;interceptor-stack name=&quot;stack1&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor1&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack2&quot;&gt;
* &lt;interceptor-ref name=&quot;intercetor2&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack1&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack3&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor3&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack2&quot; /&gt;
* &lt;/interceptor-stack&gt;
* &lt;interceptor-stack name=&quot;stack4&quot;&gt;
* &lt;interceptor-ref name=&quot;interceptor4&quot; /&gt;
* &lt;interceptor-ref name=&quot;stack3&quot; /&gt;
* &lt;/interceptor-stack&gt;
* </pre>
*
* <p>
* Assuming the interceptor has the following properties
* </p>
*
* <table border="1" summary="">
* <tr>
* <td>Interceptor</td>
* <td>property</td>
* </tr>
* <tr>
* <td>Interceptor1</td>
* <td>param1</td>
* </tr>
* <tr>
* <td>Interceptor2</td>
* <td>param2</td>
* </tr>
* <tr>
* <td>Interceptor3</td>
* <td>param3</td>
* </tr>
* <tr>
* <td>Interceptor4</td>
* <td>param4</td>
* </tr>
* </table>
*
* <p>
* We could override them as follows :
* </p>
*
* <pre>
* &lt;action ... &gt;
* &lt;!-- to override parameters of interceptor located directly in the stack --&gt;
* &lt;interceptor-ref name=&quot;stack4&quot;&gt;
* &lt;param name=&quot;interceptor4.param4&quot;&gt; ... &lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
*
* &lt;action ... &gt;
* &lt;!-- to override parameters of interceptor located under nested stack --&gt;
* &lt;interceptor-ref name=&quot;stack4&quot;&gt;
* &lt;param name=&quot;stack3.interceptor3.param3&quot;&gt; ... &lt;/param&gt;
* &lt;param name=&quot;stack3.stack2.interceptor2.param2&quot;&gt; ... &lt;/param&gt;
* &lt;param name=&quot;stack3.stack2.stack1.interceptor1.param1&quot;&gt; ... &lt;/param&gt;
* &lt;/interceptor-ref&gt;
* &lt;/action&gt;
* </pre>
*
* <!-- END SNIPPET: nestedParameterOverriding -->
*
* @author Jason Carreira
* @author tmjee
*/
public interface Interceptor extends Serializable {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link org.apache.struts2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
}
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.interceptor;
import org.apache.struts2.ActionInvocation;
/**
* PreResultListeners may be registered with an {@link ActionInvocation} to get a callback after the
* {@link org.apache.struts2.Action} has been executed but before the {@link org.apache.struts2.Result}
* is executed.
*
* @author Jason Carreira
*/
public interface PreResultListener {
/**
* This callback method will be called after the {@link org.apache.struts2.Action} execution and
* before the {@link org.apache.struts2.Result} execution.
*
* @param invocation the action invocation
* @param resultCode the result code returned by the action (eg. <code>success</code>).
*/
void beforeResult(ActionInvocation invocation, String resultCode);
}
@@ -61,7 +61,11 @@ public class ConfigurationProviderOgnlAllowlistTest extends XWorkJUnit4TestCase
Class.forName("com.opensymphony.xwork2.Action"),
Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"),
Class.forName("com.opensymphony.xwork2.Result"),
Class.forName("com.opensymphony.xwork2.SimpleAction")
Class.forName("com.opensymphony.xwork2.SimpleAction"),
Class.forName("org.apache.struts2.interceptor.Interceptor"),
Class.forName("org.apache.struts2.interceptor.ConditionalInterceptor"),
Class.forName("org.apache.struts2.Result"),
Class.forName("org.apache.struts2.Action")
);
}
@@ -85,7 +89,11 @@ public class ConfigurationProviderOgnlAllowlistTest extends XWorkJUnit4TestCase
Class.forName("com.opensymphony.xwork2.Action"),
Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"),
Class.forName("com.opensymphony.xwork2.Result"),
Class.forName("com.opensymphony.xwork2.SimpleAction")
Class.forName("com.opensymphony.xwork2.SimpleAction"),
Class.forName("org.apache.struts2.interceptor.Interceptor"),
Class.forName("org.apache.struts2.interceptor.ConditionalInterceptor"),
Class.forName("org.apache.struts2.Result"),
Class.forName("org.apache.struts2.Action")
);
}
@@ -108,7 +116,11 @@ public class ConfigurationProviderOgnlAllowlistTest extends XWorkJUnit4TestCase
Class.forName("com.opensymphony.xwork2.Validateable"),
Class.forName("com.opensymphony.xwork2.Action"),
Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"),
Class.forName("com.opensymphony.xwork2.Result")
Class.forName("com.opensymphony.xwork2.Result"),
Class.forName("org.apache.struts2.interceptor.Interceptor"),
Class.forName("org.apache.struts2.interceptor.ConditionalInterceptor"),
Class.forName("org.apache.struts2.Result"),
Class.forName("org.apache.struts2.Action")
);
}
}
@@ -403,6 +403,7 @@ public class ActionTagTest extends AbstractTagTest {
public void testExecuteButResetReturnSameInvocation() throws Exception {
Mock mockActionInv = new Mock(ActionInvocation.class);
mockActionInv.matchAndReturn("invoke", "TEST");
ActionTag tag = new ActionTag();
tag.setPageContext(pageContext);
tag.setNamespace("");
@@ -419,7 +420,7 @@ public class ActionTagTest extends AbstractTagTest {
ActionComponent component = (ActionComponent) tag.getComponent();
tag.doEndTag();
assertSame(oldInvocation, ActionContext.getContext().getActionInvocation());
assertEquals(oldInvocation.invoke(), ActionContext.getContext().getActionInvocation().invoke());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
ActionTag freshTag = new ActionTag();
@@ -432,6 +433,7 @@ public class ActionTagTest extends AbstractTagTest {
public void testExecuteButResetReturnSameInvocation_clearTagStateSet() throws Exception {
Mock mockActionInv = new Mock(ActionInvocation.class);
mockActionInv.matchAndReturn("invoke", "TEST");
ActionTag tag = new ActionTag();
tag.setPerformClearTagStateForTagPoolingServers(true); // Explicitly request tag state clearing.
tag.setPageContext(pageContext);
@@ -450,7 +452,7 @@ public class ActionTagTest extends AbstractTagTest {
ActionComponent component = (ActionComponent) tag.getComponent();
tag.doEndTag();
assertTrue(oldInvocation == ActionContext.getContext().getActionInvocation());
assertEquals(oldInvocation.invoke(), ActionContext.getContext().getActionInvocation().invoke());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
ActionTag freshTag = new ActionTag();