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