@Override
  public Response intercept(Chain chain) throws IOException {
    String requestId = String.valueOf(mNextRequestId.getAndIncrement());

    Request request = chain.request();

    int requestSize = 0;
    if (mEventReporter.isEnabled()) {
      OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request);
      mEventReporter.requestWillBeSent(inspectorRequest);
      byte[] requestBody = inspectorRequest.body();
      if (requestBody != null) {
        requestSize += requestBody.length;
      }
    }

    Response response;
    try {
      response = chain.proceed(request);
    } catch (IOException e) {
      if (mEventReporter.isEnabled()) {
        mEventReporter.httpExchangeFailed(requestId, e.toString());
      }
      throw e;
    }

    if (mEventReporter.isEnabled()) {
      if (requestSize > 0) {
        mEventReporter.dataSent(requestId, requestSize, requestSize);
      }

      Connection connection = chain.connection();
      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;
  }
Exemple #2
0
  /**
   * Peeks up to {@code byteCount} bytes from the response body and returns them as a new response
   * body. If fewer than {@code byteCount} bytes are in the response body, the full response body is
   * returned. If more than {@code byteCount} bytes are in the response body, the returned value
   * will be truncated to {@code byteCount} bytes.
   *
   * <p>It is an error to call this method after the body has been consumed.
   *
   * <p><strong>Warning:</strong> this method loads the requested bytes into memory. Most
   * applications should set a modest limit on {@code byteCount}, such as 1 MiB.
   */
  public ResponseBody peekBody(long byteCount) throws IOException {
    BufferedSource source = body.source();
    source.request(byteCount);
    Buffer copy = source.buffer().clone();

    // There may be more than byteCount bytes in source.buffer(). If there is, return a prefix.
    Buffer result;
    if (copy.size() > byteCount) {
      result = new Buffer();
      result.write(copy, byteCount);
      copy.clear();
    } else {
      result = copy;
    }

    return ResponseBody.create(body.contentType(), result.size(), result);
  }
Exemple #3
0
 public synchronized boolean equals(java.lang.Object obj) {
   if (!(obj instanceof ResponseBody)) return false;
   ResponseBody other = (ResponseBody) obj;
   if (obj == null) return false;
   if (this == obj) return true;
   if (__equalsCalc != null) {
     return (__equalsCalc == obj);
   }
   __equalsCalc = obj;
   boolean _equals;
   _equals =
       true
           && ((this.serviceConfigurationSiteViewQueryResponse == null
                   && other.getServiceConfigurationSiteViewQueryResponse() == null)
               || (this.serviceConfigurationSiteViewQueryResponse != null
                   && this.serviceConfigurationSiteViewQueryResponse.equals(
                       other.getServiceConfigurationSiteViewQueryResponse())));
   __equalsCalc = null;
   return _equals;
 }
 @Override
 public long contentLength() {
   return mBody.contentLength();
 }
 @Override
 public MediaType contentType() {
   return mBody.contentType();
 }
Exemple #6
0
 /** Closes the response body. Equivalent to {@code body().close()}. */
 @Override
 public void close() {
   body.close();
 }