You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ff...@apache.org on 2021/01/07 19:21:38 UTC

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

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 <fr...@gmail.com>
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>