cleanup various warnings and TODOs in CustomRequestLog

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-08-20 12:02:00 +10:00
parent 37a8e44347
commit 12381e9cf5
2 changed files with 78 additions and 37 deletions

View File

@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -274,18 +275,15 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
protected static final Logger LOG = Log.getLogger(CustomRequestLog.class); protected static final Logger LOG = Log.getLogger(CustomRequestLog.class);
public static final String DEFAULT_DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss ZZZ"; public static final String DEFAULT_DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss ZZZ";
public static final String NCSA_FORMAT = "%{client}a - %u %t \"%r\" %s %O"; public static final String NCSA_FORMAT = "%{client}a - %u %t \"%r\" %s %O";
public static final String EXTENDED_NCSA_FORMAT = "%{client}a - %u %t \"%r\" %s %O \"%{Referer}i\" \"%{User-Agent}i\""; public static final String EXTENDED_NCSA_FORMAT = NCSA_FORMAT + " \"%{Referer}i\" \"%{User-Agent}i\"";
private static final ThreadLocal<StringBuilder> _buffers = ThreadLocal.withInitial(() -> new StringBuilder(256));
private static ThreadLocal<StringBuilder> _buffers = ThreadLocal.withInitial(() -> new StringBuilder(256)); private final RequestLog.Writer _requestLogWriter;
private String[] _ignorePaths;
private transient PathMappings<String> _ignorePathMap;
private RequestLog.Writer _requestLogWriter;
private final MethodHandle _logHandle; private final MethodHandle _logHandle;
private final String _formatString; private final String _formatString;
private transient PathMappings<String> _ignorePathMap;
private String[] _ignorePaths;
public CustomRequestLog(RequestLog.Writer writer, String formatString) public CustomRequestLog(RequestLog.Writer writer, String formatString)
{ {
@ -445,8 +443,10 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
{ {
if (t.isLiteralString()) if (t.isLiteralString())
logHandle = updateLogHandle(logHandle, append, t.literal); logHandle = updateLogHandle(logHandle, append, t.literal);
else else if (t.isPercentCode())
logHandle = updateLogHandle(logHandle, append, lookup, t.code, t.arg, t.modifiers, t.negated); logHandle = updateLogHandle(logHandle, append, lookup, t.code, t.arg, t.modifiers, t.negated);
else
throw new IllegalStateException("bad token " + t);
} }
return logHandle; return logHandle;
@ -479,6 +479,7 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
String arg = m.group("ARG"); String arg = m.group("ARG");
String modifierString = m.group("MOD"); String modifierString = m.group("MOD");
List<Integer> modifiers = null;
boolean negated = false; boolean negated = false;
if (modifierString != null) if (modifierString != null)
{ {
@ -487,9 +488,14 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
modifierString = modifierString.substring(1); modifierString = modifierString.substring(1);
negated = true; negated = true;
} }
modifiers = new QuotedCSV(modifierString)
.getValues()
.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
} }
List<String> modifiers = new QuotedCSV(modifierString).getValues();
tokens.add(new Token(code, arg, modifiers, negated)); tokens.add(new Token(code, arg, modifiers, negated));
} }
else if (m.group("LITERAL") != null) else if (m.group("LITERAL") != null)
@ -517,18 +523,17 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
{ {
public final String code; public final String code;
public final String arg; public final String arg;
public final List<String> modifiers; public final List<Integer> modifiers;
public final boolean negated; public final boolean negated;
public final String literal; public final String literal;
public Token(String code, String arg, List<String> modifiers, boolean negated) public Token(String code, String arg, List<Integer> modifiers, boolean negated)
{ {
this.code = code; this.code = code;
this.arg = arg; this.arg = arg;
this.modifiers = modifiers; this.modifiers = modifiers;
this.negated = negated; this.negated = negated;
this.literal = null; this.literal = null;
} }
@ -538,7 +543,6 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
this.arg = null; this.arg = null;
this.modifiers = null; this.modifiers = null;
this.negated = false; this.negated = false;
this.literal = literal; this.literal = literal;
} }
@ -558,21 +562,16 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
return foldArguments(logHandle, dropArguments(dropArguments(append.bindTo(literal), 1, Request.class), 2, Response.class)); return foldArguments(logHandle, dropArguments(dropArguments(append.bindTo(literal), 1, Request.class), 2, Response.class));
} }
//TODO use integer comparisons instead of strings @SuppressWarnings("unused")
private static boolean modify(List<String> modifiers, Boolean negated, StringBuilder b, Request request, Response response) private static boolean modify(List<Integer> modifiers, Boolean negated, StringBuilder b, Request request, Response response)
{ {
String responseCode = Integer.toString(response.getStatus());
if (negated) if (negated)
{ return !modifiers.contains(response.getStatus());
return (!modifiers.contains(responseCode));
}
else else
{ return modifiers.contains(response.getStatus());
return (modifiers.contains(responseCode));
}
} }
private MethodHandle updateLogHandle(MethodHandle logHandle, MethodHandle append, MethodHandles.Lookup lookup, String code, String arg, List<String> modifiers, boolean negated) throws NoSuchMethodException, IllegalAccessException private MethodHandle updateLogHandle(MethodHandle logHandle, MethodHandle append, MethodHandles.Lookup lookup, String code, String arg, List<Integer> modifiers, boolean negated) throws NoSuchMethodException, IllegalAccessException
{ {
MethodType logType = methodType(void.class, StringBuilder.class, Request.class, Response.class); MethodType logType = methodType(void.class, StringBuilder.class, Request.class, Response.class);
MethodType logTypeArg = methodType(void.class, String.class, StringBuilder.class, Request.class, Response.class); MethodType logTypeArg = methodType(void.class, String.class, StringBuilder.class, Request.class, Response.class);
@ -908,12 +907,12 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
if (modifiers != null && !modifiers.isEmpty()) if (modifiers != null && !modifiers.isEmpty())
{ {
MethodHandle modifierTest = lookup.findStatic(CustomRequestLog.class, "modify", methodType(Boolean.TYPE, List.class, Boolean.class, StringBuilder.class, Request.class, Response.class));
MethodHandle dash = updateLogHandle(logHandle, append, "-"); MethodHandle dash = updateLogHandle(logHandle, append, "-");
MethodHandle log = foldArguments(logHandle, specificHandle); MethodHandle log = foldArguments(logHandle, specificHandle);
modifierTest = modifierTest.bindTo(modifiers).bindTo(negated);
MethodHandle modifierTest = lookup.findStatic(CustomRequestLog.class, "modify",
methodType(Boolean.TYPE, List.class, Boolean.class, StringBuilder.class, Request.class, Response.class));
modifierTest = modifierTest.bindTo(modifiers).bindTo(negated);
return MethodHandles.guardWithTest(modifierTest, log, dash); return MethodHandles.guardWithTest(modifierTest, log, dash);
} }
@ -921,57 +920,67 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
} }
//-----------------------------------------------------------------------------------// //-----------------------------------------------------------------------------------//
@SuppressWarnings("unused")
private static void logNothing(StringBuilder b, Request request, Response response) private static void logNothing(StringBuilder b, Request request, Response response)
{ {
} }
@SuppressWarnings("unused")
private static void logServerHost(StringBuilder b, Request request, Response response) private static void logServerHost(StringBuilder b, Request request, Response response)
{ {
append(b, request.getServerName()); append(b, request.getServerName());
} }
@SuppressWarnings("unused")
private static void logClientHost(StringBuilder b, Request request, Response response) private static void logClientHost(StringBuilder b, Request request, Response response)
{ {
append(b, request.getRemoteHost()); append(b, request.getRemoteHost());
} }
@SuppressWarnings("unused")
private static void logLocalHost(StringBuilder b, Request request, Response response) private static void logLocalHost(StringBuilder b, Request request, Response response)
{ {
append(b, request.getHttpChannel().getEndPoint().getLocalAddress().getAddress().getHostAddress()); append(b, request.getHttpChannel().getEndPoint().getLocalAddress().getAddress().getHostAddress());
} }
@SuppressWarnings("unused")
private static void logRemoteHost(StringBuilder b, Request request, Response response) private static void logRemoteHost(StringBuilder b, Request request, Response response)
{ {
append(b, request.getHttpChannel().getEndPoint().getRemoteAddress().getAddress().getHostAddress()); append(b, request.getHttpChannel().getEndPoint().getRemoteAddress().getAddress().getHostAddress());
} }
@SuppressWarnings("unused")
private static void logServerPort(StringBuilder b, Request request, Response response) private static void logServerPort(StringBuilder b, Request request, Response response)
{ {
b.append(request.getServerPort()); b.append(request.getServerPort());
} }
@SuppressWarnings("unused")
private static void logClientPort(StringBuilder b, Request request, Response response) private static void logClientPort(StringBuilder b, Request request, Response response)
{ {
b.append(request.getRemotePort()); b.append(request.getRemotePort());
} }
@SuppressWarnings("unused")
private static void logLocalPort(StringBuilder b, Request request, Response response) private static void logLocalPort(StringBuilder b, Request request, Response response)
{ {
b.append(request.getHttpChannel().getEndPoint().getLocalAddress().getPort()); b.append(request.getHttpChannel().getEndPoint().getLocalAddress().getPort());
} }
@SuppressWarnings("unused")
private static void logRemotePort(StringBuilder b, Request request, Response response) private static void logRemotePort(StringBuilder b, Request request, Response response)
{ {
b.append(request.getHttpChannel().getEndPoint().getRemoteAddress().getPort()); b.append(request.getHttpChannel().getEndPoint().getRemoteAddress().getPort());
} }
@SuppressWarnings("unused")
private static void logResponseSize(StringBuilder b, Request request, Response response) private static void logResponseSize(StringBuilder b, Request request, Response response)
{ {
long written = response.getHttpChannel().getBytesWritten(); long written = response.getHttpChannel().getBytesWritten();
b.append(written); b.append(written);
} }
@SuppressWarnings("unused")
private static void logResponseSizeCLF(StringBuilder b, Request request, Response response) private static void logResponseSizeCLF(StringBuilder b, Request request, Response response)
{ {
long written = response.getHttpChannel().getBytesWritten(); long written = response.getHttpChannel().getBytesWritten();
@ -981,11 +990,13 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append(written); b.append(written);
} }
@SuppressWarnings("unused")
private static void logBytesSent(StringBuilder b, Request request, Response response) private static void logBytesSent(StringBuilder b, Request request, Response response)
{ {
b.append(response.getHttpChannel().getBytesWritten()); b.append(response.getHttpChannel().getBytesWritten());
} }
@SuppressWarnings("unused")
private static void logBytesSentCLF(StringBuilder b, Request request, Response response) private static void logBytesSentCLF(StringBuilder b, Request request, Response response)
{ {
long sent = response.getHttpChannel().getBytesWritten(); long sent = response.getHttpChannel().getBytesWritten();
@ -995,38 +1006,39 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append(sent); b.append(sent);
} }
@SuppressWarnings("unused")
private static void logBytesReceived(StringBuilder b, Request request, Response response) private static void logBytesReceived(StringBuilder b, Request request, Response response)
{ {
//todo this be content received rather than consumed b.append(request.getHttpInput().getContentReceived());
b.append(request.getHttpInput().getContentConsumed());
} }
@SuppressWarnings("unused")
private static void logBytesReceivedCLF(StringBuilder b, Request request, Response response) private static void logBytesReceivedCLF(StringBuilder b, Request request, Response response)
{ {
//todo this be content received rather than consumed long received = request.getHttpInput().getContentReceived();
long received = request.getHttpInput().getContentConsumed();
if (received == 0) if (received == 0)
b.append('-'); b.append('-');
else else
b.append(received); b.append(received);
} }
@SuppressWarnings("unused")
private static void logBytesTransferred(StringBuilder b, Request request, Response response) private static void logBytesTransferred(StringBuilder b, Request request, Response response)
{ {
//todo this be content received rather than consumed b.append(request.getHttpInput().getContentReceived() + response.getHttpOutput().getWritten());
b.append(request.getHttpInput().getContentConsumed() + response.getHttpOutput().getWritten());
} }
@SuppressWarnings("unused")
private static void logBytesTransferredCLF(StringBuilder b, Request request, Response response) private static void logBytesTransferredCLF(StringBuilder b, Request request, Response response)
{ {
//todo this be content received rather than consumed long transferred = request.getHttpInput().getContentReceived() + response.getHttpOutput().getWritten();
long transferred = request.getHttpInput().getContentConsumed() + response.getHttpOutput().getWritten();
if (transferred == 0) if (transferred == 0)
b.append('-'); b.append('-');
else else
b.append(transferred); b.append(transferred);
} }
@SuppressWarnings("unused")
private static void logRequestCookie(String arg, StringBuilder b, Request request, Response response) private static void logRequestCookie(String arg, StringBuilder b, Request request, Response response)
{ {
Cookie[] cookies = request.getCookies(); Cookie[] cookies = request.getCookies();
@ -1045,6 +1057,7 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append('-'); b.append('-');
} }
@SuppressWarnings("unused")
private static void logRequestCookies(StringBuilder b, Request request, Response response) private static void logRequestCookies(StringBuilder b, Request request, Response response)
{ {
Cookie[] cookies = request.getCookies(); Cookie[] cookies = request.getCookies();
@ -1063,11 +1076,13 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
} }
} }
@SuppressWarnings("unused")
private static void logEnvironmentVar(String arg, StringBuilder b, Request request, Response response) private static void logEnvironmentVar(String arg, StringBuilder b, Request request, Response response)
{ {
append(b, System.getenv(arg)); append(b, System.getenv(arg));
} }
@SuppressWarnings("unused")
private static void logFilename(StringBuilder b, Request request, Response response) private static void logFilename(StringBuilder b, Request request, Response response)
{ {
UserIdentity.Scope scope = request.getUserIdentityScope(); UserIdentity.Scope scope = request.getUserIdentityScope();
@ -1082,16 +1097,19 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
} }
} }
@SuppressWarnings("unused")
private static void logRequestProtocol(StringBuilder b, Request request, Response response) private static void logRequestProtocol(StringBuilder b, Request request, Response response)
{ {
append(b, request.getProtocol()); append(b, request.getProtocol());
} }
@SuppressWarnings("unused")
private static void logRequestHeader(String arg, StringBuilder b, Request request, Response response) private static void logRequestHeader(String arg, StringBuilder b, Request request, Response response)
{ {
append(b, request.getHeader(arg)); append(b, request.getHeader(arg));
} }
@SuppressWarnings("unused")
private static void logKeepAliveRequests(StringBuilder b, Request request, Response response) private static void logKeepAliveRequests(StringBuilder b, Request request, Response response)
{ {
long requests = request.getHttpChannel().getConnection().getMessagesIn(); long requests = request.getHttpChannel().getConnection().getMessagesIn();
@ -1101,21 +1119,25 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append('-'); b.append('-');
} }
@SuppressWarnings("unused")
private static void logRequestMethod(StringBuilder b, Request request, Response response) private static void logRequestMethod(StringBuilder b, Request request, Response response)
{ {
append(b, request.getMethod()); append(b, request.getMethod());
} }
@SuppressWarnings("unused")
private static void logResponseHeader(String arg, StringBuilder b, Request request, Response response) private static void logResponseHeader(String arg, StringBuilder b, Request request, Response response)
{ {
append(b, response.getHeader(arg)); append(b, response.getHeader(arg));
} }
@SuppressWarnings("unused")
private static void logQueryString(StringBuilder b, Request request, Response response) private static void logQueryString(StringBuilder b, Request request, Response response)
{ {
append(b, "?" + request.getQueryString()); append(b, "?" + request.getQueryString());
} }
@SuppressWarnings("unused")
private static void logRequestFirstLine(StringBuilder b, Request request, Response response) private static void logRequestFirstLine(StringBuilder b, Request request, Response response)
{ {
append(b, request.getMethod()); append(b, request.getMethod());
@ -1125,17 +1147,19 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
append(b, request.getProtocol()); append(b, request.getProtocol());
} }
@SuppressWarnings("unused")
private static void logRequestHandler(StringBuilder b, Request request, Response response) private static void logRequestHandler(StringBuilder b, Request request, Response response)
{ {
append(b, request.getServletName()); append(b, request.getServletName());
} }
@SuppressWarnings("unused")
private static void logResponseStatus(StringBuilder b, Request request, Response response) private static void logResponseStatus(StringBuilder b, Request request, Response response)
{ {
//todo can getCommittedMetaData be null? check what happens when its aborted
b.append(response.getCommittedMetaData().getStatus()); b.append(response.getCommittedMetaData().getStatus());
} }
@SuppressWarnings("unused")
private static void logRequestTime(DateCache dateCache, StringBuilder b, Request request, Response response) private static void logRequestTime(DateCache dateCache, StringBuilder b, Request request, Response response)
{ {
b.append('['); b.append('[');
@ -1143,6 +1167,7 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append(']'); b.append(']');
} }
@SuppressWarnings("unused")
private static void logLatencyMicroseconds(StringBuilder b, Request request, Response response) private static void logLatencyMicroseconds(StringBuilder b, Request request, Response response)
{ {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
@ -1154,38 +1179,45 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append(latencyUs); b.append(latencyUs);
} }
@SuppressWarnings("unused")
private static void logLatencyMilliseconds(StringBuilder b, Request request, Response response) private static void logLatencyMilliseconds(StringBuilder b, Request request, Response response)
{ {
long latency = System.currentTimeMillis() - request.getTimeStamp(); long latency = System.currentTimeMillis() - request.getTimeStamp();
b.append(latency); b.append(latency);
} }
@SuppressWarnings("unused")
private static void logLatencySeconds(StringBuilder b, Request request, Response response) private static void logLatencySeconds(StringBuilder b, Request request, Response response)
{ {
long latency = System.currentTimeMillis() - request.getTimeStamp(); long latency = System.currentTimeMillis() - request.getTimeStamp();
b.append(TimeUnit.MILLISECONDS.toSeconds(latency)); b.append(TimeUnit.MILLISECONDS.toSeconds(latency));
} }
@SuppressWarnings("unused")
private static void logRequestAuthentication(StringBuilder b, Request request, Response response) private static void logRequestAuthentication(StringBuilder b, Request request, Response response)
{ {
append(b, getAuthentication(request, false)); append(b, getAuthentication(request, false));
} }
@SuppressWarnings("unused")
private static void logRequestAuthenticationWithDeferred(StringBuilder b, Request request, Response response) private static void logRequestAuthenticationWithDeferred(StringBuilder b, Request request, Response response)
{ {
append(b, getAuthentication(request, true)); append(b, getAuthentication(request, true));
} }
@SuppressWarnings("unused")
private static void logUrlRequestPath(StringBuilder b, Request request, Response response) private static void logUrlRequestPath(StringBuilder b, Request request, Response response)
{ {
append(b, request.getRequestURI()); append(b, request.getRequestURI());
} }
@SuppressWarnings("unused")
private static void logConnectionStatus(StringBuilder b, Request request, Response response) private static void logConnectionStatus(StringBuilder b, Request request, Response response)
{ {
b.append(request.getHttpChannel().isResponseCompleted() ? (request.getHttpChannel().isPersistent() ? '+' : '-') : 'X'); b.append(request.getHttpChannel().isResponseCompleted() ? (request.getHttpChannel().isPersistent() ? '+' : '-') : 'X');
} }
@SuppressWarnings("unused")
private static void logRequestTrailer(String arg, StringBuilder b, Request request, Response response) private static void logRequestTrailer(String arg, StringBuilder b, Request request, Response response)
{ {
HttpFields trailers = request.getTrailers(); HttpFields trailers = request.getTrailers();
@ -1195,6 +1227,7 @@ public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
b.append('-'); b.append('-');
} }
@SuppressWarnings("unused")
private static void logResponseTrailer(String arg, StringBuilder b, Request request, Response response) private static void logResponseTrailer(String arg, StringBuilder b, Request request, Response response)
{ {
Supplier<HttpFields> supplier = response.getTrailers(); Supplier<HttpFields> supplier = response.getTrailers();

View File

@ -640,6 +640,14 @@ public class HttpInput extends ServletInputStream implements Runnable
} }
} }
public long getContentReceived()
{
synchronized (_inputQ)
{
return _contentArrived;
}
}
/** /**
* This method should be called to signal that an EOF has been detected before all the expected content arrived. * This method should be called to signal that an EOF has been detected before all the expected content arrived.
* <p> * <p>