Example usage for org.apache.http.client.methods HttpPut setEntity

List of usage examples for org.apache.http.client.methods HttpPut setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut setEntity.

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:com.github.bigtoast.jfly.internal.http.JFlyImpl.java

protected JsonParser executePut(String uri, String body) {
    try {/*  w w w  .ja v  a  2s.c  om*/
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPut put = new HttpPut(uri);
        StringEntity entity = new StringEntity(body);
        put.setEntity(entity);
        HttpResponse response = client.execute(put);
        if (200 == response.getStatusLine().getStatusCode()) {
            HttpEntity responseEntity = response.getEntity();
            InputStream responseContent = responseEntity.getContent();
            return factory.createJsonParser(responseContent);
        } else {
            throw new JFlyException();
        }
    } catch (Exception e) {
        throw new JFlyException(e);
    }
}

From source file:cycronix.ctlib.CThttp.java

private HttpResponse httpput(String SourceChan, byte[] body) throws IOException {

    String url = CTwebhost + "/" + SourceChan;
    final HttpPut put = new HttpPut(url);
    if (userpass != null)
        put.setHeader("Authorization", "Basic " + userpass);

    if (body != null) {
        CTinfo.debugPrint("PUT: " + url + ", userpass: " + userpass);
        put.setEntity(new ByteArrayEntity(body));
    }//from   w w  w  .  ja  v a 2s.  c  o m
    return httpclient.execute(put);
}

From source file:net.fizzl.redditengine.impl.SimpleHttpClient.java

/**
 * Calls HTTP PUT with the Request-URI. Returns an InputStream of the response entity.
 * /*from www.  jav  a 2 s . c om*/
 * @param url      HTTP PUT URL
 * @param params   PUT parameters
 * @return         InputStream
 * @throws ClientProtocolException
 * @throws IOException
 * @throws UnexpectedHttpResponseException
 */
public InputStream put(String url, List<NameValuePair> params)
        throws ClientProtocolException, IOException, UnexpectedHttpResponseException {
    HttpPut put = new HttpPut(url);
    put.setEntity(new UrlEncodedFormEntity(params));
    return execute(put);
}

From source file:com.linkedin.multitenant.db.RocksdbDatabase.java

@Override
public DatabaseResult doInsert(Query q) {
    HttpPut put = new HttpPut(m_connStr + q.getKey());

    ByteArrayEntity bae = new ByteArrayEntity(q.getValue());
    bae.setContentType("octet-stream");
    put.setEntity(bae);

    try {/*from   w  w  w .  j a  v  a  2 s .  c  o  m*/
        String responseBody = m_client.execute(put, m_handler);
        m_log.debug("Write response: " + responseBody);
        return DatabaseResult.OK;
    } catch (Exception e) {
        m_log.error("Error in executing doInsert", e);
        return DatabaseResult.FAIL;
    }
}

From source file:com.linkedin.multitenant.db.RocksdbDatabase.java

@Override
public DatabaseResult doUpdate(Query q) {
    HttpPut put = new HttpPut(m_connStr + q.getKey());

    ByteArrayEntity bae = new ByteArrayEntity(q.getValue());
    bae.setContentType("octet-stream");
    put.setEntity(bae);

    try {/*from w  w w.  j  a  v  a 2  s.c o m*/
        String responseBody = m_client.execute(put, m_handler);
        m_log.debug("Write response: " + responseBody);
        return DatabaseResult.OK;
    } catch (Exception e) {
        m_log.error("Error in executing doUpdate", e);
        return DatabaseResult.FAIL;
    }
}

From source file:org.frontcache.hystrix.FC_BypassCache.java

/**
 * forward all kind of requests (GET, POST, PUT, ...)
 * /*  w  ww  .j  av  a2s .c  o m*/
 * @param httpclient
 * @param verb
 * @param uri
 * @param request
 * @param headers
 * @param params
 * @param requestEntity
 * @return
 * @throws Exception
 */
