001// Generated by delombok at Fri Mar 22 16:21:46 GMT 2024
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.causeway.testing.h2console.ui.webmodule;
021
022import java.util.function.Consumer;
023import org.h2.server.web.ConnectionInfo;
024import org.h2.server.web.WebServer;
025import org.h2.server.web.WebServlet;
026import org.apache.causeway.commons.internal._Constants;
027import org.apache.causeway.commons.internal.assertions._Assert;
028import org.apache.causeway.commons.internal.exceptions._Exceptions;
029import org.apache.causeway.commons.internal.reflection._Reflect;
030import lombok.NonNull;
031
032/**
033 * Provides programmatic access to otherwise protected H2 {@link WebServer} configuration.
034 */
035public interface H2WebServerWrapper {
036    /**
037     * Update the connection information setting.
038     */
039    void setConnectionInfo(ConnectionInfo connectionInfo);
040
041    /**
042     * Whether to allow other computers to connect.
043     */
044    void setAllowOthers(boolean b);
045
046    /**
047     * Whether other computers are allowed to connect.
048     */
049    boolean getAllowOthers();
050
051    /**
052     * Web Admin Password.
053     */
054    void setAdminPassword(String password);
055
056    // -- UTILITY
057    static void withH2WebServerWrapperDo(@NonNull final WebServlet webServlet, @NonNull final Consumer<H2WebServerWrapper> onConfiguration) {
058        try {
059            if (webServlet == null) {
060                throw new java.lang.NullPointerException("webServlet is marked non-null but is null");
061            }
062            if (onConfiguration == null) {
063                throw new java.lang.NullPointerException("onConfiguration is marked non-null but is null");
064            }
065            try {
066                final org.apache.causeway.testing.h2console.ui.webmodule.H2WebServerWrapper serverWrapper = H2WebServerWrapper.wrap(webServlet);
067                onConfiguration.accept(serverWrapper);
068            } catch (Throwable cause) {
069                // if for any reason wrapping fails, we fail hard to harden against potential security issues
070                throw _Exceptions.unrecoverable(cause, "Unable to customize settings for H2 console");
071            }
072        } catch (final java.lang.Throwable $ex) {
073            throw lombok.Lombok.sneakyThrow($ex);
074        }
075    }
076
077    // -- HELPER
078    private static H2WebServerWrapper wrap(@NonNull final WebServlet webServlet) {
079        try {
080            if (webServlet == null) {
081                throw new java.lang.NullPointerException("webServlet is marked non-null but is null");
082            }
083            return new H2WebServerWrapper() {
084                final WebServer webServer = (WebServer) _Reflect.getFieldOn(WebServlet.class.getDeclaredField("server"), webServlet);
085                @Override
086                public void setConnectionInfo(final ConnectionInfo connectionInfo) {
087                    try {
088                        final java.lang.reflect.Method updateSettingMethod = WebServer.class.getDeclaredMethod("updateSetting", ConnectionInfo.class);
089                        _Reflect.invokeMethodOn(updateSettingMethod, webServer, connectionInfo);
090                    } catch (final java.lang.Throwable $ex) {
091                        throw lombok.Lombok.sneakyThrow($ex);
092                    }
093                }
094                @Override
095                public void setAllowOthers(boolean b) {
096                    try {
097                        final java.lang.reflect.Method method = WebServer.class.getDeclaredMethod("setAllowOthers", boolean.class);
098                        _Reflect.invokeMethodOn(method, webServer, b);
099                        // just so we verify reflection works
100                        _Assert.assertEquals(b, getAllowOthers());
101                    } catch (final java.lang.Throwable $ex) {
102                        throw lombok.Lombok.sneakyThrow($ex);
103                    }
104                }
105                @Override
106                public boolean getAllowOthers() {
107                    try {
108                        final java.lang.reflect.Method method = WebServer.class.getDeclaredMethod("getAllowOthers", _Constants.emptyClasses);
109                        return (boolean) _Reflect.invokeMethodOn(method, webServer, _Constants.emptyObjects).getValue().get();
110                    } catch (final java.lang.Throwable $ex) {
111                        throw lombok.Lombok.sneakyThrow($ex);
112                    }
113                }
114                @Override
115                public void setAdminPassword(String password) {
116                    try {
117                        final java.lang.reflect.Method method = WebServer.class.getDeclaredMethod("setAdminPassword", String.class);
118                        _Reflect.invokeMethodOn(method, webServer, password);
119                    } catch (final java.lang.Throwable $ex) {
120                        throw lombok.Lombok.sneakyThrow($ex);
121                    }
122                }
123            };
124        } catch (final java.lang.Throwable $ex) {
125            throw lombok.Lombok.sneakyThrow($ex);
126        }
127    }
128}