001/* 002 * (C) Copyright 2013-2018 Nuxeo (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.lang3.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 @SuppressWarnings("unchecked") 059 @Override 060 public <T> T evalExpression(String expression, Class<T> expectedType) throws ELException { 061 if (StringUtils.isBlank(expression)) { 062 return null; 063 } 064 String expr = expression.trim(); 065 // compatibility code, as JEXL could resolve that kind of expression: 066 // detect if expression is in brackets #{}, otherwise add it 067 if (!expr.startsWith("#{") && !expr.startsWith("${") 068 // don't confuse error messages in case of simple mistakes in the 069 // expression 070 && !expr.endsWith("}")) { 071 expr = "#{" + expr + "}"; 072 } 073 VariableMapper vm = originalContext.getVariableMapper(); 074 // init default variables 075 ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(), 076 DocumentModel.class); 077 ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(), NuxeoPrincipal.class); 078 // add variables originally exposed by the action framework, 079 // do not add aliases currentDocument and currentUser here as they 080 // should already be available in this JSF context 081 vm.setVariable("document", documentExpr); 082 vm.setVariable("principal", userExpr); 083 vm.setVariable("currentDocument", documentExpr); 084 vm.setVariable("currentUser", userExpr); 085 // get custom context from ActionContext 086 for (String key : localVariables.keySet()) { 087 vm.setVariable(key, expressionFactory.createValueExpression(getLocalVariable(key), Object.class)); 088 } 089 090 // evaluate expression 091 ValueExpression ve = expressionFactory.createValueExpression(originalContext, expr, expectedType); 092 return (T) ve.getValue(originalContext); 093 } 094 095 @Override 096 public boolean checkCondition(String expression) throws ELException { 097 return Boolean.TRUE.equals(evalExpression(expression, Boolean.class)); 098 } 099 100}