Skip to content

Commit

Permalink
Merge pull request #821 from apache/WW-5378-no-context-fallback
Browse files Browse the repository at this point in the history
WW-5378 Add option to NOT fallback to context lookup when finding value on OgnlValueStack
  • Loading branch information
kusalk authored Jan 2, 2024
2 parents afa40c1 + eb3928c commit 9976ed3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
44 changes: 16 additions & 28 deletions core/src/main/java/com/opensymphony/xwork2/ognl/OgnlValueStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS
private transient XWorkConverter converter;
private boolean devMode;
private boolean logMissingProperties;
private boolean shouldFallbackToContext = true;

/**
* @since 6.4.0
Expand Down Expand Up @@ -172,6 +173,11 @@ protected void setLogMissingProperties(String logMissingProperties) {
this.logMissingProperties = BooleanUtils.toBoolean(logMissingProperties);
}

@Inject(value = StrutsConstants.STRUTS_OGNL_VALUE_STACK_FALLBACK_TO_CONTEXT, required = false)
protected void setShouldFallbackToContext(String shouldFallbackToContext) {
this.shouldFallbackToContext = BooleanUtils.toBoolean(shouldFallbackToContext);
}

/**
* @see com.opensymphony.xwork2.util.ValueStack#getContext()
*/
Expand Down Expand Up @@ -337,34 +343,16 @@ protected Object handleOtherException(String expr, boolean throwExceptionOnFailu
}

private Object tryFindValue(String expr) throws OgnlException {
Object value;
expr = lookupForOverrides(expr);
if (defaultType != null) {
value = findValue(expr, defaultType);
} else {
value = getValueUsingOgnl(expr);
if (value == null) {
value = findInContext(expr);
}
}
return value;
return tryFindValue(expr, defaultType);
}

private String lookupForOverrides(String expr) {
if ((overrides != null) && overrides.containsKey(expr)) {
if (overrides != null && overrides.containsKey(expr)) {
expr = (String) overrides.get(expr);
}
return expr;
}

private Object getValueUsingOgnl(String expr) throws OgnlException {
try {
return ognlUtil.getValue(expr, context, root);
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
}

public Object findValue(String expr) {
return findValue(expr, false);
}
Expand Down Expand Up @@ -419,25 +407,25 @@ protected boolean shouldLogMissingPropertyWarning(OgnlException e) {
}

private Object tryFindValue(String expr, Class asType) throws OgnlException {
Object value = null;
try {
expr = lookupForOverrides(expr);
value = getValue(expr, asType);
Object value = ognlUtil.getValue(expr, context, root, asType);
if (value == null) {
value = findInContext(expr);
return converter.convertValue(getContext(), value, asType);
if (value != null && asType != null) {
value = converter.convertValue(getContext(), value, asType);
}
}
return value;
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
return value;
}

private Object getValue(String expr, Class asType) throws OgnlException {
return ognlUtil.getValue(expr, context, root, asType);
}

protected Object findInContext(String name) {
if (!shouldFallbackToContext) {
return null;
}
return getContext().get(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ protected void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}

@Override
public ValueStack createValueStack() {
ValueStack stack = new OgnlValueStack(
xworkConverter, compoundRootAccessor, textProvider, container.getInstance(SecurityMemberAccess.class));
container.inject(stack);
return stack.getActionContext().withContainer(container).withValueStack(stack).getValueStack();
return createValueStack(null, true);
}

@Override
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new OgnlValueStack(
stack, xworkConverter, compoundRootAccessor, container.getInstance(SecurityMemberAccess.class));
container.inject(result);
return result.getActionContext().withContainer(container).withValueStack(result).getValueStack();
return createValueStack(stack, false);
}

protected ValueStack createValueStack(ValueStack stack, boolean useTextProvider) {
ValueStack newStack = new OgnlValueStack(
stack, xworkConverter, compoundRootAccessor, useTextProvider ? textProvider : null, container.getInstance(SecurityMemberAccess.class));
container.inject(newStack);
return newStack.getActionContext().withContainer(container).withValueStack(newStack).getValueStack();
}

@Inject
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/apache/struts2/StrutsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ public final class StrutsConstants {
*/
public static final String STRUTS_OGNL_LOG_MISSING_PROPERTIES = "struts.ognl.logMissingProperties";

/**
* Determines whether lookups on the ValueStack should fallback to looking in the context if the OGNL expression
* fails or returns null.
*
* @since 6.4.0
*/
public static final String STRUTS_OGNL_VALUE_STACK_FALLBACK_TO_CONTEXT = "struts.ognl.valueStackFallbackToContext";

/**
* Logs properties that are not found (very verbose)
* @deprecated as of 6.0.0. Use {@link #STRUTS_OGNL_LOG_MISSING_PROPERTIES} instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,18 @@ public void testExpOverrides() {
assertEquals("Hello World", vs.findValue("claus", String.class));
assertEquals("Hello World", vs.findValue("top", String.class));

assertNull(vs.findValue("unknown", String.class));
}

public void testExprFallbackToContext() {
vs.getContext().put("santa", "Hello Santa");
assertEquals("Hello Santa", vs.findValue("santa", String.class));
assertNull(vs.findValue("unknown", String.class));
}

public void testExprFallbackToContext_disabled() {
vs.setShouldFallbackToContext("false");
vs.getContext().put("santa", "Hello Santa");
assertNull(vs.findValue("santa", String.class));
}

public void testWarnAboutInvalidProperties() {
Expand Down

0 comments on commit 9976ed3

Please sign in to comment.