Example usage for org.apache.commons.httpclient.methods FileRequestEntity FileRequestEntity

List of usage examples for org.apache.commons.httpclient.methods FileRequestEntity FileRequestEntity

Introduction

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

Prototype

public FileRequestEntity(final File file, final String contentType) 

Source Link

Usage

From source file:org.deegree.test.services.util.HTTPTempFile.java

/**
 * Create a new HTTPTempFile for POST request
 * //from  w w w.ja v a2  s . com
 * @param url
 * @param post
 *            the content for the POST request
 * @throws IOException
 */
public HTTPTempFile(String url, File post) throws IOException {
    PostMethod method = new PostMethod(url);
    method.setRequestEntity(new FileRequestEntity(post, "text/xml"));
    this.method = method;
    doRequest();
}

From source file:org.eclipse.ecf.remoteservice.rest.client.AbstractEntityRequestType.java

public RequestEntity generateRequestEntity(String uri, IRemoteCall call, IRemoteCallable callable,
        IRemoteCallParameter paramDefault, Object paramToSerialize) throws NotSerializableException {
    if (paramToSerialize instanceof RequestEntity)
        return (RequestEntity) paramToSerialize;
    switch (requestEntityType) {
    case INPUT_STREAM_REQUEST_ENTITY:
        if (paramToSerialize instanceof InputStream) {
            return new InputStreamRequestEntity((InputStream) paramToSerialize,
                    getContentLength(call, callable, paramDefault),
                    getContentType(call, callable, paramDefault));
        }/*from   w  ww .  j  a v a2 s .c o m*/
        throw new NotSerializableException(
                "Cannot generate request entity.  Expecting InputStream and got class=" //$NON-NLS-1$
                        + paramToSerialize.getClass().getName());
    case STRING_REQUEST_ENTITY:
        if (paramToSerialize instanceof String) {
            try {
                return new StringRequestEntity((String) paramToSerialize,
                        getContentType(call, callable, paramDefault), getCharset(call, callable, paramDefault));
            } catch (UnsupportedEncodingException e) {
                throw new NotSerializableException(
                        "Could not create request entity from call parameters: " + e.getMessage()); //$NON-NLS-1$
            }
        }
        throw new NotSerializableException("Cannot generate request entity.  Expecting String and got class=" //$NON-NLS-1$
                + paramToSerialize.getClass().getName());
    case BYTEARRAY_REQUEST_ENTITY:
        if (paramToSerialize instanceof byte[]) {
            return new ByteArrayRequestEntity((byte[]) paramToSerialize,
                    getContentType(call, callable, paramDefault));
        }
        throw new NotSerializableException("Cannot generate request entity.  Expecting byte[] and got class=" //$NON-NLS-1$
                + paramToSerialize.getClass().getName());
    case FILE_REQUEST_ENTITY:
        if (paramToSerialize instanceof File) {
            return new FileRequestEntity((File) paramToSerialize, getContentType(call, callable, paramDefault));
        }
        throw new NotSerializableException("Remote call parameter with name=" + paramDefault.getName() //$NON-NLS-1$
                + " is incorrect type for creating request entity."); //$NON-NLS-1$
    default:
        throw new NotSerializableException("Request entity generation not supported for this request type"); //$NON-NLS-1$
    }
}

From source file:org.fabric8.demo.cxf.test.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 .  ja  v  a  2  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 'rest' quick start and deploy it to a local Fuse 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:org.fabric8.demo.cxf.test.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/>/*  w w  w  .j  a  v a  2s.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 'rest' quick start and deploy it to a local Fuse 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:org.fabric8.demo.cxf.test.CrmTest.java

/**
 * HTTP PUT http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the update_customer.xml file to update the customer information for customer 123.
 * <p/>//from  w  w  w .  ja  v  a  2s . com
 * On the server side, it matches the CustomerService's updateCustomer() method
 *
 * @throws Exception
 */
