Example usage for com.squareup.okhttp Headers values

List of usage examples for com.squareup.okhttp Headers values

Introduction

In this page you can find the example usage for com.squareup.okhttp Headers values.

Prototype

public List<String> values(String name) 

Source Link

Document

Returns an immutable list of the header values for name .

Usage

From source file:at.bitfire.dav4android.exception.HttpException.java

License:Open Source License

public HttpException(Response response) {
    super(response.code() + " " + response.message());

    status = response.code();// w w  w .ja  va  2 s .com
    message = response.message();

    /* As we don't know the media type and character set of request and response body,
       only printable ASCII characters will be shown in clear text. Other octets will
       be shown as "[xx]" where xx is the hex value of the octet.
     */

    // format request
    Request request = response.request();
    StringBuilder formatted = new StringBuilder();
    formatted.append(request.method() + " " + request.urlString() + "\n");
    Headers headers = request.headers();
    for (String name : headers.names())
        for (String value : headers.values(name))
            formatted.append(name + ": " + value + "\n");
    if (request.body() != null)
        try {
            formatted.append("\n");
            Buffer buffer = new Buffer();
            request.body().writeTo(buffer);
            while (!buffer.exhausted())
                appendByte(formatted, buffer.readByte());
        } catch (IOException e) {
        }
    this.request = formatted.toString();

    // format response
    formatted = new StringBuilder();
    formatted.append(response.protocol() + " " + response.code() + " " + response.message() + "\n");
    headers = response.headers();
    for (String name : headers.names())
        for (String value : headers.values(name))
            formatted.append(name + ": " + value + "\n");
    if (response.body() != null)
        try {
            formatted.append("\n");
            for (byte b : response.body().bytes())
                appendByte(formatted, b);
        } catch (IOException e) {
        }
    this.response = formatted.toString();
}

From source file:com.cdancy.artifactory.rest.config.ArtifactoryOkHttpCommandExecutorService.java

License:Apache License

@Override
protected HttpResponse invoke(Request nativeRequest) throws IOException, InterruptedException {

    OkHttpClient requestScopedClient = clientSupplier.get();
    requestScopedClient.setProxy(proxyForURI.apply(nativeRequest.uri()));
    Response response = requestScopedClient.newCall(nativeRequest).execute();

    HttpResponse.Builder<?> builder = HttpResponse.builder();
    builder.statusCode(response.code());
    builder.message(response.message());

    Builder<String, String> headerBuilder = ImmutableMultimap.builder();
    Headers responseHeaders = response.headers();

    // Check for Artifactory header and init potential file for downstream use
    File destinationFile = null;/*from w  w w.ja  v  a  2 s. co m*/
    String artFileName = responseHeaders.get("X-Artifactory-Filename");
    if (artFileName != null) {

        GAVCoordinates gavCoordinates = ArtifactoryUtils.gavFromURL(nativeRequest.url(),
                endpoint.get().toURL());
        destinationFile = ArtifactoryUtils.getGradleFile(gavCoordinates, artFileName,
                responseHeaders.get("ETag"));
        headerBuilder.put(ArtifactoryUtils.LOCATION_HEADER, destinationFile.getAbsolutePath());
    }

    for (String header : responseHeaders.names()) {
        headerBuilder.putAll(header, responseHeaders.values(header));
    }

    ImmutableMultimap<String, String> headers = headerBuilder.build();

    if (response.code() == 204 && response.body() != null) {
        response.body().close();
    } else {
        if (destinationFile != null) {
            if (!destinationFile.exists() || (destinationFile.length() != response.body().contentLength())) {
                InputStream inputStream = null;
                try {
                    inputStream = response.body().byteStream();
                    ArtifactoryUtils.resolveInputStream(inputStream, destinationFile);
                } catch (Exception e) {
                    Throwables.propagate(e);
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            }
            IOUtils.closeQuietly(response.body().byteStream());
        } else {
            Payload payload = newInputStreamPayload(response.body().byteStream());
            contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
            builder.payload(payload);
        }
    }

    builder.headers(filterOutContentHeaders(headers));
    return builder.build();
}

From source file:com.yandex.disk.rest.LoggingInterceptor.java

License:Apache License

private void logHeaders(String prefix, Headers headers) {
    for (String name : headers.names()) {
        List<String> values = headers.values(name);
        for (String value : values) {
            logger.info(prefix + name + ": " + value);
        }/*from   w  w  w . ja v  a2s  .c o  m*/
    }
}

From source file:feign.okhttp.OkHttpClient.java

License:Apache License

private static Map<String, Collection<String>> toMap(Headers headers) {
    Map<String, Collection<String>> result = new LinkedHashMap<String, Collection<String>>(headers.size());
    for (String name : headers.names()) {
        // TODO: this is very inefficient as headers.values iterate case insensitively.
        result.put(name, headers.values(name));
    }/*from w w w . jav a2s. c om*/
    return result;
}

From source file:io.takari.aether.connector.test.mockwebserver.AetherMockWebserverConnectorTest.java

License:Open Source License

private void assertContains(Headers headers, String header, String value) {
    assertTrue(headers.toString(), headers.values(header).contains(value));
}

From source file:io.takari.aether.connector.test.mockwebserver.AetherMockWebserverConnectorTest.java

License:Open Source License

private void assertContainsNoneMatching(Headers headers, String header) {
    assertTrue(headers.values(header).isEmpty());
}

From source file:org.jclouds.http.okhttp.OkHttpCommandExecutorService.java

License:Apache License

@Override
protected HttpResponse invoke(Request nativeRequest) throws IOException, InterruptedException {
    OkHttpClient requestScopedClient = globalClient.clone();
    requestScopedClient.setProxy(proxyForURI.apply(nativeRequest.uri()));

    Response response = requestScopedClient.newCall(nativeRequest).execute();

    HttpResponse.Builder<?> builder = HttpResponse.builder();
    builder.statusCode(response.code());
    builder.message(response.message());

    Builder<String, String> headerBuilder = ImmutableMultimap.builder();
    Headers responseHeaders = response.headers();
    for (String header : responseHeaders.names()) {
        headerBuilder.putAll(header, responseHeaders.values(header));
    }// ww  w .  j a va  2s .c om

    ImmutableMultimap<String, String> headers = headerBuilder.build();

    if (response.code() == 204 && response.body() != null) {
        response.body().close();
    } else {
        Payload payload = newInputStreamPayload(response.body().byteStream());
        contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
        builder.payload(payload);
    }

    builder.headers(filterOutContentHeaders(headers));

    return builder.build();
}

From source file:twitter4j.OkHttpResponse.java

License:Apache License

public OkHttpResponse(Response response, HttpClientConfiguration conf) throws IOException {
    super(conf);/*  ww w  . j a  va2s .com*/
    this.response = response;

    Headers headers = response.headers();
    Set<String> names = headers.names();
    HashMap<String, List<String>> headerFields = new HashMap<String, List<String>>();
    for (String name : names) {
        headerFields.put(name, headers.values(name));
    }
    this.headerFields = headerFields;

    is = response.body().byteStream();
    if (is != null && "gzip".equals(response.header("Content-Encoding"))) {
        is = new StreamingGZIPInputStream(is);
    }

    statusCode = response.code();
}