Search in sources :

Example 71 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project xwiki-platform by xwiki.

the class WikiManagerRestTest method executePost.

protected PostMethod executePost(String uri, String userName, String password, Object object) throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
    httpClient.getParams().setAuthenticationPreemptive(true);
    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    RequestEntity entity = new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
    postMethod.setRequestEntity(entity);
    httpClient.executeMethod(postMethod);
    return postMethod;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) StringWriter(java.io.StringWriter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 72 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project xwiki-platform by xwiki.

the class WikiManagerRestTest method executePut.

protected PutMethod executePut(String uri, String userName, String password, Object object) throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
    httpClient.getParams().setAuthenticationPreemptive(true);
    PutMethod putMethod = new PutMethod(uri);
    putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    RequestEntity entity = new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
    putMethod.setRequestEntity(entity);
    httpClient.executeMethod(putMethod);
    return putMethod;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) StringWriter(java.io.StringWriter) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 73 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project xwiki-platform by xwiki.

the class InstallMojo method executePutXml.

private PutMethod executePutXml(String uri, Object object, Marshaller marshaller, HttpClient httpClient) throws Exception {
    PutMethod putMethod = new PutMethod(uri);
    putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    RequestEntity entity = new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
    putMethod.setRequestEntity(entity);
    httpClient.executeMethod(putMethod);
    return putMethod;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) StringWriter(java.io.StringWriter) PutMethod(org.apache.commons.httpclient.methods.PutMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity)

Example 74 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesListUtil method createNewList.

public static String createNewList(String name, String items, String description, String url, String user, boolean makePrivate) {
    String postUrl = CommonData.getSpeciesListServer() + "/ws/speciesList/";
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(postUrl);
    // set the cookie from the user
    try {
        post.setRequestHeader(StringConstants.COOKIE, "ALA-Auth=" + java.net.URLEncoder.encode(user, StringConstants.UTF_8));
    } catch (Exception e) {
        // should not happen as utf-8 is a supported encoding
        LOGGER.error("failed to encode user: " + user, e);
    }
    LOGGER.debug(post.getRequestHeader(StringConstants.COOKIE));
    if (name != null && items != null) {
        try {
            java.util.Map map = new java.util.HashMap();
            map.put(StringConstants.LISTNAME, name);
            if (description != null) {
                map.put(StringConstants.DESCRIPTION, description);
            }
            if (url != null) {
                map.put("url", url);
            }
            map.put("listItems", items);
            map.put("listType", "SPATIAL_PORTAL");
            map.put("isPrivate", makePrivate);
            String content = JSONValue.toJSONString(map);
            LOGGER.debug("create new list : " + content + " for user " + user);
            String contentType = StringConstants.APPLICATION_JSON;
            String charset = StringConstants.UTF_8;
            StringRequestEntity requestEntity = new StringRequestEntity(content, contentType, charset);
            post.setRequestEntity(requestEntity);
            int result = client.executeMethod(post);
            LOGGER.debug(result);
            LOGGER.debug(post.getResponseBodyAsString());
            if (result == 201) {
                return post.getResponseHeader("druid").getValue();
            }
        } catch (Exception e) {
            LOGGER.error("Error uploading list", e);
        }
    }
    return null;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) Map(java.util.Map)

Example 75 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project ovirt-engine by oVirt.

the class AttestationService method attestHosts.

public List<AttestationValue> attestHosts(List<String> hosts) {
    String pollURI = Config.getValue(ConfigValues.PollUri);
    List<AttestationValue> values = new ArrayList<>();
    PostMethod postMethod = new PostMethod("/" + pollURI);
    try {
        postMethod.setRequestEntity(new StringRequestEntity(writeListJson(hosts)));
        postMethod.addRequestHeader("Accept", CONTENT_TYPE);
        postMethod.addRequestHeader("Content-type", CONTENT_TYPE);
        HttpClient httpClient = getClient();
        int statusCode = httpClient.executeMethod(postMethod);
        String strResponse = postMethod.getResponseBodyAsString();
        log.debug("return attested result: {}", strResponse);
        if (statusCode == 200) {
            values = parsePostedResp(strResponse);
        } else {
            log.error("attestation error: {}", strResponse);
        }
    } catch (JsonParseException e) {
        log.error("Failed to parse result: {}", e.getMessage());
        log.debug("Exception", e);
    } catch (IOException e) {
        log.error("Failed to attest hosts, make sure hosts are up and reachable: {}", e.getMessage());
        log.debug("Exception", e);
    } finally {
        postMethod.releaseConnection();
    }
    return values;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException)

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