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

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

Introduction

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

Prototype

public StringRequestEntity(String paramString1, String paramString2, String paramString3)
  throws UnsupportedEncodingException

Source Link

Usage

From source file:net.mojodna.searchable.solr.SolrIndexer.java

@Override
public void optimize() throws IndexingException {
    try {//from   w ww  . j  av a2 s .  c o m
        PostMethod post = new PostMethod(solrPath);
        post.setRequestEntity(new StringRequestEntity("<optimize waitFlush=\"false\" waitSearcher=\"false\"/>",
                "text/xml", "UTF-8"));
        log.debug("Optimizing.");
        getHttpClient().executeMethod(post);
    } catch (final IOException e) {
        throw new IndexingException(e);
    }
}

From source file:liveplugin.toolwindow.addplugin.git.jetbrains.plugins.github.api.GithubApiUtil.java

@NotNull
private static HttpMethod doREST(@NotNull final GithubAuthData auth, @NotNull final String uri,
        @Nullable final String requestBody, @NotNull final Collection<Header> headers,
        @NotNull final HttpVerb verb) throws IOException {
    HttpClient client = getHttpClient(auth.getBasicAuth(), auth.isUseProxy());
    return GithubSslSupport.getInstance().executeSelfSignedCertificateAwareRequest(client, uri,
            new ThrowableConvertor<String, HttpMethod, IOException>() {
                @Override// ww  w.  j  a  v  a2 s  .  c om
                public HttpMethod convert(String uri) throws IOException {
                    HttpMethod method;
                    switch (verb) {
                    case POST:
                        method = new PostMethod(uri);
                        if (requestBody != null) {
                            ((PostMethod) method).setRequestEntity(
                                    new StringRequestEntity(requestBody, "application/json", "UTF-8"));
                        }
                        break;
                    case GET:
                        method = new GetMethod(uri);
                        break;
                    case DELETE:
                        method = new DeleteMethod(uri);
                        break;
                    case HEAD:
                        method = new HeadMethod(uri);
                        break;
                    default:
                        throw new IllegalStateException("Wrong HttpVerb: unknown method: " + verb.toString());
                    }
                    GithubAuthData.TokenAuth tokenAuth = auth.getTokenAuth();
                    if (tokenAuth != null) {
                        method.addRequestHeader("Authorization", "token " + tokenAuth.getToken());
                    }
                    for (Header header : headers) {
                        method.addRequestHeader(header);
                    }
                    return method;
                }
            });
}

From source file:com.assemblade.client.AbstractClient.java

protected <T> T update(String path, T object, TypeReference<T> type) throws ClientException {
    PutMethod put = new PutMethod(baseUrl + path);
    try {//from  www  .ja v a2 s  . com
        try {
            put.setRequestEntity(
                    new StringRequestEntity(mapper.writeValueAsString(object), "application/json", null));
        } catch (IOException e) {
            throw new CallFailedException("Failed to serialize a request object", e);
        }
        int statusCode = executeMethod(put);
        if (statusCode == 200) {
            try {
                return mapper.readValue(put.getResponseBodyAsStream(), type);
            } catch (IOException e) {
                throw new CallFailedException("Failed to deserialize a response object", e);
            }
        } else {
            throw new InvalidStatusException(200, statusCode);
        }
    } finally {
        put.releaseConnection();
    }
}

From source file:net.jadler.JadlerStubbingIntegrationTest.java

@Test
public void havingBody() throws Exception {
    onRequest().havingBodyEqualTo("postbody").havingBody(notNullValue()).havingBody(not(isEmptyOrNullString()))
            .respond().withStatus(201);//w  w w  .j  a  v a  2  s. c om

    final PostMethod method = new PostMethod("http://localhost:" + port());
    method.setRequestEntity(new StringRequestEntity("postbody", null, null));

    int status = client.executeMethod(method);
    assertThat(status, is(201));
}

From source file:eu.eco2clouds.scheduler.bonfire.BFClientSchedulerImpl.java

