Example usage for com.squareup.okhttp Headers newBuilder

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

Introduction

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

Prototype

public Builder newBuilder() 

Source Link

Usage

From source file:com.facebook.react.modules.network.NetworkingModule.java

License:Open Source License

private @Nullable MultipartBuilder constructMultipartBody(ReadableArray body, String contentType,
        Callback callback) {//from ww  w. java 2 s  .  c o  m
    MultipartBuilder multipartBuilder = new MultipartBuilder();
    multipartBuilder.type(MediaType.parse(contentType));

    for (int i = 0, size = body.size(); i < size; i++) {
        ReadableMap bodyPart = body.getMap(i);

        // Determine part's content type.
        ReadableArray headersArray = bodyPart.getArray("headers");
        Headers headers = extractHeaders(headersArray, null);
        if (headers == null) {
            callback.invoke(0, null, "Missing or invalid header format for FormData part.");
            return null;
        }
        MediaType partContentType = null;
        String partContentTypeStr = headers.get(CONTENT_TYPE_HEADER_NAME);
        if (partContentTypeStr != null) {
            partContentType = MediaType.parse(partContentTypeStr);
            // Remove the content-type header because MultipartBuilder gets it explicitly as an
            // argument and doesn't expect it in the headers array.
            headers = headers.newBuilder().removeAll(CONTENT_TYPE_HEADER_NAME).build();
        }

        if (bodyPart.hasKey(REQUEST_BODY_KEY_STRING)) {
            String bodyValue = bodyPart.getString(REQUEST_BODY_KEY_STRING);
            multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue));
        } else if (bodyPart.hasKey(REQUEST_BODY_KEY_URI)) {
            if (partContentType == null) {
                callback.invoke(0, null, "Binary FormData part needs a content-type header.");
                return null;
            }
            String fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI);
            InputStream fileInputStream = RequestBodyUtil.getFileInputStream(getReactApplicationContext(),
                    fileContentUriStr);
            if (fileInputStream == null) {
                callback.invoke(0, null, "Could not retrieve file for uri " + fileContentUriStr);
                return null;
            }
            multipartBuilder.addPart(headers, RequestBodyUtil.create(partContentType, fileInputStream));
        } else {
            callback.invoke(0, null, "Unrecognized FormData part.");
        }
    }
    return multipartBuilder;
}

From source file:com.microsoft.windowsazure.mobileservices.sdk.testapp.framework.filters.ServiceFilterRequestMock.java

License:Open Source License

@Override
public void addHeader(String name, String val) {
    Headers currentHeaders = this.responseToUse.getHeaders();

    Headers addedHeaders = currentHeaders.newBuilder().add(name, val).build();

    ((ServiceFilterResponseMock) this.responseToUse).setHeaders(addedHeaders);
}

From source file:io.apiman.gateway.platforms.servlet.connectors.ok.HttpURLConnectionImpl.java

License:Apache License

private Headers getHeaders() throws IOException {
    if (responseHeaders == null) {
        Response response = getResponse().getResponse();
        Headers headers = response.headers();

        responseHeaders = headers.newBuilder()
                .add(Platform.get().getPrefix() + "-Response-Source", responseSourceHeader(response)).build();
    }/* w  w w.  ja  va  2s .com*/
    return responseHeaders;
}