Skip to content

Commit

Permalink
WW-5115 Reduces logging for ignored DMI related params when DMI is di…
Browse files Browse the repository at this point in the history
…sabled
  • Loading branch information
lukaszlenart committed Jan 23, 2022
1 parent e59abe4 commit 5659535
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Expand Up @@ -40,6 +40,7 @@
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

/**
* This interceptor sets all parameters on the value stack.
Expand All @@ -50,8 +51,11 @@ public class ParametersInterceptor extends MethodFilterInterceptor {

protected static final int PARAM_NAME_MAX_LENGTH = 100;

private static final Pattern DMI_IGNORED_PATTERN = Pattern.compile("^(action|method):.*", Pattern.CASE_INSENSITIVE);

private int paramNameMaxLength = PARAM_NAME_MAX_LENGTH;
private boolean devMode = false;
private boolean dmiEnabled = false;

protected boolean ordered = false;

Expand Down Expand Up @@ -79,6 +83,11 @@ public void setAcceptedPatterns(AcceptedPatternsChecker acceptedPatterns) {
this.acceptedPatterns = acceptedPatterns;
}

@Inject(value = StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION, required = false)
protected void setDynamicMethodInvocation(String dmiEnabled) {
this.dmiEnabled = Boolean.parseBoolean(dmiEnabled);
}

/**
* If the param name exceeds the configured maximum length it will not be
* accepted.
Expand All @@ -101,13 +110,10 @@ static private int countOGNLCharacters(String s) {
/**
* Compares based on number of '.' and '[' characters (fewer is higher)
*/
static final Comparator<String> rbCollator = new Comparator<String>() {
public int compare(String s1, String s2) {
int l1 = countOGNLCharacters(s1);
int l2 = countOGNLCharacters(s2);
return l1 < l2 ? -1 : (l2 < l1 ? 1 : s1.compareTo(s2));
}

static final Comparator<String> rbCollator = (s1, s2) -> {
int l1 = countOGNLCharacters(s1);
int l2 = countOGNLCharacters(s2);
return l1 < l2 ? -1 : (l2 < l1 ? 1 : s1.compareTo(s2));
};

@Override
Expand Down Expand Up @@ -286,13 +292,25 @@ protected String getParameterLogMap(HttpParameters parameters) {
}

protected boolean acceptableName(String name) {
if (isIgnoredDMI(name)) {
LOG.trace("DMI is enabled, ignoring DMI method: {}", name);
return false;
}
boolean accepted = isWithinLengthLimit(name) && !isExcluded(name) && isAccepted(name);
if (devMode && accepted) { // notify only when in devMode
LOG.debug("Parameter [{}] was accepted and will be appended to action!", name);
}
return accepted;
}

private boolean isIgnoredDMI(String name) {
if (dmiEnabled) {
return DMI_IGNORED_PATTERN.matcher(name).matches();
} else {
return false;
}
}

protected boolean isWithinLengthLimit(String name) {
boolean matchLength = name.length() <= paramNameMaxLength;
if (!matchLength) {
Expand Down
Expand Up @@ -730,6 +730,33 @@ public void testInternalParametersAreIgnored() throws Exception {
assertEquals(expected, actual);
}

public void testDMIMethodsAreIgnored() throws Exception {
// given
ParametersInterceptor interceptor = createParametersInterceptor();
final Map<String, Object> actual = injectValueStackFactory(interceptor);
ValueStack stack = injectValueStack(actual);

final Map<String, Object> expected = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
}
};

Map<String, Object> parameters = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
put("action:", "myAction");
put("method:", "doExecute");
}
};

// when
interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());

// then
assertEquals(expected, actual);
}

public void testBeanListSingleValue() throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("beanList.name", new String[]{"Superman"});
Expand Down

0 comments on commit 5659535

Please sign in to comment.