null el resolver

This commit is contained in:
Keith Donald
2008-04-05 20:17:24 +00:00
parent f805894dcc
commit 7afef9e878
8 changed files with 86 additions and 102 deletions

View File

@@ -0,0 +1,60 @@
package org.springframework.binding.expression.el;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
/**
* Resolves the special 'null' variable, indicating a null value.
*
* @author Keith Donald
*/
public class NullELResolver extends ELResolver {
private static final String NULL_VARIABLE_NAME = "null";
public Class getCommonPropertyType(ELContext context, Object base) {
return null;
}
public Iterator getFeatureDescriptors(ELContext context, Object base) {
return null;
}
public Class getType(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return null;
} else {
return null;
}
}
public Object getValue(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return null;
} else {
return null;
}
}
public boolean isReadOnly(ELContext context, Object base, Object property) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
return true;
} else {
return false;
}
}
public void setValue(ELContext context, Object base, Object property, Object value) {
if (base == null && NULL_VARIABLE_NAME.equals(property)) {
context.setPropertyResolved(true);
throw new PropertyNotWritableException("The 'null' value cannot be set");
}
}
}

View File

@@ -31,6 +31,11 @@ public class ELExpressionParserTests extends TestCase {
assertEquals(new Long(7), exp.getValue(null));
}
public void testParseNull() {
Expression exp = parser.parseExpression("null", null);
assertEquals(null, exp.getValue(null));
}
public void testParseNullExpressionString() {
String expressionString = null;
try {

View File

@@ -21,8 +21,6 @@ import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.webflow.engine.AnnotatedAction;
import org.springframework.webflow.execution.Action;
@@ -37,10 +35,12 @@ import org.springframework.webflow.execution.Action;
*/
public class ActionMethodELResolver extends ELResolver {
private static final Log logger = LogFactory.getLog(ActionMethodELResolver.class);
public Class getCommonPropertyType(ELContext elContext, Object base) {
return Action.class;
if (base instanceof Action) {
return String.class;
} else {
return null;
}
}
public Iterator getFeatureDescriptors(ELContext elContext, Object base) {

View File

@@ -65,7 +65,11 @@ public class ImplicitFlowVariableELResolver extends ELResolver {
}
public Class getCommonPropertyType(ELContext context, Object base) {
return Object.class;
if (base == null) {
return Object.class;
} else {
return null;
}
}
public Iterator getFeatureDescriptors(ELContext context, Object base) {

View File

@@ -52,7 +52,10 @@ public class RequestContextELResolver extends ELResolver {
}
public Class getCommonPropertyType(ELContext elContext, Object base) {
return Object.class;
if (base == null) {
return RequestContext.class;
}
return null;
}
public Iterator getFeatureDescriptors(ELContext elContext, Object base) {

View File

@@ -48,7 +48,11 @@ public class ScopeSearchingELResolver extends ELResolver {
}
public Class getCommonPropertyType(ELContext elContext, Object base) {
return Object.class;
if (base == null) {
return Object.class;
} else {
return null;
}
}
public Iterator getFeatureDescriptors(ELContext elContext, Object base) {

View File

@@ -1,94 +0,0 @@
/*
* Copyright 2004-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.webflow.expression.el;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
/**
* Custom EL resolver that resolves to the Spring Security Principal object for binding expressions prefixed with
* {@link #SECURITY_PRINCIPAL_VARIABLE_NAME}. For instance "#{currentUser}".
*
* @author Scott Andrews
*/
public class SpringSecurityELResolver extends ELResolver {
private static final Log logger = LogFactory.getLog(SpringSecurityELResolver.class);
/**
* Name of the security principal variable.
*/
public static final String SECURITY_PRINCIPAL_VARIABLE_NAME = "currentUser";
public Class getCommonPropertyType(ELContext elContext, Object base) {
return Object.class;
}
public Iterator getFeatureDescriptors(ELContext elContext, Object base) {
return null;
}
public Class getType(ELContext elContext, Object base, Object property) {
if (base == null && SECURITY_PRINCIPAL_VARIABLE_NAME.equals(property)) {
elContext.setPropertyResolved(true);
return Authentication.class;
} else {
return null;
}
}
public Object getValue(ELContext elContext, Object base, Object property) {
if (base == null && SECURITY_PRINCIPAL_VARIABLE_NAME.equals(property)) {
if (logger.isDebugEnabled()) {
logger.debug("Successfully resolved implicit flow variable '" + property + "'");
}
elContext.setPropertyResolved(true);
if (SecurityContextHolder.getContext() != null) {
return SecurityContextHolder.getContext().getAuthentication();
} else {
return null;
}
} else {
return null;
}
}
public boolean isReadOnly(ELContext elContext, Object base, Object property) {
if (base == null && SECURITY_PRINCIPAL_VARIABLE_NAME.equals(property)) {
elContext.setPropertyResolved(true);
return true;
} else {
return false;
}
}
public void setValue(ELContext elContext, Object base, Object property, Object value) {
if (base == null && SECURITY_PRINCIPAL_VARIABLE_NAME.equals(property)) {
elContext.setPropertyResolved(true);
throw new PropertyNotWritableException("The " + SECURITY_PRINCIPAL_VARIABLE_NAME
+ " cannot be set with an expression.");
}
}
}

View File

@@ -27,6 +27,7 @@ import javax.el.VariableMapper;
import org.springframework.binding.expression.el.DefaultELResolver;
import org.springframework.binding.expression.el.ELContextFactory;
import org.springframework.binding.expression.el.ELExpressionParser;
import org.springframework.binding.expression.el.NullELResolver;
import org.springframework.webflow.execution.RequestContext;
/**
@@ -54,6 +55,7 @@ public class WebFlowELExpressionParser extends ELExpressionParser {
RequestContext context = (RequestContext) target;
List customResolvers = new ArrayList();
customResolvers.add(new RequestContextELResolver(context));
customResolvers.add(new NullELResolver());
customResolvers.add(new ImplicitFlowVariableELResolver(context));
customResolvers.add(new ScopeSearchingELResolver(context));
customResolvers.add(new SpringBeanWebFlowELResolver(context));