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