Example usage for org.apache.http.client.methods HttpUriRequest removeHeaders

List of usage examples for org.apache.http.client.methods HttpUriRequest removeHeaders

Introduction

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

Prototype

void removeHeaders(String str);

Source Link

Usage

From source file:ch.entwine.weblounge.test.util.TestSiteUtils.java

/**
 * Test for the correct response when etag header is set.
 * //from   w w w  . j  a  v  a 2s  .co m
 * @param request
 *          the http request
 * @param eTagValue
 *          the expected etag value
 * @param logger
 *          used to log test output
 * @param params
 *          the request parameters
 * @throws Exception
 *           if processing the request fails
 */
public static void testETagHeader(HttpUriRequest request, String eTagValue, Logger logger, String[][] params)
        throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
        request.removeHeaders("If-Modified-Since");
        request.setHeader("If-None-Match", eTagValue);
        logger.info("Sending 'If-None-Match' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
        assertNull(response.getEntity());
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    httpClient = new DefaultHttpClient();
    try {
        request.removeHeaders("If-Modified-Since");
        request.setHeader("If-None-Match", "\"abcdefghijklmt\"");
        logger.info("Sending 'If-None-Match' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
        assertNotNull(response.getEntity());
        response.getEntity().consumeContent();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:ch.entwine.weblounge.test.util.TestSiteUtils.java

/**
 * Test for the correct response when modified since header is set.
 * //from  w ww .java  2 s.c om
 * @param request
 *          the http request
 * @param date
 *          the expected modification date
 * @param logger
 *          used to log test output
 * @param params
 *          the request parameters
 * @throws Exception
 *           if processing the request fails
 */
public static void testModifiedHeader(HttpUriRequest request, Date modificationDate, Logger logger,
        String[][] params) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    Date before = new Date(modificationDate.getTime() - Times.MS_PER_DAY);
    Date after = new Date(modificationDate.getTime() + Times.MS_PER_DAY);
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
    try {
        request.removeHeaders("If-None-Match");
        request.setHeader("If-Modified-Since", format.format(after));
        logger.info("Sending 'If-Modified-Since' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatusLine().getStatusCode());
        assertNull(response.getEntity());
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    httpClient = new DefaultHttpClient();
    try {
        request.removeHeaders("If-None-Match");
        request.setHeader("If-Modified-Since", format.format(before));
        logger.info("Sending 'If-Modified-Since' request to {}", request.getURI());
        HttpResponse response = TestUtils.request(httpClient, request, params);
        assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
        assertNotNull(response.getEntity());
        response.getEntity().consumeContent();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.apache.hadoop.gateway.dispatch.HttpClientDispatch.java

protected HttpResponse executeKerberosDispatch(HttpUriRequest outboundRequest, DefaultHttpClient client)
        throws IOException, ClientProtocolException {
    HttpResponse inboundResponse;//www . j  ava  2s. c  om
    outboundRequest.removeHeaders(COOKIE);
    String appCookie = appCookieManager.getCachedAppCookie();
    if (appCookie != null) {
        outboundRequest.addHeader(new BasicHeader(COOKIE, appCookie));
    }
    inboundResponse = client.execute(outboundRequest);
    // if inBoundResponse has status 401 and header WWW-Authenticate: Negoitate
    // refresh hadoop.auth.cookie and attempt one more time
    int statusCode = inboundResponse.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
        Header[] wwwAuthHeaders = inboundResponse.getHeaders(WWW_AUTHENTICATE);
        if (wwwAuthHeaders != null && wwwAuthHeaders.length != 0
                && wwwAuthHeaders[0].getValue().trim().startsWith(NEGOTIATE)) {
            appCookie = appCookieManager.getAppCookie(outboundRequest, true);
            outboundRequest.removeHeaders(COOKIE);
            outboundRequest.addHeader(new BasicHeader(COOKIE, appCookie));
            client = new DefaultHttpClient();
            inboundResponse = client.execute(outboundRequest);
        } else {
            // no supported authentication type found
            // we would let the original response propagate
        }
    } else {
        // not a 401 Unauthorized status code
        // we would let the original response propagate
    }
    return inboundResponse;
}

From source file:net.netheos.pcsapi.oauth.OAuth2SessionManager.java

@Override
public CResponse execute(HttpUriRequest request) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("{}: {}", request.getMethod(), PcsUtils.shortenUrl(request.getURI()));
    }/*from w w  w.  j ava 2 s  . c o  m*/

    if (userCredentials.getCredentials().hasExpired()) {
        refreshToken();
    }

    try {
        request.removeHeaders(HEADER_AUTHORIZATION); // in case we are retrying
        request.addHeader(HEADER_AUTHORIZATION, "Bearer " + userCredentials.getCredentials().getAccessToken());

        HttpResponse httpResponse = httpClient.execute(request);
        return new CResponse(request, httpResponse);

    } catch (IOException ex) {
        // a low level error should be retried :
        throw new CRetriableException(ex);
    }
}

From source file:org.opencastproject.kernel.security.TrustedHttpClientImpl.java

/**
 * Retries a request if the nonce timed out during the request.
 * /*from   www. j a  va 2 s. co m*/
 * @param httpUriRequest
 *          The request to be made that isn't a GET, those are handled automatically.
 * @param response
 *          The response with the bad nonce timeout in it.
 * @return A new response for the request if it was successful without the nonce timing out again or just the same
 *         response it got if it ran out of attempts.
 * @throws TrustedHttpClientException
 * @throws IOException
 * @throws ClientProtocolException
 */
private HttpResponse retryAuthAndRequestAfterNonceTimeout(HttpUriRequest httpUriRequest, HttpResponse response)
        throws TrustedHttpClientException, IOException, ClientProtocolException {
    // Get rid of old security headers with the old nonce.
    httpUriRequest.removeHeaders(AUTHORIZATION_HEADER_NAME);

    for (int i = 0; i < nonceTimeoutRetries; i++) {
        HttpClient httpClient = makeHttpClient();
        int variableDelay = 0;
        // Make sure that we have a variable delay greater than 0.
        if (retryMaximumVariableTime > 0) {
            variableDelay = generator.nextInt(retryMaximumVariableTime * MILLISECONDS_IN_SECONDS);
        }

        long totalDelay = (retryBaseDelay * MILLISECONDS_IN_SECONDS + variableDelay);
        if (totalDelay > 0) {
            logger.info("Sleeping " + totalDelay + "ms before trying request " + httpUriRequest.getURI()
                    + " again due to a " + response.getStatusLine());
            try {
                Thread.sleep(totalDelay);
            } catch (InterruptedException e) {
                logger.error("Suffered InteruptedException while trying to sleep until next retry.", e);
            }
        }
        manuallyHandleDigestAuthentication(httpUriRequest, httpClient);
        response = httpClient.execute(httpUriRequest);
        if (!hadNonceTimeoutResponse(response)) {
            responseMap.put(response, httpClient);
            break;
        }
        httpClient.getConnectionManager().shutdown();
    }
    return response;
}