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.restfiddle.handler.http.PutHandler.java

public RfResponseDTO process(RfRequestDTO rfRequestDTO) throws IOException {
    RfResponseDTO response = null;// w w  w  .  ja v a 2 s.c o  m
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPut httpPut = new HttpPut(rfRequestDTO.getApiUrl());
    httpPut.addHeader("Content-Type", "application/json");
    httpPut.setEntity(new StringEntity(rfRequestDTO.getApiBody()));
    try {
        response = processHttpRequest(httpPut, httpclient);
    } finally {
        httpclient.close();
    }
    return response;
}

From source file:com.urbancode.ud.client.PropertyClient.java

public UUID createPropDef(UUID propSheetDefId, String propSheetDefPath, String name, String description,
        String label, Boolean required, String type, String value) throws IOException, JSONException {
    UUID result;//  w  w w.  j ava2 s  . c o m

    String uri = url + "/property/propSheetDef/" + encodePath(propSheetDefPath) + ".-1/propDefs";
    JSONObject propDefObject = new JSONObject();

    propDefObject.put("name", name);
    propDefObject.put("description", description);
    propDefObject.put("label", label);
    propDefObject.put("required", required.toString());
    propDefObject.put("type", type);
    propDefObject.put("value", value);
    propDefObject.put("definitionGroupId", propSheetDefId);

    HttpPut method = new HttpPut(uri);
    method.setEntity(getStringEntity(propDefObject));

    HttpResponse response = invokeMethod(method);
    String body = getBody(response);
    result = UUID.fromString(new JSONObject(body).getString("id"));
    return result;
}

From source file:com.sonatype.nexus.perftest.maven.ArtifactDeployer.java

private void deploy0(HttpEntity entity, String groupId, String artifactId, String version, String extension)
        throws IOException {
    StringBuilder path = new StringBuilder();

    path.append(groupId.replace('.', '/')).append('/');
    path.append(artifactId).append('/');
    path.append(version).append('/');
    path.append(artifactId).append('-').append(version).append(extension);

    HttpPut httpPut = new HttpPut(repoUrl + path);
    httpPut.setEntity(entity);

    HttpResponse response;/*from   ww w .  j  a  v  a 2s  .  c  o  m*/
    try {
        response = httpclient.execute(httpPut);

        try {
            EntityUtils.consume(response.getEntity());
        } finally {
            httpPut.releaseConnection();
        }
    } catch (IOException e) {
        throw new IOException("IOException executing " + httpPut.toString(), e);
    }

    if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) {
        throw new IOException(httpPut.toString() + " : " + response.getStatusLine().toString());
    }
}

From source file:com.redhat.red.build.koji.it.AbstractWithSetupIT.java

protected void uploadFile(InputStream resourceStream, String targetPath, CloseableHttpClient client) {
    String url = formatUrl(CONTENT_CGI, targetPath);
    System.out.println("\n\n ##### START: " + name.getMethodName() + " :: Upload -> " + url + " #####\n\n");

    CloseableHttpResponse response = null;
    try {//from   ww w.  ja v  a  2s  .  c om
        HttpPut put = new HttpPut(url);
        put.setEntity(new InputStreamEntity(resourceStream));
        response = client.execute(put);
    } catch (IOException e) {
        e.printStackTrace();
        fail(String.format("Failed to execute GET request: %s. Reason: %s", url, e.getMessage()));
    }

    int code = response.getStatusLine().getStatusCode();
    if (code != 201 || code != 200) {
        fail("Failed to upload content: " + targetPath + "\nSERVER RESPONSE STATUS: "
                + response.getStatusLine());
    } else {
        System.out.println("\n\n ##### END: " + name.getMethodName() + " :: Upload -> " + url + " #####\n\n");
    }
}

From source file:de.taimos.camel_cosm.CosmProducer.java

