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

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

Introduction

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

Prototype

public HttpPut(final String uri) 

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:org.eclipse.packagedrone.testing.server.channel.UploadApiV2Test.java

protected CloseableHttpResponse upload(final URIBuilder uri, final File file)
        throws IOException, URISyntaxException {
    final HttpPut httppost = new HttpPut(uri.build());
    httppost.setEntity(new FileEntity(file));

    return httpclient.execute(httppost);
}

From source file:com.github.technosf.posterer.transports.commons.CommonsResponseModelTaskImpl.java

/**
 * @param uri/*from  w w  w  .ja  v a2s  .com*/
 * @param method
 * @return
 */
private static HttpUriRequest createRequest(URI uri, String method) {
    switch (method) {
    case "GET":
        return new HttpGet(uri);
    case "HEAD":
        return new HttpHead(uri);
    case "POST":
        return new HttpPost(uri);
    case "PUT":
        return new HttpPut(uri);
    case "DELETE":
        return new HttpDelete(uri);
    case "TRACE":
        return new HttpTrace(uri);
    case "OPTIONS":
        return new HttpOptions(uri);
    case "PATCH":
        return new HttpPatch(uri);
    }

    return null;
}

From source file:org.janusgraph.diskstorage.es.ElasticSearchIndexTest.java

@BeforeClass
public static void startElasticsearch() throws Exception {
    esr = new ElasticsearchRunner();
    esr.start();//ww  w  .j a v  a2 s.c om
    httpClient = HttpClients.createDefault();
    objectMapper = new ObjectMapper();
    host = new HttpHost(InetAddress.getByName(esr.getHostname()), ElasticsearchRunner.PORT);
    if (esr.getEsMajorVersion().value > 2) {
        IOUtils.closeQuietly(httpClient.execute(host, new HttpDelete("_ingest/pipeline/pipeline_1")));
        final HttpPut newPipeline = new HttpPut("_ingest/pipeline/pipeline_1");
        newPipeline.setHeader("Content-Type", "application/json");
        newPipeline.setEntity(
                new StringEntity("{\"description\":\"Test pipeline\",\"processors\":[{\"set\":{\"field\":\""
                        + STRING + "\",\"value\":\"hello\"}}]}", Charset.forName("UTF-8")));
        IOUtils.closeQuietly(httpClient.execute(host, newPipeline));
    }
}

From source file:com.ibm.stocator.fs.swift.SwiftOutputStream.java

/**
 * Default constructor//from  w  ww .j a  v a  2s.  c  o  m
 *
 * @param account JOSS account object
 * @param url URL connection
 * @param contentType content type
 * @param metadata input metadata
 * @param connectionManager SwiftConnectionManager
 * @throws IOException if error
 */
public SwiftOutputStream(JossAccount account, URL url, final String contentType, Map<String, String> metadata,
        SwiftConnectionManager connectionManager) throws IOException {
    mUrl = url;
    totalWritten = 0;
    mAccount = account;
    client = connectionManager.createHttpConnection();
    request = new HttpPut(mUrl.toString());
    request.addHeader("X-Auth-Token", account.getAuthToken());
    if (metadata != null && !metadata.isEmpty()) {
        for (Map.Entry<String, String> entry : metadata.entrySet()) {
            request.addHeader("X-Object-Meta-" + entry.getKey(), entry.getValue());
        }
    }

    PipedOutputStream out = new PipedOutputStream();
    final PipedInputStream in = new PipedInputStream();
    out.connect(in);
    execService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    mOutputStream = out;
    Callable<Void> task = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStreamEntity entity = new InputStreamEntity(in, -1);
            entity.setChunked(true);
            entity.setContentType(contentType);
            request.setEntity(entity);

            LOG.debug("HTTP PUT request {}", mUrl.toString());
            HttpResponse response = client.execute(request);
            int responseCode = response.getStatusLine().getStatusCode();
            LOG.debug("HTTP PUT response {}. Response code {}", mUrl.toString(), responseCode);
            if (responseCode == 401) { // Unauthorized error
                mAccount.authenticate();
                request.removeHeaders("X-Auth-Token");
                request.addHeader("X-Auth-Token", mAccount.getAuthToken());
                LOG.warn("Token recreated for {}.  Retry request", mUrl.toString());
                response = client.execute(request);
                responseCode = response.getStatusLine().getStatusCode();
            }
            if (responseCode >= 400) { // Code may have changed from retrying
                throw new IOException("HTTP Error: " + responseCode + " Reason: "
                        + response.getStatusLine().getReasonPhrase());
            }

            return null;
        }
    };
    futureTask = execService.submit(task);
}

From source file:de.adesso.referencer.search.helper.ElasticConfig.java

public static String sendHttpRequest(String url, String requestBody, String requestType) throws IOException {
    String result = null;/*from ww  w.j  ava 2 s  . co m*/
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse httpResponse;
    try {
        switch (requestType) {
        case "Get":
            httpResponse = httpclient.execute(new HttpGet(url));
            break;
        case "Post":
            HttpPost httppost = new HttpPost(url);
            if (requestBody != null)
                httppost.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET));
            httpResponse = httpclient.execute(httppost);
            break;
        case "Put":
            HttpPut httpPut = new HttpPut(url);
            httpPut.addHeader("Content-Type", "application/json");
            httpPut.addHeader("Accept", "application/json");
            if (requestBody != null)
                httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET));
            httpResponse = httpclient.execute(httpPut);
            break;
        case "Delete":
            httpResponse = httpclient.execute(new HttpDelete(url));
            break;
        default:
            httpResponse = httpclient.execute(new HttpGet(url));
            break;
        }
        try {
            HttpEntity entity1 = httpResponse.getEntity();
            if (entity1 != null) {
                long len = entity1.getContentLength();
                if (len != -1 && len < MAX_CONTENT_LENGTH) {
                    result = EntityUtils.toString(entity1, DEFAULT_CHARSET);
                } else {
                    System.out.println("Error!!!! entity length=" + len);
                }
            }
            EntityUtils.consume(entity1);
        } finally {
            httpResponse.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        httpclient.close();
    }
    return result;
}

From source file:org.elasticsearch.shell.command.HttpPutCommand.java

@SuppressWarnings("unused")
public HttpCommandResponse execute(String url, String body, String mimeType, String charsetName)
        throws IOException {
    HttpPut httpPut = new HttpPut(url);
    httpPut.setEntity(new StringEntity(body, ContentType.create(mimeType, Charset.forName(charsetName))));
    return new HttpCommandResponse(shellHttpClient.getHttpClient().execute(httpPut));
}

From source file:com.hoccer.tools.HttpHelper.java

public static HttpResponse putXML(String uri, String body)
        throws IOException, HttpClientException, HttpServerException {
    HttpPut put = new HttpPut(uri);
    insertXML(body, put);//  w  ww  .j  av a2s .  c om
    return executeHTTPMethod(put, PUT_TIMEOUT);
}

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:org.fcrepo.auth.oauth.integration.api.AbstractOAuthResourceIT.java

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

    put.setEntity(new StringEntity(content));
    return put;/*from  w ww.  ja v a2 s .c  o  m*/
}