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.ecofactor.qa.automation.consumerapi.dr.HTTPSClient.java

/**
 * Put response.//from  ww w.  j  ava 2s.c o  m
 *
 * @param url the url
 * @param json the json
 * @param httpClient the http client
 * @return the http response
 */
public static synchronized HttpResponse putResponse(final String url, final String json,
        final CloseableHttpClient httpClient) {

    try {
        checkHTTPClient(httpClient);
        final HttpPut httpPut = new HttpPut(url);
        httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPut.setHeader(HttpHeaders.ACCEPT, "application/json");

        final StringEntity params = new StringEntity(json);
        httpPut.setEntity(params);

        final CloseableHttpResponse response = httpClient.execute(httpPut);
        setLogString("Status == " + response.getStatusLine(), true);
        return response;

    } catch (IOException | HTTPClientException e) {
        setLogString("Error executing HTTPS method. Reason ::: " + e, true);
        return null;
    }
}

From source file:com.ecofactor.qa.automation.drapi.HTTPSClient.java

/**
 * Put response./*from   w  ww.j a va2  s.  c o  m*/
 * @param url the url
 * @param json the json
 * @param httpClient the http client
 * @return the http response
 */
public static synchronized HttpResponse putResponse(final String url, final String json,
        final CloseableHttpClient httpClient) {

    try {
        checkHTTPClient(httpClient);
        final HttpPut httpPut = new HttpPut(url);
        httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPut.setHeader(HttpHeaders.ACCEPT, "application/json");

        final StringEntity params = new StringEntity(json);
        httpPut.setEntity(params);

        final CloseableHttpResponse response = httpClient.execute(httpPut);
        setLogString("URL Values of the API \n" + url + "\n" + json, true);
        setLogString("Status == " + response.getStatusLine(), true);
        // setLogString("response " + response, true);
        return response;

    } catch (IOException | HTTPClientException e) {
        setLogString("Error executing HTTPS method. Reason ::: " + e, true);
        return null;
    }
}

From source file:net.ravendb.client.RavenDBAwareTests.java

protected static void startServerWithOAuth(int port) {
    HttpPut put = null;
    try {//from   www .  j  a  va 2  s  .  com
        put = new HttpPut(DEFAULT_SERVER_RUNNER_URL);
        put.setEntity(new StringEntity(getCreateServerDocumentWithApiKey(port), ContentType.APPLICATION_JSON));
        HttpResponse httpResponse = client.execute(put);
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new IllegalStateException(
                    "Invalid response on put:" + httpResponse.getStatusLine().getStatusCode());
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } finally {
        if (put != null) {
            put.releaseConnection();
        }
    }
}

From source file:org.wso2.dss.integration.test.odata.ODataSuperTenantUserTestCase.java

private static int sendPUT(String endpoint, String content, String acceptType) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPut httpPut = new HttpPut(endpoint);
    httpPut.setHeader("Accept", acceptType);
    if (null != content) {
        HttpEntity httpEntity = new ByteArrayEntity(content.getBytes("UTF-8"));
        httpPut.setHeader("Content-Type", "application/json");
        httpPut.setEntity(httpEntity);
    }//w  ww.  j  ava  2 s. com
    HttpResponse httpResponse = httpClient.execute(httpPut);
    return httpResponse.getStatusLine().getStatusCode();
}

From source file:utils.APIImporter.java

/**
 * Updated an existing API/*from   ww  w  .  j a  v  a 2s. c om*/
 * @param payload payload to update the API
 * @param apiId API id of the updating API (provider-name-version)
 * @param token access token
 * @param folderPath folder path to the imported API folder
 */
