All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.proofpoint.http.client.jetty.JettyResponse Maven / Gradle / Ivy

The newest version!
package com.proofpoint.http.client.jetty;

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.io.CountingInputStream;
import com.proofpoint.http.client.HeaderName;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.HttpFields;

import java.io.InputStream;

import static com.google.common.base.MoreObjects.toStringHelper;

class JettyResponse
        implements com.proofpoint.http.client.Response
{
    private final Response response;
    private final CountingInputStream inputStream;
    private final ListMultimap headers;

    JettyResponse(Response response, InputStream inputStream)
    {
        this.response = response;
        this.inputStream = new CountingInputStream(inputStream);
        this.headers = toHeadersMap(response.getHeaders());
    }

    @Override
    public int getStatusCode()
    {
        return response.getStatus();
    }

    @Override
    public String getStatusMessage()
    {
        return response.getReason();
    }

    @Override
    public ListMultimap getHeaders()
    {
        return headers;
    }

    @Override
    public long getBytesRead()
    {
        return inputStream.getCount();
    }

    @Override
    public InputStream getInputStream()
    {
        return inputStream;
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("statusCode", getStatusCode())
                .add("statusMessage", getStatusMessage())
                .add("headers", getHeaders())
                .toString();
    }

    private static ListMultimap toHeadersMap(HttpFields headers)
    {
        ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
        for (String name : headers.getFieldNamesCollection()) {
            for (String value : headers.getValuesList(name)) {
                builder.put(HeaderName.of(name), value);
            }
        }
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy