mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Drops deprecation interceptor
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
package org.apache.struts2.interceptor;
|
||||
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.XWorkConstants;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
* In devMode checks if application uses deprecated or unknown constants and displays warning
|
||||
* when logging level is set to DEBUG
|
||||
* <!-- END SNIPPET: description -->
|
||||
*
|
||||
* <!-- START SNIPPET: parameters -->
|
||||
* no special parameters yet
|
||||
* <!-- END SNIPPET: parameters -->
|
||||
*/
|
||||
public class DeprecationInterceptor extends AbstractInterceptor {
|
||||
|
||||
private static final Logger LOG = LogManager.getLogger(DeprecationInterceptor.class);
|
||||
|
||||
private Container container;
|
||||
private boolean devMode;
|
||||
|
||||
@Override
|
||||
public String intercept(ActionInvocation invocation) throws Exception {
|
||||
if (devMode) {
|
||||
String message = validate();
|
||||
if (message != null) {
|
||||
LOG.debug(message);
|
||||
}
|
||||
}
|
||||
return invocation.invoke();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates constants. Validation goes on only if devMode is set.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private String validate() throws Exception {
|
||||
Set<String> constants = new HashSet<>();
|
||||
|
||||
readConstants(constants, StrutsConstants.class);
|
||||
readConstants(constants, XWorkConstants.class);
|
||||
|
||||
Set<String> applicationConstants = container.getInstanceNames(String.class);
|
||||
String message = null;
|
||||
if (!constants.containsAll(applicationConstants)) {
|
||||
Set<String> deprecated = new HashSet<>(applicationConstants);
|
||||
deprecated.removeAll(constants);
|
||||
message = prepareMessage(deprecated);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private void readConstants(Set<String> constants, Class clazz) throws IllegalAccessException {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (String.class.equals(field.getType())) {
|
||||
constants.add((String) field.get(clazz));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares message to display
|
||||
*
|
||||
* @param deprecated A set with deprecated/unknown constants
|
||||
*/
|
||||
private String prepareMessage(Set<String> deprecated) {
|
||||
StringBuilder sb = new StringBuilder("\n");
|
||||
sb.append("*******************************************************************************\n");
|
||||
sb.append("** **\n");
|
||||
sb.append("** WARNING **\n");
|
||||
sb.append("** YOU USE DEPRECATED / UNKNOWN CONSTANTS **\n");
|
||||
sb.append("** **\n");
|
||||
|
||||
for (String dep : deprecated) {
|
||||
sb.append(String.format("** -> %-69s **\n", dep));
|
||||
}
|
||||
|
||||
sb.append("*******************************************************************************\n");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Inject(StrutsConstants.STRUTS_DEVMODE)
|
||||
public void setDevMode(String state) {
|
||||
this.devMode = BooleanUtils.toBoolean(state);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -215,7 +215,6 @@
|
||||
<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
|
||||
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
|
||||
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
|
||||
<interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" />
|
||||
|
||||
<!-- Basic stack -->
|
||||
<interceptor-stack name="basicStack">
|
||||
@@ -228,7 +227,6 @@
|
||||
<interceptor-ref name="actionMappingParams"/>
|
||||
<interceptor-ref name="params"/>
|
||||
<interceptor-ref name="conversionError"/>
|
||||
<interceptor-ref name="deprecation"/>
|
||||
</interceptor-stack>
|
||||
|
||||
<!-- Sample validation and workflow stack -->
|
||||
@@ -334,7 +332,6 @@
|
||||
<param name="excludeMethods">input,back,cancel,browse</param>
|
||||
</interceptor-ref>
|
||||
<interceptor-ref name="debugging"/>
|
||||
<interceptor-ref name="deprecation"/>
|
||||
</interceptor-stack>
|
||||
|
||||
<!-- The completeStack is here for backwards compatibility for
|
||||
|
||||
Reference in New Issue
Block a user