ELEvaluator.java 1.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.security.profile.Profile
 *  javax.servlet.jsp.PageContext
 *  javax.servlet.jsp.el.ELException
 *  javax.servlet.jsp.el.ExpressionEvaluator
 *  javax.servlet.jsp.el.FunctionMapper
 *  javax.servlet.jsp.el.VariableResolver
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation;

import com.day.cq.security.profile.Profile;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.FunctionMapper;
import javax.servlet.jsp.el.VariableResolver;
import org.apache.sling.api.SlingHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ELEvaluator {
    private static final Logger log = LoggerFactory.getLogger(ELEvaluator.class);

    public static String evaluate(String expr, SlingHttpServletRequest request, final PageContext pageContext) {
        final Profile profile = (Profile)request.adaptTo(Profile.class);
        ExpressionEvaluator exprEval = pageContext.getExpressionEvaluator();
        try {
            return (String)exprEval.evaluate(expr, String.class, new VariableResolver(){
                private final VariableResolver base;

                public Object resolveVariable(String name) throws ELException {
                    Object value = this.base.resolveVariable(name);
                    if (value == null && name.equals("profile")) {
                        value = profile;
                    }
                    return value;
                }
            }, null);
        }
        catch (ELException e) {
            log.warn("Error while evaluating expression: {}", (Object)expr, (Object)e);
            return expr;
        }
    }

}