private String putMethod(String url, String payload, Boolean exception) {
    // Create an instance of HttpClient.
    HttpClient client = getHttpClient();

    logger.debug("Connecting to: " + url);
    // Create a method instance.
    PutMethod method = new PutMethod(url);
    setHeaders(method);//w  w  w .j a  va 2  s  .  com

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    String response = "";

    try {
        RequestEntity entity = new StringRequestEntity(payload, SchedulerDictionary.CONTENT_TYPE_BONFIRE_XML,
                "UTF-8");
        method.setRequestEntity(entity);
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_ACCEPTED) { //TODO test for this case... 
            logger.warn(
                    "get managed experiments information... : " + url + " failed: " + method.getStatusLine());
        } else {
            // Read the response body.
            byte[] responseBody = method.getResponseBody();
            response = new String(responseBody);
        }

    } catch (HttpException e) {
        logger.warn("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
        exception = true;
    } catch (IOException e) {
        logger.warn("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
        exception = true;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    return response;
}

From source file:com.intuit.tank.httpclient3.TankHttpClient3.java

@Override
public void doPut(BaseRequest request) {
    try {/*w  w  w.ja v a 2s . com*/
        PutMethod httpput = new PutMethod(request.getRequestUrl());
        // Multiple calls can be expensive, so get it once
        String requestBody = request.getBody();
        StringRequestEntity entity = new StringRequestEntity(requestBody, request.getContentType(),
                request.getContentTypeCharSet());
        httpput.setRequestEntity(entity);
        sendRequest(request, httpput, requestBody);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.jadler.JadlerMockingIntegrationTest.java

@Test
public void havingUTF8Body() throws Exception {
    final PostMethod method = new PostMethod("http://localhost:" + port());
    method.setRequestEntity(/*from   w  ww .j av  a  2 s .  com*/
            new StringRequestEntity(STRING_WITH_DIACRITICS, "text/plain", UTF_8_CHARSET.name()));

    client.executeMethod(method);

    verifyThatRequest().havingBodyEqualTo(STRING_WITH_DIACRITICS).havingRawBodyEqualTo(UTF_8_REPRESENTATION)
            .receivedOnce();
}

From source file:eu.learnpad.core.impl.sim.XwikiBridgeInterfaceRestResource.java

@Override
public Collection<String> addProcessDefinition(String processDefinitionFileURL, String modelSetId)
        throws LpRestException {
    HttpClient httpClient = this.getAnonymousClient();
    String uri;//www.j a  va 2s  .c  o m
    if (modelSetId != null) {
        uri = String.format("%s/learnpad/sim/bridge/processes?modelsetartifactid=%s", this.restPrefix,
                modelSetId);
    } else {
        uri = String.format("%s/learnpad/sim/bridge/processes", this.restPrefix);
    }

    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader("Content-Type", "application/json");

    try {
        RequestEntity requestEntity = new StringRequestEntity(processDefinitionFileURL, "application/json",
                "UTF-8");
        postMethod.setRequestEntity(requestEntity);

        httpClient.executeMethod(postMethod);

        // Not fully tested, but is looks working for our purposes -- Gulyx
        return this.objectReaderCollection.readValue(postMethod.getResponseBodyAsStream());
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }
}

From source file:com.github.rnewson.couchdb.lucene.Database.java

private String post(final String path, final String body) throws HttpException, IOException {
    final PostMethod post = new PostMethod(url(path));
    post.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
    return execute(post);
}

From source file:com.legstar.test.cixs.AbstractHttpClientTester.java

/**
 * Perform a request/reply using text/xml as payload.
 * //from  w  ww .j  a v a  2s. c  om
 * @param url the target url
 * @param xmlRequest the XML request
 * @param soapAction the soap action
 * @return the XML reply
 * @throws Exception if something goes wrong
 */
public String postXml(final String url, final String xmlRequest, final String soapAction) throws Exception {
    PostMethod postMethod = new PostMethod(url);
    if (soapAction != null && soapAction.length() > 0) {
        postMethod.setRequestHeader("SOAPAction", soapAction);
    }
    StringRequestEntity requestEntity = new StringRequestEntity(xmlRequest, "text/xml", "utf-8");
    postMethod.setRequestEntity(requestEntity);
    int rc = HTTPCLIENT.executeMethod(postMethod);
    String xmlReply = getResponseBodyAsString(postMethod.getResponseBodyAsStream(),
            postMethod.getResponseCharSet());
    postMethod.releaseConnection();
    assertEquals(postMethod.getStatusText() + " " + xmlReply, 200, rc);
    return xmlReply;
}