001/* 002 * (C) Copyright 2006-2015 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 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.usermapper.extension; 020 021import java.io.Serializable; 022import java.util.Map; 023 024import javax.script.Bindings; 025import javax.script.ScriptEngine; 026import javax.script.ScriptException; 027import javax.script.SimpleBindings; 028 029import org.apache.commons.lang.StringUtils; 030import org.nuxeo.automation.scripting.internals.ScriptingCache; 031import org.nuxeo.ecm.core.api.NuxeoPrincipal; 032 033/** 034 * {@link UserMapper} implementation using Nashorn to implement logic using JavaScript 035 * 036 * @author tiry 037 * @since 7.4 038 */ 039 040public class NashornUserMapper extends AbstractUserMapper { 041 042 protected ScriptEngine engine; 043 044 protected final String mapperSource; 045 046 protected final String wrapperSource; 047 048 public NashornUserMapper(String mapperScript, String wrapperScript) { 049 super(); 050 mapperSource = mapperScript; 051 wrapperSource = wrapperScript; 052 } 053 054 @Override 055 public Object wrapNuxeoPrincipal(NuxeoPrincipal principal, Object userObject, Map<String, Serializable> params) { 056 if (StringUtils.isEmpty(wrapperSource)) { 057 return null; 058 } 059 Bindings bindings = new SimpleBindings(); 060 bindings.put("nuxeoPrincipal", principal); 061 bindings.put("userObject", userObject); 062 bindings.put("params", params); 063 try { 064 engine.eval(wrapperSource, bindings); 065 } catch (ScriptException e) { 066 log.error("Error while executing JavaScript mapper", e); 067 } 068 return bindings.get("userObject"); 069 } 070 071 @Override 072 public void init(Map<String, String> params) throws Exception { 073 ScriptingCache scripting = new ScriptingCache(true); 074 engine = scripting.getScriptEngine(); 075 } 076 077 @Override 078 public void release() { 079 // NOP 080 } 081 082 @Override 083 protected void resolveAttributes(Object userObject, Map<String, Serializable> searchAttributes, 084 Map<String, Serializable> userAttributes, Map<String, Serializable> profileAttributes) { 085 Bindings bindings = new SimpleBindings(); 086 bindings.put("searchAttributes", searchAttributes); 087 bindings.put("profileAttributes", profileAttributes); 088 bindings.put("userAttributes", userAttributes); 089 bindings.put("userObject", userObject); 090 091 try { 092 engine.eval(mapperSource, bindings); 093 } catch (ScriptException e) { 094 log.error("Error while executing JavaScript mapper", e); 095 } 096 } 097 098}