Search in sources :

Example 1 with DefaultResponseHandler

use of com.taobao.weex.devtools.inspector.network.DefaultResponseHandler in project WeexErosFramework by bmfe.

the class WeexOkhttp3Interceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String requestId = String.valueOf(mNextRequestId.getAndIncrement());
    Request request = chain.request();
    mEventReporter = NetworkEventReporterManager.get();
    RequestBodyHelper requestBodyHelper = null;
    if (mEventReporter != null) {
        requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
        OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
        mEventReporter.requestWillBeSent(inspectorRequest);
    }
    Response response;
    try {
        response = chain.proceed(request);
    } catch (IOException e) {
        if (mEventReporter != null) {
            mEventReporter.httpExchangeFailed(requestId, e.toString());
        }
        throw e;
    }
    if (mEventReporter != null) {
        if (requestBodyHelper.hasBody()) {
            requestBodyHelper.reportDataSent();
        }
        Connection connection = chain.connection();
        mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
        ResponseBody body = response.body();
        MediaType contentType = null;
        InputStream responseStream = null;
        if (body != null) {
            contentType = body.contentType();
            responseStream = body.byteStream();
        }
        responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
        if (responseStream != null) {
            response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
        }
    }
    return response;
}
Also used : InputStream(java.io.InputStream) Request(okhttp3.Request) Connection(okhttp3.Connection) IOException(java.io.IOException) DefaultResponseHandler(com.taobao.weex.devtools.inspector.network.DefaultResponseHandler) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) RequestBodyHelper(com.taobao.weex.devtools.inspector.network.RequestBodyHelper) MediaType(okhttp3.MediaType)

Example 2 with DefaultResponseHandler

use of com.taobao.weex.devtools.inspector.network.DefaultResponseHandler in project WeexErosFramework by bmfe.

the class WeexOkhttp3Interceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String requestId = String.valueOf(mNextRequestId.getAndIncrement());
    Request request = chain.request();
    mEventReporter = NetworkEventReporterManager.get();
    RequestBodyHelper requestBodyHelper = null;
    if (mEventReporter != null) {
        requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
        OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
        mEventReporter.requestWillBeSent(inspectorRequest);
    }
    Response response;
    try {
        response = chain.proceed(request);
    } catch (IOException e) {
        if (mEventReporter != null) {
            mEventReporter.httpExchangeFailed(requestId, e.toString());
        }
        throw e;
    }
    if (mEventReporter != null) {
        if (requestBodyHelper.hasBody()) {
            requestBodyHelper.reportDataSent();
        }
        Connection connection = chain.connection();
        mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
        ResponseBody body = response.body();
        MediaType contentType = null;
        InputStream responseStream = null;
        if (body != null) {
            contentType = body.contentType();
            responseStream = body.byteStream();
        }
        responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
        if (responseStream != null) {
            response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
        }
    }
    return response;
}
Also used : InputStream(java.io.InputStream) Request(okhttp3.Request) Connection(okhttp3.Connection) IOException(java.io.IOException) DefaultResponseHandler(com.taobao.weex.devtools.inspector.network.DefaultResponseHandler) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) RequestBodyHelper(com.taobao.weex.devtools.inspector.network.RequestBodyHelper) MediaType(okhttp3.MediaType)

Aggregations

DefaultResponseHandler (com.taobao.weex.devtools.inspector.network.DefaultResponseHandler)2 RequestBodyHelper (com.taobao.weex.devtools.inspector.network.RequestBodyHelper)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Connection (okhttp3.Connection)2 MediaType (okhttp3.MediaType)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 ResponseBody (okhttp3.ResponseBody)2