aboutsummaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/main/java/com/yahoo/container/logging/AccessLogEntry.java
blob: 452466d7d05509c54dffd28863f3adf5089a5c10 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

import com.yahoo.collections.ListMap;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;

import javax.security.auth.x500.X500Principal;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

/**
 * <p>Information to be logged in the access log.</p>
 *
 * <p>This class contains the union of all information that can be
 * logged with all the supported access log formats.</p>
 *
 * <p>The add methods can be called multiple times,
 * but the parameters should be different for each
 * invocation of the same method.</p>
 *
 * This class is thread-safe, but the inner class {@link AdInfo} is not.
 *
 * @author Tony Vaagenes
 * @author bakksjo
 * @author bjorncs
 */
public class AccessLogEntry {
    public enum CookieType {
        b,
        l,
        n,
        geocookie,
        I,
        R,
        Y,
        M;
    }

    // Sadly, there's no way to do compile-time validation of these field references.
    private static final String[] FIELDS_EXCLUDED_FROM_TOSTRING = new String[] {
            "monitor"
    };

    private final Object monitor = new Object();

    private List<AdInfo> adInfos;
    private String spaceID;

    private String ipV4AddressInDotDecimalNotation;
    private long timeStampMillis;
    private long durationBetweenRequestResponseMillis;
    private long numBytesReturned;
    private URI uri;


    private String remoteAddress;
    private int remotePort;
    private String peerAddress;
    private int peerPort;

    private CookieType cookieType;
    private String cookie;
    private String weekOfRegistration;
    private String profile;
    private String internationalInfo;
    private String contentAttribute;
    private String webfactsDigitalSignature;
    private String errorMessage;
    private String fileName;
    private String userAgent;
    private String referer;
    private String user;
    private HitCounts hitCounts;
    private String requestExtra;
    private String responseExtra;
    private Boolean resultFromCache;
    private String httpMethod;
    private String httpVersion;
    private String partner;
    private String adRationale;
    private String incrementSlotByOneRequest;
    private String zDataIncrementSlotByOneRequest;
    private String hostString;
    private int statusCode;
    private String scheme;
    private int localPort;
    private Principal principal;
    private X500Principal sslPrincipal;
    private String rawPath;
    private String rawQuery;

    private ListMap<String,String> keyValues=null;

    public void setCookie( CookieType type, String cookie) {
        synchronized (monitor) {
            requireNull(this.cookieType);
            requireNull(this.cookie);
            this.cookieType = type;
            this.cookie = cookie;
        }
    }

    public CookieType getCookieType() {
        synchronized (monitor) {
            return cookieType;
        }
    }

    public String getCookie() {
        synchronized (monitor) {
            return cookie;
        }
    }

    public void setWeekOfRegistration( String weekOfRegistration ) {
        synchronized (monitor) {
            requireNull(this.weekOfRegistration);
            this.weekOfRegistration = weekOfRegistration;
        }
    }

    public String getWeekOfRegistration() {
        synchronized (monitor) {
            return weekOfRegistration;
        }
    }

    public void setProfile( String profile ) {
        synchronized (monitor) {
            requireNull(this.profile);
            this.profile = profile;
        }
    }

    public String getProfile() {
        synchronized (monitor) {
            return profile;
        }
    }

    public void setInternationalInfo( String intl ) {
        synchronized (monitor) {
            requireNull(this.internationalInfo);
            this.internationalInfo = intl;
        }
    }

    public String getInternationalInfo() {
        synchronized (monitor) {
            return internationalInfo;
        }
    }

    public void setContentAttribute( String contentAttribute ) {
        synchronized (monitor) {
            requireNull(this.contentAttribute);
            this.contentAttribute = contentAttribute;
        }
    }

    public String getContentAttribute() {
        synchronized (monitor) {
            return contentAttribute;
        }
    }

    public void setAdSpaceID(String spaceID) {
        synchronized (monitor) {
            requireNull(this.spaceID);
            this.spaceID = spaceID;
        }
    }

    public String getAdSpaceID() {
        synchronized (monitor) {
            return spaceID;
        }
    }

    public void addAdInfo(AdInfo adInfo) {
        synchronized (monitor) {
            if (adInfos == null) {
                adInfos = new ArrayList<>();
            }
            adInfos.add( adInfo );
        }
    }

    public List<AdInfo> getAdInfos() {
        synchronized (monitor) {
            if (adInfos == null) {
                return Collections.emptyList();
            }
            // TODO: The returned list is unmodifiable, but its elements are not. But we're all friendly here, right?
            return Collections.unmodifiableList(adInfos);
        }
    }

