Search in sources :

Example 1 with TTransportException

use of com.evernote.thrift.transport.TTransportException 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 2 with TTransportException

use of com.evernote.thrift.transport.TTransportException in project Notes by lguipeng.

the class TEvernoteHttpClient method flush.

@Deprecated
public void flush() throws TTransportException {
    long timer = System.currentTimeMillis();
    HttpEntity httpEntity;
    // Extract request and reset buffer
    try {
        // Prepare http post request
        HttpPost request = new HttpPost(url.toExternalForm());
        this.request = request;
        request.addHeader("Content-Type", "application/x-thrift");
        request.addHeader("Cache-Control", "no-transform");
        if (customHeaders != null) {
            for (Map.Entry<String, String> header : customHeaders.entrySet()) {
                request.addHeader(header.getKey(), header.getValue());
            }
        }
        InputStreamEntity entity = new InputStreamEntity(requestBuffer.getInputStream(), requestBuffer.getBytesWritten());
        request.setEntity(entity);
        request.addHeader("Accept", "application/x-thrift");
        request.addHeader("User-Agent", userAgent == null ? "Java/THttpClient" : userAgent);
        request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        DefaultHttpClient dHTTP = getHTTPClient();
        //noinspection ConstantConditions
        HttpResponse response = dHTTP.execute(request);
        httpEntity = response.getEntity();
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode != 200) {
            if (httpEntity != null) {
                httpEntity.consumeContent();
            }
            throw new TTransportException("HTTP Response code: " + responseCode);
        }
        // Read the responses
        requestBuffer.reset();
        inputStream = response.getEntity().getContent();
    } catch (Exception ex) {
        throw new TTransportException(ex);
    } finally {
        try {
            requestBuffer.reset();
        } catch (IOException ignored) {
        }
        this.request = null;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) TTransportException(com.evernote.thrift.transport.TTransportException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TTransportException (com.evernote.thrift.transport.TTransportException)2 IOException (java.io.IOException)2 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 Response (com.squareup.okhttp.Response)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BufferedSink (okio.BufferedSink)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1