summaryrefslogtreecommitdiffstats
path: root/application/src/main/java/com/yahoo/application/container/handler/Request.java
blob: fef0f275be1bf566380c60a83d1030cd761d1dde (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container.handler;

import com.google.common.annotations.Beta;
import net.jcip.annotations.Immutable;

import java.nio.charset.StandardCharsets;

/**
 * A request for use with {@link com.yahoo.application.container.JDisc#handleRequest(Request)}.
 *
 * @author Einar M R Rosenvinge
 * @see Response
 */
@Immutable
@Beta
public class Request {

    private final Headers headers = new Headers();
    private final String uri;
    private final byte[] body;
    private final Method method;

    /**
     * Creates a Request with an empty body.
     *
     * @param uri the URI of the request
     */
    public Request(String uri) {
        this(uri, new byte[0]);
    }

    /**
     * Creates a GET Request with a message body.
     *
     * @param uri the URI of the request
     * @param body the body of the request
     */
    public Request(String uri, byte[] body) {
        this(uri, body, Method.GET);
    }

    /**
     * Creates a GET Request with a message body.
     *
     * @param uri the URI of the request
     * @param body the body of the request as a UTF-8 string
     */
    public Request(String uri, String body) {
        this(uri, body.getBytes(StandardCharsets.UTF_8), Method.GET);
    }

    /**
     * Creates a Request with a message body.
     *
     * @param uri the URI of the request
     * @param body the body of the request
     */
    public Request(String uri, byte[] body, Method method) {
        this.uri = uri;
        this.body = body;
        this.method = method;
    }

    /**
     * Creates a Request with a message body.
     *
     * @param uri the URI of the request
     * @param body the body of the request as a UTF-8 string
     */
    public Request(String uri, String body, Method method) {
        this(uri, body.getBytes(StandardCharsets.UTF_8), method);
    }
    /**
     * Returns a mutable multi-map of headers for this Request.
     *
     * @return a mutable multi-map of headers for this Request
     */
    public Headers getHeaders() {
        return headers;
    }

    /**
     * Returns the body of this Request.
     *
     * @return the body of this Request
     */
    public byte[] getBody() {
        return body;
    }

    /**
     * Returns the URI of this Request.
     *
     * @return the URI of this Request
     */
    public String getUri() {
        return uri;
    }

    @Override
    public String toString() {
        String bodyStr = (body == null || body.length == 0) ? "[empty]" : "[omitted]";
        return "Request to " + uri + ", headers: " + headers + ", body: " + bodyStr;
    }

    public Method getMethod() {
        return method;
    }

    public enum Method {
        OPTIONS,
        GET,
        HEAD,
        POST,
        PUT,
        PATCH,
        DELETE,
        TRACE,
        CONNECT
    }

}