Example usage for org.apache.commons.httpclient.methods PostMethod setRequestBody

List of usage examples for org.apache.commons.httpclient.methods PostMethod setRequestBody

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod setRequestBody.

Prototype

public void setRequestBody(NameValuePair[] paramArrayOfNameValuePair) throws IllegalArgumentException 

Source Link

Usage

From source file:org.apache.servicemix.samples.bridge.BridgeTest.java

public void testSimpleCall() throws Exception {

    ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Destination outQueue = new ActiveMQQueue("bridge.output");
    Connection connection = factory.createConnection();
    connection.start();/*  w  w  w . j av  a 2 s  .  co  m*/
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer createConsumer = session.createConsumer(outQueue);
    createConsumer.setMessageListener(new MessageListener() {

        public void onMessage(Message arg0) {
            BridgeTest.recieved = true;
            ActiveMQTextMessage msg = (ActiveMQTextMessage) arg0;
            assertNotNull(msg);
        }

    });

    HttpClient client = new HttpClient();

    PostMethod post = new PostMethod(url);
    post.setRequestBody(new FileInputStream("src/test/resources/request.xml"));

    int executeMethod = client.executeMethod(post);

    assertEquals(202, executeMethod);

    int maxTry = 100;
    while (!recieved || maxTry == 0) {
        Thread.sleep(200);
        maxTry--;
    }

    session.close();
    connection.close();

}

From source file:org.apache.sling.commons.testing.integration.HttpTestBase.java

/** Execute a POST request and check status
 * @return the HttpMethod executed//  w  w  w.jav a2  s. com
 * @throws IOException */
public HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = httpClient.executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.accessManager.AbstractAccessManagerTest.java

/** Execute a POST request and check status */
protected void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode,
        List<NameValuePair> postParams, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);/*from  w  w w.  j av  a 2 s .  c om*/

    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }

        final int status = httpClient.executeMethod(post);
        if (assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.auth.AuthenticationResponseCodeTest.java

protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        List<Header> headers, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);/*from  ww w  .  ja va  2s . co m*/

    if (headers != null) {
        for (Header header : headers) {
            post.addRequestHeader(header);
        }
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = H.getHttpClient().executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.AuthenticatedTestUtil.java

/** Execute a POST request and check status */
public void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode,
        List<NameValuePair> postParams, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);//from  w w  w .j  ava 2  s.  com

    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }

        final int status = httpClient.executeMethod(post);
        if (assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.AuthenticatedTestUtil.java

/** retrieve the contents of given URL and assert its content type
 * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
 * @throws IOException// ww  w . j  av a  2 s  . c  o  m
 * @throws HttpException */
public String getAuthenticatedPostContent(Credentials creds, String url, String expectedContentType,
        List<NameValuePair> postParams, int expectedStatusCode) throws IOException {
    final PostMethod post = new PostMethod(url);

    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }

        final int status = httpClient.executeMethod(post);
        final InputStream is = post.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = post.getResponseCharSet();
        final byte[] buffer = new byte[16384];
        int n = 0;
        while ((n = is.read(buffer, 0, buffer.length)) > 0) {
            content.append(new String(buffer, 0, n, charset));
        }
        assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")",
                expectedStatusCode, status);
        final Header h = post.getResponseHeader("Content-Type");
        if (expectedContentType == null) {
            if (h != null) {
                fail("Expected null Content-Type, got " + h.getValue());
            }
        } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
            // no check
        } else if (h == null) {
            fail("Expected Content-Type that starts with '" + expectedContentType
                    + " but got no Content-Type header at " + url);
        } else {
            assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url
                    + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
        }
        return content.toString();

    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.login.RedirectOnLoginErrorTest.java

/** Execute a POST request and check status
  * @return the HttpMethod executed//from   w  w  w  .  ja  va  2 s. c  o  m
  * @throws IOException */
private HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        String assertMessage, String referer) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setDoAuthentication(false);

    //set the referer to indicate where we came from
    post.setRequestHeader("Referer", referer);

    //set Accept header to trick sling into treating the request as from a browser
    post.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = httpClient.executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.login.SelectorRedirectOnLoginErrorTest.java

/** Execute a POST request and check status
  * @return the HttpMethod executed//  ww w. j av  a 2  s. co m
  * @throws IOException */
private HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        String assertMessage, String referer) throws IOException {
    // TODO - method copied from org.apache.sling.launchpad.webapp.integrationtest.login.RedirectOnLoginErrorTest
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setDoAuthentication(false);

    //set the referer to indicate where we came from
    post.setRequestHeader("Referer", referer);

    //set Accept header to trick sling into treating the request as from a browser
    post.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = httpClient.executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.archive.wayback.resourcestore.http.FileLocationDBClient.java

private void doPostMethod(final String operation, final String arcName, final String arcUrl)
        throws IOException {
    PostMethod method = new PostMethod(serverUrl);
    NameValuePair[] data = { new NameValuePair(FileLocationDBServlet.OPERATION_ARGUMENT, operation),
            new NameValuePair(FileLocationDBServlet.NAME_ARGUMENT, arcName),
            new NameValuePair(FileLocationDBServlet.URL_ARGUMENT, arcUrl) };
    method.setRequestBody(data);
    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("Method failed: " + method.getStatusLine());
    }//from   w  w w . java  2  s .  com
    String responseString = method.getResponseBodyAsString();
    if (!responseString.startsWith(OK_RESPONSE_PREFIX)) {
        throw new IOException(responseString);
    }
}

From source file:org.archive.wayback.resourcestore.locationdb.RemoteResourceFileLocationDB.java

private void doPostMethod(final String operation, final String arcName, final String arcUrl)
        throws IOException {
    PostMethod method = new PostMethod(serverUrl);
    NameValuePair[] data = { new NameValuePair(ResourceFileLocationDBServlet.OPERATION_ARGUMENT, operation),
            new NameValuePair(ResourceFileLocationDBServlet.NAME_ARGUMENT, arcName),
            new NameValuePair(ResourceFileLocationDBServlet.URL_ARGUMENT, arcUrl) };
    method.setRequestBody(data);
    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("Method failed: " + method.getStatusLine());
    }/*  w  w  w. j  ava2  s.  com*/
    String responseString = method.getResponseBodyAsString();
    if (!responseString.startsWith(OK_RESPONSE_PREFIX)) {
        throw new IOException(responseString);
    }
}