aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/HttpResponse.java
blob: f7138ba0e2b02d26f53a3f895252d907da1ef04b (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
// 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;

import com.yahoo.jdisc.HeaderFields;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.servlet.ServletOrJdiscHttpResponse;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * A HTTP response.
 *
 * @author Einar M R Rosenvinge
 */
public class HttpResponse extends Response implements ServletOrJdiscHttpResponse {

    private final HeaderFields trailers = new HeaderFields();
    private boolean chunkedEncodingEnabled = true;
    private String message;

    public interface Status extends Response.Status {
        int REQUEST_ENTITY_TOO_LARGE = REQUEST_TOO_LONG;
        int REQUEST_RANGE_NOT_SATISFIABLE = REQUESTED_RANGE_NOT_SATISFIABLE;
    }

    protected HttpResponse(Request request, int status, String message, Throwable error) {
        super(status, error);
        this.message = message;
    }

    public boolean isChunkedEncodingEnabled() {
        if (headers().contains(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED)) {
            return true;
        }
        if (headers().containsKey(HttpHeaders.Names.CONTENT_LENGTH)) {
            return false;
        }
        return chunkedEncodingEnabled;
    }

    public void setChunkedEncodingEnabled(boolean chunkedEncodingEnabled) {
        this.chunkedEncodingEnabled = chunkedEncodingEnabled;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public void copyHeaders(HeaderFields target) {
        target.addAll(headers());
    }

    public List<Cookie> decodeSetCookieHeader() {
        List<String> cookies = headers().get(HttpHeaders.Names.SET_COOKIE);
        if (cookies == null) {
            return Collections.emptyList();
        }
        List<Cookie> ret = new LinkedList<>();
        for (String cookie : cookies) {
            ret.add(Cookie.fromSetCookieHeader(cookie));
        }
        return ret;
    }

    public void encodeSetCookieHeader(List<Cookie> cookies) {
        headers().remove(HttpHeaders.Names.SET_COOKIE);
        for (Cookie cookie : cookies) {
            headers().add(HttpHeaders.Names.SET_COOKIE, Cookie.toSetCookieHeaders(Arrays.asList(cookie)));
        }
    }

    /**
     * <p>Returns the set of trailer header fields of this HttpResponse. These are typically meta-data that should have
     * been part of {@link #headers()}, but were not available prior to calling {@link
     * ResponseHandler#handleResponse(Response)}. You must NOT WRITE to these headers AFTER calling {@link
     * ContentChannel#close(CompletionHandler)}, and you must NOT READ from these headers BEFORE {@link
     * ContentChannel#close(CompletionHandler)} has been called.</p>
     *
     * <p><b>NOTE:</b> These headers are NOT thread-safe. You need to explicitly synchronized on the returned object to
     * prevent concurrency issues such as ConcurrentModificationExceptions.</p>
     *
     * @return The trailer headers of this HttpRequest.
     */
    public HeaderFields trailers() {
        return trailers;
    }

    public static boolean isServerError(Response response) {
        return (response.getStatus() >= 500) && (response.getStatus() < 600);
    }

    public static HttpResponse newInstance(int status) {
        return new HttpResponse(null, status, null, null);
    }

    public static HttpResponse newInstance(int status, String message) {
        return new HttpResponse(null, status, message, null);
    }

    public static HttpResponse newError(Request request, int status, Throwable error) {
        return new HttpResponse(request, status, formatMessage(error), error);
    }

    public static HttpResponse newInternalServerError(Request request, Throwable error) {
        return new HttpResponse(request, Status.INTERNAL_SERVER_ERROR, formatMessage(error), error);
    }

    private static String formatMessage(Throwable t) {
        String msg = t.getMessage();
        return msg != null ? msg : t.toString();
    }

}