aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/http/AccessLogUtil.java
blob: c3df181fbcb09655cb9d8c9553a9205b5aeb36a3 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.http;

import com.yahoo.jdisc.http.HttpRequest;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.List;

/**
 * @author bakksjo
 */
public class AccessLogUtil {

    public static String getHttpMethod(final HttpRequest httpRequest) {
        return httpRequest.getMethod().toString();
    }

    public static URI getUri(final HttpRequest httpRequest) {
        return httpRequest.getUri();
    }

    public static String getHttpVersion(final HttpRequest httpRequest) {
        return httpRequest.getVersion().toString();
    }

    public static String getReferrerHeader(final HttpRequest httpRequest) {
        // Yes, the header name is misspelled in the standard
        return getFirstHeaderValue(httpRequest, "Referer");
    }

    public static String getUserAgentHeader(final HttpRequest httpRequest) {
        return getFirstHeaderValue(httpRequest, "User-Agent");
    }

    public static InetSocketAddress getRemoteAddress(final HttpRequest httpRequest) {
        return (InetSocketAddress) httpRequest.getRemoteAddress();
    }

    private static String getFirstHeaderValue(final HttpRequest httpRequest, final String headerName) {
        final List<String> headerValues = httpRequest.headers().get(headerName);
        return (headerValues == null || headerValues.isEmpty()) ? "" : headerValues.get(0);
    }

}