    /**
     * This class is NOT thread-safe. It is assumed that a single instance is created/written by a single thread,
     * and all reads happen-after creation/population, i.e. no mutation after the instance is shared between threads.
     */
    public static class AdInfo {

        private String adServerString;
        private String adId;
        private String matchId;
        private String position;
        private String property;
        private String cpc;
        private String adClientVersion;
        private String linkId;
        private String bidPosition;

        public void setAdID(String id) {
            this.adId = id;
        }

        public String getAdID() {
            return adId;
        }

        public void setMatchID(String id) {
            this.matchId = id;
        }

        public String getMatchID() {
            return matchId;
        }

        public void setPosition(String position) {
            this.position = position;
        }

        public String getPosition() {
            return position;
        }

        public void setProperty(String property) {
            this.property = property;
        }

        public String getProperty() {
            return property;
        }

        public void setCPC(String cpc) {
            this.cpc = cpc;
        }

        public String getCPC() {
            return cpc;
        }

        public void setAdClientVersion(String adClientVersion) {
            this.adClientVersion = adClientVersion;
        }

        public String getAdClientVersion() {
            return adClientVersion;
        }

        public void setLinkID(String id) {
            this.linkId = id;
        }

        public String getLinkID() {
            return linkId;
        }

        public void setBidPosition(String bidPosition) {
            this.bidPosition = bidPosition;
        }

        public String getBidPosition() {
            return bidPosition;
        }

        public AdInfo() {}

        AdInfo(String adServerString) {
            this.adServerString = adServerString;
        }

        String getAdServerString() {
            return adServerString;
        }
    }

    public void setWebfactsDigitalSignature(String signature) {
        synchronized (monitor) {
            requireNull(this.webfactsDigitalSignature);
            this.webfactsDigitalSignature = signature;
        }
    }

    public String getWebfactsDigitalSignature() {
        synchronized (monitor) {
            return webfactsDigitalSignature;
        }
    }

    public void setErrorMessage(String errorMessage) {
        synchronized (monitor) {
            requireNull(this.errorMessage);
            this.errorMessage = errorMessage;
        }
    }

    public String getErrorMessage() {
        synchronized (monitor) {
            return errorMessage;
        }
    }

    public void setFileName(String fileName) {
        synchronized (monitor) {
            requireNull(this.fileName);
            this.fileName = fileName;
        }
    }

    public String getFileName() {
        synchronized (monitor) {
            return fileName;
        }
    }

    public void setUserAgent(String userAgent) {
        synchronized (monitor) {
            requireNull(this.userAgent);
            this.userAgent = userAgent;
        }
    }

    public String getUserAgent() {
        synchronized (monitor) {
            return userAgent;
        }
    }

    public void setReferer(String referer) {
        synchronized (monitor) {
            requireNull(this.referer);
            this.referer = referer;
        }
    }

    public String getReferer() {
        synchronized (monitor) {
            return referer;
        }
    }

    public void setUser(final String user) {
        synchronized (monitor) {
            requireNull(this.user);
            this.user = user;
        }
    }

    public String getUser() {
        synchronized (monitor) {
            return user;
        }
    }

    public void setHitCounts(final HitCounts hitCounts) {
        synchronized (monitor) {
            requireNull(this.hitCounts);
            this.hitCounts = hitCounts;
        }
    }

    public HitCounts getHitCounts() {
        synchronized (monitor) {
            return hitCounts;
        }
    }

    public String getRequestExtra() {
        synchronized (monitor) {
            return requestExtra;
        }
    }

    public String getResponseExtra() {
        synchronized (monitor) {
            return responseExtra;
        }
    }

    public void addKeyValue(String key,String value) {
        synchronized (monitor) {
            if (keyValues == null) {
                keyValues = new ListMap<>();
            }
            keyValues.put(key,value);
        }
    }

    public Map<String, List<String>> getKeyValues() {
        synchronized (monitor) {
            if (keyValues == null) {
                return null;
            }

            final Map<String, List<String>> newMapWithImmutableValues = mapValues(
                    keyValues.entrySet(),
                    valueList -> Collections.unmodifiableList(new ArrayList<>(valueList)));
            return Collections.unmodifiableMap(newMapWithImmutableValues);
        }
    }

