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:fr.liglab.adele.cilia.workbench.restmonitoring.utils.http.HttpHelper.java

public static void put(PlatformID platformID, String target, String data) throws CiliaException {
    String url = getURL(platformID, target);
    HttpPut httpRequest = new HttpPut(url);

    // entity//from w w w  .  j ava 2s . co  m
    StringEntity entity = new StringEntity(data, ContentType.create("text/plain", "UTF-8"));
    httpRequest.setEntity(entity);

    // HTTP request
    HttpClient httpClient = getClient();
    try {
        httpClient.execute(httpRequest, new BasicResponseHandler());
        httpClient.getConnectionManager().shutdown();
    } catch (Exception e) {
        httpClient.getConnectionManager().shutdown();
        throw new CiliaException("can't perform HTTP PUT request", e);
    }
}

From source file:org.fcrepo.oai.integration.AbstractOAIProviderIT.java

protected static HttpPut putDSMethod(final String pid, final String ds, final String content)
        throws UnsupportedEncodingException {
    final HttpPut put = new HttpPut(serverAddress + pid + "/" + ds);

    put.setEntity(new StringEntity(content));
    return put;//from   www. j  a  va 2s. c  om
}

From source file:com.deployd.Deployd.java

public static JSONObject put(JSONObject input, String uri)
        throws ClientProtocolException, IOException, JSONException {

    HttpPut post = new HttpPut(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(input.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}

From source file:io.fabric8.apps.infinispan.InfinispanServerKubernetesTest.java

/**
 * Method that puts a String value in cache.
 *//*from  www .j  ava 2s .  c  o m*/
public static void putMethod(String serverURL, String key, String value) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPut put = new HttpPut(serverURL + "/" + key);
    put.addHeader(new BasicHeader("Content-Type", "text/plain"));
    put.setEntity(new StringEntity(value));
    client.execute(put);
}

From source file:org.bfr.querytools.spectrumbridge.SpectrumBridgeQuery.java

public static HttpResponse register(int antennaHeight, double latitude, double longitude)
        throws ClientProtocolException, IOException {
    String queryUrl = String.format(Locale.US, "%s/devices/%s/%s/%d", baseUrl, countryCode, fccId, serial);

    // Query XML/*from   w  w  w.  j av  a 2s .c  o m*/
    String queryXml = String.format(Locale.US, xmlTemplate, antennaHeight, deviceType, latitude, longitude);

    HttpClient client = createClient();

    HttpPut request = new HttpPut(queryUrl);

    request.addHeader("Content-Type", "application/xml");

    request.setEntity(new StringEntity(queryXml, "UTF-8"));

    request.addHeader("SBI-Sequence", "100");

    //      for (Header header : request.getAllHeaders())
    //         System.out.println(">> " + header.getName() + ": " + header.getValue() );

    return client.execute(request);

}

From source file:in.huohua.peterson.network.NetworkUtils.java

public static HttpResponse httpQuery(final HttpRequest request) throws ClientProtocolException, IOException {
    final HttpRequestBase requestBase;
    if (request.getHttpMethod().equals(HttpRequest.HTTP_METHOD_GET)) {
        final StringBuilder builder = new StringBuilder(request.getUrl());
        if (request.getParams() != null) {
            if (!request.getUrl().contains("?")) {
                builder.append("?");
            } else {
                builder.append("&");
            }/* www  .  j  av a2  s .  c om*/
            builder.append(request.getParamsAsString());
        }
        final HttpGet get = new HttpGet(builder.toString());
        requestBase = get;
    } else if (request.getHttpMethod().equals(HttpRequest.HTTP_METHOD_POST)) {
        final HttpPost post = new HttpPost(request.getUrl());
        if (request.getEntity() != null) {
            post.setEntity(request.getEntity());
        } else {
            post.setEntity(new UrlEncodedFormEntity(request.getParamsAsList(), "UTF-8"));
        }
        requestBase = post;
    } else if (request.getHttpMethod().equals(HttpRequest.HTTP_METHOD_PUT)) {
        final HttpPut put = new HttpPut(request.getUrl());
        if (request.getEntity() != null) {
            put.setEntity(request.getEntity());
        } else {
            put.setEntity(new UrlEncodedFormEntity(request.getParamsAsList(), "UTF-8"));
        }
        requestBase = put;
    } else if (request.getHttpMethod().equals(HttpRequest.HTTP_METHOD_DELETE)) {
        final StringBuilder builder = new StringBuilder(request.getUrl());
        if (request.getParams() != null) {
            if (!request.getUrl().contains("?")) {
                builder.append("?");
            } else {
                builder.append("&");
            }
            builder.append(request.getParamsAsString());
        }
        final HttpDelete delete = new HttpDelete(builder.toString());
        requestBase = delete;
    } else {
        return null;
    }

    if (request.getHeaders() != null) {
        final Iterator<Map.Entry<String, String>> iterator = request.getHeaders().entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<String, String> kv = iterator.next();
            requestBase.setHeader(kv.getKey(), kv.getValue());
        }
    }
    return HTTP_CLIENT.execute(requestBase);
}

