Search in sources :

Example 1 with RequestBodyHelper

use of com.facebook.stetho.inspector.network.RequestBodyHelper in project stetho by facebook.

the class StethoInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String requestId = mEventReporter.nextRequestId();
    Request request = chain.request();
    RequestBodyHelper requestBodyHelper = null;
    if (mEventReporter.isEnabled()) {
        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.isEnabled()) {
            mEventReporter.httpExchangeFailed(requestId, e.toString());
        }
        throw e;
    }
    if (mEventReporter.isEnabled()) {
        if (requestBodyHelper != null && requestBodyHelper.hasBody()) {
            requestBodyHelper.reportDataSent();
        }
        Connection connection = chain.connection();
        if (connection == null) {
            throw new IllegalStateException("No connection associated with this request; " + "did you use addInterceptor instead of addNetworkInterceptor?");
        }
        mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response, connection));
        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(com.squareup.okhttp.Request) Connection(com.squareup.okhttp.Connection) IOException(java.io.IOException) DefaultResponseHandler(com.facebook.stetho.inspector.network.DefaultResponseHandler) ResponseBody(com.squareup.okhttp.ResponseBody) Response(com.squareup.okhttp.Response) RequestBodyHelper(com.facebook.stetho.inspector.network.RequestBodyHelper) MediaType(com.squareup.okhttp.MediaType)

Example 2 with RequestBodyHelper

use of com.facebook.stetho.inspector.network.RequestBodyHelper in project stetho by facebook.

the class StethoInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String requestId = mEventReporter.nextRequestId();
    Request request = chain.request();
    RequestBodyHelper requestBodyHelper = null;
    if (mEventReporter.isEnabled()) {
        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.isEnabled()) {
            mEventReporter.httpExchangeFailed(requestId, e.toString());
        }
        throw e;
    }
    if (mEventReporter.isEnabled()) {
        if (requestBodyHelper != null && requestBodyHelper.hasBody()) {
            requestBodyHelper.reportDataSent();
        }
        Connection connection = chain.connection();
        if (connection == null) {
            throw new IllegalStateException("No connection associated with this request; " + "did you use addInterceptor instead of addNetworkInterceptor?");
        }
        mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response, connection));
        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) IOException(java.io.IOException) DefaultResponseHandler(com.facebook.stetho.inspector.network.DefaultResponseHandler) RequestBodyHelper(com.facebook.stetho.inspector.network.RequestBodyHelper)

Example 3 with RequestBodyHelper

use of com.facebook.stetho.inspector.network.RequestBodyHelper in project stetho by facebook.

the class StethoURLConnectionManagerImpl method preConnect.

/**
 * @see StethoURLConnectionManager#preConnect
 */
public void preConnect(HttpURLConnection connection, @Nullable SimpleRequestEntity requestEntity) {
    throwIfConnection();
    mConnection = connection;
    if (isStethoActive()) {
        mRequestBodyHelper = new RequestBodyHelper(mStethoHook, getStethoRequestId());
        mInspectorRequest = new URLConnectionInspectorRequest(getStethoRequestId(), mFriendlyName, connection, requestEntity, mRequestBodyHelper);
        mStethoHook.requestWillBeSent(mInspectorRequest);
    }
}
Also used : RequestBodyHelper(com.facebook.stetho.inspector.network.RequestBodyHelper)

Aggregations

RequestBodyHelper (com.facebook.stetho.inspector.network.RequestBodyHelper)3 DefaultResponseHandler (com.facebook.stetho.inspector.network.DefaultResponseHandler)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Connection (com.squareup.okhttp.Connection)1 MediaType (com.squareup.okhttp.MediaType)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 ResponseBody (com.squareup.okhttp.ResponseBody)1