You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2023/11/10 15:12:24 UTC
(sling-org-apache-sling-engine) branch master updated: SLING-12133 : Include time spend in authentication in request log
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git
The following commit(s) were added to refs/heads/master by this push:
new 6b0675f SLING-12133 : Include time spend in authentication in request log
6b0675f is described below
commit 6b0675f0a8700afc9829118508eedb30f2fe75b9
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Nov 10 16:12:17 2023 +0100
SLING-12133 : Include time spend in authentication in request log
---
.../sling/engine/impl/log/RequestLoggerFilter.java | 2 +-
.../engine/impl/log/RequestLoggerPreprocessor.java | 71 ++++++++++++++++++++++
.../engine/impl/log/RequestLoggerResponse.java | 9 +--
.../engine/impl/log/ClientAbortExceptionTest.java | 2 +-
4 files changed, 78 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java
index 7c9ee14..38821a7 100644
--- a/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java
+++ b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerFilter.java
@@ -66,7 +66,7 @@ public final class RequestLoggerFilter implements Filter {
ServletException {
final RequestLoggerRequest rlreq = new RequestLoggerRequest((HttpServletRequest) request);
- final RequestLoggerResponse rlres = new RequestLoggerResponse((HttpServletResponse) response);
+ final RequestLoggerResponse rlres = new RequestLoggerResponse(request, (HttpServletResponse) response);
log(this.requestEntry, rlreq, rlres);
try {
diff --git a/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerPreprocessor.java b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerPreprocessor.java
new file mode 100644
index 0000000..58f6b4d
--- /dev/null
+++ b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerPreprocessor.java
@@ -0,0 +1,71 @@
+/*
+ * 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.sling.engine.impl.log;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.propertytypes.ServiceRanking;
+import org.osgi.service.http.whiteboard.Preprocessor;
+
+/**
+ * Record the starting time of the request and make it available to the request logger
+ */
+@Component(service = Preprocessor.class)
+@ServiceRanking(Integer.MAX_VALUE)
+public class RequestLoggerPreprocessor implements Preprocessor {
+
+ private static final String ATTR_NAME = Preprocessor.class.getName() + ".startTime";
+
+ @Override
+ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
+ throws IOException, ServletException {
+ request.setAttribute(ATTR_NAME, System.currentTimeMillis());
+ try {
+ chain.doFilter(request, response);
+ } finally {
+ request.removeAttribute(ATTR_NAME);
+ }
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // nothing to do
+ }
+
+ @Override
+ public void destroy() {
+ // nothing to do
+ }
+
+ public static long getRequestStartTime(final ServletRequest request) {
+ final Object val = request == null ? null : request.getAttribute(ATTR_NAME);
+ if (val instanceof Long) {
+ return (Long)val;
+ }
+ // default is *now*
+ return System.currentTimeMillis();
+ }
+}
diff --git a/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java
index 76999c1..9d71a5d 100644
--- a/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java
+++ b/src/main/java/org/apache/sling/engine/impl/log/RequestLoggerResponse.java
@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletRequest;
import javax.servlet.WriteListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@@ -65,11 +66,11 @@ class RequestLoggerResponse extends HttpServletResponseWrapper {
// probably be supported
// the request counter
- private long requestId;
+ private final long requestId;
// the system time in ms when the request entered the system, this is
// the time this instance was created
- private long requestStart;
+ private final long requestStart;
// the system time in ms when the request exited the system, this is
// the time of the call to the requestEnd() method
@@ -92,11 +93,11 @@ class RequestLoggerResponse extends HttpServletResponseWrapper {
// headers
private Map<String, Object> headers;
- RequestLoggerResponse(HttpServletResponse response) {
+ RequestLoggerResponse(final ServletRequest request, final HttpServletResponse response) {
super(response);
this.requestId = requestCounter.getAndIncrement();
- this.requestStart = System.currentTimeMillis();
+ this.requestStart = RequestLoggerPreprocessor.getRequestStartTime(request);
}
/**
diff --git a/src/test/java/org/apache/sling/engine/impl/log/ClientAbortExceptionTest.java b/src/test/java/org/apache/sling/engine/impl/log/ClientAbortExceptionTest.java
index 0b9a286..20d7a92 100644
--- a/src/test/java/org/apache/sling/engine/impl/log/ClientAbortExceptionTest.java
+++ b/src/test/java/org/apache/sling/engine/impl/log/ClientAbortExceptionTest.java
@@ -69,7 +69,7 @@ public class ClientAbortExceptionTest {
will(returnValue(sos));
}});
- r = new RequestLoggerResponse(raw);
+ r = new RequestLoggerResponse(null, raw);
}
@Test(expected=ClientAbortException.class)