/
CustomRequestLog.java
1210 lines (1051 loc) · 39.2 KB
/
CustomRequestLog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2019 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.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.Cookie;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.QuotedCSV;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.DateCache;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import static java.lang.invoke.MethodHandles.dropArguments;
import static java.lang.invoke.MethodHandles.foldArguments;
import static java.lang.invoke.MethodType.methodType;
/**
* A flexible RequestLog, which produces log strings in a customizable format.
* The Logger takes a format string where request characteristics can be added using "%" format codes which are
* replaced by the corresponding value in the log output.
* <p>
* The terms server, client, local and remote are used to refer to the different addresses and ports
* which can be logged. Server and client refer to the logical addresses which can be modified in the request
* headers. Where local and remote refer to the physical addresses which may be a proxy between the
* end-user and the server.
*
*
* <br><br>Percent codes are specified in the format %MODIFIERS{PARAM}CODE
* <pre>
* MODIFIERS:
* Optional list of comma separated HTTP status codes which may be preceded by a single "!" to indicate
* negation. If the status code is not in the list the literal string "-" will be logged instead of
* the resulting value from the percent code.
* {PARAM}:
* Parameter string which may be optional depending on the percent code used.
* CODE:
* A one or two character code specified by the {@link CustomRequestLog} table of format codes.
* </pre>
*
* <table>
* <caption>Format Codes</caption>
* <tr>
* <td><b>Format String</b></td>
* <td><b>Description</b></td>
* </tr>
*
* <tr>
* <td>%%</td>
* <td>The percent sign.</td>
* </tr>
*
* <tr>
* <td valign="top">%{format}a</td>
* <td>
* Address or Hostname. Valid formats are {server, client, local, remote}
* Optional format parameter which will be server by default.
* <br>
* Where server and client are the logical addresses which can be modified in the request headers, while local and
* remote are the physical addresses so may be a proxy between the end-user and the server.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{format}p</td>
* <td>
* Port. Valid formats are {server, client, local, remote}
* Optional format parameter which will be server by default.
* <br>
* Where server and client are the logical ports which can be modified in the request headers, while local and
* remote are the physical ports so may be to a proxy between the end-user and the server.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{CLF}I</td>
* <td>
* Size of request in bytes, excluding HTTP headers.
* Optional parameter with value of "CLF" to use CLF format, i.e. a '-' rather than a 0 when no bytes are sent.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{CLF}O</td>
* <td>
* Size of response in bytes, excluding HTTP headers.
* Optional parameter with value of "CLF" to use CLF format, i.e. a '-' rather than a 0 when no bytes are sent.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{CLF}S</td>
* <td>
* Bytes transferred (received and sent). This is the combination of %I and %O.
* Optional parameter with value of "CLF" to use CLF format, i.e. a '-' rather than a 0 when no bytes are sent.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{VARNAME}C</td>
* <td>
* The contents of cookie VARNAME in the request sent to the server. Only version 0 cookies are fully supported.
* Optional VARNAME parameter, without this parameter %C will log all cookies from the request.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%D</td>
* <td>The time taken to serve the request, in microseconds.</td>
* </tr>
*
* <tr>
* <td valign="top">%{VARNAME}e</td>
* <td>The contents of the environment variable VARNAME.</td>
* </tr>
*
* <tr>
* <td valign="top">%f</td>
* <td>Filename.</td>
* </tr>
*
* <tr>
* <td valign="top">%H</td>
* <td>The request protocol.</td>
* </tr>
*
* <tr>
* <td valign="top">%{VARNAME}i</td>
* <td>The contents of VARNAME: header line(s) in the request sent to the server.</td>
* </tr>
*
* <tr>
* <td valign="top">%k</td>
* <td>Number of keepalive requests handled on this connection.
* Interesting if KeepAlive is being used, so that, for example, a '1' means the first keepalive request
* after the initial one, '2' the second, etc...; otherwise this is always 0 (indicating the initial request).</td>
* </tr>
*
* <tr>
* <td valign="top">%m</td>
* <td>The request method.</td>
* </tr>
*
* <tr>
* <td valign="top">%{VARNAME}o</td>
* <td>The contents of VARNAME: header line(s) in the response.</td>
* </tr>
*
* <tr>
* <td valign="top">%q</td>
* <td>The query string (prepended with a ? if a query string exists, otherwise an empty string).</td>
* </tr>
*
* <tr>
* <td valign="top">%r</td>
* <td>First line of request.</td>
* </tr>
*
* <tr>
* <td valign="top">%R</td>
* <td>The handler generating the response (if any).</td>
* </tr>
*
* <tr>
* <td valign="top">%s</td>
* <td>Response status.</td>
* </tr>
*
* <tr>
* <td valign="top">%{format|timeZone|locale}t</td>
* <td>
* The time that the request was received.
* Optional parameter in one of the following formats {format}, {format|timeZone} or {format|timeZone|locale}.<br><br>
*
* <pre>
* Format Parameter: (default format [18/Sep/2011:19:18:28 -0400] where the last number indicates the timezone offset from GMT.)
* Must be in a format supported by {@link DateCache}
*
* TimeZone Parameter:
* Default timeZone GMT
* Must be in a format supported by {@link TimeZone#getTimeZone(String)}
*
* Locale Parameter:
* Default locale {@link Locale#getDefault()}
* Must be in a format supported by {@link Locale#forLanguageTag(String)}</pre>
* </td>
* </tr>
*
* <tr>
* <td valign="top">%T</td>
* <td>The time taken to serve the request, in seconds.</td>
* </tr>
*
* <tr>
* <td valign="top">%{UNIT}T</td>
* <td>The time taken to serve the request, in a time unit given by UNIT.
* Valid units are ms for milliseconds, us for microseconds, and s for seconds.
* Using s gives the same result as %T without any format; using us gives the same result as %D.</td>
* </tr>
*
* <tr>
* <td valign="top">%{d}u</td>
* <td>
* Remote user if the request was authenticated. May be bogus if return status (%s) is 401 (unauthorized).
* Optional parameter d, with this parameter deferred authentication will also be checked.
* </td>
* </tr>
*
* <tr>
* <td valign="top">%U</td>
* <td>The URL path requested, not including any query string.</td>
* </tr>
*
* <tr>
* <td valign="top">%X</td>
* <td>
* Connection status when response is completed:
* <pre>
* X = Connection aborted before the response completed.
* + = Connection may be kept alive after the response is sent.
* - = Connection will be closed after the response is sent.</pre>
* </td>
* </tr>
*
* <tr>
* <td valign="top">%{VARNAME}^ti</td>
* <td>The contents of VARNAME: trailer line(s) in the request sent to the server.</td>
* </tr>
*
* <tr>
* <td>%{VARNAME}^to</td>
* <td>The contents of VARNAME: trailer line(s) in the response sent from the server.</td>
* </tr>
* </table>
*/
@ManagedObject("Custom format request log")
public class CustomRequestLog extends ContainerLifeCycle implements RequestLog
{
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 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\"";
private static ThreadLocal<StringBuilder> _buffers = ThreadLocal.withInitial(() -> new StringBuilder(256));
private String[] _ignorePaths;
private transient PathMappings<String> _ignorePathMap;
private RequestLog.Writer _requestLogWriter;
private final MethodHandle _logHandle;
private final String _formatString;
public CustomRequestLog(RequestLog.Writer writer, String formatString)
{
_formatString = formatString;
_requestLogWriter = writer;
addBean(_requestLogWriter);
try
{
_logHandle = getLogHandle(formatString);
}
catch (NoSuchMethodException | IllegalAccessException e)
{
throw new IllegalStateException(e);
}
}
public CustomRequestLog(String file)
{
this(file, EXTENDED_NCSA_FORMAT);
}
public CustomRequestLog(String file, String format)
{
this(new RequestLogWriter(file), format);
}
@ManagedAttribute("The RequestLogWriter")
public RequestLog.Writer getWriter()
{
return _requestLogWriter;
}
/**
* Writes the request and response information to the output stream.
*
* @see org.eclipse.jetty.server.RequestLog#log(Request, Response)
*/
@Override
public void log(Request request, Response response)
{
try
{
if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null)
return;
StringBuilder sb = _buffers.get();
sb.setLength(0);
_logHandle.invoke(sb, request, response);
String log = sb.toString();
_requestLogWriter.write(log);
}
catch (Throwable e)
{
LOG.warn(e);
}
}
/**
* Extract the user authentication
*
* @param request The request to extract from
* @param checkDeferred Whether to check for deferred authentication
* @return The string to log for authenticated user.
*/
protected static String getAuthentication(Request request, boolean checkDeferred)
{
Authentication authentication = request.getAuthentication();
String name = null;
boolean deferred = false;
if (checkDeferred && authentication instanceof Authentication.Deferred)
{
authentication = ((Authentication.Deferred)authentication).authenticate(request);
deferred = true;
}
if (authentication instanceof Authentication.User)
name = ((Authentication.User)authentication).getUserIdentity().getUserPrincipal().getName();
return (name == null) ? null : (deferred ? ("?" + name) : name);
}
/**
* Set request paths that will not be logged.
*
* @param ignorePaths array of request paths
*/
public void setIgnorePaths(String[] ignorePaths)
{
_ignorePaths = ignorePaths;
}
/**
* Retrieve the request paths that will not be logged.
*
* @return array of request paths
*/
public String[] getIgnorePaths()
{
return _ignorePaths;
}
/**
* Retrieve the format string.
*
* @return the format string
*/
@ManagedAttribute("format string")
public String getFormatString()
{
return _formatString;
}
/**
* Set up request logging and open log file.
*
* @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
*/
@Override
protected synchronized void doStart() throws Exception
{
if (_ignorePaths != null && _ignorePaths.length > 0)
{
_ignorePathMap = new PathMappings<>();
for (String ignorePath : _ignorePaths)
{
_ignorePathMap.put(ignorePath, ignorePath);
}
}
else
_ignorePathMap = null;
super.doStart();
}
private static void append(StringBuilder buf, String s)
{
if (s == null || s.length() == 0)
buf.append('-');
else
buf.append(s);
}
private static void append(String s, StringBuilder buf)
{
append(buf, s);
}
private MethodHandle getLogHandle(String formatString) throws NoSuchMethodException, IllegalAccessException
{
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle append = lookup.findStatic(CustomRequestLog.class, "append", methodType(void.class, String.class, StringBuilder.class));
MethodHandle logHandle = lookup.findStatic(CustomRequestLog.class, "logNothing", methodType(void.class, StringBuilder.class, Request.class, Response.class));
List<Token> tokens = getTokens(formatString);
Collections.reverse(tokens);
for (Token t : tokens)
{
if (t.isLiteralString())
logHandle = updateLogHandle(logHandle, append, t.literal);
else
logHandle = updateLogHandle(logHandle, append, lookup, t.code, t.arg, t.modifiers, t.negated);
}
return logHandle;
}
private static List<Token> getTokens(String formatString)
{
/*
Extracts literal strings and percent codes out of the format string.
We will either match a percent code of the format %MODIFIERS{PARAM}CODE, or a literal string
until the next percent code or the end of the formatString is reached.
where
MODIFIERS is an optional comma separated list of numbers.
{PARAM} is an optional string parameter to the percent code.
CODE is a 1 to 2 character string corresponding to a format code.
*/
final Pattern PATTERN = Pattern.compile("^(?:%(?<MOD>!?[0-9,]+)?(?:\\{(?<ARG>[^}]+)})?(?<CODE>(?:(?:ti)|(?:to)|[a-zA-Z%]))|(?<LITERAL>[^%]+))(?<REMAINING>.*)", Pattern.DOTALL | Pattern.MULTILINE);
List<Token> tokens = new ArrayList<>();
String remaining = formatString;
while (remaining.length() > 0)
{
Matcher m = PATTERN.matcher(remaining);
if (m.matches())
{
if (m.group("CODE") != null)
{
String code = m.group("CODE");
String arg = m.group("ARG");
String modifierString = m.group("MOD");
boolean negated = false;
if (modifierString != null)
{
if (modifierString.startsWith("!"))
{
modifierString = modifierString.substring(1);
negated = true;
}
}
List<String> modifiers = new QuotedCSV(modifierString).getValues();
tokens.add(new Token(code, arg, modifiers, negated));
}
else if (m.group("LITERAL") != null)
{
String literal = m.group("LITERAL");
tokens.add(new Token(literal));
}
else
{
throw new IllegalStateException("formatString parsing error");
}
remaining = m.group("REMAINING");
}
else
{
throw new IllegalArgumentException("Invalid format string");
}
}
return tokens;
}
private static class Token
{
public final String code;
public final String arg;
public final List<String> modifiers;
public final boolean negated;
public final String literal;
public Token(String code, String arg, List<String> modifiers, boolean negated)
{
this.code = code;
this.arg = arg;
this.modifiers = modifiers;
this.negated = negated;
this.literal = null;
}
public Token(String literal)
{
this.code = null;
this.arg = null;
this.modifiers = null;
this.negated = false;
this.literal = literal;
}
public boolean isLiteralString()
{
return (literal != null);
}
public boolean isPercentCode()
{
return (code != null);
}
}
private MethodHandle updateLogHandle(MethodHandle logHandle, MethodHandle append, String literal)
{
return foldArguments(logHandle, dropArguments(dropArguments(append.bindTo(literal), 1, Request.class), 2, Response.class));
}
//TODO use integer comparisons instead of strings
private static boolean modify(List<String> modifiers, Boolean negated, StringBuilder b, Request request, Response response)
{
String responseCode = Integer.toString(response.getStatus());
if (negated)
{
return (!modifiers.contains(responseCode));
}
else
{
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
{
MethodType logType = methodType(void.class, StringBuilder.class, Request.class, Response.class);
MethodType logTypeArg = methodType(void.class, String.class, StringBuilder.class, Request.class, Response.class);
//TODO should we throw IllegalArgumentExceptions when given arguments for codes which do not take them
MethodHandle specificHandle;
switch (code)
{
case "%":
{
specificHandle = dropArguments(dropArguments(append.bindTo("%"), 1, Request.class), 2, Response.class);
break;
}
case "a":
{
if (arg == null || arg.isEmpty())
arg = "server";
String method;
switch (arg)
{
case "server":
method = "logServerHost";
break;
case "client":
method = "logClientHost";
break;
case "local":
method = "logLocalHost";
break;
case "remote":
method = "logRemoteHost";
break;
default:
throw new IllegalArgumentException("Invalid arg for %a");
}
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "p":
{
if (arg == null || arg.isEmpty())
arg = "server";
String method;
switch (arg)
{
case "server":
method = "logServerPort";
break;
case "client":
method = "logClientPort";
break;
case "local":
method = "logLocalPort";
break;
case "remote":
method = "logRemotePort";
break;
default:
throw new IllegalArgumentException("Invalid arg for %p");
}
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "I":
{
String method;
if (arg == null || arg.isEmpty())
method = "logBytesReceived";
else if (arg.equalsIgnoreCase("clf"))
method = "logBytesReceivedCLF";
else
throw new IllegalArgumentException("Invalid argument for %I");
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "O":
{
String method;
if (arg == null || arg.isEmpty())
method = "logBytesSent";
else if (arg.equalsIgnoreCase("clf"))
method = "logBytesSentCLF";
else
throw new IllegalArgumentException("Invalid argument for %O");
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "S":
{
String method;
if (arg == null || arg.isEmpty())
method = "logBytesTransferred";
else if (arg.equalsIgnoreCase("clf"))
method = "logBytesTransferredCLF";
else
throw new IllegalArgumentException("Invalid argument for %S");
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "C":
{
if (arg == null || arg.isEmpty())
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestCookies", logType);
}
else
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestCookie", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
}
break;
}
case "D":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logLatencyMicroseconds", logType);
break;
}
case "e":
{
if (arg == null || arg.isEmpty())
throw new IllegalArgumentException("No arg for %e");
specificHandle = lookup.findStatic(CustomRequestLog.class, "logEnvironmentVar", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
break;
}
case "f":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logFilename", logType);
break;
}
case "H":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestProtocol", logType);
break;
}
case "i":
{
if (arg == null || arg.isEmpty())
throw new IllegalArgumentException("No arg for %i");
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestHeader", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
break;
}
case "k":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logKeepAliveRequests", logType);
break;
}
case "m":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestMethod", logType);
break;
}
case "o":
{
if (arg == null || arg.isEmpty())
throw new IllegalArgumentException("No arg for %o");
specificHandle = lookup.findStatic(CustomRequestLog.class, "logResponseHeader", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
break;
}
case "q":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logQueryString", logType);
break;
}
case "r":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestFirstLine", logType);
break;
}
case "R":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestHandler", logType);
break;
}
case "s":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logResponseStatus", logType);
break;
}
case "t":
{
String format = DEFAULT_DATE_FORMAT;
TimeZone timeZone = TimeZone.getTimeZone("GMT");
Locale locale = Locale.getDefault();
if (arg != null && !arg.isEmpty())
{
String[] args = arg.split("\\|");
switch (args.length)
{
case 1:
format = args[0];
break;
case 2:
format = args[0];
timeZone = TimeZone.getTimeZone(args[1]);
break;
case 3:
format = args[0];
timeZone = TimeZone.getTimeZone(args[1]);
locale = Locale.forLanguageTag(args[2]);
break;
default:
throw new IllegalArgumentException("Too many \"|\" characters in %t");
}
}
DateCache logDateCache = new DateCache(format, locale, timeZone);
MethodType logTypeDateCache = methodType(void.class, DateCache.class, StringBuilder.class, Request.class, Response.class);
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestTime", logTypeDateCache);
specificHandle = specificHandle.bindTo(logDateCache);
break;
}
case "T":
{
if (arg == null)
arg = "s";
String method;
switch (arg)
{
case "s":
method = "logLatencySeconds";
break;
case "us":
method = "logLatencyMicroseconds";
break;
case "ms":
method = "logLatencyMilliseconds";
break;
default:
throw new IllegalArgumentException("Invalid arg for %T");
}
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "u":
{
String method;
if (arg == null || arg.isEmpty())
method = "logRequestAuthenticationWithDeferred";
else
method = "logRequestAuthentication";
specificHandle = lookup.findStatic(CustomRequestLog.class, method, logType);
break;
}
case "U":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logUrlRequestPath", logType);
break;
}
case "X":
{
specificHandle = lookup.findStatic(CustomRequestLog.class, "logConnectionStatus", logType);
break;
}
case "ti":
{
if (arg == null || arg.isEmpty())
throw new IllegalArgumentException("No arg for %ti");
specificHandle = lookup.findStatic(CustomRequestLog.class, "logRequestTrailer", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
break;
}
case "to":
{
if (arg == null || arg.isEmpty())
throw new IllegalArgumentException("No arg for %to");
specificHandle = lookup.findStatic(CustomRequestLog.class, "logResponseTrailer", logTypeArg);
specificHandle = specificHandle.bindTo(arg);
break;
}
default:
throw new IllegalArgumentException("Unsupported code %" + code);
}
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 log = foldArguments(logHandle, specificHandle);
modifierTest = modifierTest.bindTo(modifiers).bindTo(negated);
return MethodHandles.guardWithTest(modifierTest, log, dash);
}
return foldArguments(logHandle, specificHandle);
}
//-----------------------------------------------------------------------------------//
private static void logNothing(StringBuilder b, Request request, Response response)
{
}
private static void logServerHost(StringBuilder b, Request request, Response response)
{
append(b, request.getServerName());
}
private static void logClientHost(StringBuilder b, Request request, Response response)
{
append(b, request.getRemoteHost());
}
private static void logLocalHost(StringBuilder b, Request request, Response response)
{
append(b, request.getHttpChannel().getEndPoint().getLocalAddress().getAddress().getHostAddress());
}
private static void logRemoteHost(StringBuilder b, Request request, Response response)
{
append(b, request.getHttpChannel().getEndPoint().getRemoteAddress().getAddress().getHostAddress());
}
private static void logServerPort(StringBuilder b, Request request, Response response)
{
b.append(request.getServerPort());
}
private static void logClientPort(StringBuilder b, Request request, Response response)
{
b.append(request.getRemotePort());
}
private static void logLocalPort(StringBuilder b, Request request, Response response)
{
b.append(request.getHttpChannel().getEndPoint().getLocalAddress().getPort());
}
private static void logRemotePort(StringBuilder b, Request request, Response response)
{
b.append(request.getHttpChannel().getEndPoint().getRemoteAddress().getPort());
}
private static void logResponseSize(StringBuilder b, Request request, Response response)
{
long written = response.getHttpChannel().getBytesWritten();
b.append(written);
}
private static void logResponseSizeCLF(StringBuilder b, Request request, Response response)
{
long written = response.getHttpChannel().getBytesWritten();
if (written == 0)
b.append('-');
else
b.append(written);
}
private static void logBytesSent(StringBuilder b, Request request, Response response)
{
b.append(response.getHttpChannel().getBytesWritten());
}
private static void logBytesSentCLF(StringBuilder b, Request request, Response response)
{
long sent = response.getHttpChannel().getBytesWritten();
if (sent == 0)
b.append('-');
else
b.append(sent);
}
private static void logBytesReceived(StringBuilder b, Request request, Response response)
{