[cxf] branch 3.4.x-fixes updated: [CXF-8412]introduce EnableRecordRequestStartTime propery so that request process duration time can be logged for http-undertow transport

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[cxf] branch 3.4.x-fixes updated: [CXF-8412]introduce EnableRecordRequestStartTime propery so that request process duration time can be logged for http-undertow transport

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

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


The following commit(s) were added to refs/heads/3.4.x-fixes by this push:
     new 10e1322  [CXF-8412]introduce EnableRecordRequestStartTime propery so that request process duration time can be logged for http-undertow transport
10e1322 is described below

commit 10e1322cfb148d8ebc37b43ef2d3afd74dcdd226
Author: Freeman Fang <[hidden email]>
AuthorDate: Fri Jan 22 11:36:06 2021 -0500

    [CXF-8412]introduce EnableRecordRequestStartTime propery so that request process duration time can be logged for http-undertow transport
   
    (cherry picked from commit 356c0eca80991fa821e95326f6a8578fc018a017)
---
 .../http_undertow/UndertowHTTPServerEngine.java    | 18 +++++
 .../handlers/CxfUndertowLogHandler.java            | 81 ++++++++++++++++++----
 .../http_undertow/undertowBasicAuthServer.xml      | 13 +++-
 3 files changed, 96 insertions(+), 16 deletions(-)

diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngine.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngine.java
index 76b4f77..3f3b561 100644
--- a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngine.java
+++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngine.java
@@ -69,6 +69,9 @@ public class UndertowHTTPServerEngine implements ServerEngine {
     public static final String DO_NOT_CHECK_URL_PROP = "org.apache.cxf.transports.http_undertow.DontCheckUrl";
     
     public static final String ENABLE_HTTP2_PROP = "org.apache.cxf.transports.http_undertow.EnableHttp2";
+    
+    public static final String ENABLE_RECORD_REQUEST_START_TIME_PROP =
+        "org.apache.cxf.transports.http_undertow.EnableRecordRequestStartTime";
 
     private static final Logger LOG = LogUtils.getL7dLogger(UndertowHTTPServerEngine.class);
 
@@ -203,6 +206,9 @@ public class UndertowHTTPServerEngine implements ServerEngine {
         if (this.shouldEnableHttp2(undertowHTTPHandler.getBus())) {
             result.setServerOption(UndertowOptions.ENABLE_HTTP2, Boolean.TRUE);
         }
+        if (this.shouldEnableRecordRequestStartTime(undertowHTTPHandler.getBus())) {
+            result.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, Boolean.TRUE);
+        }
         if (tlsServerParameters != null) {
             if (this.sslContext == null) {
                 this.sslContext = createSSLContext();
@@ -318,6 +324,18 @@ public class UndertowHTTPServerEngine implements ServerEngine {
         }
         return PropertyUtils.isTrue(prop);
     }
+    
+    private boolean shouldEnableRecordRequestStartTime(Bus bus) {
+
+        Object prop = null;
+        if (bus != null) {
+            prop = bus.getProperty(ENABLE_RECORD_REQUEST_START_TIME_PROP);
+        }
+        if (prop == null) {
+            prop = SystemPropertyAction.getPropertyOrNull(ENABLE_RECORD_REQUEST_START_TIME_PROP);
+        }
+        return PropertyUtils.isTrue(prop);
+    }
 
     protected void checkRegistedContext(URL url) {
 
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfUndertowLogHandler.java
similarity index 59%
rename from systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java
rename to rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfUndertowLogHandler.java
index 509c95b..00a2350 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/LogHandler.java
+++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfUndertowLogHandler.java
@@ -16,8 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
-package org.apache.cxf.systest.http_undertow;
+package org.apache.cxf.transport.http_undertow.handlers;
 
 import java.io.File;
 import java.io.IOException;
@@ -35,14 +34,20 @@ 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 {
 
+
+
+public class CxfUndertowLogHandler implements CXFUndertowHttpHandler {
+    
     private HttpHandler next;
     private AccessLogHandler accessLogHandler;
-
+    private String pattern;
+    private String outPutDirectory;
+    private String baseName;
+    private String suffix;
+    
     @Override
     public void handleRequest(HttpServerExchange exchange) throws Exception {
-
         if (accessLogHandler == null) {
             buildLogHandler();
         }
@@ -52,20 +57,34 @@ public class LogHandler implements CXFUndertowHttpHandler {
     @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());
+        if (this.getOutPutDirectory() == null) {
+            this.setOutPutDirectory("./data/log");
+        }
+        if (this.getBaseName() == null) {
+            this.setBaseName("request.");
+        }
+        if (this.getSuffix() == null) {
+            this.setSuffix("log");
+        }
+        if (this.getPattern() == null) {
+            this.setPattern("combined");
+        }
+        AccessLogReceiver logReceiver = DefaultAccessLogReceiver.builder()
+            .setLogWriteExecutor(xnioWorker)
+            .setOutputDirectory(new File(this.getOutPutDirectory()).toPath())
+            .setLogBaseName(this.getBaseName())
+            .setLogNameSuffix(this.getSuffix()).setRotate(true).build();
+        this.accessLogHandler = new AccessLogHandler(handler, logReceiver, this.getPattern(),
+                             AccessLogHandler.class.getClassLoader());
 
     }
-
+    
     public static XnioWorker createWorker(ClassLoader loader) {
         try {
             if (loader == null) {
@@ -78,4 +97,38 @@ public class LogHandler implements CXFUndertowHttpHandler {
             return null;
         }
     }
-}
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+
+    public String getOutPutDirectory() {
+        return outPutDirectory;
+    }
+
+    public void setOutPutDirectory(String outPutDirectory) {
+        this.outPutDirectory = outPutDirectory;
+    }
+
+    public String getBaseName() {
+        return baseName;
+    }
+
+    public void setBaseName(String baseName) {
+        this.baseName = baseName;
+    }
+
+    public String getSuffix() {
+        return suffix;
+    }
+
+    public void setSuffix(String suffix) {
+        this.suffix = suffix;
+    }
+
+
+}
\ No newline at end of file
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 d3be533..47c19db 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
@@ -20,8 +20,14 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:httpu="http://cxf.apache.org/transports/http-undertow/configuration"
-    xsi:schemaLocation="http://cxf.apache.org/transports/http-undertow/configuration http://cxf.apache.org/schemas/configuration/http-undertow.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+    xmlns:cxf="http://cxf.apache.org/core"
+    xsi:schemaLocation="http://cxf.apache.org/transports/http-undertow/configuration http://cxf.apache.org/schemas/configuration/http-undertow.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
     <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"/>
+    <cxf:bus name="cxf" id="cxf">
+       <cxf:properties>
+           <entry key="org.apache.cxf.transports.http_undertow.EnableRecordRequestStartTime" value="true"/>
+       </cxf:properties>
+    </cxf:bus>    
     <httpu:engine-factory bus="cxf">
         <httpu:engine port="${testutil.ports.UndertowBasicAuthServer}">
             <httpu:handlers>
@@ -30,7 +36,10 @@
                     <property name="maximumConcurrentRequests" value="1" />
                     <property name="queueSize" value="1"/>
                 </bean>
-                <bean class="org.apache.cxf.systest.http_undertow.LogHandler"/>
+                <bean class="org.apache.cxf.transport.http_undertow.handlers.CxfUndertowLogHandler">
+                    <property name="pattern" value="Time Taken: %T %D %h %l %u %t %r %s %b %I"/>
+                    <property name="outPutDirectory" value="target"/>
+                </bean>
             </httpu:handlers>
         </httpu:engine>
     </httpu:engine-factory>