    private static <K, V1, V2> Map<K, V2> mapValues(
            final Set<Map.Entry<K, V1>> entrySet,
            final Function<V1, V2> valueConverter) {
        return entrySet.stream()
                .collect(toMap(
                        entry -> entry.getKey(),
                        entry -> valueConverter.apply(entry.getValue())));
    }

    public void setResultFromCache(boolean fromCache) {
        synchronized (monitor) {
            requireNull(this.resultFromCache);
            this.resultFromCache = fromCache;
        }
    }

    public Boolean getResultFromCache() {
        synchronized (monitor) {
            return resultFromCache;
        }
    }

    public enum HttpMethod {
        GET, POST;
    }

    public void setHttpMethod(HttpMethod method) {
        setHttpMethod(method.toString());
    }

    public void setHttpMethod(String method) {
        synchronized (monitor) {
            requireNull(this.httpMethod);
            this.httpMethod = method;
        }
    }

    public String getHttpMethod() {
        synchronized (monitor) {
            return httpMethod;
        }
    }

    public void setHttpVersion(final String httpVersion) {
        synchronized (monitor) {
            requireNull(this.httpVersion);
            this.httpVersion = httpVersion;
        }
    }

    public String getHttpVersion() {
        synchronized (monitor) {
            return httpVersion;
        }
    }

    public void setPartner(String partner) {
        synchronized (monitor) {
            requireNull(this.partner);
            this.partner = partner;
        }
    }

    public String getPartner() {
        synchronized (monitor) {
            return partner;
        }
    }

    public void setAdRationale(String adRationale) {
        synchronized (monitor) {
            requireNull(this.adRationale);
            this.adRationale = adRationale;
        }
    }

    public String getAdRationale() {
        synchronized (monitor) {
            return adRationale;
        }
    }

    public void setIncrementSlotByOneRequest(String slotName) {
        synchronized (monitor) {
            requireNull(this.incrementSlotByOneRequest);
            this.incrementSlotByOneRequest = slotName;
        }
    }

    public String getIncrementSlotByOneRequest() {
        synchronized (monitor) {
            return incrementSlotByOneRequest;
        }
    }

    public void setZDataIncrementSlotByOneRequest(String slotName) {
        synchronized (monitor) {
            requireNull(this.zDataIncrementSlotByOneRequest);
            this.zDataIncrementSlotByOneRequest = slotName;
        }
    }

    public String getZDataIncrementSlotByOneRequest() {
        synchronized (monitor) {
            return zDataIncrementSlotByOneRequest;
        }
    }

    public void setHostString(String hostString) {
        synchronized (monitor) {
            requireNull(this.hostString);
            this.hostString = hostString;
        }
    }

    public String getHostString() {
        synchronized (monitor) {
            return hostString;
        }
    }

    public void setIpV4Address(String ipV4AddressInDotDecimalNotation) {
        synchronized (monitor) {
            requireNull(this.ipV4AddressInDotDecimalNotation);
            this.ipV4AddressInDotDecimalNotation = ipV4AddressInDotDecimalNotation;
        }
    }

    public String getIpV4Address() {
        synchronized (monitor) {
            return ipV4AddressInDotDecimalNotation;
        }
    }

    public void setTimeStamp(long numMillisSince1Jan1970AtMidnightUTC) {
        synchronized (monitor) {
            requireZero(this.timeStampMillis);
            timeStampMillis = numMillisSince1Jan1970AtMidnightUTC;
        }
    }

    public long getTimeStampMillis() {
        synchronized (monitor) {
            return timeStampMillis;
        }
    }

    public void setDurationBetweenRequestResponse(long timeInMillis) {
        synchronized (monitor) {
            requireZero(this.durationBetweenRequestResponseMillis);
            durationBetweenRequestResponseMillis = timeInMillis;
        }
    }

    public long getDurationBetweenRequestResponseMillis() {
        synchronized (monitor) {
            return durationBetweenRequestResponseMillis;
        }
    }

    public void setReturnedContentSize(int byteCount) {
        setReturnedContentSize((long) byteCount);
    }

    public void setReturnedContentSize(long byteCount) {
        synchronized (monitor) {
            requireZero(this.numBytesReturned);
            numBytesReturned = byteCount;
        }
    }

    public long getReturnedContentSize() {
        synchronized (monitor) {
            return numBytesReturned;
        }
    }

    /**
     * @deprecated Use {@link #setRawPath(String)} and {@link #setRawQuery(String)} instead.
     */
    @Deprecated
    public void setURI(final URI uri) {
        synchronized (monitor) {
            requireNull(this.uri);
            this.uri = uri;
        }
    }

