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