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