001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.actions; 018 019import javax.el.ELContext; 020import javax.el.ELException; 021import javax.el.ExpressionFactory; 022import javax.el.ValueExpression; 023import javax.el.VariableMapper; 024 025import org.apache.commons.lang.StringUtils; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoPrincipal; 028 029/** 030 * Default EL action context 031 * 032 * @since 5.7.3 033 */ 034public class ELActionContext extends AbstractActionContext implements ActionContext { 035 036 private static final long serialVersionUID = 1L; 037 038 protected final ELContext originalContext; 039 040 protected final ExpressionFactory expressionFactory; 041 042 public ELActionContext(ELContext originalContext, ExpressionFactory expressionFactory) { 043 super(); 044 this.originalContext = originalContext; 045 this.expressionFactory = expressionFactory; 046 } 047 048 @Override 049 public boolean checkCondition(String expression) throws ELException { 050 if (StringUtils.isBlank(expression) || (expression != null && StringUtils.isBlank(expression.trim()))) { 051 return false; 052 } 053 String expr = expression.trim(); 054 // compatibility code, as JEXL could resolve that kind of expression: 055 // detect if expression is in brackets #{}, otherwise add it 056 if (!expr.startsWith("#{") && !expr.startsWith("${") 057 // don't confuse error messages in case of simple mistakes in the 058 // expression 059 && !expr.endsWith("}")) { 060 expr = "#{" + expr + "}"; 061 } 062 VariableMapper vm = originalContext.getVariableMapper(); 063 // init default variables 064 ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(), 065 DocumentModel.class); 066 ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(), NuxeoPrincipal.class); 067 // add variables originally exposed by the action framework, 068 // do not add aliases currentDocument and currentUser here as they 069 // should already be available in this JSF context 070 vm.setVariable("document", documentExpr); 071 vm.setVariable("principal", userExpr); 072 vm.setVariable("currentDocument", documentExpr); 073 vm.setVariable("currentUser", userExpr); 074 // get custom context from ActionContext 075 for (String key : localVariables.keySet()) { 076 vm.setVariable(key, expressionFactory.createValueExpression(getLocalVariable(key), Object.class)); 077 } 078 079 // evaluate expression 080 ValueExpression ve = expressionFactory.createValueExpression(originalContext, expr, Boolean.class); 081 return Boolean.TRUE.equals(ve.getValue(originalContext)); 082 } 083 084}