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

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

Introduction

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

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:com.groupon.jenkins.dotci.notifiers.WebhookNotifier.java

@Override
protected boolean notify(DynamicBuild build, BuildListener listener) {
    Map<String, ?> options = (Map<String, ?>) getOptions();
    HttpClient client = getHttpClient();
    String requestUrl = (String) options.get("url");
    PostMethod post = new PostMethod(requestUrl);

    Map<String, String> payload = (Map<String, String>) options.get("payload");
    ObjectMapper objectMapper = new ObjectMapper();

    try {//from w ww  . j  a  v  a  2  s . c om
        String payloadJson = objectMapper.writeValueAsString(payload);
        StringRequestEntity requestEntity = new StringRequestEntity(payloadJson, "application/json", "UTF-8");
        post.setRequestEntity(requestEntity);
        int statusCode = client.executeMethod(post);
        listener.getLogger().println(
                "Posted Paylod " + payloadJson + " to " + requestUrl + " with response code " + statusCode);
    } catch (Exception e) {
        listener.getLogger().print("Failed to make a POST to webhook. Check Jenkins logs for exceptions.");
        LOGGER.log(Level.WARNING, "Error posting to webhook", e);
        return false;
    } finally {
        post.releaseConnection();
    }
    return false;
}

From source file:example.crm.CrmIT.java

/**
 * HTTP POST http://localhost:9003/customers is used to upload the contents of
 * the add_customer.xml file to add a new customer to the system.
 * <p/>/*from   w w w.  j  a  v  a2  s . co  m*/
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
public void postCustomerTest() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.xml").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/xml");
    RequestEntity entity = new FileRequestEntity(input, "application/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'camel-netty4-http' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'camel-netty4-http' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:example.crm.CrmIT.java

/**
 * HTTP POST http://localhost:9003/customers is used to upload the contents of
 * the add_customer.json file to add a new customer to the system.
 * <p/>//ww  w  . j a v a2 s  .c  o m
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
public void postCustomerTestJson() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.json").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/json");
    RequestEntity entity = new FileRequestEntity(input, "application/json; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'camel-netty4-http' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'camel-netty4-http' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:com.example.CrmTest.java

/**
 * HTTP POST http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the add_customer.xml file to add a new customer to the system.
 * <p/>//from  w w w . j  a v  a  2 s  .c o m
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
public void postCustomerTest() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.xml").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/xml");
    RequestEntity entity = new FileRequestEntity(input, "application/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'cxf-cdi' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'cxf-cdi' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:com.example.CrmTest.java

/**
 * HTTP POST http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the add_customer.json file to add a new customer to the system.
 * <p/>//from w  ww. j a  v  a  2 s.c om
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
public void postCustomerTestJson() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.json").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/json");
    RequestEntity entity = new FileRequestEntity(input, "application/json; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'cxf-cdi' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'cxf-cdi' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:com.bdaum.juploadr.uploadapi.locrrest.upload.LocrUpload.java

@Override
public boolean execute() throws ProtocolException, CommunicationException {
    HttpClient httpClient = HttpClientFactory.getHttpClient(getSession().getAccount());

    // checkAuthorization(client);
    this.monitor.uploadStarted(new UploadEvent(image, 0, true, false));

    PostMethod post = new PostMethod(POSTURL);
    List<Part> parts = getParts();
    MultipartRequestEntity entity = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]),
            post.getParams());/*w  w  w  .j  a v  a 2 s.  c  o  m*/
    post.setRequestEntity(entity);

    try {

        int status = httpClient.executeMethod(post);
        if (status == HttpStatus.SC_OK) {
            // deal with the response
            try {
                String response = post.getResponseBodyAsString();
                post.releaseConnection();
                boolean success = parseResponse(response);
                if (success) {
                    image.setState(UploadImage.STATE_UPLOADED);
                    ImageUploadResponse resp = new ImageUploadResponse(
                            ((LocrUploadResponseHandler) handler).getPhotoID());
                    this.monitor.uploadFinished(new UploadCompleteEvent(resp, image));

                } else {
                    throw new UploadFailedException(Messages.getString("juploadr.ui.error.status")); //$NON-NLS-1$
                }

            } catch (IOException e) {
                // TODO: Is it safe to assume the upload failed here?
                this.fail(Messages.getString("juploadr.ui.error.response.unreadable") //$NON-NLS-1$
                        + e.getMessage(), e);
            }
        } else {
            this.fail(Messages.getString("juploadr.ui.error.bad.http.response", status), null); //$NON-NLS-1$
        }
    } catch (ConnectException ce) {
        this.fail(Messages.getString("juploadr.ui.error.unable.to.connect"), ce); //$NON-NLS-1$
    } catch (NoRouteToHostException route) {
        this.fail(Messages.getString("juploadr.ui.error.no.internet"), route); //$NON-NLS-1$
    } catch (UnknownHostException uhe) {
        this.fail(Messages.getString("juploadr.ui.error.unknown.host"), uhe); //$NON-NLS-1$

    } catch (HttpException e) {
        this.fail(Messages.getString("juploadr.ui.error.http.exception") + e, e); //$NON-NLS-1$
    } catch (IOException e) {
        this.fail(Messages.getString("juploadr.ui.error.simple.ioexception") + e.getMessage() + "" //$NON-NLS-1$ //$NON-NLS-2$
                + e, e);
    }
    return true;
}

