aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/ProxyResponse.java
blob: 16f1ac3eb07c6fe2fe9d701fa37d4e09046cbaa7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import com.yahoo.container.jdisc.HttpResponse;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.NameValuePair;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;

/**
 * Proxies response back to client, keeps Content-Type header if it is present
 *
 * @author olaa
 * @author freva
 */
class ProxyResponse extends HttpResponse {

    private final CloseableHttpResponse clientResponse;

    ProxyResponse(CloseableHttpResponse clientResponse) {
        super(clientResponse.getCode());
        this.clientResponse = clientResponse;
    }

    @Override
    public String getContentType() {
        return Optional.ofNullable(clientResponse.getFirstHeader("Content-Type"))
                .map(NameValuePair::getValue)
                .orElseGet(super::getContentType);
    }

    @Override
    public void render(OutputStream outputStream) throws IOException {
        try (clientResponse) {
            if (clientResponse.getEntity() != null)
                clientResponse.getEntity().writeTo(outputStream);
        }
    }

    @Override
    public long maxPendingBytes() {
        return 1 << 25; // 32MB
    }

}