Example usage for org.apache.commons.httpclient HttpMethod addRequestHeader

List of usage examples for org.apache.commons.httpclient HttpMethod addRequestHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod addRequestHeader.

Prototype

public abstract void addRequestHeader(String paramString1, String paramString2);

Source Link

Usage

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>//ww  w.j a  v a2 s .  c o  m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/SendRedirect.jsp
 */
public void testRedirect() throws Exception {

    String url = "http://localhost:9080/non_ok/SendRedirect.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * A 0 length body should give a 0 length nongzipped body and content length
 * Manual Test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_caching_filter/empty.html
 *//*from   ww w. ja v a  2 s . c  o  m*/
public void testIfModifiedZeroLengthHTML() throws Exception {

    String url = "http://localhost:9080/empty_caching_filter/empty.html";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertTrue(
            HttpURLConnection.HTTP_OK == responseCode || HttpURLConnection.HTTP_NOT_MODIFIED == responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertTrue("".equals(responseBody) || null == responseBody);
    checkNullOrZeroContentLength(httpMethod);
}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * Servlets and JSPs can send content even when the response is set to no content.
 * In this case there should not be a body but there is. Orion seems to kill the body
 * after is has left the Servlet filter chain. To avoid wget going into an inifinite
 * retry loop, and presumably some other web clients, the content length should be 0
 * and the body 0.// w  w w.j  ava  2  s . c o  m
 * <p/>
 * wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_caching_filter/SC_NOT_MODIFIED.jsp
 */
public void testNotModifiedJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_caching_filter/SC_NOT_MODIFIED.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);

}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * Servlets and JSPs can send content even when the response is set to no content.
 * In this case there should not be a body but there is. Orion seems to kill the body
 * after is has left the Servlet filter chain. To avoid wget going into an inifinite
 * retry loop, and presumably some other web clients, the content length should be 0
 * and the body 0.//from  w w  w .j a v a 2s  .  c  o m
 * <p/>
 * Manual Test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_caching_filter/SC_NO_CONTENT.jsp
 */
public void testNoContentJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_caching_filter/SC_NO_CONTENT.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);

}

From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java

/**
 * Pasa los headers de un request a otro. Copia todos salvo algunos
 * prohibidos que no tienen sentido.//  w  w w  .  j  av  a 2 s.co m
 */
// CHECKSTYLE:DESIGN:OFF
protected void proxyHeaders(final HttpServletRequest request, final HttpMethod method) {

    Enumeration<String> names = request.getHeaderNames();

    while (names.hasMoreElements()) {
        String name = names.nextElement().toLowerCase();
        Enumeration<String> headers = request.getHeaders(name);
        if (!forbiddenHeader.contains(name)) {
            while (headers.hasMoreElements()) {
                method.addRequestHeader(name, headers.nextElement());

            }
        }
    }
}

From source file:com.sa.npopa.samples.hbase.rest.client.Client.java

/**
 * Execute a transaction method given a complete URI.
 * @param method the transaction method//from w  w  w .j  ava  2  s .  c o  m
 * @param headers HTTP header values to send
 * @param uri a properly urlencoded URI
 * @return the HTTP response code
 * @throws IOException
 */
public int executeURI(HttpMethod method, Header[] headers, String uri) throws IOException {
    method.setURI(new URI(uri, true));
    for (Map.Entry<String, String> e : extraHeaders.entrySet()) {
        method.addRequestHeader(e.getKey(), e.getValue());
    }
    if (headers != null) {
        for (Header header : headers) {
            method.addRequestHeader(header);
        }
    }
    long startTime = System.currentTimeMillis();
    int code = httpClient.executeMethod(method);
    long endTime = System.currentTimeMillis();
    if (LOG.isDebugEnabled()) {
        LOG.debug(method.getName() + " " + uri + " " + code + " " + method.getStatusText() + " in "
                + (endTime - startTime) + " ms");
    }
    return code;
}

From source file:com.springsource.insight.plugin.apache.http.hc3.HttpClientExecutionCollectionAspectTest.java

