Search in sources :

Example 41 with BufferedSink

use of okio.BufferedSink in project buck by facebook.

the class DefaultDefectReporter method uploadReport.

private DefectSubmitResult uploadReport(final DefectReport defectReport, DefectSubmitResult.Builder defectSubmitResult, ClientSideSlb slb) throws IOException {
    long timeout = rageConfig.getHttpTimeout();
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).build();
    HttpService httpService = new RetryingHttpService(buckEventBus, new LoadBalancedService(slb, httpClient, buckEventBus), rageConfig.getMaxUploadRetries());
    try {
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.addHeader(REQUEST_PROTOCOL_VERSION, rageConfig.getProtocolVersion().name().toLowerCase());
        requestBuilder.post(new RequestBody() {

            @Override
            public MediaType contentType() {
                return MediaType.parse("application/x-www-form-urlencoded");
            }

            @Override
            public void writeTo(BufferedSink bufferedSink) throws IOException {
                writeReport(defectReport, bufferedSink.outputStream());
            }
        });
        HttpResponse response = httpService.makeRequest(rageConfig.getReportUploadPath(), requestBuilder);
        String responseBody;
        try (InputStream inputStream = response.getBody()) {
            responseBody = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
        }
        if (response.statusCode() == HTTP_SUCCESS_CODE) {
            defectSubmitResult.setIsRequestSuccessful(true);
            if (rageConfig.getProtocolVersion().equals(AbstractRageConfig.RageProtocolVersion.SIMPLE)) {
                return defectSubmitResult.setReportSubmitMessage(responseBody).setReportSubmitLocation(responseBody).build();
            } else {
                // Decode Json response.
                RageJsonResponse json = objectMapper.readValue(responseBody.getBytes(Charsets.UTF_8), RageJsonResponse.class);
                return defectSubmitResult.setIsRequestSuccessful(json.getRequestSuccessful()).setReportSubmitErrorMessage(json.getErrorMessage()).setReportSubmitMessage(json.getMessage()).setReportSubmitLocation(json.getRageUrl()).build();
            }
        } else {
            throw new IOException(String.format("Connection to %s returned code %d and message: %s", response.requestUrl(), response.statusCode(), responseBody));
        }
    } catch (IOException e) {
        throw new IOException(String.format("Failed uploading report because [%s].", e.getMessage()));
    } finally {
        httpService.close();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) MediaType(okhttp3.MediaType) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) RequestBody(okhttp3.RequestBody)

Example 42 with BufferedSink

use of okio.BufferedSink in project buck by facebook.

the class ThriftArtifactCache method toOkHttpRequest.

private static Request.Builder toOkHttpRequest(final ThriftArtifactCacheProtocol.Request request) {
    Request.Builder builder = new Request.Builder().addHeader(PROTOCOL_HEADER, PROTOCOL.toString().toLowerCase());
    builder.post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return HYBRID_THRIFT_STREAM_CONTENT_TYPE;
        }

        @Override
        public long contentLength() throws IOException {
            return request.getRequestLengthBytes();
        }

        @Override
        public void writeTo(BufferedSink bufferedSink) throws IOException {
            request.writeAndClose(bufferedSink.outputStream());
        }
    });
    return builder;
}
Also used : BuckCacheStoreRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheStoreRequest) Request(okhttp3.Request) BuckCacheRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheRequest) BuckCacheFetchRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheFetchRequest) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 43 with BufferedSink

use of okio.BufferedSink in project Notes by lguipeng.

the class TAndroidTransport method flush.

@Override
public void flush() throws TTransportException {
    Util.closeQuietly(mResponseBody);
    mResponseBody = null;
    RequestBody requestBody = new RequestBody() {

        @Override
        public MediaType contentType() {
            if (mHeaders != null && mHeaders.containsKey("Content-Type")) {
                return MediaType.parse(mHeaders.get("Content-Type"));
            } else {
                return MEDIA_TYPE_THRIFT;
            }
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            copy(mByteStore.getInputStream(), sink.outputStream());
        }
    };
    try {
        Request.Builder builder = new Request.Builder().url(mUrl).post(requestBody);
        if (mHeaders != null) {
            for (String name : mHeaders.keySet()) {
                builder.header(name, mHeaders.get(name));
            }
        }
        Response response = mHttpClient.newCall(builder.build()).execute();
        if (response.code() != 200) {
            throw new TTransportException("HTTP Response code: " + response.code() + ", message " + response.message());
        }
        mResponseBody = response.body().byteStream();
    } catch (Exception e) {
        throw new TTransportException(e);
    } finally {
        try {
            mByteStore.reset();
        } catch (IOException ignored) {
        }
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) TTransportException(com.evernote.thrift.transport.TTransportException) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException) RequestBody(com.squareup.okhttp.RequestBody)

Example 44 with BufferedSink

use of okio.BufferedSink in project k-9 by k9mail.

the class TestMessage method writeTo.

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));
    bufferedSink.writeUtf8("[message data]");
    bufferedSink.emit();
}
Also used : BufferedSink(okio.BufferedSink)

Example 45 with BufferedSink

use of okio.BufferedSink in project android-rest-client by darko1002001.

the class RequestBodyUtils method create.

public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return mediaType;
        }

        @Override
        public long contentLength() {
            try {
                return inputStream.available();
            } catch (IOException e) {
                return 0;
            }
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            Source source = null;
            try {
                source = Okio.source(inputStream);
                sink.writeAll(source);
            } finally {
                Util.closeQuietly(source);
            }
        }
    };
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) Source(okio.Source) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

BufferedSink (okio.BufferedSink)91 Test (org.junit.Test)31 IOException (java.io.IOException)30 Buffer (okio.Buffer)19 File (java.io.File)15 RequestBody (okhttp3.RequestBody)14 GzipSink (okio.GzipSink)12 Source (okio.Source)11 Response (okhttp3.Response)10 BufferedSource (okio.BufferedSource)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 Socket (java.net.Socket)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3