This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 92bdcb99b8d camel-test-infra-jetty: cleanup the context handler 
configuration api
92bdcb99b8d is described below

commit 92bdcb99b8d0fbd0d8adcb5c197f4bb0984ea7e2
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Fri Sep 9 17:39:50 2022 +0200

    camel-test-infra-jetty: cleanup the context handler configuration api
---
 .../component/ahc/ws/WsProducerConsumerTest.java   |   7 +-
 .../camel/component/ahc/ws/WsProducerTest.java     |   7 +-
 .../ahc/ws/WsProducerUsingStreamingTest.java       |   7 +-
 .../camel/component/ahc/ws/WssProducerTest.java    |   7 +-
 .../websocket/WebsocketCamelRouterTestSupport.java |   7 +-
 ...bsocketCamelRouterWithInitParamTestSupport.java |  10 +-
 .../AtomEntryPollingConsumerWithBasicAuthTest.java |   9 +-
 .../atom/AtomPollingConsumerWithBasicAuthTest.java |   9 +-
 test-infra/camel-test-infra-jetty/pom.xml          |   6 +
 .../infra/jetty/services/JettyConfiguration.java   | 279 +++++++++++++++------
 .../jetty/services/JettyConfigurationBuilder.java  |  64 ++++-
 .../infra/jetty/services/JettyEmbeddedService.java |  55 +---
 12 files changed, 316 insertions(+), 151 deletions(-)

diff --git 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerConsumerTest.java
 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerConsumerTest.java
