aboutsummaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/main/java/com/yahoo/container/logging/AccessLogEntry.java
blob: b943c03f48f98845e8416051fca3e5ba0ab7f79a (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
// 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 javax.security.auth.x500.X500Principal;
import java.net.InetAddress;
import java.net.InetSocketAddress;
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.
 *
 * @author Tony Vaagenes
 * @author bakksjo
 * @author bjorncs
 */
public class AccessLogEntry {

    // 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 String ipV4AddressInDotDecimalNotation;
    private long timeStampMillis;
    private long durationBetweenRequestResponseMillis;
    private long numBytesReturned;

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

    private String profile;
    private String errorMessage;
    private String fileName;
    private String userAgent;
    private String referer;
    private String user;
    private HitCounts hitCounts;
    private String httpMethod;
    private String httpVersion;
    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 setProfile( String profile ) {
        synchronized (monitor) {
            requireNull(this.profile);
            this.profile = profile;
        }
    }

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

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

    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 "AccessLogEntry{" +
                    "ipV4AddressInDotDecimalNotation='" + ipV4AddressInDotDecimalNotation + '\'' +
                    ", timeStampMillis=" + timeStampMillis +
                    ", durationBetweenRequestResponseMillis=" + durationBetweenRequestResponseMillis +
                    ", numBytesReturned=" + numBytesReturned +
                    ", remoteAddress='" + remoteAddress + '\'' +
                    ", remotePort=" + remotePort +
                    ", peerAddress='" + peerAddress + '\'' +
                    ", peerPort=" + peerPort +
                    ", profile='" + profile + '\'' +
                    ", errorMessage='" + errorMessage + '\'' +
                    ", fileName='" + fileName + '\'' +
                    ", userAgent='" + userAgent + '\'' +
                    ", referer='" + referer + '\'' +
                    ", user='" + user + '\'' +
                    ", hitCounts=" + hitCounts +
                    ", httpMethod='" + httpMethod + '\'' +
                    ", httpVersion='" + httpVersion + '\'' +
                    ", hostString='" + hostString + '\'' +
                    ", statusCode=" + statusCode +
                    ", scheme='" + scheme + '\'' +
                    ", localPort=" + localPort +
                    ", principal=" + principal +
                    ", sslPrincipal=" + sslPrincipal +
                    ", rawPath='" + rawPath + '\'' +
                    ", rawQuery='" + rawQuery + '\'' +
                    ", keyValues=" + keyValues +
                    '}';
        }
    }

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

}