Search in sources :

Example 81 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project fuse-karaf by jboss-fuse.

the class CrmTest method putCustomerTest.

/**
 * 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/>
 * On the server side, it matches the CustomerService's updateCustomer() method
 *
 * @throws Exception
 */
@Test
public void putCustomerTest() 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");
        fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
    assertEquals(result, 200);
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) File(java.io.File) Test(org.junit.Test)

Example 82 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project fuse-karaf by jboss-fuse.

the class CrmSecureTest method putCustomerTest.

/**
 * 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/>
 * On the server side, it matches the CustomerService's updateCustomer() method
 *
 * @throws Exception
 */
@Test
public void putCustomerTest() throws IOException {
    LOG.info("============================================");
    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);
    put.getHostAuthState().setAuthScheme(scheme);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    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");
        fail("Connection error");
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
    assertEquals(result, 200);
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) File(java.io.File) Test(org.junit.Test)

Example 83 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project ecf by eclipse.

the class TestBasicAuth method testPutBasicAuthentication.

public void testPutBasicAuthentication() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
    HttpState state = new HttpState();
    AuthScope authscope = new AuthScope(this.server.getLocalAddress(), this.server.getLocalPort(), "test");
    state.setCredentials(authscope, creds);
    this.client.setState(state);
    this.server.setRequestHandler(handlerchain);
    PutMethod put = new PutMethod("/test/");
    put.setRequestEntity(new StringRequestEntity("Test body"));
    try {
        this.client.executeMethod(put);
        assertEquals("Test body", put.getResponseBodyAsString());
    } finally {
        put.releaseConnection();
    }
    assertNotNull(put.getStatusLine());
    assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode());
    Header auth = put.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = put.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) EchoService(org.apache.commons.httpclient.EchoService) HttpState(org.apache.commons.httpclient.HttpState) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 84 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project entity-api by europeana.

the class HttpConnection method putURL.

/**
 * This method makes PUT request for given URL and JSON body parameter.
 * @param url
 * @param jsonParamValue
 * @return ResponseEntity that comprises response body in JSON format, headers and status code.
 * @throws IOException
 */
public ResponseEntity<String> putURL(String url, String jsonParamValue) throws IOException {
    HttpClient client = this.getHttpClient(CONNECTION_RETRIES, TIMEOUT_CONNECTION);
    PutMethod put = new PutMethod(url);
    put.setRequestBody(jsonParamValue);
    try {
        client.executeMethod(put);
        return buildResponseEntity(put);
    } finally {
        put.releaseConnection();
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 85 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project lobcder by skoulouzis.

the class WebDAVTest method testPutIfEtag.

public void testPutIfEtag() throws HttpException, IOException, DavException, URISyntaxException {
    System.out.println("testPutIfEtag");
    String testcol = root + "testResourceForPutIfEtag/";
    String testuri = testcol + "iftest";
    int status;
    try {
        MkColMethod mkcol = new MkColMethod(testcol);
        status = client.executeMethod(mkcol);
        assertEquals(HttpStatus.SC_CREATED, status);
        PutMethod put = new PutMethod(testuri);
        String condition = "<" + testuri + "> ([" + "\"an-etag-this-testcase-invented\"" + "])";
        put.setRequestEntity(new StringRequestEntity("1"));
        put.setRequestHeader("If", condition);
        status = client.executeMethod(put);
        assertEquals("status: " + status, HttpStatus.SC_PRECONDITION_FAILED, status);
    } finally {
        utils.deleteResource(testuri, true);
    }
}
Also used : PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)94 Test (org.junit.Test)49 GetMethod (org.apache.commons.httpclient.methods.GetMethod)29 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)19 Page (org.xwiki.rest.model.jaxb.Page)15 HttpClient (org.apache.commons.httpclient.HttpClient)14 IOException (java.io.IOException)13 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)13 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)10 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)7 Link (org.xwiki.rest.model.jaxb.Link)7 Header (org.apache.commons.httpclient.Header)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)6 File (java.io.File)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Object (org.xwiki.rest.model.jaxb.Object)5