private static void updateApi(String payload, String apiId, String token, String folderPath)
        throws APIImportException {
    String url = config.getPublisherUrl() + "apis/" + apiId;
    CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate());
    HttpPut request = new HttpPut(url);
    request.setEntity(new StringEntity(payload, ImportExportConstants.CHARSET));
    request.setHeader(HttpHeaders.AUTHORIZATION, ImportExportConstants.CONSUMER_KEY_SEGMENT + " " + token);
    request.setHeader(HttpHeaders.CONTENT_TYPE, ImportExportConstants.CONTENT_JSON);
    try {
        CloseableHttpResponse response = client.execute(request);
        if (response.getStatusLine().getStatusCode() == Response.Status.OK.getStatusCode()) {
            //getting API uuid
            String responseString = EntityUtils.toString(response.getEntity());
            String uuid = ImportExportUtils.readJsonValues(responseString, ImportExportConstants.UUID);
            updateAPIDocumentation(uuid, apiId, token, folderPath);
            addAPIImage(folderPath, token, uuid);
            System.out.println("API " + apiId + " updated successfully");
        } else {
            String status = response.getStatusLine().toString();
            log.error(status);
            System.out.println(apiId + " update unsuccessful");
        }
    } catch (IOException e) {
        errorMsg = "Error occurred while updating, API " + apiId;
        log.error(errorMsg, e);
        throw new APIImportException(errorMsg, e);
    }
}

From source file:net.modelbased.proasense.storage.registry.RestRequest.java

public static String putData(URI uri, String data) {
    URI target = null;// w w w  .  jav a  2 s  . c  o  m
    try {
        target = new URI(uri.toString() + DISPATCHER_PATH);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }
    HttpClient client = new DefaultHttpClient();
    HttpPut request = new HttpPut(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        StringEntity seContent = new StringEntity(data);
        seContent.setContentType("text/json");
        request.setEntity(seContent);
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (response.trim().length() > 2) {
        throw new IllegalAccessError("Sensor not registred: " + response);
    }
    return response;
}

From source file:org.fcrepo.it.SparqlRecipesIT.java

private static void putDummyDatastream(final String path, final String mimeType) throws IOException {
    final HttpPut put = new HttpPut(getURIForPid(path));
    put.setHeader("Content-type", mimeType);
    put.setEntity(new StringEntity("garbage"));
    Assert.assertEquals(201, getStatusCode(put));
}

From source file:org.sensapp.android.sensappdroid.restrequests.RestRequest.java

public static String putData(Uri uri, String data) throws RequestErrorException {
    URI target;/* ww  w.  ja va  2s. c  o  m*/
    try {
        target = new URI(uri.toString() + DISPATCHER_PATH);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        throw new RequestErrorException(e1.getMessage());
    }
    HttpClient client = new DefaultHttpClient();
    HttpPut request = new HttpPut(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        StringEntity seContent = new StringEntity(data);
        seContent.setContentType("text/json");
        request.setEntity(seContent);
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        throw new RequestErrorException(e.getMessage());
    }
    if (response.trim().length() > 2) {
        throw new RequestErrorException("Sensor not registred: " + response);
    }
    return response;
}

From source file:com.ibm.watson.movieapp.dialog.rest.UtilityFunctions.java

/**
 * Makes HTTP PUT request/*from  ww  w . j a v a 2 s  .  c o  m*/
 * <p>
 * This makes HTTP PUT requests to the url provided.</p>
 * 
 * @param httpClient the http client used to make the request
 * @param url the url for the request
 * @param requestJson the JSON object containing the parameters for the PUT request
 * @return the JSON object response
 * @throws ClientProtocolException if it is unable to execute the call
 * @throws IOException if it is unable to execute the call
 * @throws IllegalStateException if the input stream could not be parsed correctly
 * @throws HttpException if the HTTP call responded with a status code other than 200 or 201
 */
public static JsonObject httpPut(CloseableHttpClient httpClient, String url, JsonObject requestJson)
        throws ClientProtocolException, IOException, IllegalStateException, HttpException {
    HttpPut httpPut = new HttpPut(url);
    httpPut.addHeader("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$
    httpPut.addHeader("Accept", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$
    String inp = requestJson.toString();
    StringEntity input = new StringEntity(inp, ContentType.APPLICATION_JSON);
    httpPut.setEntity(input);
    try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
        return UtilityFunctions.parseHTTPResponse(response, url);
    } catch (ClientProtocolException e) {
        throw e;
    }
}

From source file:com.ninehcom.userinfo.util.HttpUtils.java

/**
 * Put stream/*from w  ww  . j a  va  2s  . co m*/
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers,
        Map<String, String> querys, byte[] body) throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (body != null) {
        request.setEntity(new ByteArrayEntity(body));
    }

    return httpClient.execute(request);
}