Example usage for org.apache.http.client.methods CloseableHttpResponse setEntity

List of usage examples for org.apache.http.client.methods CloseableHttpResponse setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse setEntity.

Prototype

void setEntity(HttpEntity httpEntity);

Source Link

Usage

From source file:org.esigate.Driver.java

/**
 * Perform rendering on a single url content, and append result to "writer". Automatically follows redirects
 * //from   ww  w  . j  av  a  2s .c  om
 * @param pageUrl
 *            Address of the page containing the template
 * @param incomingRequest
 *            originating request object
 * @param renderers
 *            the renderers to use in order to transform the output
 * @return The resulting response
 * @throws IOException
 *             If an IOException occurs while writing to the writer
 * @throws HttpErrorPage
 *             If an Exception occurs while retrieving the template
 */
public CloseableHttpResponse render(String pageUrl, IncomingRequest incomingRequest, Renderer... renderers)
        throws IOException, HttpErrorPage {
    DriverRequest driverRequest = new DriverRequest(incomingRequest, this, pageUrl);

    // Replace ESI variables in URL
    // TODO: should be performed in the ESI extension
    String resultingPageUrl = VariablesResolver.replaceAllVariables(pageUrl, driverRequest);

    String targetUrl = ResourceUtils.getHttpUrlWithQueryString(resultingPageUrl, driverRequest, false);

    String currentValue;
    CloseableHttpResponse response;

    // Retrieve URL
    // Get from cache to prevent multiple request to the same url if
    // multiple fragments are used.

    String cacheKey = CACHE_RESPONSE_PREFIX + targetUrl;
    Pair<String, CloseableHttpResponse> cachedValue = incomingRequest.getAttribute(cacheKey);
    // content and response were not in cache
    if (cachedValue == null) {
        OutgoingRequest outgoingRequest = requestExecutor.createOutgoingRequest(driverRequest, targetUrl,
                false);
        headerManager.copyHeaders(driverRequest, outgoingRequest);
        response = requestExecutor.execute(outgoingRequest);
        int redirects = MAX_REDIRECTS;
        try {
            while (redirects > 0
                    && redirectStrategy.isRedirected(outgoingRequest, response, outgoingRequest.getContext())) {
                redirects--;
                outgoingRequest = requestExecutor.createOutgoingRequest(driverRequest, redirectStrategy
                        .getLocationURI(outgoingRequest, response, outgoingRequest.getContext()).toString(),
                        false);
                headerManager.copyHeaders(driverRequest, outgoingRequest);
                response = requestExecutor.execute(outgoingRequest);
            }
        } catch (ProtocolException e) {
            throw new HttpErrorPage(HttpStatus.SC_BAD_GATEWAY, "Invalid response from server", e);
        }
        response = headerManager.copyHeaders(outgoingRequest, incomingRequest, response);
        currentValue = HttpResponseUtils.toString(response, this.eventManager);
        // Cache
        cachedValue = new ImmutablePair<String, CloseableHttpResponse>(currentValue, response);
        incomingRequest.setAttribute(cacheKey, cachedValue);
    }
    currentValue = cachedValue.getKey();
    response = cachedValue.getValue();

    logAction("render", pageUrl, renderers);

    // Apply renderers
    currentValue = performRendering(pageUrl, driverRequest, response, currentValue, renderers);

    response.setEntity(new StringEntity(currentValue, HttpResponseUtils.getContentType(response)));

    return response;
}