Example usage for org.apache.http.impl.client.cache CachingHttpClient CACHE_RESPONSE_STATUS

List of usage examples for org.apache.http.impl.client.cache CachingHttpClient CACHE_RESPONSE_STATUS

Introduction

In this page you can find the example usage for org.apache.http.impl.client.cache CachingHttpClient CACHE_RESPONSE_STATUS.

Prototype

String CACHE_RESPONSE_STATUS

To view the source code for org.apache.http.impl.client.cache CachingHttpClient CACHE_RESPONSE_STATUS.

Click Source Link

Document

This is the name under which the org.apache.http.client.cache.CacheResponseStatus of a request (for example, whether it resulted in a cache hit) will be recorded if an HttpContext is provided during execution.

Usage

From source file:fr.ippon.wip.http.hc.TransformerResponseInterceptor.java

/**
 * If httpResponse must be transformed, creates an instance of
 * WIPTransformer, executes WIPTransformer#transform on the response content
 * and updates the response entity accordingly.
 * /*  w w w .  j  ava  2  s  .com*/
 * @param httpResponse
 * @param context
 * @throws HttpException
 * @throws IOException
 */
public void process(HttpResponse httpResponse, HttpContext context) throws HttpException, IOException {
    PortletRequest portletRequest = HttpClientResourceManager.getInstance().getCurrentPortletRequest();
    PortletResponse portletResponse = HttpClientResourceManager.getInstance().getCurrentPortletResponse();
    WIPConfiguration config = WIPUtil.getConfiguration(portletRequest);
    RequestBuilder request = HttpClientResourceManager.getInstance().getCurrentRequest();

    if (httpResponse == null) {
        // No response -> no transformation
        LOG.warning("No response to transform.");
        return;
    }

    HttpEntity entity = httpResponse.getEntity();
    if (entity == null) {
        // No entity -> no transformation
        return;
    }

    ContentType contentType = ContentType.getOrDefault(entity);
    String mimeType = contentType.getMimeType();

    String actualURL;
    RedirectLocations redirectLocations = (RedirectLocations) context
            .getAttribute("http.protocol.redirect-locations");
    if (redirectLocations != null)
        actualURL = Iterables.getLast(redirectLocations.getAll()).toString();
    else if (context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS) == CacheResponseStatus.CACHE_HIT) {
        actualURL = request.getRequestedURL();
    } else {
        HttpRequest actualRequest = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost actualHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        actualURL = actualHost.toURI() + actualRequest.getRequestLine().getUri();
    }

    // Check if actual URI must be transformed
    if (!config.isProxyURI(actualURL))
        return;

    // a builder for creating a WIPTransformer instance
    TransformerBuilder transformerBuilder = new TransformerBuilder().setActualURL(actualURL)
            .setMimeType(mimeType).setPortletRequest(portletRequest).setPortletResponse(portletResponse)
            .setResourceType(request.getResourceType()).setXmlReaderPool(xmlReaderPool);

    // Creates an instance of Transformer depending on ResourceType and
    // MimeType
    int status = transformerBuilder.build();
    if (status == TransformerBuilder.STATUS_NO_TRANSFORMATION)
        return;

    WIPTransformer transformer = transformerBuilder.getTransformer();
    // Call WIPTransformer#transform method and update the response Entity
    // object
    try {
        String content = EntityUtils.toString(entity);
        String transformedContent = ((AbstractTransformer) transformer).transform(content);

        StringEntity transformedEntity;
        if (contentType.getCharset() != null) {
            transformedEntity = new StringEntity(transformedContent, contentType);
        } else {
            transformedEntity = new StringEntity(transformedContent);
        }
        transformedEntity.setContentType(contentType.toString());
        httpResponse.setEntity(transformedEntity);

    } catch (SAXException e) {
        LOG.log(Level.SEVERE, "Could not transform HTML", e);
        throw new IllegalArgumentException(e);
    } catch (TransformerException e) {
        LOG.log(Level.SEVERE, "Could not transform HTML", e);
        throw new IllegalArgumentException(e);
    }
}

From source file:fr.ippon.wip.http.hc.HttpClientExecutor.java

private String getActualUrl(PortletRequest portletRequest, HttpContext context, RequestBuilder request,
        HttpResponse response) {// w w w .jav a2s  . c o m
    boolean cacheUsed = (context
            .getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS) == CacheResponseStatus.CACHE_HIT);
    boolean notFoundResponse = (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);

    /*
     * ExecutionContext.HTTP_REQUEST and ExecutionContext.HTTP_TARGET_HOST are not set when the cache is used
     * or when the resource is ignored by WIP
     */
    if (cacheUsed || notFoundResponse)
        return request.getRequestedURL();

    // Get final URL (ie. perhaps redirected)
    HttpUriRequest actualRequest = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost actualHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    return (actualRequest.getURI().isAbsolute()) ? actualRequest.getURI().toString()
            : (actualHost.toURI() + actualRequest.getURI());
}