Example usage for org.apache.http.client.cache CacheResponseStatus toString

List of usage examples for org.apache.http.client.cache CacheResponseStatus toString

Introduction

In this page you can find the example usage for org.apache.http.client.cache CacheResponseStatus toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.esigate.extension.FragmentLogging.java

@Override
public boolean event(EventDefinition id, Event event) {

    FragmentEvent e = (FragmentEvent) event;

    if (EventManager.EVENT_FRAGMENT_PRE.equals(id)) {
        // Keep track of the start time.
        e.getHttpContext().setAttribute(TIME, System.currentTimeMillis(), true);
    } else {//w  w  w .  jav  a2s  .c o m
        int statusCode = e.getHttpResponse().getStatusLine().getStatusCode();

        // Log only if info or issue
        if (LOG.isInfoEnabled() || statusCode >= HttpStatus.SC_BAD_REQUEST) {

            // Log last result only
            HttpRequest httpRequest = e.getHttpContext().getSentRequest();

            // Create log message
            HttpHost targetHost = e.getHttpContext().getTargetHost();

            String requestLine = httpRequest.getRequestLine().toString();
            String statusLine = e.getHttpResponse().getStatusLine().toString();

            String reqHeaders = ArrayUtils.toString(httpRequest.getAllHeaders());
            String respHeaders = ArrayUtils.toString(e.getHttpResponse().getAllHeaders());

            String cache = "";
            CacheResponseStatus cacheResponseStatus = (CacheResponseStatus) e.getHttpContext()
                    .getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);
            if (cacheResponseStatus != null) {
                cache = cacheResponseStatus.toString();
            }

            long time = System.currentTimeMillis() - (Long) e.getHttpContext().removeAttribute(TIME, true);

            StringBuilder logMessage = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
            logMessage.append(driver.getConfiguration().getInstanceName());
            logMessage.append(" ");
            // Display target host, protocol and port
            if (targetHost != null) {
                logMessage.append(targetHost.getSchemeName());
                logMessage.append("://");
                logMessage.append(targetHost.getHostName());

                if (targetHost.getPort() != -1) {
                    logMessage.append(":");
                    logMessage.append(targetHost.getPort());
                }

                logMessage.append(" - ");
            }

            logMessage.append(requestLine).append(" ").append(reqHeaders).append(" -> ").append(statusLine)
                    .append(" (").append(time).append(" ms) ").append(cache).append(" ").append(respHeaders);

            // Log level according to status code.
            if (statusCode >= HttpStatus.SC_BAD_REQUEST) {
                LOG.warn(logMessage.toString());
            } else {
                LOG.info(logMessage.toString());
            }
        }
    }

    // Continue processing
    return true;
}