Search in sources :

Example 1 with HttpTransaction

use of com.readystatesoftware.chuck.internal.data.HttpTransaction in project chuck by jgilfelt.

the class ChuckInterceptor method update.

private int update(HttpTransaction transaction, Uri uri) {
    ContentValues values = LocalCupboard.getInstance().withEntity(HttpTransaction.class).toContentValues(transaction);
    int updated = context.getContentResolver().update(uri, values, null, null);
    if (showNotification && updated > 0) {
        notificationHelper.show(transaction);
    }
    return updated;
}
Also used : ContentValues(android.content.ContentValues) HttpTransaction(com.readystatesoftware.chuck.internal.data.HttpTransaction)

Example 2 with HttpTransaction

use of com.readystatesoftware.chuck.internal.data.HttpTransaction in project chuck by jgilfelt.

the class ChuckInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    RequestBody requestBody = request.body();
    boolean hasRequestBody = requestBody != null;
    HttpTransaction transaction = new HttpTransaction();
    transaction.setRequestDate(new Date());
    transaction.setMethod(request.method());
    transaction.setUrl(request.url().toString());
    transaction.setRequestHeaders(request.headers());
    if (hasRequestBody) {
        if (requestBody.contentType() != null) {
            transaction.setRequestContentType(requestBody.contentType().toString());
        }
        if (requestBody.contentLength() != -1) {
            transaction.setRequestContentLength(requestBody.contentLength());
        }
    }
    transaction.setRequestBodyIsPlainText(!bodyHasUnsupportedEncoding(request.headers()));
    if (hasRequestBody && transaction.requestBodyIsPlainText()) {
        BufferedSource source = getNativeSource(new Buffer(), bodyGzipped(request.headers()));
        Buffer buffer = source.buffer();
        requestBody.writeTo(buffer);
        Charset charset = UTF8;
        MediaType contentType = requestBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(UTF8);
        }
        if (isPlaintext(buffer)) {
            transaction.setRequestBody(readFromBuffer(buffer, charset));
        } else {
            transaction.setResponseBodyIsPlainText(false);
        }
    }
    Uri transactionUri = create(transaction);
    long startNs = System.nanoTime();
    Response response;
    try {
        response = chain.proceed(request);
    } catch (Exception e) {
        transaction.setError(e.toString());
        update(transaction, transactionUri);
        throw e;
    }
    long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
    ResponseBody responseBody = response.body();
    // includes headers added later in the chain
    transaction.setRequestHeaders(response.request().headers());
    transaction.setResponseDate(new Date());
    transaction.setTookMs(tookMs);
    transaction.setProtocol(response.protocol().toString());
    transaction.setResponseCode(response.code());
    transaction.setResponseMessage(response.message());
    transaction.setResponseContentLength(responseBody.contentLength());
    if (responseBody.contentType() != null) {
        transaction.setResponseContentType(responseBody.contentType().toString());
    }
    transaction.setResponseHeaders(response.headers());
    transaction.setResponseBodyIsPlainText(!bodyHasUnsupportedEncoding(response.headers()));
    if (HttpHeaders.hasBody(response) && transaction.responseBodyIsPlainText()) {
        BufferedSource source = getNativeSource(response);
        source.request(Long.MAX_VALUE);
        Buffer buffer = source.buffer();
        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            try {
                charset = contentType.charset(UTF8);
            } catch (UnsupportedCharsetException e) {
                update(transaction, transactionUri);
                return response;
            }
        }
        if (isPlaintext(buffer)) {
            transaction.setResponseBody(readFromBuffer(buffer.clone(), charset));
        } else {
            transaction.setResponseBodyIsPlainText(false);
        }
        transaction.setResponseContentLength(buffer.size());
    }
    update(transaction, transactionUri);
    return response;
}
Also used : Buffer(okio.Buffer) HttpTransaction(com.readystatesoftware.chuck.internal.data.HttpTransaction) Request(okhttp3.Request) Charset(java.nio.charset.Charset) Uri(android.net.Uri) Date(java.util.Date) IOException(java.io.IOException) EOFException(java.io.EOFException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) BufferedSource(okio.BufferedSource)

Example 3 with HttpTransaction

use of com.readystatesoftware.chuck.internal.data.HttpTransaction in project chuck by jgilfelt.

the class ChuckInterceptor method create.

private Uri create(HttpTransaction transaction) {
    ContentValues values = LocalCupboard.getInstance().withEntity(HttpTransaction.class).toContentValues(transaction);
    Uri uri = context.getContentResolver().insert(ChuckContentProvider.TRANSACTION_URI, values);
    transaction.setId(Long.valueOf(uri.getLastPathSegment()));
    if (showNotification) {
        notificationHelper.show(transaction);
    }
    retentionManager.doMaintenance();
    return uri;
}
Also used : ContentValues(android.content.ContentValues) HttpTransaction(com.readystatesoftware.chuck.internal.data.HttpTransaction) Uri(android.net.Uri)

Aggregations

HttpTransaction (com.readystatesoftware.chuck.internal.data.HttpTransaction)3 ContentValues (android.content.ContentValues)2 Uri (android.net.Uri)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 Date (java.util.Date)1 MediaType (okhttp3.MediaType)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 Buffer (okio.Buffer)1 BufferedSource (okio.BufferedSource)1