Example usage for org.apache.http.impl.execchain ClientExecChain execute

List of usage examples for org.apache.http.impl.execchain ClientExecChain execute

Introduction

In this page you can find the example usage for org.apache.http.impl.execchain ClientExecChain execute.

Prototype

CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext,
        HttpExecutionAware execAware) throws IOException, HttpException;

Source Link

Document

Executes th request either by transmitting it to the target server or by passing it onto the next executor in the request execution chain.

Usage

From source file:org.callimachusproject.client.ProxyClientExecDecorator.java

public ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
    return new ClientExecChain() {
        public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
            HttpHost host = route.getTargetHost();
            if (host != null) {
                ClientExecChain proxy = proxies.get(host);
                if (proxy != null) {
                    ClientExecChain execChain = new AuthenticationClientExecChain(proxy);
                    return execChain.execute(route, request, context, execAware);
                }/*  ww  w . j a v a 2 s  . co  m*/
            }
            return mainExec.execute(route, request, context, execAware);
        }
    };
}

From source file:org.esigate.cache.CacheAdapter.java

public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) {
    return new ClientExecChain() {

        /**/*from   ww  w  .j a  v  a2 s  .co m*/
         * Removes client http cache directives like "Cache-control" and "Pragma". Users must not be able to bypass
         * the cache just by making a refresh in the browser. Generates X-cache header.
         * 
         */
        @Override
        public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                HttpClientContext httpClientContext, HttpExecutionAware execAware)
                throws IOException, HttpException {
            OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext);

            // Switch route for the cache to generate the right cache key
            CloseableHttpResponse response = wrapped.execute(route, request, context, execAware);

            // Remove previously added Cache-control header
            if (request.getRequestLine().getMethod().equalsIgnoreCase("GET")
                    && (staleWhileRevalidate > 0 || staleIfError > 0)) {
                response.removeHeader(response.getLastHeader("Cache-control"));
            }
            // Add X-cache header
            if (xCacheHeader) {
                if (context != null) {
                    CacheResponseStatus cacheResponseStatus = (CacheResponseStatus) context
                            .getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);
                    String xCacheString;
                    if (cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT)) {
                        xCacheString = "HIT";
                    } else if (cacheResponseStatus.equals(CacheResponseStatus.VALIDATED)) {
                        xCacheString = "VALIDATED";
                    } else {
                        xCacheString = "MISS";
                    }
                    xCacheString += " from " + route.getTargetHost().toHostString();
                    xCacheString += " (" + request.getRequestLine().getMethod() + " "
                            + request.getRequestLine().getUri() + ")";
                    response.addHeader("X-Cache", xCacheString);
                }
            }

            // Remove Via header
            if (!viaHeader && response.containsHeader("Via")) {
                response.removeHeaders("Via");
            }
            return response;
        }
    };
}

From source file:org.esigate.http.ProxyingHttpClientBuilder.java

/**
 * Decorate with fetch event managements
 * /*from w  ww.  ja v  a 2 s.  c  om*/
 * @param wrapped
 * @return the decorated ClientExecChain
 */
private ClientExecChain addFetchEvent(final ClientExecChain wrapped) {
    return new ClientExecChain() {

        @Override
        public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                HttpClientContext httpClientContext, HttpExecutionAware execAware) {
            OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext);
            // Create request event
            FetchEvent fetchEvent = new FetchEvent(context, request);

            eventManager.fire(EventManager.EVENT_FETCH_PRE, fetchEvent);

            if (fetchEvent.isExit()) {
                if (fetchEvent.getHttpResponse() == null) {
                    // Provide an error page in order to avoid a NullPointerException
                    fetchEvent.setHttpResponse(HttpErrorPage.generateHttpResponse(
                            HttpStatus.SC_INTERNAL_SERVER_ERROR,
                            "An extension stopped the processing of the request without providing a response"));
                }
            } else {
                try {
                    fetchEvent.setHttpResponse(wrapped.execute(route, request, context, execAware));
                } catch (IOException | HttpException e) {
                    fetchEvent.setHttpResponse(HttpErrorPage.generateHttpResponse(e));
                }
            }

            // Update the event and fire post event
            eventManager.fire(EventManager.EVENT_FETCH_POST, fetchEvent);

            return fetchEvent.getHttpResponse();
        }
    };

}

From source file:org.esigate.cache.CacheAdapter.java

public ClientExecChain wrapBackendHttpClient(final ClientExecChain wrapped) {
    return new ClientExecChain() {

        private boolean isCacheableStatus(int statusCode) {
            return (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_NOT_FOUND
                    || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR
                    || statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE
                    || statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_GATEWAY_TIMEOUT);
        }//from   ww w.j  ava2 s  .  c om

        /**
         * Fire pre-fetch and post-fetch events Enables cache for all GET requests if cache ttl was forced to a
         * certain duration in the configuration. This is done even for non 200 return codes! This is a very
         * aggressive but efficient caching policy. Adds "stale-while-revalidate" and "stale-if-error" cache-control
         * directives depending on the configuration.
         * 
         * @throws HttpException
         * @throws IOException
         */
        @Override
        public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                HttpClientContext httpClientContext, HttpExecutionAware execAware)
                throws IOException, HttpException {
            OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext);

            CloseableHttpResponse response = wrapped.execute(route, request, context, execAware);

            String method = request.getRequestLine().getMethod();
            int statusCode = response.getStatusLine().getStatusCode();

            // If ttl is set, force caching even for error pages
            if (ttl > 0 && method.equalsIgnoreCase("GET") && isCacheableStatus(statusCode)) {
                response.removeHeaders("Date");
                response.removeHeaders("Cache-control");
                response.removeHeaders("Expires");
                response.setHeader("Date", DateUtils.formatDate(new Date(System.currentTimeMillis())));
                response.setHeader("Cache-control", "public, max-age=" + ttl);
                response.setHeader("Expires",
                        DateUtils.formatDate(new Date(System.currentTimeMillis() + ((long) ttl) * 1000)));
            }
            if (request.getRequestLine().getMethod().equalsIgnoreCase("GET")) {
                String cacheControlHeader = "";
                if (staleWhileRevalidate > 0) {
                    cacheControlHeader += "stale-while-revalidate=" + staleWhileRevalidate;
                }
                if (staleIfError > 0) {
                    if (cacheControlHeader.length() > 0) {
                        cacheControlHeader += ",";
                    }
                    cacheControlHeader += "stale-if-error=" + staleIfError;
                }
                if (cacheControlHeader.length() > 0) {
                    response.addHeader("Cache-control", cacheControlHeader);
                }
            }

            return response;
        }

    };
}