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