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