org.apache.commons.httpclient.HttpVersion Java Examples

The following examples show how to use org.apache.commons.httpclient.HttpVersion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ESBJAVA4846HttpProtocolVersionTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Sending HTTP1.1 message")
public void sendingHTTP11Message() throws Exception {
    PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
    RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "urn:getQuote");
    HttpMethodParams params = new HttpMethodParams();
    params.setVersion(HttpVersion.HTTP_1_1);
    post.setParams(params);
    HttpClient httpClient = new HttpClient();
    String httpVersion = "";

    try {
        httpClient.executeMethod(post);
        post.getResponseBodyAsString();
        httpVersion = post.getStatusLine().getHttpVersion();
    } finally {
        post.releaseConnection();
    }
    Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_1.toString(), "Http version mismatched");
}
 
Example #2
Source File: ESBJAVA4846HttpProtocolVersionTestCase.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Sending HTTP1.0 message")
public void sendingHTTP10Message() throws Exception {
    PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
    RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "urn:getQuote");
    HttpMethodParams params = new HttpMethodParams();
    params.setVersion(HttpVersion.HTTP_1_0);
    post.setParams(params);
    HttpClient httpClient = new HttpClient();
    String httpVersion = "";

    try {
        httpClient.executeMethod(post);
        post.getResponseBodyAsString();
        httpVersion = post.getStatusLine().getHttpVersion();
    } finally {
        post.releaseConnection();
    }
    Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_0.toString(), "Http version mismatched");

}
 
Example #3
Source File: ESBJAVA4846HttpProtocolVersionTestCase.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Sending HTTP1.1 message")
public void sendingHTTP11Message() throws Exception {
    PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
    RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "urn:getQuote");
    HttpMethodParams params = new HttpMethodParams();
    params.setVersion(HttpVersion.HTTP_1_1);
    post.setParams(params);
    HttpClient httpClient = new HttpClient();
    String httpVersion = "";

    try {
        httpClient.executeMethod(post);
        post.getResponseBodyAsString();
        httpVersion = post.getStatusLine().getHttpVersion();
    } finally {
        post.releaseConnection();
    }
    Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_1.toString(), "Http version mismatched");
}
 
Example #4
Source File: EntityEnclosingMethod.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Generates <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt>
 * request header, as long as no <tt>Content-Length</tt> request header
 * already exists.
 *
 * @param state current state of http requests
 * @param conn the connection to use for I/O
 *
 * @throws IOException when errors occur reading or writing to/from the
 *         connection
 * @throws HttpException when a recoverable error occurs
 */
protected void addContentLengthRequestHeader(HttpState state,
                                             HttpConnection conn)
throws IOException, HttpException {
    LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader("
              + "HttpState, HttpConnection)");

    if ((getRequestHeader("content-length") == null) 
        && (getRequestHeader("Transfer-Encoding") == null)) {
        long len = getRequestContentLength();
        if (len < 0) {
            if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
                addRequestHeader("Transfer-Encoding", "chunked");
            } else {
                throw new ProtocolException(getEffectiveVersion() + 
                    " does not support chunk encoding");
            }
        } else {
            addRequestHeader("Content-Length", String.valueOf(len));
        }
    }
}
 
Example #5
Source File: ExpectContinueMethod.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Sets the <tt>Expect</tt> header if it has not already been set, 
 * in addition to the "standard" set of headers.
 *
 * @param state the {@link HttpState state} information associated with this method
 * @param conn the {@link HttpConnection connection} used to execute
 *        this HTTP method
 *
 * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
 *                     can be recovered from.
 * @throws HttpException  if a protocol exception occurs. Usually protocol exceptions 
 *                    cannot be recovered from.
 */
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException {
    LOG.trace("enter ExpectContinueMethod.addRequestHeaders(HttpState, HttpConnection)");
    
    super.addRequestHeaders(state, conn);
    // If the request is being retried, the header may already be present
    boolean headerPresent = (getRequestHeader("Expect") != null);
    // See if the expect header should be sent
    // = HTTP/1.1 or higher
    // = request body present

    if (getParams().isParameterTrue(HttpMethodParams.USE_EXPECT_CONTINUE) 
    && getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1) 
    && hasRequestContent())
    {
        if (!headerPresent) {
            setRequestHeader("Expect", "100-continue");
        }
    } else {
        if (headerPresent) {
            removeRequestHeader("Expect");
        }
    }
}
 