    /**
     * @deprecated Use {@link #getRawPath()} and {@link #getRawQuery()} instead. This method may return wrong path.
     */
    @Deprecated
    public URI getURI() {
        synchronized (monitor) {
            return uri;
        }
    }

    public void setRemoteAddress(String remoteAddress) {
        synchronized (monitor) {
            requireNull(this.remoteAddress);
            this.remoteAddress = remoteAddress;
        }
    }

    public void setRemoteAddress(final InetSocketAddress remoteAddress) {
        setRemoteAddress(getIpAddressAsString(remoteAddress));
    }

    private static String getIpAddressAsString(final InetSocketAddress remoteAddress) {
        final InetAddress inetAddress = remoteAddress.getAddress();
        if (inetAddress == null) {
            return null;
        }
        return inetAddress.getHostAddress();
    }

    public String getRemoteAddress() {
        synchronized (monitor) {
            return remoteAddress;
        }
    }

    public void setRemotePort(int remotePort) {
        synchronized (monitor) {
            requireZero(this.remotePort);
            this.remotePort = remotePort;
        }
    }

    public int getRemotePort() {
        synchronized (monitor) {
            return remotePort;
        }
    }

    public void setPeerAddress(final String peerAddress) {
        synchronized (monitor) {
            requireNull(this.peerAddress);
            this.peerAddress = peerAddress;
        }
    }

    public void setPeerPort(int peerPort) {
        synchronized (monitor) {
            requireZero(this.peerPort);
            this.peerPort = peerPort;
        }
    }

    public int getPeerPort() {
        synchronized (monitor) {
            return peerPort;
        }
    }

    public String getPeerAddress() {
        synchronized (monitor) {
            return peerAddress;
        }
    }

    public void setStatusCode(int statusCode) {
        synchronized (monitor) {
            requireZero(this.statusCode);
            this.statusCode = statusCode;
        }
    }

    public int getStatusCode() {
        synchronized (monitor) {
            return statusCode;
        }
    }

    public String getScheme() {
        synchronized (monitor) {
            return scheme;
        }
    }

    public void setScheme(String scheme) {
        synchronized (monitor) {
            requireNull(this.scheme);
            this.scheme = scheme;
        }
    }

    public int getLocalPort() {
        synchronized (monitor) {
            return localPort;
        }
    }

    public void setLocalPort(int localPort) {
        synchronized (monitor) {
            requireZero(this.localPort);
            this.localPort = localPort;
        }
    }

    public Principal getUserPrincipal() {
        synchronized (monitor) {
            return principal;
        }
    }

    public void setUserPrincipal(Principal principal) {
        synchronized (monitor) {
            requireNull(this.principal);
            this.principal = principal;
        }
    }

    public Principal getSslPrincipal() {
        synchronized (monitor) {
            return sslPrincipal;
        }
    }

    public void setSslPrincipal(X500Principal sslPrincipal) {
        synchronized (monitor) {
            requireNull(this.sslPrincipal);
            this.sslPrincipal = sslPrincipal;
        }
    }

    public void setRawPath(String rawPath) {
        synchronized (monitor) {
            requireNull(this.rawPath);
            this.rawPath = rawPath;
        }
    }

    public String getRawPath() {
        synchronized (monitor) {
            return rawPath;
        }
    }

    public void setRawQuery(String rawQuery) {
        synchronized (monitor) {
            requireNull(this.rawQuery);
            this.rawQuery = rawQuery;
        }
    }

    public Optional<String> getRawQuery() {
        synchronized (monitor) {
            return Optional.ofNullable(rawQuery);
        }
    }

    @Override
    public String toString() {
        synchronized (monitor) {
            return new ReflectionToStringBuilder(this)
                    .setExcludeFieldNames(FIELDS_EXCLUDED_FROM_TOSTRING)
                    .toString();
        }
    }

    private static void requireNull(final Object value) {
        if (value != null) {
            throw new IllegalStateException("Attempt to overwrite field that has been assigned. Value: " + value);
        }
    }

    private static void requireZero(final long value) {
        if (value != 0) {
            throw new IllegalStateException("Attempt to overwrite field that has been assigned. Value: " + value);
        }
    }

    private static void requireZero(final int value) {
        if (value != 0) {
            throw new IllegalStateException("Attempt to overwrite field that has been assigned. Value: " + value);
        }
    }

}