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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;
import com.google.common.base.Preconditions;
import com.yahoo.container.logging.AccessLogEntry;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.HttpRequest;
import java.util.Map;
import java.util.Optional;
/**
* A wrapper RequestHandler that enables access logging. By wrapping the request handler, we are able to wrap the
* response handler as well. Hence, we can populate the access log entry with information from both the request
* and the response. This wrapper also adds the access log entry to the request context, so that request handlers
* may add information to it.
*
* Does not otherwise interfere with the request processing of the delegate request handler.
*
* @author bakksjo
* $Id$
*/
public class AccessLoggingRequestHandler extends AbstractRequestHandler {
public static final String CONTEXT_KEY_ACCESS_LOG_ENTRY
= AccessLoggingRequestHandler.class.getName() + "_access-log-entry";
public static Optional<AccessLogEntry> getAccessLogEntry(final HttpRequest jdiscRequest) {
final Map<String, Object> requestContextMap = jdiscRequest.context();
return getAccessLogEntry(requestContextMap);
}
public static Optional<AccessLogEntry> getAccessLogEntry(final Map<String, Object> requestContextMap) {
return Optional.ofNullable(
(AccessLogEntry) requestContextMap.get(CONTEXT_KEY_ACCESS_LOG_ENTRY));
}
private final RequestHandler delegate;
private final AccessLogEntry accessLogEntry;
public AccessLoggingRequestHandler(
final RequestHandler delegateRequestHandler,
final AccessLogEntry accessLogEntry) {
this.delegate = delegateRequestHandler;
this.accessLogEntry = accessLogEntry;
}
@Override
public ContentChannel handleRequest(final Request request, final ResponseHandler handler) {
Preconditions.checkArgument(request instanceof HttpRequest, "Expected HttpRequest, got " + request);
final HttpRequest httpRequest = (HttpRequest) request;
httpRequest.context().put(CONTEXT_KEY_ACCESS_LOG_ENTRY, accessLogEntry);
final ResponseHandler accessLoggingResponseHandler = new AccessLoggingResponseHandler(handler, accessLogEntry);
final ContentChannel requestContentChannel = delegate.handleRequest(request, accessLoggingResponseHandler);
return requestContentChannel;
}
private static class AccessLoggingResponseHandler implements ResponseHandler {
private final ResponseHandler delegateHandler;
private final AccessLogEntry accessLogEntry;
public AccessLoggingResponseHandler(
final ResponseHandler delegateHandler,
final AccessLogEntry accessLogEntry) {
this.delegateHandler = delegateHandler;
this.accessLogEntry = accessLogEntry;
}
@Override
public ContentChannel handleResponse(Response response) {
return delegateHandler.handleResponse(response);
}
}
}
|