Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Myers2015-06-28 21:37:06 +0000
committerGreg Wilkins2015-07-02 05:57:26 +0000
commit2d0bedd9bf5f037402243715f84267c8c82da1e1 (patch)
tree9a689b2fadfd531ea6ef60049c8a6ee214b85307
parent75f74ff76f43a88272c794ef8fcf782dda64fb40 (diff)
downloadorg.eclipse.jetty.project-2d0bedd9bf5f037402243715f84267c8c82da1e1.tar.gz
org.eclipse.jetty.project-2d0bedd9bf5f037402243715f84267c8c82da1e1.tar.xz
org.eclipse.jetty.project-2d0bedd9bf5f037402243715f84267c8c82da1e1.zip
[Bug 470664] Handle multiple RequestLogHandler in chain
Signed-off-by: John Myers <jgmyers@proofpoint.com> Change-Id: Iff4d80957f43ddeafc212500cf912ae9c928b261
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java10
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogCollection.java46
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/handler/RequestLogHandler.java2
-rw-r--r--jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java68
4 files changed, 125 insertions, 1 deletions
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java
index 79c113502b..11eab003b2 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java
@@ -145,6 +145,16 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
_requestLog = requestLog;
}
+ public void addRequestLog(RequestLog requestLog)
+ {
+ if (_requestLog==null)
+ _requestLog = requestLog;
+ else if (_requestLog instanceof RequestLogCollection)
+ ((RequestLogCollection) _requestLog).add(requestLog);
+ else
+ _requestLog = new RequestLogCollection(_requestLog, requestLog);
+ }
+
public MetaData.Response getCommittedMetaData()
{
return _committedMetaData;
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogCollection.java
new file mode 100644
index 0000000000..1d9562d695
--- /dev/null
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/RequestLogCollection.java
@@ -0,0 +1,46 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ========================================================================
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.server;
+
+import java.util.ArrayList;
+
+import static java.util.Arrays.asList;
+
+class RequestLogCollection
+ implements RequestLog
+{
+ private final ArrayList<RequestLog> delegates;
+
+ public RequestLogCollection(RequestLog... requestLogs)
+ {
+ delegates = new ArrayList<>(asList(requestLogs));
+ }
+
+ public void add(RequestLog requestLog)
+ {
+ delegates.add(requestLog);
+ }
+
+ @Override
+ public void log(Request request, Response response)
+ {
+ for (RequestLog delegate:delegates)
+ delegate.log(request, response);
+ }
+}
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/RequestLogHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/RequestLogHandler.java
index 525bbc4076..4075f1a839 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/RequestLogHandler.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/RequestLogHandler.java
@@ -52,7 +52,7 @@ public class RequestLogHandler extends HandlerWrapper
throws IOException, ServletException
{
if (baseRequest.getDispatcherType()==DispatcherType.REQUEST)
- baseRequest.getHttpChannel().setRequestLog(_requestLog);
+ baseRequest.getHttpChannel().addRequestLog(_requestLog);
if (_handler!=null)
_handler.handle(target,baseRequest, request, response);
}
diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
index 3761266e43..510b9fb8a0 100644
--- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
+++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
@@ -439,6 +439,74 @@ public class RequestLogHandlerTest
}
}
+ @Test(timeout = 4000)
+ public void testMultipleLogHandlers() throws Exception
+ {
+ Server server = new Server();
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(0);
+ server.setConnectors(new Connector[]{connector});
+
+ List<CaptureLog> captureLogs = new ArrayList<>();
+ List<Handler> handlerList = new ArrayList<>();
+ handlerList.add(testHandler);
+
+ for (int i = 0; i < 4; ++i) {
+ CaptureLog captureLog = new CaptureLog();
+ captureLogs.add(captureLog);
+ RequestLogHandler requestLog = new RequestLogHandler();
+ requestLog.setRequestLog(captureLog);
+ handlerList.add(requestLog);
+ }
+
+ HandlerCollection handlers = new HandlerCollection();
+ handlers.setHandlers(handlerList.toArray(new Handler[0]));
+ server.setHandler(handlers);
+
+ try
+ {
+ server.start();
+
+ String host = connector.getHost();
+ if (host == null)
+ {
+ host = "localhost";
+ }
+ int port = connector.getLocalPort();
+
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
+
+ // Make call to test handler
+ HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();
+ try
+ {
+ connection.setAllowUserInteraction(false);
+
+ // log response status code
+ int statusCode = connection.getResponseCode();
+ LOG.debug("Response Status Code: {}",statusCode);
+
+ if (statusCode == 200)
+ {
+ // collect response message and log it
+ String content = getResponseContent(connection);
+ LOG.debug("Response Content: {}",content);
+ }
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+
+ for (CaptureLog captureLog:captureLogs)
+ assertRequestLog(captureLog);
+ }
+ finally
+ {
+ server.stop();
+ }
+ }
+
/**
* Test a RequestLogHandler at the end of a HandlerCollection and also with the default ErrorHandler as server bean in place.
* @throws Exception if test failure

Back to the top