private HttpResponse forward(HttpClient httpclient, String verb, String uri, HttpServletRequest request,
        Map<String, List<String>> headers, InputStream requestEntity) throws Exception {

    URL host = context.getOriginURL();
    HttpHost httpHost = FCUtils.getHttpHost(host);
    uri = (host.getPath() + uri).replaceAll("/{2,}", "/");

    HttpRequest httpRequest;
    switch (verb.toUpperCase()) {
    case "POST":
        HttpPost httpPost = new HttpPost(uri + context.getRequestQueryString());
        httpRequest = httpPost;
        httpPost.setEntity(new InputStreamEntity(requestEntity, request.getContentLength()));
        break;
    case "PUT":
        HttpPut httpPut = new HttpPut(uri + context.getRequestQueryString());
        httpRequest = httpPut;
        httpPut.setEntity(new InputStreamEntity(requestEntity, request.getContentLength()));
        break;
    case "PATCH":
        HttpPatch httpPatch = new HttpPatch(uri + context.getRequestQueryString());
        httpRequest = httpPatch;
        httpPatch.setEntity(new InputStreamEntity(requestEntity, request.getContentLength()));
        break;
    default:
        httpRequest = new BasicHttpRequest(verb, uri + context.getRequestQueryString());
    }

    try {
        httpRequest.setHeaders(FCUtils.convertHeaders(headers));
        Header acceptEncoding = httpRequest.getFirstHeader("accept-encoding");
        if (acceptEncoding != null && acceptEncoding.getValue().contains("gzip")) {
            httpRequest.setHeader("accept-encoding", "gzip");
        }
        HttpResponse originResponse = httpclient.execute(httpHost, httpRequest);
        return originResponse;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        // httpclient.getConnectionManager().shutdown();
    }
}

From source file:io.crate.integrationtests.BlobHttpIntegrationTest.java

protected CloseableHttpResponse put(String uri, String body) throws IOException {
    HttpPut httpPut = new HttpPut(Blobs.url(address, uri));
    if (body != null) {
        StringEntity bodyEntity = new StringEntity(body);
        httpPut.setEntity(bodyEntity);
    }//from  ww  w  .  ja v  a  2s  . c  om
    return executeAndDefaultAssertions(httpPut);
}

From source file:jp.co.gui.aruga.watch.ClockHttpRequest.java

public boolean update(Todo todo) throws IOException {
    if (todo.getId() == null)
        throw new UnsupportedOperationException();

    HttpPut request = new HttpPut(url + "/json/" + todo.getId());

    String json = om.writeValueAsString(todo);
    StringEntity se = new StringEntity(json, encode);
    request.addHeader("Content-type", "application/json");
    request.setEntity(se);
    HttpResponse hr = httpClient.execute(request);
    String a = EntityUtils.toString(hr.getEntity());
    if (a != null && hr.getStatusLine().getStatusCode() < 400) {
        return true;
    } else {//ww w .  ja v  a2  s.  co  m
        return false;
    }
}

From source file:hmock.BasicHMockTest.java

@Test
public void testUrlEncodedFormPutRequest() throws Exception {

    HMock.respond().body("A,B,C").when().put("/employees").param("name", equalTo("John"));

    HttpPut put = new HttpPut("http://localhost:7357/employees");
    NameValuePair[] parameters = { new BasicNameValuePair("name", "John") };
    put.setEntity(new UrlEncodedFormEntity(Arrays.asList(parameters)));

    HttpResponse response = _httpclient.execute(put);

    String responsebody = IOUtils.toString(response.getEntity().getContent());
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("A,B,C", responsebody);

    put = new HttpPut("http://localhost:7357/employees");
    parameters = new NameValuePair[] { new BasicNameValuePair("name", "John2") };
    put.setEntity(new UrlEncodedFormEntity(Arrays.asList(parameters)));
    response = _httpclient.execute(put);
    EntityUtils.consume(response.getEntity());
    assertEquals(404, response.getStatusLine().getStatusCode());
}

From source file:com.swisscom.refimpl.boundary.MIB3Client.java

public EasypayResponse modifyAuthSubscription(String merchantId, String easypayAuthId, String operation)
        throws IOException, HttpException {
    HttpPut method = null;
    try {/*w  ww. java 2s . c o  m*/
        method = new HttpPut(MIB3Client.AUTHSUBSCRIPTIONS_URL + "/" + easypayAuthId);
        String entity = "{\"operation\": \"" + operation + "\"}";
        method.setEntity(new StringEntity(entity,
                ContentType.create("application/vnd.ch.swisscom.easypay.authsubscription+json", "UTF-8")));
        addSignature(method, "PUT", "/authsubscriptions/" + easypayAuthId, merchantId,
                "application/vnd.ch.swisscom.easypay.authsubscription+json", entity.getBytes("UTF-8"));
        HttpResponse response = httpClient.execute(method);
        return new EasypayResponse(new String(EntityUtils.toByteArray(response.getEntity())),
                response.getStatusLine().getStatusCode());
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
        ;
    }
}