From source file:es.ugr.swad.swadroid.webservices.RestEasy.java

public static HttpResponse doPut(String url, JSONObject c) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPut request = new HttpPut(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);

}

From source file:com.netflix.raigad.utils.SystemUtils.java

public static String runHttpPutCommand(String url, String jsonBody) throws IOException {
    String return_;
    DefaultHttpClient client = new DefaultHttpClient();
    InputStream isStream = null;/*from  ww  w. ja v  a  2 s  . c om*/
    try {
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 1000;
        int timeoutSocket = 1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        client.setParams(httpParameters);

        HttpPut putRequest = new HttpPut(url);
        putRequest.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8));
        putRequest.setHeader("Content-type", "application/json");

        HttpResponse resp = client.execute(putRequest);

        if (resp == null || resp.getEntity() == null) {
            throw new ESHttpException("Unable to execute PUT URL (" + url
                    + ") Exception Message: < Null Response or Null HttpEntity >");
        }

        isStream = resp.getEntity().getContent();

        if (resp.getStatusLine().getStatusCode() != 200) {

            throw new ESHttpException("Unable to execute PUT URL (" + url + ") Exception Message: ("
                    + IOUtils.toString(isStream, StandardCharsets.UTF_8.toString()) + ")");
        }

        return_ = IOUtils.toString(isStream, StandardCharsets.UTF_8.toString());
        logger.debug("PUT URL API: {} with JSONBody {} returns: {}", url, jsonBody, return_);
    } catch (Exception e) {
        throw new ESHttpException(
                "Caught an exception during execution of URL (" + url + ")Exception Message: (" + e + ")");
    } finally {
        if (isStream != null)
            isStream.close();
    }
    return return_;
}

From source file:tech.beshu.ror.integration.DynamicVariablesTests.java

private static void insertDoc(String indexName, RestClient restClient) {
    if (adminClient == null) {
        adminClient = restClient;/* www.  jav a  2 s. c  o m*/
    }

    String docPath = "/" + indexName + "/documents/doc-asd";
    try {
        HttpPut request = new HttpPut(restClient.from(docPath) + "?refresh=true");
        request.setHeader("Content-Type", "application/json");
        request.setHeader("refresh", "true");
        request.setHeader("timeout", "50s");
        request.setEntity(new StringEntity("{\"title\": \"" + indexName + "\"}"));
        System.out.println(body(restClient.execute(request)));

    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("Test problem", e);
    }

}

From source file:org.fcrepo.integration.AbstractResourceIT.java

protected static HttpPut putDSMethod(final String pid, final String ds, final String content)
        throws UnsupportedEncodingException {
    final HttpPut put = new HttpPut(serverAddress + pid + "/" + ds + "/fcr:content");

    put.setEntity(new StringEntity(content));
    return put;//ww w .j  a va2  s  . co m
}