@Test
public void putCutomerTest() throws IOException {

    LOG.info("Sent HTTP PUT request to update customer info");

    String inputFile = this.getClass().getResource("/update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod(CUSTOMER_SERVICE_URL);
    RequestEntity entity = new FileRequestEntity(input, "application/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    int result = 0;
    try {
        result = httpclient.executeMethod(put);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        LOG.info(put.getResponseBodyAsString());
    } 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 Fuse 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
        put.releaseConnection();
    }
    LOG.info("HTTP: " + result);

    Assert.assertEquals(result, 200);
}

From source file:org.jboss.examples.simpleRESTWS.client.Client.java

public static void main(String args[]) throws Exception {

    /**/*from  w  w w. ja  va2s .c o  m*/
     * HTTP GET http://localhost:8181/cxf/crm/customerservice/customers/123
     * returns the XML document representing customer 123
     *
     * On the server side, it matches the CustomerService's getCustomer() method
     */
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8181/cxf/crm/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    /**
     * HTTP GET http://localhost:8181/cxf/crm/customerservice/orders/223/products/323
     * returns the XML document representing product 323 in order 223
     *
     * On the server side, it matches the Order's getProduct() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:8181/cxf/crm/customerservice/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    /**
     * HTTP PUT http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
     * the update_customer.xml file to update the customer information for customer 123.
     *
     * On the server side, it matches the CustomerService's updateCustomer() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    /**
     * 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.
     *
     * On the server side, it matches the CustomerService's addCustomer() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("add_customer.xml").getFile();
    input = new File(inputFile);
    PostMethod post = new PostMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:org.jboss.fuse.examples.cxf.jaxrs.security.client.Client.java

public static void main(String args[]) throws Exception {
    // Now we need to use the basic authentication to send the request
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
    // Use basic authentication
    AuthScheme scheme = new BasicScheme();

    /**//from  w  ww  .  j a va2 s.co  m
     * HTTP GET http://localhost:8181/cxf/securecrm/customerservice/customers/123
     * returns the XML document representing customer 123
     *
     * On the server side, it matches the CustomerService's getCustomer() method
     */
    System.out.println("Sent HTTP GET request to query customer info with basic authentication info.");
    GetMethod get = new GetMethod("http://localhost:8181/cxf/securecrm/customerservice/customers/123");
    get.getHostAuthState().setAuthScheme(scheme);
    try {
        httpClient.executeMethod(get);
        System.out.println(get.getResponseBodyAsString());
    } finally {
        get.releaseConnection();
    }

    /**
     * HTTP GET http://localhost:8181/cxf/securecrm/customerservice/customers/123
     * without passing along authentication credentials - this will result in a security exception in the response.
     */
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query customer info without basic authentication info.");
    get = new GetMethod("http://localhost:8181/cxf/securecrm/customerservice/customers/123");
    try {
        httpClient.executeMethod(get);
        // we should get the security exception here
        System.out.println(get.getResponseBodyAsString());
    } finally {
        get.releaseConnection();
    }

    /**
     * HTTP GET http://localhost:8181/cxf/securecrm/customerservice/orders/223/products/323
     * returns the XML document representing product 323 in order 223
     *
     * On the server side, it matches the Order's getProduct() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    get = new GetMethod("http://localhost:8181/cxf/securecrm/customerservice/orders/223/products/323");
    get.getHostAuthState().setAuthScheme(scheme);
    try {
        httpClient.executeMethod(get);
        System.out.println(get.getResponseBodyAsString());
    } finally {
        get.releaseConnection();
    }

    /**
     * HTTP PUT http://localhost:8181/cxf/securecrm/customerservice/customers is used to upload the contents of
     * the update_customer.xml file to update the customer information for customer 123.
     *
     * On the server side, it matches the CustomerService's updateCustomer() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");

    String inputFile = Client.class.getResource("update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod("http://localhost:8181/cxf/securecrm/customerservice/customers");
    put.getHostAuthState().setAuthScheme(scheme);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);

    try {
        int result = httpClient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    /**
     * HTTP POST http://localhost:8181/cxf/securecrm/customerservice/customers is used to upload the contents of
     * the add_customer.xml file to add a new customer to the system.
     *
     * On the server side, it matches the CustomerService's addCustomer() method
     */
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = Client.class.getResource("add_customer.xml").getFile();
    input = new File(inputFile);
    PostMethod post = new PostMethod("http://localhost:8181/cxf/securecrm/customerservice/customers");
    post.getHostAuthState().setAuthScheme(scheme);
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);

    try {
        int result = httpClient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:org.jboss.quickstarts.fuse.rest.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  . ja v  a 2s . com
 * 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", "text/xml");
    RequestEntity entity = new FileRequestEntity(input, "text/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 Fuse 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:org.jboss.quickstarts.fuse.rest.CrmTest.java

/**
 * HTTP PUT http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
 * the update_customer.xml file to update the customer information for customer 123.
 * <p/>/*  w w w.j  a  v  a2 s  . c o m*/
 * On the server side, it matches the CustomerService's updateCustomer() method
 *
 * @throws Exception
 */
@Test
public void putCutomerTest() throws IOException {

    LOG.info("Sent HTTP PUT request to update customer info");

    String inputFile = this.getClass().getResource("/update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod(CUSTOMER_SERVICE_URL);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    int result = 0;
    try {
        result = httpclient.executeMethod(put);
        LOG.info("Response status code: " + result);
        LOG.info("Response body: ");
        LOG.info(put.getResponseBodyAsString());
    } 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 Fuse 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
        put.releaseConnection();
    }

    Assert.assertEquals(result, 200);
}

From source file:org.jboss.quickstarts.fuse.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/>//www . j ava 2  s  . c  om
 * 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 Fuse 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"));

}