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 groovy.lang.Binding; 024import groovy.lang.GroovyClassLoader; 025import groovy.lang.Script; 026 027import java.io.IOException; 028import java.io.Serializable; 029import java.util.HashMap; 030import java.util.Map; 031 032import org.apache.commons.lang.StringUtils; 033import org.codehaus.groovy.runtime.InvokerHelper; 034import org.nuxeo.ecm.core.api.NuxeoPrincipal; 035 036/** 037 * Implement the {@link UserMapper} using Groovy Scripting for the mapping part 038 * 039 * @author tiry 040 * @since 7.4 041 */ 042public class GroovyUserMapper extends AbstractUserMapper { 043 044 protected final String mapperSource; 045 046 protected final String wrapperSource; 047 048 protected GroovyClassLoader loader; 049 050 protected Class<?> mapperClass; 051 052 protected Class<?> wrapperClass; 053 054 public GroovyUserMapper(String mapperScript, String wrapperScript) { 055 super(); 056 mapperSource = mapperScript; 057 wrapperSource = wrapperScript; 058 } 059 060 @Override 061 public void init(Map<String, String> params) { 062 loader = new GroovyClassLoader(this.getClass().getClassLoader()); 063 mapperClass = loader.parseClass(mapperSource); 064 if (!StringUtils.isEmpty(wrapperSource)) { 065 wrapperClass = loader.parseClass(wrapperSource); 066 } 067 } 068 069 @Override 070 protected void resolveAttributes(Object userObject, Map<String, Serializable> searchAttributes, 071 Map<String, Serializable> userAttributes, Map<String, Serializable> profileAttributes) { 072 Map<String, Object> context = new HashMap<>(); 073 context.put("searchAttributes", searchAttributes); 074 context.put("profileAttributes", profileAttributes); 075 context.put("userAttributes", userAttributes); 076 context.put("userObject", userObject); 077 Binding binding = new Binding(context); 078 Script script = InvokerHelper.createScript(mapperClass, binding); 079 script.run(); 080 } 081 082 @Override 083 public Object wrapNuxeoPrincipal(NuxeoPrincipal principal, Object userObject, Map<String, Serializable> params) { 084 Map<String, Object> context = new HashMap<>(); 085 context.put("nuxeoPrincipal", principal); 086 context.put("userObject", userObject); 087 context.put("params", params); 088 Binding binding = new Binding(context); 089 Script script = InvokerHelper.createScript(wrapperClass, binding); 090 script.run(); 091 return context.get("userObject"); 092 } 093 094 @Override 095 public void release() { 096 loader.clearCache(); 097 try { 098 loader.close(); 099 } catch (IOException e) { 100 log.error("Error during Groovy cleanup", e); 101 } 102 } 103 104}