@Override
public void process(Exchange exchange) throws Exception {
    String key = this.endpoint.getApiKey();

    final String keyHeader = exchange.getIn().getHeader("CosmKey", String.class);
    if (keyHeader != null) {
        key = keyHeader;//from w w w . ja v a2  s  . co m
    }

    if (key == null) {
        throw new RuntimeCamelException("No ApiKey present!");
    }

    final HttpClient httpclient = new DefaultHttpClient();
    final HttpPut put = new HttpPut(this.url);
    put.setHeader("X-PachubeApiKey", key);
    put.setEntity(new StringEntity(exchange.getIn().getBody(String.class)));

    final HttpResponse response = httpclient.execute(put);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode >= 400) {
        throw new RuntimeCamelException("Error: " + response.getStatusLine().getReasonPhrase());
    }
    CosmProducer.LOG.info(response.toString());
}

From source file:org.jboss.pnc.mavenrepositorymanager.IndyPromotionValidationTest.java

private boolean put(CloseableHttpClient client, String url, String content) throws IOException {
    HttpPut put = new HttpPut(url);
    put.setEntity(new StringEntity(content));
    return client.execute(put, response -> {
        try {//from ww w  .ja  va 2  s  . c om
            return response.getStatusLine().getStatusCode() == 201;
        } finally {
            if (response instanceof CloseableHttpResponse) {
                IOUtils.closeQuietly((CloseableHttpResponse) response);
            }
        }
    });
}

From source file:net.gcolin.httpquery.HttpHandlerImpl.java

@Override
public Request put(String uri, String str) {
    HttpPut put = new HttpPut(uri);
    try {/*from  w w w.j  av  a  2 s .c  o  m*/
        put.setEntity(new StringEntity(str));
    } catch (UnsupportedEncodingException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
    }
    return new RequestImpl(put);
}

From source file:org.jenkinsci.plugins.relution_publisher.net.requests.EntityRequest.java

private HttpUriRequest createHttpRequest(final Method method, final String uri, final HttpEntity entity) {

    switch (method) {
    default://from   w w  w  .ja  va  2  s. c  om
    case GET:
        return new HttpGet(uri);

    case POST:
        final HttpPost post = new HttpPost(uri);
        if (entity != null) {
            post.setEntity(entity);
        }
        return post;

    case PUT:
        final HttpPut put = new HttpPut(uri);
        if (entity != null) {
            put.setEntity(entity);
        }
        return put;

    case DELETE:
        return new HttpDelete(uri);
    }
}

From source file:com.nominanuda.web.http.ServletHelperTest.java

@Test
public void testCopyRequest() throws Exception {
    final String msg = "mio";
    final String mediaType = CT_APPLICATION_OCTET_STREAM;
    Server server = startServer(10000, new AbstractHandler() {
        public void handle(String arg0, Request jettyReq, HttpServletRequest servletReq,
                HttpServletResponse arg3) throws IOException, ServletException {
            HttpRequest r = servletHelper.copyRequest(servletReq, false);
            asyncAssertEquals("bar", r.getFirstHeader("X-foo").getValue());
            asyncAssertEquals("PUT", r.getRequestLine().getMethod());
            HttpEntity e = ((HttpEntityEnclosingRequest) r).getEntity();
            asyncAssert(msg.getBytes("UTF-8").length == e.getContentLength(), "length");
            asyncAssert(e.getContentType().getValue().startsWith(mediaType));
            asyncAssertEquals(mediaType, ContentType.get(e).getMimeType());
            asyncAssertEquals(msg, EntityUtils.toString(e));
        }/*from ww w  .j av a2s  .  c o m*/
    });
    HttpClient c = buildClient(1);
    HttpPut req = new HttpPut("http://localhost:10000/foo/bar?a=b&a=");
    req.setEntity(new StringEntity(msg, ContentType.create(mediaType, CS_UTF_8)));
    req.addHeader("X-foo", "bar");
    c.execute(req);
    server.stop();
    dumpFailures(System.err);
    Assert.assertFalse(isFailed());
}

From source file:org.flowable.admin.service.engine.EventSubscriptionService.java

public void triggerSignalEvent(ServerConfig serverConfig, String eventName) {
    ObjectNode node = JsonNodeFactory.instance.objectNode();
    node.put("action", "signalEventReceived");
    node.put("signalName", eventName);

    HttpPut put = clientUtil.createPut("runtime/executions", serverConfig);
    put.setEntity(clientUtil.createStringEntity(node));

    clientUtil.executeRequest(put, serverConfig);
}