Skip to content

Commit

Permalink
EmptyStringToNullELResolver fixed for Tomcat
Browse files Browse the repository at this point in the history
Signed-off-by: pizzi80 <[email protected]>
  • Loading branch information
pizzi80 committed Jun 22, 2023
1 parent 7dba707 commit e712a28
Showing 1 changed file with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,48 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// if ( String.class == targetType && context instanceof org.apache.el.lang.EvaluationContext ) // && context instanceof com.sun.faces.el.ELContextImpl
// System.out.println("value:["+value+"] - targetType:["+targetType+"]" + " {"+context+"}");

// NOTA: la soluzione proposta da BalusC ad oggi crea dei malfunzionamenti a JSF durante la valutazione di espressioni EL
// ad esempio se faccio #{'hello'.concat(null)} darebbe errore perché null verrebbe trattato appunto come null e String.concat(null)
// lancia un NullPointer, invece EL fa una conversione null -> '' e ti salva la vita

// Invece per ottenere il comportamento desiderato in fase di input
// sembra che EL-context in questione sia EvaluationContext
public class EmptyStringToNullELResolver extends ELResolver {

static final String EVALUATION_CONTEXT_CLASS_NAME = "EvaluationContext";

static final Map<Class<? extends ELContext>,Boolean> isEvaluationContext = new ConcurrentHashMap<>();

@Override
public <T> T convertToType(ELContext context, Object value, Class<T> targetType) {
static final Map<Class<? extends ELContext>,Boolean> isEvaluationContextCache = new ConcurrentHashMap<>();

// if ( String.class == targetType && context instanceof org.apache.el.lang.EvaluationContext ) // && context instanceof com.sun.faces.el.ELContextImpl
// System.out.println("value:["+value+"] - targetType:["+targetType+"]" + " {"+context+"}");
static boolean isEvaluationContext( ELContext context ) {
return isEvaluationContextCache.computeIfAbsent( context.getClass() , clazz -> clazz.getName().endsWith(EVALUATION_CONTEXT_CLASS_NAME) );
}

// NOTA: la soluzione proposta da BalusC ad oggi crea dei malfunzionamenti a JSF durante la valutazione di espressioni EL
// ad esempio se faccio #{'hello'.concat(null)} darebbe errore perché null verrebbe trattato appunto come null e String.concat(null)
// lancia un NullPointer, invece EL fa una conversione null -> '' e ti salva la vita
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
}

// Invece per ottenere il comportamento desiderato in fase di input
// sembra che EL-context in questione sia EvaluationContext
@Override
@SuppressWarnings("unchecked")
public <T> T convertToType(ELContext context, Object value, Class<T> targetType) {

if ( value == null &&
String.class.equals(targetType) &&
isEvaluationContext.computeIfAbsent( context.getClass() , clazz -> clazz.getName().endsWith(EVALUATION_CONTEXT_CLASS_NAME) )
) {
targetType == String.class &&
isEvaluationContext(context) ) {

context.setPropertyResolved(true);
return (T) null;
//return (T) null;
}
//try {
return (T) value;
//} catch (ClassCastException e) {
// return null;
//}
}

@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
return (T) value;
}

@Override
public Class<?> getType(ELContext context, Object base, Object property) {
return String.class;
return null;
}

@Override
Expand Down

0 comments on commit e712a28

Please sign in to comment.