[cxf] branch 3.3.x-fixes updated (7866570 -> d56c7af)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[cxf] branch 3.3.x-fixes updated (7866570 -> d56c7af)

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

ffang pushed a change to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git.


    from 7866570  Recording .gitmergeinfo Changes
     new 40d04f5  [CXF-8385]a test to verify http-undertow transport access log
     new d56c7af  [CXF-8405]add RequestLimitingHandler for http-undertow transport

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../handlers/CxfRequestLimitingHandler.java        | 83 ++++++++++++++++++++++
 .../cxf/systest/http_undertow/LogHandler.java      | 81 +++++++++++++++++++++
 .../http_undertow/UndertowBasicAuthTest.java       | 44 ++++++++++++
 .../http_undertow/undertowBasicAuthServer.xml      |  7 +-
 4 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100644 rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java
 create mode 100644 systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java

Reply | Threaded
Open this post in threaded view
|

[cxf] 01/02: [CXF-8385]a test to verify http-undertow transport access log

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

ffang pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 40d04f5bba0110016044ca5eb39408b38db4cfb7
Author: Freeman Fang <[hidden email]>
AuthorDate: Thu Dec 3 14:22:22 2020 -0500

    [CXF-8385]a test to verify http-undertow transport access log
   
    (cherry picked from commit 97b064dbcd33c59905be09fe6eb822e098e8d570)
    (cherry picked from commit f5545ab0263f21b7ebdabaad80c88d7d86e0406e)
---
 .../cxf/systest/http_undertow/LogHandler.java      | 81 ++++++++++++++++++++++
 .../http_undertow/UndertowBasicAuthTest.java       |  8 +++
 .../http_undertow/undertowBasicAuthServer.xml      |  3 +-
 3 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java
new file mode 100644
index 0000000..509c95b
--- /dev/null
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.http_undertow;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.cxf.transport.http_undertow.CXFUndertowHttpHandler;
+import org.xnio.OptionMap;
+import org.xnio.Options;
+import org.xnio.Xnio;
+import org.xnio.XnioWorker;
+
+import io.undertow.Undertow;
+import io.undertow.server.HttpHandler;
+import io.undertow.server.HttpServerExchange;
+import io.undertow.server.handlers.accesslog.AccessLogHandler;
+import io.undertow.server.handlers.accesslog.AccessLogReceiver;
+import io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver;
+
+public class LogHandler implements CXFUndertowHttpHandler {
+
+    private HttpHandler next;
+    private AccessLogHandler accessLogHandler;
+
+    @Override
+    public void handleRequest(HttpServerExchange exchange) throws Exception {
+
+        if (accessLogHandler == null) {
+            buildLogHandler();
+        }
+        this.accessLogHandler.handleRequest(exchange);
+    }
+
+    @Override
+    public void setNext(HttpHandler nextHandler) {
+        this.next = nextHandler;
+
+    }
+
+    private void buildLogHandler() {
+        HttpHandler handler = this.next;
+        XnioWorker xnioWorker = createWorker(this.getClass().getClassLoader());
+        AccessLogReceiver logReceiver = DefaultAccessLogReceiver.builder().setLogWriteExecutor(xnioWorker)
+            .setOutputDirectory(new File("target").toPath()).setLogBaseName("request.")
+            .setLogNameSuffix("log").setRotate(true).build();
+        this.accessLogHandler = new AccessLogHandler(handler, logReceiver, "combined",
+                                                     AccessLogHandler.class.getClassLoader());
+
+    }
+
+    public static XnioWorker createWorker(ClassLoader loader) {
+        try {
+            if (loader == null) {
+                loader = Undertow.class.getClassLoader();
+            }
+            Xnio xnio = Xnio.getInstance(loader);
+            return xnio.createWorker(OptionMap.builder().set(Options.THREAD_DAEMON, true).getMap());
+        } catch (IOException ignore) {
+
+            return null;
+        }
+    }
+}
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
index 3b4c7fd..27e9c40 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.systest.http_undertow;
 
+import java.io.File;
 import java.net.URL;
 
 import javax.xml.namespace.QName;
@@ -77,6 +78,13 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
     public void testBasicAuth() throws Exception {
         assertEquals("Hello Alice", greeter.greetMe("Alice"));
     }
