retrofit.mime.MimeUtil Java Examples

The following examples show how to use retrofit.mime.MimeUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestAdapter.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * Log request headers and body. Consumes request body and returns identical replacement.
 */
private Request logAndReplaceRequest(Request request) throws IOException {
    log.log(String.format("---> HTTP %s %s", request.getMethod(), request.getUrl()));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
        for (Header header : request.getHeaders()) {
            log.log(header.getName() + ": " + header.getValue());
        }

        long bodySize = 0;
        TypedOutput body = request.getBody();
        if (body != null) {
            bodySize = body.length();
            String bodyMime = body.mimeType();

            if (bodyMime != null) {
                log.log("Content-Type: " + bodyMime);
            }
            if (bodySize != -1) {
                log.log("Content-Length: " + bodySize);
            }

            if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
                if (!request.getHeaders().isEmpty()) {
                    log.log("");
                }
                if (!(body instanceof TypedByteArray)) {
                    // Read the entire response body to we can log it and replace the original response
                    request = Utils.readBodyToBytesIfNecessary(request);
                    body = request.getBody();
                }

                byte[] bodyBytes = ((TypedByteArray) body).getBytes();
                bodySize = bodyBytes.length;
                String bodyCharset = MimeUtil.parseCharset(bodyMime);
                String bodyString = new String(bodyBytes, bodyCharset);
                for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
                    int end = Math.min(len, i + LOG_CHUNK_SIZE);
                    log.log(bodyString.substring(i, end));
                }
            }
        }

        log.log(String.format("---> END HTTP (%s-byte body)", bodySize));
    }

    return request;
}
 
Example #2
Source File: RestAdapter.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * Log response headers and body. Consumes response body and returns identical replacement.
 */
private Response logAndReplaceResponse(String url, Response response, long elapsedTime) throws IOException {
    log.log(String.format("<--- HTTP %s %s (%sms)", response.getStatus(), url, elapsedTime));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
        for (Header header : response.getHeaders()) {
            log.log(header.getName() + ": " + header.getValue());
        }

        long bodySize = 0;
        TypedInput body = response.getBody();
        if (body != null) {
            bodySize = body.length();

            if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
                if (!response.getHeaders().isEmpty()) {
                    log.log("");
                }

                if (!(body instanceof TypedByteArray)) {
                    // Read the entire response body to we can log it and replace the original response
                    response = Utils.readBodyToBytesIfNecessary(response);
                    body = response.getBody();
                }

                byte[] bodyBytes = ((TypedByteArray) body).getBytes();
                bodySize = bodyBytes.length;
                String bodyMime = body.mimeType();
                String bodyCharset = MimeUtil.parseCharset(bodyMime);
                String bodyString = new String(bodyBytes, bodyCharset);
                for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
                    int end = Math.min(len, i + LOG_CHUNK_SIZE);
                    log.log(bodyString.substring(i, end));
                }
            }
        }

        log.log(String.format("<--- END HTTP (%s-byte body)", bodySize));
    }

    return response;
}