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.jsf;
020
021import javax.el.ELContext;
022import javax.el.ELException;
023import javax.el.ExpressionFactory;
024import javax.el.ValueExpression;
025import javax.el.VariableMapper;
026import javax.faces.context.FacesContext;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.platform.actions.AbstractActionContext;
032import org.nuxeo.ecm.platform.actions.ActionContext;
033import org.nuxeo.ecm.platform.ui.web.util.SeamContextHelper;
034
035/**
036 * @since 5.7.3
037 */
038public class JSFActionContext 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 JSFActionContext(FacesContext faces) {
047        super();
048        this.originalContext = faces.getELContext();
049        this.expressionFactory = faces.getApplication().getExpressionFactory();
050    }
051
052    public JSFActionContext(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        ELContext finalContext = new JSFELContext(originalContext);
073        VariableMapper vm = finalContext.getVariableMapper();
074        // init default variables
075        ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(),
076                DocumentModel.class);
077        ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(), NuxeoPrincipal.class);
078        vm.setVariable("actionContextDocument", documentExpr);
079        // add variables originally exposed by the action framework,
080        // do not add aliases currentDocument and currentUser here as they
081        // should already be available in this JSF context
082        vm.setVariable("document", documentExpr);
083        vm.setVariable("principal", 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        // expose Seam context for compatibility, although available its
089        // components should be natively exposed in this JSF context
090        putLocalVariable("SeamContext", new SeamContextHelper());
091
092        // evaluate expression
093        ValueExpression ve = expressionFactory.createValueExpression(finalContext, expr, Boolean.class);
094        return Boolean.TRUE.equals(ve.getValue(finalContext));
095    }
096
097}