001/*
002 * (C) Copyright 2013 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.actions;
020
021import javax.el.ELContext;
022import javax.el.ELException;
023import javax.el.ExpressionFactory;
024import javax.el.ValueExpression;
025import javax.el.VariableMapper;
026
027import org.apache.commons.lang.StringUtils;
028import org.jboss.el.ExpressionFactoryImpl;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.platform.el.ExpressionContext;
032
033/**
034 * Default EL action context
035 *
036 * @since 5.7.3
037 */
038public class ELActionContext extends AbstractActionContext implements ActionContext {
039
040    private static final long serialVersionUID = 1L;
041
042    protected final ELContext originalContext;
043
044    protected final ExpressionFactory expressionFactory;
045
046    public static final ExpressionFactory EXPRESSION_FACTORY = new ExpressionFactoryImpl();
047
048    public ELActionContext() {
049        this(new ExpressionContext(), EXPRESSION_FACTORY);
050    }
051
052    public ELActionContext(ELContext originalContext, ExpressionFactory expressionFactory) {
053        super();
054        this.originalContext = originalContext;
055        this.expressionFactory = expressionFactory;
056    }
057
058    @Override
059    public boolean checkCondition(String expression) throws ELException {
060        if (StringUtils.isBlank(expression) || (expression != null && StringUtils.isBlank(expression.trim()))) {
061            return false;
062        }
063        String expr = expression.trim();
064        // compatibility code, as JEXL could resolve that kind of expression:
065        // detect if expression is in brackets #{}, otherwise add it
066        if (!expr.startsWith("#{") && !expr.startsWith("${")
067        // don't confuse error messages in case of simple mistakes in the
068        // expression
069                && !expr.endsWith("}")) {
070            expr = "#{" + expr + "}";
071        }
072        VariableMapper vm = originalContext.getVariableMapper();
073        // init default variables
074        ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(),
075                DocumentModel.class);
076        ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(), NuxeoPrincipal.class);
077        // add variables originally exposed by the action framework,
078        // do not add aliases currentDocument and currentUser here as they
079        // should already be available in this JSF context
080        vm.setVariable("document", documentExpr);
081        vm.setVariable("principal", userExpr);
082        vm.setVariable("currentDocument", documentExpr);
083        vm.setVariable("currentUser", userExpr);
084        // get custom context from ActionContext
085        for (String key : localVariables.keySet()) {
086            vm.setVariable(key, expressionFactory.createValueExpression(getLocalVariable(key), Object.class));
087        }
088
089        // evaluate expression
090        ValueExpression ve = expressionFactory.createValueExpression(originalContext, expr, Boolean.class);
091        return Boolean.TRUE.equals(ve.getValue(originalContext));
092    }
093
094}