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.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@SuppressWarnings("deprecation")
@Test/*from w  w  w.ja  v  a2 s .co m*/
public void testGetPlainLong() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/booksplain";

    PostMethod post = new PostMethod(endpointAddress);
    post.addRequestHeader("Content-Type", "text/plain");
    post.addRequestHeader("Accept", "text/plain");
    post.setRequestBody("12345");
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        assertEquals(200, result);
        assertEquals(post.getResponseBodyAsString(), "12345");
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}

From source file:org.apache.forrest.http.client.PostFile.java

private PostMethod prepareFilePost(String solrBase, String srcUrl) throws IOException, MalformedURLException {
    PostMethod filePost = new PostMethod(solrBase);
    filePost.addRequestHeader("Content-type", "text/xml; charset=utf-8");
    filePost.addRequestHeader("User-Agent", AGENT);
    filePost.setRequestBody(new URL(srcUrl).openStream());
    return filePost;
}

From source file:org.apache.forrest.http.client.PostFile.java

private PostMethod prepareFilePost(String solrBase, InputStream src) throws IOException, MalformedURLException {
    PostMethod filePost = new PostMethod(solrBase);
    filePost.addRequestHeader("Content-type", "text/xml; charset=utf-8");
    filePost.addRequestHeader("User-Agent", AGENT);
    filePost.setRequestBody(src);
    return filePost;
}

From source file:org.apache.manifoldcf.examples.ManifoldCFAPIConnect.java

/** Perform an API POST operation.
*@param restPath is the URL path of the REST object, starting with "/".
*@param input is the input JSON.//from w w w .j  a v  a  2 s.c om
*@return the json response.
*/
public String performAPIRawPostOperation(String restPath, String input) throws IOException {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(formURL(restPath));
    method.setRequestHeader("Content-type", "text/plain; charset=UTF-8");
    method.setRequestBody(input);
    int response = client.executeMethod(method);
    byte[] responseData = method.getResponseBody();
    // We presume that the data is utf-8, since that's what the API
    // uses throughout.
    String responseString = new String(responseData, "utf-8");
    if (response != HttpStatus.SC_CREATED)
        throw new IOException("API http error; expected " + HttpStatus.SC_CREATED + ", saw "
                + Integer.toString(response) + ": " + responseString);
    return responseString;
}

From source file:org.apache.oodt.security.sso.opensso.SSOProxy.java

public String authenticate(String username, String password) {
    HttpClient httpClient = new HttpClient();
    PostMethod post = new PostMethod(AUTH_ENDPOINT);
    String response;//ww  w.ja  v  a  2 s  . co m
    String ssoToken = null;

    NameValuePair[] data = { new NameValuePair("username", username), new NameValuePair("password", password),
            new NameValuePair("uri", "realm/lmmp") };

    post.setRequestBody(data);

    try {
        httpClient.executeMethod(post);
        if (post.getStatusCode() != HttpStatus.SC_OK) {
            throw new HttpException(post.getStatusLine().toString());
        }
        response = post.getResponseBodyAsString().trim();
        ssoToken = response.substring(9);
    } catch (Exception e) {
        LOG.log(Level.SEVERE, e.getMessage());
    } finally {
        post.releaseConnection();
    }

    return ssoToken;
}

From source file:org.apache.oodt.security.sso.opensso.SSOProxy.java

public IdentityDetails readIdentity(String username, String token) throws IOException, SingleSignOnException {
    HttpClient httpClient = new HttpClient();
    PostMethod post = new PostMethod(IDENT_READ_ENDPOINT);
    LOG.log(Level.INFO, "Obtaining identity: username: [" + username + "]: token: [" + token + "]: REST url: ["
            + IDENT_READ_ENDPOINT + "]");
    NameValuePair[] data = { new NameValuePair("name", username), new NameValuePair("admin", token) };

    post.setRequestBody(data);

    httpClient.executeMethod(post);/*from w w  w  .  ja v a 2  s  .c o m*/
    if (post.getStatusCode() != HttpStatus.SC_OK) {
        throw new SingleSignOnException(post.getStatusLine().toString());
    }

    return parseIdentityDetails(post.getResponseBodyAsString().trim());

}