From source file:com.curl.orb.client.ClientUtil.java

Object requestToORB(InstanceManagementRequest request, String uri, boolean hasManyResult)
        throws ORBClientException, ORBServerException {
    AbstractSerializer serializer = SerializerFactory.getInstance().getSerializer();
    OutputStream outputStream = new ByteArrayOutputStream();
    Object returnedObject = null;
    InputStream inputStream = null;
    PostMethod method = null;
    try {/* www. j  av a  2s .  c  o m*/
        serializer.serialize(request, null, outputStream);
        httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        method = new PostMethod(uri);
        method.setRequestHeader("Content-Length", ((ByteArrayOutputStream) outputStream).size() + "");
        method.setRequestEntity(new ByteArrayRequestEntity(((ByteArrayOutputStream) outputStream).toByteArray(),
                "application/octet-stream; charset=UTF-8"));
        httpClient.executeMethod(method);
        method.getResponseBody();
        inputStream = (ByteArrayInputStream) method.getResponseBodyAsStream();
        SerializableStreamReader deserializer = new CurlSerializableStreamReader(inputStream);
        returnedObject = ((InstanceManagementResponse) deserializer.read()).getBody();
        if (hasManyResult) {
            List<Object> resultList = new ArrayList<Object>();
            resultList.add(returnedObject);
            Object innerresult = null;
            while ((innerresult = deserializer.read()) != null)
                resultList.add(innerresult);
            return resultList;
        }
        if (returnedObject instanceof ExceptionContent) {
            throw new ORBServerException((ExceptionContent) returnedObject);
        }
    } catch (SerializerException e) {
        throw new ORBClientException(e);
    } catch (HttpException e) {
        throw new ORBClientException(e);
    } catch (IOException e) {
        throw new ORBClientException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                throw new ORBClientException(e);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        throw new ORBClientException(e);
                    }
                }
                method.releaseConnection();
            }
        }
    }
    return returnedObject;
}

From source file:io.fabric8.quickstarts.restdsl.spark.CrmIT.java

/**
 * HTTP POST http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the add_customer.xml file to add a new customer to the system.
 * <p/>//ww  w .j a  v a 2s. c  o  m
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
@Ignore
public void postCustomerTest() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.xml").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/xml");
    RequestEntity entity = new FileRequestEntity(input, "application/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'rest' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'rest' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:io.fabric8.quickstarts.fabric.rest.secure.CrmSecureTest.java

/**
 * HTTP POST http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the add_customer.xml file to add a new customer to the system.
 * <p/>/*  ww w.  j av a 2 s. c o  m*/
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
public void postCustomerTest() throws IOException {
    LOG.info("============================================");
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.xml").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.getHostAuthState().setAuthScheme(scheme);
    post.addRequestHeader("Accept", "text/xml");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);

    String res = "";

    try {
        int result = httpClient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'rest' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'rest' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}

From source file:io.fabric8.quickstarts.restdsl.spark.CrmIT.java

/**
 * HTTP POST http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the add_customer.json file to add a new customer to the system.
 * <p/>/*w ww  .j  ava2 s.  co m*/
 * On the server side, it matches the CustomerService's addCustomer() method
 *
 * @throws Exception
 */
@Test
@Ignore
public void postCustomerTestJson() throws IOException {
    LOG.info("Sent HTTP POST request to add customer");
    String inputFile = this.getClass().getResource("/add_customer.json").getFile();
    File input = new File(inputFile);
    PostMethod post = new PostMethod(CUSTOMER_SERVICE_URL);
    post.addRequestHeader("Accept", "application/json");
    RequestEntity entity = new FileRequestEntity(input, "application/json; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    String res = "";

    try {
        int result = httpclient.executeMethod(post);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        res = post.getResponseBodyAsString();
        LOG.info(res);
    } catch (IOException e) {
        LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
        LOG.error(
                "You should build the 'rest' quick start and deploy it to a local Fabric8 before running this test");
        LOG.error("Please read the README.md file in 'rest' quick start root");
        Assert.fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    Assert.assertTrue(res.contains("Jack"));

}