+    
+    @org.junit.Test
+    public void testRequestLog() throws Exception {
+        assertEquals("Hello Log", greeter.greetMe("Log"));
+        File logFile = new File("target/request.log");
+        assertTrue(logFile.exists());
+    }
 
     @org.junit.Test
     public void testGetWSDL() throws Exception {
diff --git a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
index a30a944..dfe1ba2 100644
--- a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
+++ b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
@@ -26,7 +26,8 @@
         <httpu:engine port="${testutil.ports.UndertowBasicAuthServer}">
             <httpu:handlers>
                 <bean class="org.apache.cxf.systest.http_undertow.UndertowBasicAuthHandler"/>
+                <bean class="org.apache.cxf.systest.http_undertow.LogHandler"/>
             </httpu:handlers>
         </httpu:engine>
     </httpu:engine-factory>
-</beans>
\ No newline at end of file
+</beans>

Reply | Threaded
Open this post in threaded view
|

[cxf] 02/02: [CXF-8405]add RequestLimitingHandler for http-undertow transport

ffang
In reply to this post by ffang
This is an automated email from the ASF dual-hosted git repository.

ffang pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit d56c7af70c3acd0e8277524080d95ab28964cc78
Author: Freeman Fang <[hidden email]>
AuthorDate: Thu Jan 7 14:13:00 2021 -0500

    [CXF-8405]add RequestLimitingHandler for http-undertow transport
   
    (cherry picked from commit d19a801b7673d2b0b7f2e657e63b8450ea04c8d4)
    (cherry picked from commit 22e5008312bf9024089c3148747940e434d641a0)
---
 .../handlers/CxfRequestLimitingHandler.java        | 83 ++++++++++++++++++++++
 .../http_undertow/UndertowBasicAuthTest.java       | 36 ++++++++++
 .../http_undertow/undertowBasicAuthServer.xml      |  4 ++
 3 files changed, 123 insertions(+)

diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java
new file mode 100644
index 0000000..af1dbfb
--- /dev/null
+++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java
@@ -0,0 +1,83 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.transport.http_undertow.handlers;
+
+import org.apache.cxf.transport.http_undertow.CXFUndertowHttpHandler;
+
+import io.undertow.server.HttpHandler;
+import io.undertow.server.HttpServerExchange;
+import io.undertow.server.handlers.RequestLimitingHandler;
+
+public class CxfRequestLimitingHandler implements CXFUndertowHttpHandler {
+    
+    private HttpHandler next;
+    private RequestLimitingHandler requestLimitingHandler;
+    private int maximumConcurrentRequests;
+    private int queueSize;
+    
+    public CxfRequestLimitingHandler(int maximumConcurrentRequests, int queueSize) {
+        this.setMaximumConcurrentRequests(maximumConcurrentRequests);
+        this.setQueueSize(queueSize);
+    }
+    
+    public CxfRequestLimitingHandler() {
+        this.setMaximumConcurrentRequests(1);
+        this.setQueueSize(1);
+    }
+
+    @Override
+    public void handleRequest(HttpServerExchange exchange) throws Exception {
+        
+        if (requestLimitingHandler == null) {
+            buildLogHandler();
+        }
+        this.requestLimitingHandler.handleRequest(exchange);
+    }
+
+    @Override
+    public void setNext(HttpHandler nextHandler) {
+        this.next = nextHandler;
+        
+    }
+    
+    private void buildLogHandler() {
+        HttpHandler handler = this.next;
+        this.requestLimitingHandler =
+            new RequestLimitingHandler(getMaximumConcurrentRequests(), getQueueSize(), handler);
+
+    }
+
+    public int getMaximumConcurrentRequests() {
+        return maximumConcurrentRequests;
+    }
+
+    public void setMaximumConcurrentRequests(int maximumConcurrentRequests) {
+        this.maximumConcurrentRequests = maximumConcurrentRequests;
+    }
+
+    public int getQueueSize() {
+        return queueSize;
+    }
+
+    public void setQueueSize(int queueSize) {
+        this.queueSize = queueSize;
+    }
+    
+}
+
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
index 27e9c40..5919ce1 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
@@ -21,6 +21,9 @@ package org.apache.cxf.systest.http_undertow;
 
 import java.io.File;
 import java.net.URL;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import javax.xml.namespace.QName;
 import javax.xml.ws.BindingProvider;
@@ -87,6 +90,19 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
     }
 
     @org.junit.Test
+    public void testRequestLimitHandler() throws Exception {
+        CountDownLatch latch = new CountDownLatch(50);
+
+        ExecutorService executor = Executors.newFixedThreadPool(200);
+
+        for (int i = 0; i < 50; i++) {
+            executor.execute(new SendRequest(latch));
+        }
+        latch.await();
+        
+    }
+    
+    @org.junit.Test
     public void testGetWSDL() throws Exception {
         BusFactory bf = BusFactory.newInstance();
         Bus bus = bf.createBus();
@@ -110,4 +126,24 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
             c.setAuthorization(authorizationPolicy);
         }
     }
+    
+    class SendRequest implements Runnable {
+        private CountDownLatch latch;
+
+        SendRequest(CountDownLatch latch) {
+            this.latch = latch;
+        }
+
+        public void run() {
+            try {
+                assertEquals("Hello Request Limit", greeter.greetMe("Request Limit"));
+            } catch (Throwable ex) {
+                //some requests are expected to fail and receive 503 error
+                //cause Server side limit the concurrent request
+                assertTrue(ex.getCause().getMessage().contains("503: Service Unavailable"));
+            } finally {
+                latch.countDown();
+            }
+        }
+    }
 }
diff --git a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
index dfe1ba2..9e662a4 100644
--- a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
+++ b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
@@ -26,6 +26,10 @@
         <httpu:engine port="${testutil.ports.UndertowBasicAuthServer}">
             <httpu:handlers>
                 <bean class="org.apache.cxf.systest.http_undertow.UndertowBasicAuthHandler"/>
+                <bean class="org.apache.cxf.transport.http_undertow.handlers.CxfRequestLimitingHandler">
+                    <property name="maximumConcurrentRequests" value="1" />
+                    <property name="queueSize" value="1"/>
+                </bean>
                 <bean class="org.apache.cxf.systest.http_undertow.LogHandler"/>
             </httpu:handlers>
         </httpu:engine>