From source file:org.apache.oodt.security.sso.opensso.SSOProxy.java

public UserDetails getUserAttributes(String token) throws IOException, SingleSignOnException {
    HttpClient httpClient = new HttpClient();
    PostMethod post = new PostMethod(IDENT_ATTR_ENDPOINT);
    LOG.log(Level.INFO,//  w  ww  .jav  a 2s  . c  om
            "Obtaining user attributes: token: [" + token + "]: REST url: [" + IDENT_ATTR_ENDPOINT + "]");
    NameValuePair[] data = { new NameValuePair("subjectid", token) };

    post.setRequestBody(data);

    httpClient.executeMethod(post);
    if (post.getStatusCode() != HttpStatus.SC_OK) {
        throw new SingleSignOnException(post.getStatusLine().toString());
    }

    return parseUserDetails(post.getResponseBodyAsString().trim());

}

From source file:org.apache.oodt.security.sso.opensso.SSOProxy.java

public void logout(String token) {
    HttpClient httpClient = new HttpClient();
    PostMethod post = new PostMethod(LOG_ENDPOINT);
    LOG.log(Level.INFO, "Logging out: token: [" + token + "]: REST url: [" + LOG_ENDPOINT + "]");
    NameValuePair[] data = { new NameValuePair("subjectid", token) };
    post.setRequestBody(data);

    try {/*from  w ww  .  ja va 2 s  .  c o m*/
        httpClient.executeMethod(post);
        if (post.getStatusCode() != HttpStatus.SC_OK) {
            throw new HttpException(post.getStatusLine().toString());
        }
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        LOG.log(Level.SEVERE, e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        LOG.log(Level.SEVERE, e.getMessage());
    } finally {
        post.releaseConnection();
    }
}

From source file:org.apache.servicemix.http.HttpSoapTest.java

public void testSoapFault12() throws Exception {
    TransformComponentSupport echo = new TransformComponentSupport() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            Fault f = exchange.createFault();
            f.setContent(new StringSource("<hello xmlns='myuri'>this is a fault</hello>"));
            f.setProperty(JBIMarshaler.SOAP_FAULT_REASON, "My reason");
            throw new FaultException(null, exchange, f);
        }/*  w w  w  .  ja  v a 2 s  .c om*/
    };
    echo.setService(new QName("urn:test", "echo"));
    echo.setEndpoint("echo");
    container.activateComponent(echo, "echo");

    HttpEndpoint ep1 = createInOutEndpoint("ep1");
    ep1.setTargetService(new QName("urn:test", "echo"));
    ep1.setTargetEndpoint("echo");
    ep1.setLocationURI("http://localhost:8193/ep1/");
    ep1.setRoleAsString("consumer");
    ep1.setSoap(true);

    HttpEndpoint ep2 = createInOutEndpoint("ep2");
    ep2.setTargetService(new QName("urn:test", "http"));
    ep2.setTargetEndpoint("ep3");
    ep2.setLocationURI("http://localhost:8193/ep2/");
    ep2.setRoleAsString("consumer");
    ep2.setSoap(true);

    HttpEndpoint ep3 = createInOutEndpoint("ep3");
    ep3.setLocationURI("http://localhost:8193/ep1/");
    ep3.setRoleAsString("provider");
    ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
    ep3.setSoap(true);

    HttpComponent http = new HttpComponent();
    http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 });
    container.activateComponent(http, "http1");

    container.start();

    PostMethod method = new PostMethod("http://localhost:8193/ep2/");
    method.setRequestEntity(
            new InputStreamRequestEntity(getClass().getResourceAsStream("soap-request-12.xml")));
    int state = new HttpClient().executeMethod(method);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
    SourceTransformer st = new SourceTransformer();
    Node node = st.toDOMNode(new StreamSource(method.getResponseBodyAsStream()));
    logger.info(st.toString(node));

    Element e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtil.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtil.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));

    method = new PostMethod("http://localhost:8193/ep2/");
    method.setRequestBody("hello");
    state = new HttpClient().executeMethod(method);
    String str = method.getResponseBodyAsString();
    logger.info(str);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
    node = st.toDOMNode(new StringSource(str));
    e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtil.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtil.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));

    method = new PostMethod("http://localhost:8193/ep2/");
    method.setRequestBody("<hello/>");
    state = new HttpClient().executeMethod(method);
    str = method.getResponseBodyAsString();
    logger.info(str);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, state);
    node = st.toDOMNode(new StringSource(str));
    e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));
}

