Skip to content

Commit

Permalink
Fix request log on Jetty 10.0.x
Browse files Browse the repository at this point in the history
Signed-off-by: Steffen Nießing <[email protected]>
  • Loading branch information
zUniQueX committed Sep 2, 2022
1 parent 50449f8 commit ee7d2da
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package ch.qos.logback.access.jetty;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -30,7 +32,15 @@
* @author Joakim Erdfelt
*/
public class JettyModernServerAdapter extends JettyServerAdapter {
private static final Method RESPONSE_GET_HTTP_FIELDS;

static {
try {
RESPONSE_GET_HTTP_FIELDS = Response.class.getDeclaredMethod("getHttpFields");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

public JettyModernServerAdapter(Request jettyRequest, Response jettyResponse) {
super(jettyRequest, jettyResponse);
Expand All @@ -52,9 +62,16 @@ public long getRequestTimestamp() {
}

@Override
@SuppressWarnings("unchecked")
public Map<String, String> buildResponseHeaderMap() {
Map<String, String> responseHeaderMap = new HashMap<String, String>();
Iterator<HttpField> httpFieldIter = response.getHttpFields().iterator();
Iterable<HttpField> httpFields;
try {
httpFields = (Iterable<HttpField>) RESPONSE_GET_HTTP_FIELDS.invoke(response);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Iterator<HttpField> httpFieldIter = httpFields.iterator();
while (httpFieldIter.hasNext()) {
HttpField httpField = httpFieldIter.next();
String key = httpField.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.File;
import java.net.URL;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -65,6 +66,14 @@
* even bad requests, and context-less requests.
* </p>
* <p>
* <b>Note:</b>
* Jetty 9.4.x and Jetty 10.0.x don't have equal method signatures for all methods. E.g. the return type for
* {@code Response#getHttpFields()} changed from {@code HttpFields} to {@code HttpFields.Mutable}.
* To properly support Jetty 9.4.x and Jetty 10.0.x reflection is used to resolve the correct method.
* If this is too slow for an application, the {@link #makeJettyServerAdapter(Request, Response)} method can be used
* to override the {@link JettyServerAdapter} implementation.
* </p>
* <p>
* The internals of the Jetty Request and Response objects track the state of the object at the time
* they are committed (the actual state during the application when an action on the network commits the
* request/response exchange). This prevents behaviors from 3rd party libraries
Expand Down Expand Up @@ -244,7 +253,7 @@ enum State {
String resource;

// Jetty 9.4.x and newer is considered modern.
boolean modernJettyRequestLog;
protected boolean modernJettyRequestLog;
boolean quiet = false;

public RequestLogImpl() {
Expand All @@ -269,7 +278,7 @@ public void log(Request jettyRequest, Response jettyResponse) {
aai.appendLoopOnAppenders(accessEvent);
}

private JettyServerAdapter makeJettyServerAdapter(Request jettyRequest, Response jettyResponse) {
protected JettyServerAdapter makeJettyServerAdapter(Request jettyRequest, Response jettyResponse) {
if (modernJettyRequestLog) {
return new JettyModernServerAdapter(jettyRequest, jettyResponse);
} else {
Expand Down Expand Up @@ -467,13 +476,21 @@ public FilterReply getFilterChainDecision(IAccessEvent event) {
}


@Override
public void addLifeCycleListener(LifeCycle.Listener listener) {
// we'll implement this when asked
}

@Override
public void removeLifeCycleListener(LifeCycle.Listener listener) {
// we'll implement this when asked
}

public boolean addEventListener(EventListener eventListener) {
// we'll implement this when asked
return false;
}

public boolean removeEventListener(EventListener eventListener) {
// we'll implement this when asked
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void eventGoesToAppenders() throws Exception {

assertEquals("127.0.0.1", event.getRemoteHost());
assertEquals("localhost", event.getServerName());
assertEquals("text/plain;charset=utf-8", event.getResponseHeader("Content-Type"));
listAppender.list.clear();
}

Expand Down

0 comments on commit ee7d2da

Please sign in to comment.