Search in sources :

Example 91 with StringRequestEntity

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

the class TestConnectionPersistence method testConnPersisenceHTTP11.

public void testConnPersisenceHTTP11() throws Exception {
    this.server.setHttpService(new EchoService());
    AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    this.client.getParams().setVersion(HttpVersion.HTTP_1_1);
    this.client.setHttpConnectionManager(connman);
    PostMethod httppost = new PostMethod("/test/");
    httppost.setRequestEntity(new StringRequestEntity("stuff"));
    try {
        this.client.executeMethod(httppost);
    } finally {
        httppost.releaseConnection();
    }
    assertTrue(connman.getConection().isOpen());
    httppost = new PostMethod("/test/");
    httppost.setRequestEntity(new StringRequestEntity("more stuff"));
    try {
        this.client.executeMethod(httppost);
    } finally {
        httppost.releaseConnection();
    }
    assertTrue(connman.getConection().isOpen());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 92 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project pinpoint by naver.

the class HttpClient3EntityExtractor method getEntity.

@Override
public String getEntity(HttpMethod httpMethod) {
    if (httpMethod instanceof EntityEnclosingMethod) {
        final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;
        final RequestEntity entity = entityEnclosingMethod.getRequestEntity();
        if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) {
            try {
                String entityValue;
                String charSet = entityEnclosingMethod.getRequestCharSet();
                charSet = StringUtils.defaultIfEmpty(charSet, HttpConstants.DEFAULT_CONTENT_CHARSET);
                if (entity instanceof ByteArrayRequestEntity || entity instanceof StringRequestEntity) {
                    entityValue = entityUtilsToString(entity, charSet);
                } else {
                    entityValue = entity.getClass() + " (ContentType:" + entity.getContentType() + ")";
                }
                return entityValue;
            } catch (Exception e) {
                if (isDebug) {
                    logger.debug("Failed to get entity. httpMethod={}", httpMethod, e);
                }
            }
        }
    }
    return null;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 93 with StringRequestEntity

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

the class TestWebWAVFS method testCopy.

@Test
public void testCopy() throws VlException, IOException {
    System.err.println("testCopy");
    String testFileURI1 = this.uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    String testcol = root + "testResourceId/";
    try {
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
        int status = client1.executeMethod(put);
        assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED);
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        String cont = get.getResponseBodyAsString();
        assertEquals(TestSettings.TEST_DATA, cont);
        MkColMethod mkcol = new MkColMethod(testcol);
        status = client1.executeMethod(mkcol);
        assertEquals(HttpStatus.SC_CREATED, status);
        String targetURL = testcol + TestSettings.TEST_FILE_NAME1 + ".txt";
        CopyMethod cp = new CopyMethod(testFileURI1, targetURL, true);
        status = client1.executeMethod(cp);
        assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED);
        get = new GetMethod(targetURL);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        cont = get.getResponseBodyAsString();
        System.out.println(cont);
        assertEquals(TestSettings.TEST_DATA, cont);
    } finally {
        utils.deleteResource(testcol, false);
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : MkColMethod(org.apache.jackrabbit.webdav.client.methods.MkColMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) CopyMethod(org.apache.jackrabbit.webdav.client.methods.CopyMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Example 94 with StringRequestEntity

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

the class TestWebWAVFS method testInconsistency.

@Test
public void testInconsistency() throws VlException, IOException {
    System.err.println("testInconsistency");
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    try {
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
        int status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        Set<PDRIDesc> pdris = null;
        // Wait for replication
        utils.waitForReplication(testFileURI1);
        pdris = getPdris(TestSettings.TEST_FILE_NAME1 + ".txt");
        // Delete the physical data
        deletePhysicalData(pdris);
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        String cont = get.getResponseBodyAsString();
        // There is no offical status to get from this but  confilct seems apropriate:
        // Status code (409) indicating that the request could not be
        // completed due to a conflict with the current state of the resource.
        // Meaning at this time we have no physical data
        assertEquals(HttpStatus.SC_CONFLICT, status);
    } catch (Exception ex) {
        Logger.getLogger(TestWebWAVFS.class.getName()).log(Level.SEVERE, null, ex);
        fail(ex.getMessage());
    } finally {
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PDRIDesc(nl.uva.cs.lobcder.tests.TestREST.PDRIDesc) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) VlException(nl.uva.vlet.exception.VlException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 95 with StringRequestEntity

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

the class TestWebWAVFS method testPROPFIND_PUT_PROPFIND_GET_PUT.

@Test
public void testPROPFIND_PUT_PROPFIND_GET_PUT() throws IOException, DavException {
    System.err.println("testPROPFIND_PUT_PROPFIND_GET_PUT");
    // Make sure it's deleted
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    DeleteMethod del = new DeleteMethod(testFileURI1);
    int status = client1.executeMethod(del);
    assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_NOT_FOUND);
    try {
        // PROPFIND file is not there
        testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
        PropFindMethod propFind = new PropFindMethod(testFileURI1, DavConstants.PROPFIND_ALL_PROP_INCLUDE, DavConstants.DEPTH_0);
        status = client1.executeMethod(propFind);
        assertEquals(HttpStatus.SC_NOT_FOUND, status);
        // PUT create an empty file
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity("\n", "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        // PROPFIND get proerties
        propFind = new PropFindMethod(testFileURI1, DavConstants.PROPFIND_ALL_PROP_INCLUDE, DavConstants.DEPTH_0);
        status = client1.executeMethod(propFind);
        assertEquals(HttpStatus.SC_MULTI_STATUS, status);
        MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
        MultiStatusResponse[] responses = multiStatus.getResponses();
        DavPropertySet allProp = utils.getProperties(responses[0]);
        // DavPropertyIterator iter = allProp.iterator();
        // while (iter.hasNext()) {
        // DavProperty<?> p = iter.nextProperty();
        // System.out.println("P: " + p.getName() + " " + p.getValue());
        // }
        String isCollStr = (String) allProp.get(DavPropertyName.ISCOLLECTION).getValue();
        Boolean isCollection = Boolean.getBoolean(isCollStr);
        assertFalse(isCollection);
        String lenStr = (String) allProp.get(DavPropertyName.GETCONTENTLENGTH).getValue();
        assertEquals(Long.valueOf(lenStr), Long.valueOf("\n".length()));
        String contentType = (String) allProp.get(DavPropertyName.GETCONTENTTYPE).getValue();
        assertEquals("text/plain; charset=UTF-8", contentType);
        // GET the file
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals("\n", get.getResponseBodyAsString());
        // PUT
        put = new PutMethod(testFileURI1);
        String content = get.getResponseBodyAsString() + TestSettings.TEST_DATA;
        put.setRequestEntity(new StringRequestEntity(content, "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals(content, get.getResponseBodyAsString());
        put = new PutMethod(testFileURI1);
        content = get.getResponseBodyAsString() + TestSettings.TEST_DATA;
        put.setRequestEntity(new StringRequestEntity(content, "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals(content, get.getResponseBodyAsString());
    } finally {
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Aggregations

StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)102 PostMethod (org.apache.commons.httpclient.methods.PostMethod)63 HttpClient (org.apache.commons.httpclient.HttpClient)33 Test (org.junit.Test)23 PutMethod (org.apache.commons.httpclient.methods.PutMethod)19 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)18 IOException (java.io.IOException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)12 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)12 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)12 Header (org.apache.commons.httpclient.Header)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9 GetMethod (org.apache.commons.httpclient.methods.GetMethod)9 InputStream (java.io.InputStream)8 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 StringWriter (java.io.StringWriter)6 Map (java.util.Map)6 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)6 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)6