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 javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025import org.apache.catalina.connector.Request; 026import org.apache.catalina.connector.RequestFacade; 027import org.apache.commons.lang3.reflect.FieldUtils; 028import org.keycloak.adapters.AdapterDeploymentContext; 029import org.keycloak.adapters.KeycloakDeployment; 030import org.keycloak.adapters.tomcat.CatalinaHttpFacade; 031 032/** 033 * 034 * @since 7.4 035 */ 036 037public class DeploymentResult { 038 private boolean isOk; 039 040 private static KeycloakDeployment keycloakDeployment; 041 042 private HttpServletRequest httpServletRequest; 043 private HttpServletResponse httpServletResponse; 044 private Request request; 045 private CatalinaHttpFacade facade; 046 047 public DeploymentResult(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { 048 this.httpServletRequest = httpServletRequest; 049 this.httpServletResponse = httpServletResponse; 050 } 051 052 boolean isOk() { 053 return isOk; 054 } 055 056 public static KeycloakDeployment getKeycloakDeployment() { 057 return keycloakDeployment; 058 } 059 060 public Request getRequest() { 061 return request; 062 } 063 064 public CatalinaHttpFacade getFacade() { 065 return facade; 066 } 067 068 public DeploymentResult invokeOn(AdapterDeploymentContext deploymentContext) { 069 070 // In Tomcat, a HttpServletRequest and a HttpServletResponse are wrapped in a Facades 071 request = unwrapRequest((RequestFacade) httpServletRequest); 072 facade = new CatalinaHttpFacade(httpServletResponse, request); 073 074 if (keycloakDeployment == null) { 075 keycloakDeployment = deploymentContext.resolveDeployment(facade); 076 } 077 if (keycloakDeployment.isConfigured()) { 078 isOk = true; 079 return this; 080 } 081 isOk = false; 082 return this; 083 } 084 085 /** 086 * Get the wrapper {@link Request} hidden in a {@link RequestFacade} object 087 * 088 * @param requestFacade, the main RequestFacade object 089 * @return the wrapper {@link Request} in {@link RequestFacade} 090 */ 091 private Request unwrapRequest(RequestFacade requestFacade) { 092 try { 093 return (Request) FieldUtils.readField(requestFacade, "request", true); 094 } catch (ReflectiveOperationException e) { 095 throw new RuntimeException(e); 096 } 097 } 098}