[Bug 470664] Handle multiple RequestLogHandler in chain

Signed-off-by: John Myers <jgmyers@proofpoint.com>
Change-Id: Iff4d80957f43ddeafc212500cf912ae9c928b261
This commit is contained in:
John Myers 2015-06-28 14:37:06 -07:00 committed by Greg Wilkins
parent 75f74ff76f
commit 2d0bedd9bf
4 changed files with 125 additions and 1 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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