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; 025 026import com.fasterxml.jackson.databind.ObjectMapper; 027 028import org.apache.commons.lang3.reflect.MethodUtils; 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 return (KeycloakDeployment) MethodUtils.invokeMethod(builder, true, "internalBuild", adapterConfig); 060 } catch (ReflectiveOperationException e) { 061 throw new RuntimeException(e); 062 } 063 } 064 065 public static AdapterConfig loadAdapterConfig(InputStream is) { 066 ObjectMapper mapper = new ObjectMapper(new SystemPropertiesJsonParserFactory()); 067 AdapterConfig adapterConfig; 068 try { 069 adapterConfig = mapper.readValue(is, AdapterConfig.class); 070 } catch (IOException e) { 071 throw new RuntimeException(e); 072 } 073 return adapterConfig; 074 } 075 076}