WW-5610 Extend Struts 7 forwards compat to more interceptors

This commit is contained in:
Kusal Kithul-Godage
2026-02-02 18:27:01 +11:00
parent a5b736b488
commit 87d8feaa8f
4 changed files with 51 additions and 20 deletions
@@ -98,8 +98,8 @@ public abstract class MethodFilterInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if (applyInterceptor(invocation)) {
return doIntercept(invocation);
if (applyInterceptor((org.apache.struts2.ActionInvocation) invocation)) {
return doIntercept((org.apache.struts2.ActionInvocation) invocation);
}
return invocation.invoke();
}
@@ -114,6 +114,10 @@ public abstract class MethodFilterInterceptor extends AbstractInterceptor {
return applyMethod;
}
protected boolean applyInterceptor(org.apache.struts2.ActionInvocation invocation) {
return applyInterceptor(ActionInvocation.adapt(invocation));
}
/**
* Subclasses must override to implement the interceptor logic.
*
@@ -123,4 +127,7 @@ public abstract class MethodFilterInterceptor extends AbstractInterceptor {
*/
protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
protected String doIntercept(org.apache.struts2.ActionInvocation invocation) throws Exception {
return doIntercept(ActionInvocation.adapt(invocation));
}
}
@@ -257,9 +257,13 @@ public class ValidationInterceptor extends MethodFilterInterceptor {
}
}
protected void doBeforeInvocation(org.apache.struts2.ActionInvocation invocation) throws Exception {
doBeforeInvocation(ActionInvocation.adapt(invocation));
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
doBeforeInvocation(invocation);
doBeforeInvocation((org.apache.struts2.ActionInvocation) invocation);
return invocation.invoke();
}
@@ -135,7 +135,7 @@ public class TokenInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
LOG.debug("Intercepting invocation to check for valid transaction token.");
return handleToken(invocation);
return handleToken((org.apache.struts2.ActionInvocation) invocation);
}
protected String handleToken(ActionInvocation invocation) throws Exception {
@@ -144,22 +144,19 @@ public class TokenInterceptor extends MethodFilterInterceptor {
HttpSession session = ServletActionContext.getRequest().getSession(true);
synchronized (session.getId().intern()) {
if (!TokenHelper.validToken()) {
return handleInvalidToken(invocation);
return handleInvalidToken((org.apache.struts2.ActionInvocation) invocation);
}
}
return handleValidToken(invocation);
return handleValidToken((org.apache.struts2.ActionInvocation) invocation);
}
protected String handleToken(org.apache.struts2.ActionInvocation invocation) throws Exception {
return handleToken(ActionInvocation.adapt(invocation));
}
/**
* Determines what to do if an invalid token is provided. If the action implements {@link ValidationAware}
*
* @param invocation the action invocation where the invalid token failed
* @return the return code to indicate should be processed
* @throws Exception when any unexpected error occurs.
*/
protected String handleInvalidToken(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
String errorMessage = getErrorMessage(invocation);
String errorMessage = getErrorMessage((org.apache.struts2.ActionInvocation) invocation);
if (action instanceof ValidationAware) {
((ValidationAware) action).addActionError(errorMessage);
@@ -170,6 +167,17 @@ public class TokenInterceptor extends MethodFilterInterceptor {
return INVALID_TOKEN_CODE;
}
/**
* Determines what to do if an invalid token is provided. If the action implements {@link ValidationAware}
*
* @param invocation the action invocation where the invalid token failed
* @return the return code to indicate should be processed
* @throws Exception when any unexpected error occurs.
*/
protected String handleInvalidToken(org.apache.struts2.ActionInvocation invocation) throws Exception {
return handleInvalidToken(ActionInvocation.adapt(invocation));
}
protected String getErrorMessage(ActionInvocation invocation) {
Object action = invocation.getAction();
if (action instanceof TextProvider) {
@@ -178,6 +186,14 @@ public class TokenInterceptor extends MethodFilterInterceptor {
return textProvider.getText(INVALID_TOKEN_MESSAGE_KEY, DEFAULT_ERROR_MESSAGE);
}
protected String getErrorMessage(org.apache.struts2.ActionInvocation invocation) {
return getErrorMessage(ActionInvocation.adapt(invocation));
}
protected String handleValidToken(ActionInvocation invocation) throws Exception {
return invocation.invoke();
}
/**
* Called when a valid token is found. This method invokes the action by can be changed to do something more
* interesting.
@@ -186,8 +202,8 @@ public class TokenInterceptor extends MethodFilterInterceptor {
* @return invocation result
* @throws Exception when any unexpected error occurs.
*/
protected String handleValidToken(ActionInvocation invocation) throws Exception {
return invocation.invoke();
protected String handleValidToken(org.apache.struts2.ActionInvocation invocation) throws Exception {
return handleValidToken(ActionInvocation.adapt(invocation));
}
}
@@ -55,7 +55,7 @@ public class InvocationSessionStore {
return null;
}
final ActionInvocation savedInvocation = invocationContext.invocation;
final ActionInvocation savedInvocation = ActionInvocation.adapt(invocationContext.invocation);
if (savedInvocation != null) {
// WW-5026 - Preserve the previous PageContext (even if null) and restore it to the
// ActionContext after loading the savedInvocation context. The saved context's PageContext
@@ -72,6 +72,10 @@ public class InvocationSessionStore {
return savedInvocation;
}
public static void storeInvocation(String key, String token, ActionInvocation invocation) {
storeInvocation(key, token, (org.apache.struts2.ActionInvocation) invocation);
}
/**
* Stores the DefaultActionInvocation and ActionContext into the Session using the provided key for loading later using
* {@link #loadInvocation}
@@ -80,7 +84,7 @@ public class InvocationSessionStore {
* @param token token for check
* @param invocation the action invocation
*/
public static void storeInvocation(String key, String token, ActionInvocation invocation) {
public static void storeInvocation(String key, String token, org.apache.struts2.ActionInvocation invocation) {
InvocationContext invocationContext = new InvocationContext(invocation, token);
Map<String, Object> invocationMap = getInvocationMap();
invocationMap.put(key, invocationContext);
@@ -120,11 +124,11 @@ public class InvocationSessionStore {
private static final long serialVersionUID = -286697666275777888L;
//WW-4873 transient since 2.5.15
transient ActionInvocation invocation;
transient org.apache.struts2.ActionInvocation invocation;
String token;
public InvocationContext(ActionInvocation invocation, String token) {
public InvocationContext(org.apache.struts2.ActionInvocation invocation, String token) {
this.invocation = invocation;
this.token = token;
}