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.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java

public static void sendPut(String serverURL, String postBody) throws Exception {

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPut post = new HttpPut(serverURL);

    StringEntity entity = new StringEntity(postBody, "UTF-8");

    post.setEntity(entity);

    HttpResponse response = client.execute(post);
    // System.out.println("\nSending 'PUT' request to URL : " + serverURL);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*from w  w w.j av  a2  s  .c  om*/
    }
    client.close();
    // System.out.println(result.toString());
}

From source file:com.mber.client.HTTParty.java

public static Call put(final String url, final JSONObject data)
        throws UnsupportedEncodingException, IOException {
    HttpPut request = new HttpPut(url);
    request.setEntity(toStringEntity(data));

    return execute(request);
}

From source file:org.fcrepo.it.BasicIT.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(content1));
    Assert.assertEquals(201, getStatusCode(put));
}

From source file:com.mber.client.HTTParty.java

public static Call put(final String url, final File file) throws IOException {
    FileEntity entity = new FileEntity(file);
    entity.setContentType("application/octet-stream");

    HttpPut request = new HttpPut(url);
    request.setEntity(entity);

    return execute(request);
}

From source file:com.mber.client.HTTParty.java

public static Call put(final String url, final File file, final LoggingOutputStream.Listener listener)
        throws IOException {
    LoggingFileEntity entity = new LoggingFileEntity(file, listener);
    entity.setContentType("application/octet-stream");

    HttpPut request = new HttpPut(url);
    request.setEntity(entity);

    return execute(request);
}

From source file:onl.area51.filesystem.http.client.HttpUtils.java

public static void send(char[] path, Function<char[], String> remoteUri, Function<char[], Path> getPath,
        Supplier<String> userAgent) throws IOException {
    if (path == null || path.length == 0) {
        throw new FileNotFoundException("/");
    }// www .java2  s  . c om

    String uri = remoteUri.apply(path);
    if (uri != null) {

        HttpEntity entity = new PathEntity(getPath.apply(path));

        LOG.log(Level.FINE, () -> "Sending " + uri);

        HttpPut put = new HttpPut(uri);
        put.setHeader(USER_AGENT, userAgent.get());
        put.setEntity(entity);

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = client.execute(put)) {

                int returnCode = response.getStatusLine().getStatusCode();
                LOG.log(Level.FINE,
                        () -> "ReturnCode " + returnCode + ": " + response.getStatusLine().getReasonPhrase());
            }
        }
    }
}

From source file:org.jboss.capedwarf.tools.BulkLoader.java

private static void sendPacket(UploadPacket packet, DefaultHttpClient client, String url) throws IOException {
    HttpPut put = new HttpPut(url);
    System.out.println("Uploading packet of " + packet.size() + " entities");
    put.setEntity(new FileEntity(packet.getFile(), "application/capedwarf-data"));
    HttpResponse response = client.execute(put);
    response.getEntity().writeTo(new ByteArrayOutputStream());
    System.out.println("Received response " + response);
}

From source file:com.uber.stream.kafka.mirrormaker.common.utils.HttpClientUtils.java

public static int putData(final HttpClient httpClient, final RequestConfig requestConfig, final String host,
        final int port, final String path, final JSONObject entity) throws IOException, URISyntaxException {
    URI uri = new URIBuilder().setScheme("http").setHost(host).setPort(port).setPath(path).build();

    HttpPut httpPut = new HttpPut(uri);
    httpPut.setConfig(requestConfig);/*  w  w  w.  ja v a2  s.  c  om*/

    StringEntity params = new StringEntity(entity.toJSONString());
    httpPut.setEntity(params);

    return httpClient.execute(httpPut, HttpClientUtils.createResponseCodeExtractor());
}

From source file:org.opencastproject.remotetest.server.resource.SchedulerResources.java

public static HttpResponse addEvent(TrustedHttpClient client, String event) throws Exception {
    HttpPut put = new HttpPut(getServiceUrl() + "event");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("event", event));
    put.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(put);
}

From source file:org.eclipse.lyo.testsuite.server.trsutils.SendUtil.java

/**
 * /*from  w  ww  .j ava  2  s.co m*/
 * @param uri             resource uri for update
 * @param httpClient      client used to put data to the uri
 * @param httpContext     http context to use for the call
 * @param content         content to be used in the updation 
 * @throws SendException  if an error occurs in putting data to the uri
 */

public static boolean updateResource(String uri, HttpClient httpClient, HttpContext httpContext,
        String contentType, String content) throws SendException {
    boolean resourceUpdated = false;
    if (uri == null)
        throw new IllegalArgumentException(Messages.getServerString("send.util.uri.null")); //$NON-NLS-1$
    if (httpClient == null)
        throw new IllegalArgumentException(Messages.getServerString("send.util.httpclient.null")); //$NON-NLS-1$
    try {
        new URL(uri); // Make sure URL is valid

        HttpPut put = new HttpPut(uri);
        StringEntity entity = new StringEntity(content);
        put.setEntity(entity);
        put.setHeader(HttpConstants.ACCEPT, HttpConstants.CT_APPLICATION_RDF_XML);
        put.addHeader(HttpConstants.CONTENT_TYPE, contentType);
        put.addHeader(HttpConstants.CACHE_CONTROL, "max-age=0"); //$NON-NLS-1$
        HttpResponse resp = httpClient.execute(put);

        try {
            if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                HttpErrorHandler.responseToException(resp);
            }
            resourceUpdated = true;
            HttpResponseUtil.finalize(resp);
        } finally {
            try {
                if (entity != null) {
                    EntityUtils.consume(entity);
                }
            } catch (IOException e) {
                // ignore
            }
        }

    } catch (Exception e) {
        String uriLocation = Messages.getServerString("send.util.uri.unidentifiable"); //$NON-NLS-1$

        if (uri != null && !uri.isEmpty()) {
            uriLocation = uri;
        }
        throw new SendException(MessageFormat.format(Messages.getServerString("send.util.retrieve.error"), //$NON-NLS-1$
                uriLocation));
    }

    return resourceUpdated;
}