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.lang3.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 @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 ELContext finalContext = new JSFELContext(originalContext); 074 VariableMapper vm = finalContext.getVariableMapper(); 075 // init default variables 076 ValueExpression documentExpr = expressionFactory.createValueExpression(getCurrentDocument(), 077 DocumentModel.class); 078 ValueExpression userExpr = expressionFactory.createValueExpression(getCurrentPrincipal(), NuxeoPrincipal.class); 079 vm.setVariable("actionContextDocument", documentExpr); 080 // add variables originally exposed by the action framework, 081 // do not add aliases currentDocument and currentUser here as they 082 // should already be available in this JSF context 083 vm.setVariable("document", documentExpr); 084 vm.setVariable("principal", 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 // expose Seam context for compatibility, although available its 090 // components should be natively exposed in this JSF context 091 putLocalVariable("SeamContext", new SeamContextHelper()); 092 093 // evaluate expression 094 ValueExpression ve = expressionFactory.createValueExpression(finalContext, expr, Boolean.class); 095 return (T) ve.getValue(finalContext); 096 } 097 098 @Override 099 public boolean checkCondition(String expression) throws ELException { 100 return Boolean.TRUE.equals(evalExpression(expression, Boolean.class)); 101 } 102 103}