001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.binding.alias;
020
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import javax.el.ELException;
026import javax.el.ValueExpression;
027import javax.el.VariableMapper;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.el.ValueExpressionLiteral;
032
033/**
034 * @since 5.6
035 */
036@SuppressWarnings({ "unchecked", "rawtypes" })
037public class AliasVariableMapperWrapper extends VariableMapper {
038
039    private static final Log log = LogFactory.getLog(AliasVariableMapperWrapper.class);
040
041    protected final VariableMapper orig;
042
043    protected final List<String> blockedPatterns;
044
045    protected Map vars;
046
047    public AliasVariableMapperWrapper(VariableMapper orig, List<String> blockedPatterns) {
048        super();
049        this.orig = orig;
050        this.blockedPatterns = blockedPatterns;
051    }
052
053    /**
054     * First tries to resolve against the inner Map, then the wrapped ValueExpression, unless target is an
055     * {@link AliasVariableMapper} that blocks this variable pattern.
056     */
057    @Override
058    public ValueExpression resolveVariable(String variable) {
059        ValueExpression ve = null;
060        try {
061            if (vars != null) {
062                ve = (ValueExpression) vars.get(variable);
063            }
064            if (ve == null) {
065                // resolve to a value expression resolving to null if variable
066                // is supposed to be blocked
067                if (variable != null && blockedPatterns != null) {
068                    for (String blockedPattern : blockedPatterns) {
069                        if (blockedPattern == null) {
070                            continue;
071                        }
072                        boolean doBlock = false;
073                        if (blockedPattern.endsWith("*")) {
074                            String pattern = blockedPattern.substring(0, blockedPattern.length() - 1);
075                            if (variable.startsWith(pattern)) {
076                                doBlock = true;
077                            }
078                        } else if (blockedPattern.equals(variable)) {
079                            doBlock = true;
080                        }
081                        if (doBlock) {
082                            if (log.isDebugEnabled()) {
083                                log.debug("Blocked expression var='" + variable + "'");
084                            }
085                            return getNullValueExpression();
086                        }
087                    }
088                }
089                return orig.resolveVariable(variable);
090            }
091            return ve;
092        } catch (StackOverflowError e) {
093            throw new ELException("Could not Resolve Variable [Overflow]: " + variable, e);
094        }
095    }
096
097    protected ValueExpression getNullValueExpression() {
098        return new ValueExpressionLiteral(null, Object.class);
099    }
100
101    @Override
102    public ValueExpression setVariable(String variable, ValueExpression expression) {
103        if (vars == null) {
104            vars = new HashMap();
105        }
106        return (ValueExpression) vars.put(variable, expression);
107    }
108
109}