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

jbonofre pushed a commit to branch karaf-4.4.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.4.x by this push:
     new 8c7b2acdc2 KARAF-7804 - provide unique servlet-name when registering 
http proxy servlets
8c7b2acdc2 is described below

commit 8c7b2acdc2ee02af44e4f0f50212af2698c2eef3
Author: Aleksy Wróblewski <aleksy.wroblew...@bbbit.io>
AuthorDate: Fri Jan 26 14:12:17 2024 +0100

    KARAF-7804 - provide unique servlet-name when registering http proxy 
servlets
    
    (cherry picked from commit c310ce274aab5be7f9cd81c2b549b8e4978ab786)
---
 .../karaf/http/core/internal/ProxyServiceImpl.java   | 20 +++++++++++++++++++-
 .../test/java/org/apache/karaf/itests/HttpTest.java  |  8 ++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/http/src/main/java/org/apache/karaf/http/core/internal/ProxyServiceImpl.java 
b/http/src/main/java/org/apache/karaf/http/core/internal/ProxyServiceImpl.java
index 5e86fcffc6..2991e70e81 100644
--- 
a/http/src/main/java/org/apache/karaf/http/core/internal/ProxyServiceImpl.java
+++ 
b/http/src/main/java/org/apache/karaf/http/core/internal/ProxyServiceImpl.java
@@ -101,6 +101,7 @@ public class ProxyServiceImpl implements ProxyService {
                 String[] proxySplit = proxyString.split(" ");
                 if (proxySplit.length == 3) {
                     String url = proxySplit[0].trim();
+                    validateUrl(url);
                     String proxyTo = proxySplit[1].trim();
                     String balancingPolicy = proxySplit[2].trim();
                     if (!proxies.containsKey(url)) {
@@ -111,6 +112,9 @@ public class ProxyServiceImpl implements ProxyService {
                         addProxyInternal(proxy);
                     }
                 }
+                else {
+                    LOG.error("Incorrect configuration line {}", proxyString);
+                }
             }
         }
     }
@@ -131,7 +135,9 @@ public class ProxyServiceImpl implements ProxyService {
                     proxyServlet.setBalancingPolicy(balancingPolicy);
                 }
             }
-            httpService.registerServlet(proxy.getUrl(), proxyServlet, new 
Hashtable(), null);
+            Hashtable<String, String> props = new Hashtable<>();
+            props.put("servlet-name", getUniqueServletName(proxy));
+            httpService.registerServlet(proxy.getUrl(), proxyServlet, props, 
null);
             proxies.put(proxy.getUrl(), proxy);
         } catch (Exception e) {
             LOG.error("Can't add {} proxy to {}", proxy.getUrl(), 
proxy.getProxyTo(), e);
@@ -160,4 +166,16 @@ public class ProxyServiceImpl implements ProxyService {
         }
     }
 
+    private void validateUrl(String url)
+    {
+        if (url == null || !url.startsWith("/"))
+        {
+            throw new IllegalArgumentException("Proxy URL does not start with 
'/': " + url);
+        }
+    }
+
+    private String getUniqueServletName(Proxy proxy)
+    {
+        return proxy.getUrl().substring(1);
+    }
 }
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/HttpTest.java 
b/itests/test/src/test/java/org/apache/karaf/itests/HttpTest.java
index 3374b681e6..69cc2b1ba2 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/HttpTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/HttpTest.java
@@ -59,6 +59,7 @@ public class HttpTest extends BaseTest {
     @Test
     public void testProxy() throws Exception {
         executeCommand("http:proxy-add /test1 http://karaf.apache.org";);
+        executeCommand("http:proxy-add /test2 http://karaf.apache.org";);
 
         String output = executeCommand("http:proxy-balancing-list");
         System.out.println(output);
@@ -68,6 +69,13 @@ public class HttpTest extends BaseTest {
         output = executeCommand("http:proxy-list");
         System.out.println(output);
         assertContains("/test1", output);
+        assertContains("/test2", output);
     }
 
+    @Test
+    public void testIncorrectProxyUrlFails() throws Exception {
+        executeCommand("http:proxy-add no-slash-prefix 
http://karaf.apache.org";);
+        String output = executeCommand("http:proxy-list");
+        assertContainsNot("no-slash-prefix", output);
+    }
 }

Reply via email to