Skip to content

Commit

Permalink
WW-3714 Ensure ReflectionExceptionHandler, WithLazyParams, ParamNameA…
Browse files Browse the repository at this point in the history
…wareResult marker interfaces respected
  • Loading branch information
kusalk committed Oct 22, 2024
1 parent 36a890b commit b622e5d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ public String invoke() throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,28 @@ public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraCo

if (resultClassName != null) {
Object o = objectFactory.buildBean(resultClassName, extraContext);
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);
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ public void setOgnlUtil(OgnlUtil ognlUtil) {
}

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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraCo

if (resultClassName != null) {
Object o = objectFactory.buildBean(resultClassName, extraContext);
Map<String, String> params = resultConfig.getParams();
if (params != null) {
setParameters(extraContext, o, params);
}
if (o instanceof Result) {
result = (Result) o;
} else if (o instanceof org.apache.struts2.Result) {
Expand All @@ -63,15 +67,23 @@ public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraCo
if (result == null) {
throw new ConfigurationException("Class [" + resultClassName + "] does not implement Result", resultConfig);
}
Map<String, String> params = resultConfig.getParams();
if (params != null) {
setParameters(extraContext, result, params);
}
}
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();
Expand All @@ -86,6 +98,18 @@ protected void setParameters(Map<String, Object> extraContext, Result result, Ma
}

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);
Expand All @@ -94,5 +118,4 @@ protected void setParameter(Result result, String name, String value, Map<String
reflectionProvider.setProperty(name, value, result, extraContext, true);
}
}

}

0 comments on commit b622e5d

Please sign in to comment.