index b0be9ccf8bb..709574ca1ee 100644
--- 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerConsumerTest.java
+++ 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerConsumerTest.java
@@ -48,8 +48,11 @@ public class WsProducerConsumerTest extends CamelTestSupport 
{
             .emptyTemplate()
             .withPort(AvailablePortFinder.getNextAvailable())
             .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-            .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration(
-                    TestServletFactory.class.getName(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .withServletConfiguration()
+            .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                    TestServletFactory.class.getName(),
+                    
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .build()
             .build();
 
     public JettyEmbeddedService service = new 
JettyEmbeddedService(JETTY_CONFIGURATION);
diff --git 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerTest.java
 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerTest.java
index 43d54be03b2..4f46f27ebc4 100644
--- 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerTest.java
+++ 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerTest.java
@@ -30,8 +30,11 @@ public class WsProducerTest extends WsProducerTestBase {
             .emptyTemplate()
             .withPort(AvailablePortFinder.getNextAvailable())
             .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-            .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration(
-                    TestServletFactory.class.getName(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .withServletConfiguration()
+            .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                    TestServletFactory.class.getName(),
+                    
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .build()
             .build();
     @RegisterExtension
     public JettyEmbeddedService service = new 
JettyEmbeddedService(jettyConfiguration);
diff --git 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerUsingStreamingTest.java
 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerUsingStreamingTest.java
index ab0ac4dabb8..dd89099e84d 100644
--- 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerUsingStreamingTest.java
+++ 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WsProducerUsingStreamingTest.java
@@ -31,8 +31,11 @@ public class WsProducerUsingStreamingTest extends 
WsProducerTestBase {
             .emptyTemplate()
             .withPort(AvailablePortFinder.getNextAvailable())
             .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-            .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration(
-                    TestServletFactory.class.getName(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .withServletConfiguration()
+            .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                    TestServletFactory.class.getName(),
+                    
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .build()
             .build();
     @RegisterExtension
     public JettyEmbeddedService service = new 
JettyEmbeddedService(jettyConfiguration);
diff --git 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WssProducerTest.java
 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WssProducerTest.java
index 9901c9c834b..81a682f373b 100644
--- 
a/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WssProducerTest.java
+++ 
b/components/camel-ahc-ws/src/test/java/org/apache/camel/component/ahc/ws/WssProducerTest.java
@@ -39,8 +39,11 @@ public class WssProducerTest extends WsProducerTestBase {
             .emptyTemplate()
             .withPort(AvailablePortFinder.getNextAvailable())
             .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-            .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration(
-                    TestServletFactory.class.getName(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .withServletConfiguration()
+            .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                    TestServletFactory.class.getName(),
+                    
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+            .build()
             .withSslContext(() -> {
                 try {
                     return 
WssProducerTest.defineSSLContextServerParameters().createSSLContext(null);
diff --git 
a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterTestSupport.java
 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterTestSupport.java
index 0c22ee3d3e8..8414bd69279 100644
--- 
a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterTestSupport.java
+++ 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterTestSupport.java
@@ -36,8 +36,11 @@ public abstract class WebsocketCamelRouterTestSupport 
extends CamelTestSupport {
                 .emptyTemplate()
                 .withPort(PORT)
                 .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-                .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration<>(
-                        new CamelWebSocketServlet(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC, "CamelWsServlet"))
+                .withServletConfiguration()
+                .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                        new CamelWebSocketServlet(),
+                        
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC,
 "CamelWsServlet"))
+                .build()
                 .build();
 
         service = new JettyEmbeddedService(jettyConfiguration);
diff --git 
a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterWithInitParamTestSupport.java
 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterWithInitParamTestSupport.java
index 43cc1fbf827..0fe77d55622 100644
--- 
a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterWithInitParamTestSupport.java
+++ 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterWithInitParamTestSupport.java
@@ -32,9 +32,10 @@ public abstract class 
WebsocketCamelRouterWithInitParamTestSupport extends Camel
 
     @BeforeEach
     void setupJetty() {
-        final JettyConfiguration.ServletConfiguration<CamelWebSocketServlet> 
servletConfiguration
-                = new JettyConfiguration.ServletConfiguration<>(
-                        new CamelWebSocketServlet(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC, "CamelWsServlet");
+        final 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<CamelWebSocketServlet>
 servletConfiguration
+                = new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                        new CamelWebSocketServlet(),
+                        
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC,
 "CamelWsServlet");
 
         servletConfiguration.addInitParameter("events", "true");
 
@@ -42,7 +43,8 @@ public abstract class 
WebsocketCamelRouterWithInitParamTestSupport extends Camel
                 .emptyTemplate()
                 .withPort(PORT)
                 .withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
-                .addServletConfiguration(servletConfiguration).build();
+                
.withServletConfiguration().addServletConfiguration(servletConfiguration).build()
+                .build();
 
         service = new JettyEmbeddedService(jettyConfiguration);
         service.initialize();
diff --git 
a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomEntryPollingConsumerWithBasicAuthTest.java
 
b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomEntryPollingConsumerWithBasicAuthTest.java
index 97d017eac7a..d23f123fde4 100644
--- 
a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomEntryPollingConsumerWithBasicAuthTest.java
+++ 
b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomEntryPollingConsumerWithBasicAuthTest.java
@@ -33,9 +33,12 @@ public class AtomEntryPollingConsumerWithBasicAuthTest 
extends AtomEntryPollingC
     public JettyEmbeddedService service = new JettyEmbeddedService(
             JettyConfigurationBuilder.bareTemplate()
                     .withPort(PORT)
-                    .addBasicUser("camel", "camelPass", "Private!")
-                    .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration<>(
-                            new MyHttpServlet(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+                    .withServletConfiguration()
+                    .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                            new MyHttpServlet(),
+                            
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+                    .addBasicAuthUser("camel", "camelPass", "Private!")
+                    .build()
                     .build());
 
     @Override
diff --git 
a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomPollingConsumerWithBasicAuthTest.java
 
b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomPollingConsumerWithBasicAuthTest.java
index ab1e254073c..d82ce3b32a9 100644
--- 
a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomPollingConsumerWithBasicAuthTest.java
+++ 
b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomPollingConsumerWithBasicAuthTest.java
@@ -33,9 +33,12 @@ public class AtomPollingConsumerWithBasicAuthTest extends 
AtomPollingConsumerTes
     public JettyEmbeddedService service = new JettyEmbeddedService(
             JettyConfigurationBuilder.bareTemplate()
                     .withPort(PORT)
-                    .addBasicUser("camel", "camelPass", "Private!")
-                    .addServletConfiguration(new 
JettyConfiguration.ServletConfiguration<>(
-                            new MyHttpServlet(), 
JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+                    .withServletConfiguration()
+                    .addServletConfiguration(new 
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<>(
+                            new MyHttpServlet(),
+                            
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration.ROOT_PATH_SPEC))
+                    .addBasicAuthUser("camel", "camelPass", "Private!")
+                    .build()
                     .build());
 
     @Override
diff --git a/test-infra/camel-test-infra-jetty/pom.xml 
b/test-infra/camel-test-infra-jetty/pom.xml
index efe071c016d..ab8e786776f 100644
--- a/test-infra/camel-test-infra-jetty/pom.xml
+++ b/test-infra/camel-test-infra-jetty/pom.xml
@@ -50,6 +50,12 @@
             <artifactId>awaitility</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-webapp</artifactId>
+            <version>${jetty-version}</version>
+        </dependency>
+
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-servlet</artifactId>
diff --git 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java
 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java
index 52210ef20ca..72ec45ff643 100644
--- 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java
+++ 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfiguration.java
@@ -23,12 +23,25 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.function.Consumer;
 
 import javax.net.ssl.SSLContext;
 import javax.servlet.Servlet;
 
 import org.apache.camel.util.KeyValueHolder;
+import org.eclipse.jetty.security.ConstraintMapping;
+import org.eclipse.jetty.security.ConstraintSecurityHandler;
+import org.eclipse.jetty.security.HashLoginService;
+import org.eclipse.jetty.security.SecurityHandler;
+import org.eclipse.jetty.security.UserStore;
+import org.eclipse.jetty.security.authentication.BasicAuthenticator;
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.handler.ContextHandler;
+import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jetty.util.security.Constraint;
+import org.eclipse.jetty.util.security.Credential;
+import org.eclipse.jetty.webapp.WebAppContext;
 
 /**
  * A configuration holder for embedded Jetty instances
@@ -36,93 +49,227 @@ import org.eclipse.jetty.servlet.ServletHolder;
 public class JettyConfiguration {
     public static final String ROOT_CONTEXT_PATH = "/";
 
-    /**
-     * A configuration holder for Jetty servlet holders
-     * 
-     * @param <T>
-     */
-    public static class ServletConfiguration<T> {
-        public static final String ROOT_PATH_SPEC = "/*";
+    public abstract static class AbstractContextHandlerConfiguration<T> {
 
-        private final T servlet;
-        private final String pathSpec;
-        private Map<String, String> initParameters = new HashMap<>();
-        private String name;
+        protected final String contextPath;
 
-        public ServletConfiguration(T servlet, String pathSpec) {
-            this.servlet = servlet;
-            this.pathSpec = pathSpec;
-            this.name = null;
+        public AbstractContextHandlerConfiguration(String contextPath) {
+            this.contextPath = contextPath;
         }
 
-        public ServletConfiguration(T servlet, String pathSpec, String name) {
-            this.servlet = servlet;
-            this.pathSpec = pathSpec;
-            this.name = name;
+        public String getContextPath() {
+            return contextPath;
         }
 
-        public T getServlet() {
-            return servlet;
+        public void customize(Consumer<T> customizer, T handler) {
+            customizer.accept(handler);
         }
 
-        public String getPathSpec() {
-            return pathSpec;
-        }
+        abstract T resolve();
+    }
 
-        public void addInitParameter(String param, String value) {
-            initParameters.put(param, value);
+    public static class ContextHandlerConfiguration extends 
AbstractContextHandlerConfiguration<ContextHandler> {
+        public ContextHandlerConfiguration(String contextPath) {
+            super(contextPath);
         }
 
-        public Map<String, String> getInitParameters() {
-            return Collections.unmodifiableMap(initParameters);
+        @Override
+        ContextHandler resolve() {
+            ContextHandler contextHandler = new ContextHandler(contextPath);
+
+            return contextHandler;
         }
+    }
 
-        public ServletHolder buildServletHolder() {
-            ServletHolder servletHolder = resolveServletHolder();
+    public static class WebContextConfiguration extends 
AbstractContextHandlerConfiguration<WebAppContext> {
+        private final String webApp;
 
-            if (!initParameters.isEmpty()) {
-                servletHolder.setInitParameters(initParameters);
-            }
+        public WebContextConfiguration(String webApp, String contextPath) {
+            super(contextPath);
+            this.webApp = webApp;
+        }
 
-            return servletHolder;
+        public String getWebApp() {
+            return webApp;
         }
 
-        public String getName() {
-            return name;
+        public WebAppContext resolve() {
+            return new WebAppContext(webApp, super.getContextPath());
         }
+    }
 
-        private ServletHolder resolveServletHolder() {
-            if (servlet instanceof ServletHolder) {
-                return (ServletHolder) servlet;
+    public static class ServletHandlerConfiguration extends 
AbstractContextHandlerConfiguration<ServletContextHandler> {
+        /**
+         * A configuration holder for Jetty servlet holders
+         *
+         * @param <T>
+         */
+        public static class ServletConfiguration<T> {
+            public static final String ROOT_PATH_SPEC = "/*";
+
+            private final T servlet;
+            private final String pathSpec;
+            private Map<String, String> initParameters = new HashMap<>();
+            private String name;
+
+            public ServletConfiguration(T servlet, String pathSpec) {
+                this.servlet = servlet;
+                this.pathSpec = pathSpec;
+                this.name = null;
             }
 
-            ServletHolder servletHolder = new ServletHolder();
+            public ServletConfiguration(T servlet, String pathSpec, String 
name) {
+                this.servlet = servlet;
+                this.pathSpec = pathSpec;
+                this.name = name;
+            }
 
-            if (name != null) {
-                servletHolder.setName(name);
+            public T getServlet() {
+                return servlet;
             }
 
-            if (servlet instanceof String) {
-                servletHolder.setClassName((String) servlet);
-            } else {
-                if (servlet instanceof Servlet) {
-                    servletHolder.setServlet((Servlet) servlet);
+            public String getPathSpec() {
+                return pathSpec;
+            }
+
+            public void addInitParameter(String param, String value) {
+                initParameters.put(param, value);
+            }
+
+            public Map<String, String> getInitParameters() {
+                return Collections.unmodifiableMap(initParameters);
+            }
+
+            public ServletHolder buildServletHolder() {
+                ServletHolder servletHolder = resolveServletHolder();
+
+                if (!initParameters.isEmpty()) {
+                    servletHolder.setInitParameters(initParameters);
+                }
+
+                return servletHolder;
+            }
+
+            public String getName() {
+                return name;
+            }
+
+            private ServletHolder resolveServletHolder() {
+                if (servlet instanceof ServletHolder) {
+                    return (ServletHolder) servlet;
+                }
+
+                ServletHolder servletHolder = new ServletHolder();
+
+                if (name != null) {
+                    servletHolder.setName(name);
+                }
+
+                if (servlet instanceof String) {
+                    servletHolder.setClassName((String) servlet);
                 } else {
-                    throw new IllegalArgumentException(
-                            "Unknown servlet type: " + (servlet == null ? 
"null" : servlet.getClass()));
+                    if (servlet instanceof Servlet) {
+                        servletHolder.setServlet((Servlet) servlet);
+                    } else {
+                        throw new IllegalArgumentException(
+                                "Unknown servlet type: " + (servlet == null ? 
"null" : servlet.getClass()));
+                    }
                 }
+
+                return servletHolder;
+            }
+        }
+
+        private String realm;
+        private List<KeyValueHolder<String, String>> userInfos = new 
ArrayList<>();
+        private List<ServletConfiguration<?>> servletConfigurations = new 
ArrayList<>();
+
+        public ServletHandlerConfiguration(String contextPath) {
+            super(contextPath);
+        }
+
+        public void addBasicAuthUser(String username, String password, String 
realm) {
+            this.realm = Objects.requireNonNull(realm);
+            addBasicAuthUser(new KeyValueHolder<>(username, password));
+        }
+
+        public void addBasicAuthUser(KeyValueHolder<String, String> userInfo) {
+            userInfos.add(userInfo);
+        }
+
+        public List<KeyValueHolder<String, String>> getBasicUsers() {
+            return Collections.unmodifiableList(userInfos);
+        }
+
+        public String getRealm() {
+            return realm;
+        }
+
+        void addServletConfiguration(ServletConfiguration<?> 
servletConfiguration) {
+            servletConfigurations.add(servletConfiguration);
+        }
+
+        public List<ServletConfiguration<?>> getServletConfigurations() {
+            return Collections.unmodifiableList(servletConfigurations);
+        }
+
+        private SecurityHandler basicAuth(List<KeyValueHolder<String, String>> 
userInfoList, String realm) {
+
+            HashLoginService l = new HashLoginService();
+            UserStore us = new UserStore();
+
+            for (var userInfo : userInfoList) {
+                // In order: data1 == username, data2 == password
+                us.addUser(userInfo.getKey(), 
Credential.getCredential(userInfo.getValue()), new String[] { "user" });
+
             }
 
-            return servletHolder;
+            l.setName(realm);
+            l.setUserStore(us);
+
+            Constraint constraint = new Constraint();
+            constraint.setName(Constraint.__BASIC_AUTH);
+            constraint.setRoles(new String[] { "user" });
+            constraint.setAuthenticate(true);
+
+            ConstraintMapping cm = new ConstraintMapping();
+            cm.setConstraint(constraint);
+            cm.setPathSpec(ServletConfiguration.ROOT_PATH_SPEC);
+
+            ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
+            csh.setAuthenticator(new BasicAuthenticator());
+            csh.setRealmName("myrealm");
+            csh.addConstraintMapping(cm);
+            csh.setLoginService(l);
+
+            return csh;
+        }
+
+        @Override
+        ServletContextHandler resolve() {
+            ServletContextHandler contextHandler = new 
ServletContextHandler(ServletContextHandler.SESSIONS);
+
+            if (!userInfos.isEmpty()) {
+                contextHandler.setSecurityHandler(basicAuth(userInfos, realm));
+            }
+
+            contextHandler.setContextPath(super.getContextPath());
+
+            for (ServletConfiguration servletConfiguration : 
servletConfigurations) {
+                
contextHandler.addServlet(servletConfiguration.buildServletHolder(), 
servletConfiguration.getPathSpec());
+            }
+
+            return contextHandler;
         }
     }
 
     private int port;
     private SSLContext sslContext;
-    private List<ServletConfiguration<?>> servletConfigurations = new 
ArrayList<>();
+
     private String contextPath;
-    private String realm;
-    private List<KeyValueHolder<String, String>> userInfos = new ArrayList<>();
+    private AbstractContextHandlerConfiguration<? extends Handler> 
contextHandlerConfiguration;
+
+    private WebContextConfiguration webContextConfiguration;
 
     public int getPort() {
         return port;
@@ -148,28 +295,20 @@ public class JettyConfiguration {
         this.contextPath = contextPath;
     }
 
-    void addServletConfiguration(ServletConfiguration<?> servletConfiguration) 
{
-        servletConfigurations.add(servletConfiguration);
-    }
-
-    public List<ServletConfiguration<?>> getServletConfigurations() {
-        return Collections.unmodifiableList(servletConfigurations);
-    }
-
-    void addBasicAuthUser(String username, String password, String realm) {
-        this.realm = Objects.requireNonNull(realm);
-        addBasicAuthUser(new KeyValueHolder<>(username, password));
+    public void setContextHandlerConfiguration(
+            AbstractContextHandlerConfiguration<? extends Handler> 
contextHandlerConfiguration) {
+        this.contextHandlerConfiguration = contextHandlerConfiguration;
     }
 
-    void addBasicAuthUser(KeyValueHolder<String, String> userInfo) {
-        userInfos.add(userInfo);
+    public WebContextConfiguration getWebContextConfiguration() {
+        return webContextConfiguration;
     }
 
-    public List<KeyValueHolder<String, String>> getBasicUsers() {
-        return Collections.unmodifiableList(userInfos);
+    void setWebContextConfiguration(WebContextConfiguration 
webContextConfiguration) {
+        this.webContextConfiguration = webContextConfiguration;
     }
 
-    public String getRealm() {
-        return realm;
+    public AbstractContextHandlerConfiguration<? extends Handler> 
getContextHandlerConfiguration() {
+        return contextHandlerConfiguration;
     }
 }
diff --git 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java
 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java
index eca93943322..8dbf524b8a7 100644
--- 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java
+++ 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyConfigurationBuilder.java
@@ -17,14 +17,64 @@
 
 package org.apache.camel.test.infra.jetty.services;
 
+import java.util.function.Consumer;
 import java.util.function.Supplier;
 
 import javax.net.ssl.SSLContext;
 
+import org.apache.camel.util.KeyValueHolder;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+
 /**
  * This builder can be used to build and configure a configuration holder for 
embedded Jetty instances
  */
 public final class JettyConfigurationBuilder {
+    public static class ServletConfigurationBuilder {
+        private final JettyConfiguration jettyConfiguration;
+        private final JettyConfigurationBuilder jettyConfigurationBuilder;
+
+        private JettyConfiguration.ServletHandlerConfiguration 
servletHandlerConfiguration;
+
+        public ServletConfigurationBuilder(JettyConfigurationBuilder builder, 
JettyConfiguration jettyConfiguration) {
+            this.jettyConfigurationBuilder = builder;
+            this.jettyConfiguration = jettyConfiguration;
+
+            servletHandlerConfiguration
+                    = new 
JettyConfiguration.ServletHandlerConfiguration(jettyConfiguration.getContextPath());
+        }
+
+        public ServletConfigurationBuilder customize(
+                Consumer<ServletContextHandler> customizer, 
ServletContextHandler handler) {
+            servletHandlerConfiguration.customize(customizer, handler);
+
+            return this;
+        }
+
+        public ServletConfigurationBuilder addBasicAuthUser(String username, 
String password, String realm) {
+            servletHandlerConfiguration.addBasicAuthUser(username, password, 
realm);
+
+            return this;
+        }
+
+        public ServletConfigurationBuilder 
addBasicAuthUser(KeyValueHolder<String, String> userInfo) {
+            servletHandlerConfiguration.addBasicAuthUser(userInfo);
+
+            return this;
+        }
+
+        public ServletConfigurationBuilder addServletConfiguration(
+                
JettyConfiguration.ServletHandlerConfiguration.ServletConfiguration<?> 
servletConfiguration) {
+            
servletHandlerConfiguration.addServletConfiguration(servletConfiguration);
+
+            return this;
+        }
+
+        public JettyConfigurationBuilder build() {
+            
jettyConfiguration.setContextHandlerConfiguration(servletHandlerConfiguration);
+            return jettyConfigurationBuilder;
+        }
+    }
+
     private JettyConfiguration jettyConfiguration = new JettyConfiguration();
 
     private JettyConfigurationBuilder() {
@@ -46,20 +96,18 @@ public final class JettyConfigurationBuilder {
         return withSslContext(contextSupplier::get);
     }
 
-    public JettyConfigurationBuilder 
addServletConfiguration(JettyConfiguration.ServletConfiguration 
servletConfiguration) {
-        jettyConfiguration.addServletConfiguration(servletConfiguration);
-
-        return this;
+    public ServletConfigurationBuilder withServletConfiguration() {
+        return new ServletConfigurationBuilder(this, jettyConfiguration);
     }
 
-    public JettyConfigurationBuilder addBasicUser(String username, String 
password, String realm) {
-        jettyConfiguration.addBasicAuthUser(username, password, realm);
+    public JettyConfigurationBuilder withContextPath(String contextPath) {
+        jettyConfiguration.setContextPath(contextPath);
 
         return this;
     }
 
-    public JettyConfigurationBuilder withContextPath(String contextPath) {
-        jettyConfiguration.setContextPath(contextPath);
+    public JettyConfigurationBuilder 
withWebAppContext(JettyConfiguration.WebContextConfiguration 
webContextConfiguration) {
+        jettyConfiguration.setWebContextConfiguration(webContextConfiguration);
 
         return this;
     }
diff --git 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyEmbeddedService.java
 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyEmbeddedService.java
index a636594e4d6..567fc9207eb 100644
--- 
a/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyEmbeddedService.java
+++ 
b/test-infra/camel-test-infra-jetty/src/test/java/org/apache/camel/test/infra/jetty/services/JettyEmbeddedService.java
@@ -17,26 +17,16 @@
 
 package org.apache.camel.test.infra.jetty.services;
 
-import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import javax.net.ssl.SSLContext;
 
 import org.apache.camel.test.infra.jetty.common.JettyProperties;
-import org.apache.camel.util.KeyValueHolder;
 import org.awaitility.Awaitility;
-import org.eclipse.jetty.security.ConstraintMapping;
-import org.eclipse.jetty.security.ConstraintSecurityHandler;
-import org.eclipse.jetty.security.HashLoginService;
-import org.eclipse.jetty.security.SecurityHandler;
-import org.eclipse.jetty.security.UserStore;
-import org.eclipse.jetty.security.authentication.BasicAuthenticator;
+import org.eclipse.jetty.server.Handler;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.server.SslConnectionFactory;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.util.security.Constraint;
-import org.eclipse.jetty.util.security.Credential;
 import org.eclipse.jetty.util.ssl.SslContextFactory;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
@@ -75,38 +65,6 @@ public class JettyEmbeddedService implements JettyService, 
BeforeEachCallback, A
         return connector;
     }
 
-    private SecurityHandler basicAuth(List<KeyValueHolder<String, String>> 
userInfoList, String realm) {
-
-        HashLoginService l = new HashLoginService();
-        UserStore us = new UserStore();
-
-        for (var userInfo : userInfoList) {
-            // In order: data1 == username, data2 == password
-            us.addUser(userInfo.getKey(), 
Credential.getCredential(userInfo.getValue()), new String[] { "user" });
-
-        }
-
-        l.setName(realm);
-        l.setUserStore(us);
-
-        Constraint constraint = new Constraint();
-        constraint.setName(Constraint.__BASIC_AUTH);
-        constraint.setRoles(new String[] { "user" });
-        constraint.setAuthenticate(true);
-
-        ConstraintMapping cm = new ConstraintMapping();
-        cm.setConstraint(constraint);
-        cm.setPathSpec(JettyConfiguration.ServletConfiguration.ROOT_PATH_SPEC);
-
-        ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
-        csh.setAuthenticator(new BasicAuthenticator());
-        csh.setRealmName("myrealm");
-        csh.addConstraintMapping(cm);
-        csh.setLoginService(l);
-
-        return csh;
-    }
-
     @Override
     public void registerProperties() {
         System.setProperty(JettyProperties.JETTY_ADDRESS, "localhost:" + 
getPort());
@@ -121,17 +79,8 @@ public class JettyEmbeddedService implements JettyService, 
BeforeEachCallback, A
 
             server.addConnector(connector);
 
-            ServletContextHandler contextHandler = new 
ServletContextHandler(ServletContextHandler.SESSIONS);
-
-            var basicUsers = jettyConfiguration.getBasicUsers();
-            if (!basicUsers.isEmpty()) {
-                contextHandler.setSecurityHandler(basicAuth(basicUsers, 
jettyConfiguration.getRealm()));
-            }
+            Handler contextHandler = 
jettyConfiguration.getContextHandlerConfiguration().resolve();
 
-            contextHandler.setContextPath(jettyConfiguration.getContextPath());
-            for (JettyConfiguration.ServletConfiguration servletConfiguration 
: jettyConfiguration.getServletConfigurations()) {
-                
contextHandler.addServlet(servletConfiguration.buildServletHolder(), 
servletConfiguration.getPathSpec());
-            }
             server.setHandler(contextHandler);
 
             server.start();

Reply via email to