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.GzipFilterTest.java

/**
 * When the servlet container forwards to a page does it work?
 * <p/>/*from  w  w w .  j  a  va2  s. co  m*/
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/ForwardFromGzip.jsp
 */
public void testForward() throws Exception {

    String url = "http://localhost:9080/non_ok/ForwardFromGzip.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);
    assertEquals("gzip", httpMethod.getResponseHeader("Content-Encoding").getValue());
}

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

/**
 * A 0 length body should give a 0 length gzip body and content length
 * <p/>//from  w ww  .j  a v a 2 s .c  o  m
 * 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_gzip/empty.html
 */
public void testZeroLengthHTML() throws Exception {

    String url = "http://localhost:9080/empty_gzip/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);
    assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    checkNullOrZeroContentLength(httpMethod);
}

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

/**
 * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
 * 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 ww  . j  a v a  2 s. c om*/
 * <p/>
 * Manual test: wget -d --server-response --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp
 */
public void testNotModifiedJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_gzip/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);
    byte[] responseBody = httpMethod.getResponseBody();
    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.GzipFilterTest.java

/**
 * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
 * 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  va 2s  . com
 * <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_gzip/SC_NO_CONTENT.jsp
 */
public void testNoContentJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_gzip/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);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);
}

From source file:eu.eco2clouds.scheduler.em.EMClientHC.java

private void setHeaders(HttpMethod method, String group) {
    method.addRequestHeader(SchedulerDictionary.X_BONFIRE_ASSERTED_SELECTED_GROUP, group);
    setHeaders(method);// www .j a va 2  s.c om
}

From source file:com.smartitengineering.util.rest.client.jersey.cache.CustomApacheHttpClientResponseResolver.java

private void addHeaders(Headers headers, HttpMethod method) {
    if (!headers.isEmpty()) {
        for (org.codehaus.httpcache4j.Header header : headers) {
            method.addRequestHeader(header.getName(), header.getValue());
        }/*from  www  .j  a v  a  2s. c om*/
    }
}

From source file:de.bermuda.arquillian.example.ContentTypeProxyServlet.java

private void copyRequestHeaders(HttpServletRequest req, HttpMethod post) {
    Enumeration<String> headerNames = req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        post.addRequestHeader(headerName, req.getHeader(headerName));
        log.info("Copying request header: " + headerName + ": " + req.getHeader(headerName));
    }//w w  w . j ava2 s.c o m
}

From source file:com.yahoo.flowetl.services.http.BaseHttpGenerator.java

/**
 * Applies any common properties to the http method based on properties of
 * the http params given.// ww  w .  j  a  v a 2  s.c  o m
 */
protected void applyCommonProperties(HttpParams in, HttpMethod toCall) {
    // common ops
    if (in.headers != null) {
        for (Entry<String, String> e : in.headers.entrySet()) {
            toCall.addRequestHeader(e.getKey(), e.getValue());
        }
    }
    // we handle our own retries
    toCall.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    if (StringUtils.isBlank(in.userAgent) == false && toCall.getRequestHeader(USER_AGENT_HEADER) == null) {
        toCall.setRequestHeader(USER_AGENT_HEADER, in.userAgent);
    } else if (toCall.getRequestHeader(USER_AGENT_HEADER) == null) {
        toCall.setRequestHeader(USER_AGENT_HEADER, userAgent);
    }
}

From source file:fr.unix_experience.owncloud_sms.engine.OCHttpClient.java

public int execute(HttpMethod req) throws IOException {
    String basicAuth = "Basic "
            + Base64.encodeToString((_username + ":" + _password).getBytes(), Base64.NO_WRAP);
    req.setDoAuthentication(true);/* ww  w  . ja  v  a 2  s  . c  om*/
    req.addRequestHeader("Authorization", basicAuth);
    executeMethod(req);
    return followRedirections(req);
}

From source file:eu.eco2clouds.scheduler.bonfire.BFClientSchedulerImpl.java

private void setHeaders(HttpMethod method) {
    method.addRequestHeader(SchedulerDictionary.X_BONFIRE_ASSERTED_ID, userId);
    method.addRequestHeader(SchedulerDictionary.X_BONFIRE_GROUPS_ID, groupId);
    method.addRequestHeader("User-Agent", SchedulerDictionary.USER_AGENT);
    method.addRequestHeader("Accept", SchedulerDictionary.ACCEPT);
}