From source file:org.apache.servicemix.http.HttpSoapTest.java

public void testSoapFault11() throws Exception {
    TransformComponentSupport echo = new TransformComponentSupport() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            Fault f = exchange.createFault();
            f.setContent(new StringSource("<hello xmlns='myuri'>this is a fault</hello>"));
            f.setProperty(JBIMarshaler.SOAP_FAULT_REASON, "My reason");
            throw new FaultException(null, exchange, f);
        }/*from   w  ww.  ja v  a 2 s.  c  om*/
    };
    echo.setService(new QName("urn:test", "echo"));
    echo.setEndpoint("echo");
    container.activateComponent(echo, "echo");

    HttpEndpoint ep1 = createInOutEndpoint("ep1");
    ep1.setTargetService(new QName("urn:test", "echo"));
    ep1.setTargetEndpoint("echo");
    ep1.setLocationURI("http://localhost:8194/ep1/");
    ep1.setRoleAsString("consumer");
    ep1.setSoap(true);
    ep1.setSoapVersion("1.1");

    HttpEndpoint ep2 = createInOutEndpoint("ep2");
    ep2.setTargetService(new QName("urn:test", "http"));
    ep2.setTargetEndpoint("ep3");
    ep2.setLocationURI("http://localhost:8194/ep2/");
    ep2.setRoleAsString("consumer");
    ep2.setSoap(true);
    ep2.setSoapVersion("1.1");

    HttpEndpoint ep3 = createInOutEndpoint("ep3");
    ep3.setLocationURI("http://localhost:8194/ep1/");
    ep3.setRoleAsString("provider");
    ep3.setSoap(true);
    ep3.setSoapVersion("1.1");

    HttpComponent http = new HttpComponent();
    http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 });
    container.activateComponent(http, "http1");

    container.start();

    PostMethod method = new PostMethod("http://localhost:8194/ep2/");
    method.setRequestEntity(new InputStreamRequestEntity(getClass().getResourceAsStream("soap-request.xml")));
    int state = new HttpClient().executeMethod(method);
    String str = method.getResponseBodyAsString();
    logger.info(str);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
    SourceTransformer st = new SourceTransformer();
    Node node = st.toDOMNode(new StringSource(str));

    Element e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));

    method = new PostMethod("http://localhost:8194/ep2/");
    method.setRequestBody("hello");
    state = new HttpClient().executeMethod(method);
    str = method.getResponseBodyAsString();
    logger.info(str);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state);
    node = st.toDOMNode(new StringSource(str));
    e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));

    method = new PostMethod("http://localhost:8194/ep2/");
    method.setRequestBody("<hello/>");
    state = new HttpClient().executeMethod(method);
    str = method.getResponseBodyAsString();
    logger.info(str);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, state);
    node = st.toDOMNode(new StringSource(str));
    e = ((Document) node).getDocumentElement();
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e));
    e = DOMUtils.getFirstChildElement(e);
    assertEquals(new QName(SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e));
}