private Map<String, String> runHeadersObfuscationTest(String testName, Collection<String> headerSet,
        boolean defaultHeaders) throws IOException {
    HttpClient httpClient = new HttpClient();
    String uri = createTestUri(testName);
    HttpMethod method = new GetMethod(uri);
    for (String name : headerSet) {
        if ("WWW-Authenticate".equalsIgnoreCase(name)) {
            continue; // this is a response header
        }/* www  .  j  a v  a  2 s  .c  o  m*/
        method.addRequestHeader(name, name);
    }

    HttpClientExecutionCollectionAspect aspectInstance = getAspect();
    HttpObfuscator obfuscator = aspectInstance.getHttpHeadersObfuscator();
    if (!defaultHeaders) {
        for (String name : HttpObfuscator.DEFAULT_OBFUSCATED_HEADERS_LIST) {
            if ("WWW-Authenticate".equalsIgnoreCase(name)) {
                continue; // this is a response header
            }
            method.addRequestHeader(name, name);
        }
        obfuscator.incrementalUpdate(HttpObfuscator.OBFUSCATED_HEADERS_SETTING,
                StringUtil.implode(headerSet, ","));
    }

    int response = httpClient.executeMethod(method);
    Operation op = assertExecutionResult(uri, method, response, false);
    OperationMap reqDetails = op.get("request", OperationMap.class);
    OperationList reqHeaders = reqDetails.get("headers", OperationList.class);
    Map<String, String> requestHeaders = toHeadersMap(reqHeaders);
    OperationMap rspDetails = op.get("response", OperationMap.class);
    OperationList rspHeaders = rspDetails.get("headers", OperationList.class);
    Map<String, String> responseHeaders = toHeadersMap(rspHeaders);
    Map<String, String> hdrsMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    if (MapUtil.size(requestHeaders) > 0) {
        hdrsMap.putAll(requestHeaders);
    }
    if (MapUtil.size(responseHeaders) > 0) {
        hdrsMap.putAll(responseHeaders);
    }

    Collection<?> obscuredValues = (ObscuredValueSetMarker) obfuscator.getSensitiveValueMarker();
    for (String name : headerSet) {
        String value = hdrsMap.get(name);
        assertNotNull("Missing value for header: " + name, value);
        assertTrue("Unobscured value of " + name, obscuredValues.contains(value));
    }

    if (!defaultHeaders) {
        for (String name : HttpObfuscator.DEFAULT_OBFUSCATED_HEADERS_LIST) {
            assertFalse("Un-necessarily obscured value of " + name, obscuredValues.contains(name));
        }
    }

    return hdrsMap;
}

From source file:net.sf.j2ep.requesthandlers.RequestHandlerBase.java

/**
 * Will write all request headers stored in the request to the method that
 * are not in the set of banned headers.
 * The Accept-Endocing header is also changed to allow compressed content
 * connection to the server even if the end client doesn't support that. 
 * A Via headers is created as well in compliance with the RFC.
 * //from www . j  a  v  a 2  s  .co  m
 * @param method The HttpMethod used for this connection
 * @param request The incoming request
 * @throws HttpException 
 */
protected void setHeaders(HttpMethod method, HttpServletRequest request) throws HttpException {
    Enumeration headers = request.getHeaderNames();
    String connectionToken = request.getHeader("connection");

    while (headers.hasMoreElements()) {
        String name = (String) headers.nextElement();
        boolean isToken = (connectionToken != null && name.equalsIgnoreCase(connectionToken));

        if (!isToken && !bannedHeaders.contains(name.toLowerCase())) {
            Enumeration value = request.getHeaders(name);
            while (value.hasMoreElements()) {
                method.addRequestHeader(name, (String) value.nextElement());
            }
        }
    }

    setProxySpecificHeaders(method, request);
}

From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>//from   ww  w . j  a v  a2  s . c  o m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9090/non_ok/PageNotFound.jsp
 */
@Test
public void testNotFound() throws Exception {

    String url = buildUrl("/non_ok/PageNotFound.jsp");
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>// www .  j a  v a 2 s  .co m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9090/non_ok/SendRedirect.jsp
 */
@Test
public void testRedirect() throws Exception {

    String url = buildUrl("/non_ok/SendRedirect.jsp");
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}