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:bad.robot.http.apache.ApacheHttpClient.java

@Override
public HttpResponse put(URL url, bad.robot.http.HttpPut message) {
    HttpPut put = new HttpPut(url.toExternalForm());
    for (Header header : message.getHeaders())
        put.addHeader(header.name(), header.value());
    put.setEntity(new HttpRequestToEntity(message).asHttpEntity());
    return execute(put);
}

From source file:com.spokenpic.net.RestClientPut.java

@Override
protected RestResult doPost() {
    HttpPut httpPut = new HttpPut(getSchemeServer() + this.uri);
    setupHttp(httpPut);//from w  w  w .j  a v  a 2 s.  co  m
    try {
        StringEntity data = new StringEntity(this.data, "UTF-8");
        data.setChunked(false);
        httpPut.setEntity(data);
        httpPut.setHeader("Content-type", "application/json");
        HttpResponse httpResponse = HttpManager.execute(httpPut);
        return returnResponse(httpResponse);
    } catch (Exception e) {
        Log.d("RestClientFilePUT", "Error doPost: " + e.toString());
        return errorResponse("Fatal error during PUT");
    }
}

From source file:org.apache.hadoop.hbase.client.example.TestHttpProxyExample.java

@Test
public void test() throws Exception {
    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
        HttpPut put = new HttpPut(
                String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER));
        put.setEntity(EntityBuilder.create().setText(VALUE)
                .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build());
        try (CloseableHttpResponse resp = client.execute(put)) {
            assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
        }/*from   www  .ja v  a  2s  . c o m*/
        HttpGet get = new HttpGet(
                String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER));
        try (CloseableHttpResponse resp = client.execute(get)) {
            assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
            assertEquals("value", Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent())));
        }
        get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever", FAMILY,
                QUALIFIER));
        try (CloseableHttpResponse resp = client.execute(get)) {
            assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode());
        }
    }
}

From source file:org.gradle.internal.resource.transport.http.HttpResourceUploader.java

public void upload(Factory<InputStream> source, Long contentLength, URI destination) throws IOException {
    HttpPut method = new HttpPut(destination);
    final RepeatableInputStreamEntity entity = new RepeatableInputStreamEntity(source, contentLength,
            ContentType.APPLICATION_OCTET_STREAM);
    method.setEntity(entity);
    HttpResponse response = http.performHttpRequest(method);
    EntityUtils.consume(response.getEntity());
    if (!http.wasSuccessful(response)) {
        throw new IOException(
                String.format("Could not PUT '%s'. Received status code %s from server: %s", destination,
                        response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
    }/*w  w  w.ja  v a  2 s  .c o m*/

}

From source file:com.jzboy.couchdb.http.CouchHttpClient.java

/**
 * Sends a PUT request to the specified CouchDB endpoint.
 * Use this version to add attachments./*from   ww  w.j ava2 s  . co m*/
 * @param uri      CouchDB API endpoint
 * @param data      request data
 * @param mimeType   the MIME type of the data. This is added as a Content-Type header to the request
 * @return the JSON tree parsed form the response body
 * @throws IOException      if the HttpClient throws an IOException
 * @throws CouchDBException   if CouchDB returns an error - response code >= 300. The response details are
 * encapsulated in the exception.
 */
public JsonNode put(URI uri, byte[] data, String mimeType) throws IOException, CouchDBException {
    HttpPut req = new HttpPut(uri);
    req.setEntity(new ByteArrayEntity(data));
    req.addHeader("Content-Type", mimeType);
    return execRequest(req);
}

From source file:com.emc.storageos.driver.dellsc.scapi.rest.RestClient.java

/**
 * Execute a PUT REST call./*from w ww.  j  av  a 2  s  .com*/
 *
 * @param path The relative path.
 * @param payload The PUT payload.
 * @return The execution result.
 */
public RestResult put(String path, String payload) {
    HttpPut httpPut = new HttpPut(formatUrl(path));
    httpPut.setEntity(new StringEntity(payload, StandardCharsets.UTF_8));
    return executeRequest(httpPut);
}

From source file:ca.uhn.fhir.rest.client.apache.ApacheHttpClient.java

private HttpRequestBase constructRequestBase(HttpEntity theEntity) {
    String url = myUrl.toString();
    switch (myRequestType) {
    case DELETE://from  w  w w  .j a v  a2s. co m
        return new HttpDelete(url);
    case OPTIONS:
        return new HttpOptions(url);
    case POST:
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(theEntity);
        return httpPost;
    case PUT:
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(theEntity);
        return httpPut;
    case GET:
    default:
        return new HttpGet(url);
    }
}

From source file:org.talend.dataprep.command.preparation.UpdateStepRowMetadata.java

private HttpRequestBase onExecute(String preparationId, List<Step> steps) {
    try {//from   w w  w. j av  a  2  s  .c  o m
        final String stepsAsJson = objectMapper.writeValueAsString(steps);
        final HttpPut updater = new HttpPut(
                preparationServiceUrl + "/preparations/" + preparationId + "/steps");
        updater.setHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE);
        updater.setEntity(new StringEntity(stepsAsJson, APPLICATION_JSON));
        return updater;
    } catch (JsonProcessingException e) {
        throw new TDPException(UNEXPECTED_EXCEPTION, e);
    }
}

From source file:com.digitalarx.android.syncadapter.ContactSyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {//ww w  .  j  av a  2s  . c om
    setAccount(account);
    setContentProviderClient(provider);
    Cursor c = getLocalContacts(false);
    if (c.moveToFirst()) {
        do {
            String lookup = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            String a = getAddressBookUri();
            String uri = a + lookup + ".vcf";
            FileInputStream f;
            try {
                f = getContactVcard(lookup);
                HttpPut query = new HttpPut(uri);
                byte[] b = new byte[f.available()];
                f.read(b);
                query.setEntity(new ByteArrayEntity(b));
                fireRawRequest(query);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            } catch (OperationCanceledException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (AuthenticatorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (c.moveToNext());
        // } while (c.moveToNext());
    }

}

From source file:com.devbliss.doctest.httpfactory.PutWithoutRedirectImpl.java

public HttpPut createPutRequest(URI uri, Object payload) throws IOException {
    HttpPut httpPut = new HttpPut(uri);
    HttpParams params = new BasicHttpParams();
    params.setParameter(HANDLE_REDIRECTS, false);
    httpPut.setParams(params);/*from   w  w w  .j a v a 2  s. com*/

    if (payload != null) {
        httpPut.setEntity(entityBuilder.buildEntity(payload));
    }

    return httpPut;
}