Search in sources :

Example 76 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.

the class PublicApiHttpClient method post.

public HttpResponse post(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation, final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation, null);
    String url = endpoint.getUrl();
    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = null;
        if (cmisBinding.equals(Binding.atom)) {
            requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");
        } else if (cmisBinding.equals(Binding.browser)) {
            requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        }
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 77 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.

the class PublicApiHttpClient method post.

public HttpResponse post(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType, final Map<String, String> params) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, params);
    String url = endpoint.getUrl();
    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        if (contentType == null || contentType.isEmpty()) {
            contentType = "application/json";
        }
        StringRequestEntity requestEntity = new StringRequestEntity(body, contentType, "UTF-8");
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 78 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.

the class PublicApiHttpClient method post.

public HttpResponse post(final Class<?> c, final RequestContext rq, final Object entityId, final Object relationshipEntityId, final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null);
    String url = endpoint.getUrl();
    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 79 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity 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 80 with StringRequestEntity

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

the class TestPostParameterEncoding method testPostSetRequestBody.

public void testPostSetRequestBody() throws Exception {
    PostMethod post = new PostMethod("/foo");
    String body = "this+is+the+body";
    post.setRequestEntity(new StringRequestEntity(body));
    assertEquals(body, getRequestAsString(post.getRequestEntity()));
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

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