001/* 002 * (C) Copyright 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 * François Maturel 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.keycloak; 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.lang.reflect.Constructor; 025import java.lang.reflect.InvocationTargetException; 026import java.lang.reflect.Method; 027 028import org.codehaus.jackson.map.ObjectMapper; 029import org.keycloak.adapters.KeycloakDeployment; 030import org.keycloak.adapters.KeycloakDeploymentBuilder; 031import org.keycloak.representations.adapters.config.AdapterConfig; 032import org.keycloak.util.SystemPropertiesJsonParserFactory; 033 034/** 035 * This class is developed to overcome a Jackson version problem between Nuxeo and Keycloak.<br> 036 * Nuxeo uses Jackson version 1.8.x where Keycloak uses 1.9.x<br> 037 * Sadly the {@link ObjectMapper#setSerializationInclusion} method is not in 1.8.x<br> 038 * Then {@link KeycloakNuxeoDeployment} is the same class as {@link KeycloakDeploymentBuilder}, rewriting static method 039 * {@link KeycloakDeploymentBuilder#loadAdapterConfig} to avoid the use of 040 * {@link ObjectMapper#setSerializationInclusion} 041 * 042 * @since 7.4 043 */ 044public class KeycloakNuxeoDeployment { 045 046 /** 047 * Invokes KeycloakDeploymentBuilder.internalBuild with reflection to avoid rewriting source code 048 * 049 * @param is the configuration file {@link InputStream} 050 * @return the {@link KeycloakDeployment} corresponding to the configuration file 051 */ 052 public static KeycloakDeployment build(InputStream is) { 053 AdapterConfig adapterConfig = loadAdapterConfig(is); 054 055 try { 056 Constructor<KeycloakDeploymentBuilder> constructor = KeycloakDeploymentBuilder.class.getDeclaredConstructor(); 057 constructor.setAccessible(true); 058 KeycloakDeploymentBuilder builder = constructor.newInstance(); 059 060 Method method = KeycloakDeploymentBuilder.class.getDeclaredMethod("internalBuild", AdapterConfig.class); 061 method.setAccessible(true); 062 return (KeycloakDeployment) method.invoke(builder, adapterConfig); 063 } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { 064 throw new RuntimeException(e); 065 } 066 } 067 068 public static AdapterConfig loadAdapterConfig(InputStream is) { 069 ObjectMapper mapper = new ObjectMapper(new SystemPropertiesJsonParserFactory()); 070 AdapterConfig adapterConfig; 071 try { 072 adapterConfig = mapper.readValue(is, AdapterConfig.class); 073 } catch (IOException e) { 074 throw new RuntimeException(e); 075 } 076 return adapterConfig; 077 } 078 079}