Example #6
Source File: ESBJAVA4846HttpProtocolVersionTestCase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Sending HTTP1.0 message")
public void sendingHTTP10Message() throws Exception {
    PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
    RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "urn:getQuote");
    HttpMethodParams params = new HttpMethodParams();
    params.setVersion(HttpVersion.HTTP_1_0);
    post.setParams(params);
    HttpClient httpClient = new HttpClient();
    String httpVersion = "";

    try {
        httpClient.executeMethod(post);
        post.getResponseBodyAsString();
        httpVersion = post.getStatusLine().getHttpVersion();
    } finally {
        post.releaseConnection();
    }
    Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_0.toString(), "Http version mismatched");

}
 
Example #7
Source File: HttpMethodParams.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns {@link HttpVersion HTTP protocol version} to be used by the 
 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that 
 * this collection of parameters applies to. 
 *
 * @return {@link HttpVersion HTTP protocol version}
 */
public HttpVersion getVersion() { 
    Object param = getParameter(PROTOCOL_VERSION);
    if (param == null) {
        return HttpVersion.HTTP_1_1;
    }
    return (HttpVersion)param;
}
 
Example #8
Source File: CloudMetadataScanner.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
private static HttpMethod createRequestMethod(
        HttpRequestHeader header, HttpBody body, HttpMethodParams params) throws URIException {
    HttpMethod httpMethod = new ZapGetMethod();
    httpMethod.setURI(header.getURI());
    httpMethod.setParams(params);
    params.setVersion(HttpVersion.HTTP_1_1);

    String msg = header.getHeadersAsString();

    String[] split = Pattern.compile("\\r\\n", Pattern.MULTILINE).split(msg);
    String token = null;
    String name = null;
    String value = null;

    int pos = 0;
    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }

        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);
    }
    if (body != null && body.length() > 0 && (httpMethod instanceof EntityEnclosingMethod)) {
        EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
        post.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
    }
    httpMethod.setFollowRedirects(false);
    return httpMethod;
}
 
Example #9
Source File: VmRuntimeJettyKitchenSink2Test.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the deferredTask handler is installed.
 */
public void testDeferredTask() throws Exception {
  // Replace the API proxy delegate so we can fake API responses.
  FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
  ApiProxy.setDelegate(fakeApiProxy);

  // Add a api response so the task queue api is happy.
  TaskQueueBulkAddResponse taskAddResponse = new TaskQueueBulkAddResponse();
  TaskResult taskResult = taskAddResponse.addTaskResult();
  taskResult.setResult(ErrorCode.OK.getValue());
  taskResult.setChosenTaskName("abc");
  fakeApiProxy.addApiResponse(taskAddResponse);

  // Issue a deferredTaskRequest with payload.
  String testData = "0987654321acbdefghijklmn";
  String[] lines = fetchUrl(createUrl("/testTaskQueue?deferredTask=1&deferredData=" + testData));
  TaskQueueBulkAddRequest request = new TaskQueueBulkAddRequest();
  request.parseFrom(fakeApiProxy.getLastRequest().requestData);
  assertEquals(1, request.addRequestSize());
  TaskQueueAddRequest addRequest = request.getAddRequest(0);
  assertEquals(TaskQueueAddRequest.RequestMethod.POST.getValue(), addRequest.getMethod());

  // Pull out the request and fire it at the app.
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  PostMethod post = new PostMethod(createUrl(addRequest.getUrl()).toString());
  post.getParams().setVersion(HttpVersion.HTTP_1_0);
  // Add the required Task queue header, plus any headers from the request.
  post.addRequestHeader("X-AppEngine-QueueName", "1");
  for (TaskQueueAddRequest.Header header : addRequest.headers()) {
    post.addRequestHeader(header.getKey(), header.getValue());
  }
  post.setRequestEntity(new ByteArrayRequestEntity(addRequest.getBodyAsBytes()));
  int httpCode = httpClient.executeMethod(post);
  assertEquals(HttpURLConnection.HTTP_OK, httpCode);

  // Verify that the task was handled and that the payload is correct.
  lines = fetchUrl(createUrl("/testTaskQueue?getLastPost=1"));
  assertEquals("deferredData:" + testData, lines[lines.length - 1]);
}
 
Example #10
Source File: HttpMethodParams.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Assigns the {@link HttpVersion HTTP protocol version} to be used by the 
 * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that 
 * this collection of parameters applies to. 
 *
 * @param version the {@link HttpVersion HTTP protocol version}
 */
public void setVersion(HttpVersion version) {
    setParameter(PROTOCOL_VERSION, version);
}