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

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

Introduction

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

Prototype

boolean containsHeader(String str);

Source Link

Usage

From source file:org.esigate.DriverTest.java

public void testHeadersFilteredWhenError500() throws Exception {
    Properties properties = new Properties();
    properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost");
    HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
            HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
    response.addHeader("Content-type", "Text/html;Charset=UTF-8");
    response.addHeader("Transfer-Encoding", "dummy");
    HttpEntity httpEntity = new StringEntity("Error", "UTF-8");
    response.setEntity(httpEntity);//from   w  w w  .ja v a2s .  c  om
    mockConnectionManager.setResponse(response);
    Driver driver = createMockDriver(properties, mockConnectionManager);
    CloseableHttpResponse driverResponse;
    try {
        driverResponse = driver.proxy("/", request.build());
        fail("We should get an HttpErrorPage");
    } catch (HttpErrorPage e) {
        driverResponse = e.getHttpResponse();
    }
    int statusCode = driverResponse.getStatusLine().getStatusCode();
    assertEquals("Status code", HttpStatus.SC_INTERNAL_SERVER_ERROR, statusCode);
    assertFalse("Header 'Transfer-Encoding'", driverResponse.containsHeader("Transfer-Encoding"));
}

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

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

        /**/*from w w  w.jav  a  2  s  .c o  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:synapticloop.b2.request.BaseB2Request.java

/**
 * Execute an HTTP HEAD request and return the response for further parsing
 *
 * @return the response object//from   www.  j a va 2s .  c o  m
 *
 * @throws B2ApiException if something went wrong with the call
 * @throws IOException if there was an error communicating with the API service
 */
protected CloseableHttpResponse executeHead() throws B2ApiException, IOException {
    URI uri = this.buildUri();

    LOGGER.debug("HEAD request to URL '{}'", uri.toString());

    HttpHead httpHead = new HttpHead(uri);

    CloseableHttpResponse httpResponse = this.execute(httpHead);

    switch (httpResponse.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
        return httpResponse;
    }

    final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()),
            new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                    httpResponse.getStatusLine().getReasonPhrase()));
    if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) {
        throw failure
                .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue()));
    }
    throw failure;
}

From source file:synapticloop.b2.request.BaseB2Request.java

/**
 * Execute a GET request, returning the data stream from the response.
 *
 * @return The response from the GET request
 *
 * @throws B2ApiException if there was an error with the request
 * @throws IOException if there was an error communicating with the API service
 *///from   w  ww.ja  v a  2s .c  o m
protected CloseableHttpResponse executeGet() throws B2ApiException, IOException {
    URI uri = this.buildUri();

    HttpGet httpGet = new HttpGet(uri);

    CloseableHttpResponse httpResponse = this.execute(httpGet);

    // you will either get an OK or a partial content
    switch (httpResponse.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
    case HttpStatus.SC_PARTIAL_CONTENT:
        return httpResponse;
    }

    final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()),
            new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                    httpResponse.getStatusLine().getReasonPhrase()));
    if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) {
        throw failure
                .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue()));
    }
    throw failure;
}

From source file:synapticloop.b2.request.BaseB2Request.java

/**
 * Execute a POST request returning the response data as a String
 *
 * @return the response data as a string
 *
 * @throws B2ApiException if there was an error with the call, most notably
 *     a non OK status code (i.e. not 200)
 * @throws IOException if there was an error communicating with the API service
 *///  w w w  .j av a2  s  .c o m
protected CloseableHttpResponse executePost() throws B2ApiException, IOException {
    URI uri = this.buildUri();

    String postData = convertPostData();
    HttpPost httpPost = new HttpPost(uri);

    httpPost.setEntity(new StringEntity(postData, APPLICATION_JSON));
    CloseableHttpResponse httpResponse = this.execute(httpPost);

    switch (httpResponse.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
        return httpResponse;
    }

    final B2ApiException failure = new B2ApiException(EntityUtils.toString(httpResponse.getEntity()),
            new HttpResponseException(httpResponse.getStatusLine().getStatusCode(),
                    httpResponse.getStatusLine().getReasonPhrase()));
    if (httpResponse.containsHeader(HttpHeaders.RETRY_AFTER)) {
        throw failure
                .withRetry(Integer.valueOf(httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER).getValue()));
    }
    throw failure;
}