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.marklogic.client.test.util.TestServerBootstrapper.java

private void installBootstrapExtension() throws IOException {

    DefaultHttpClient client = new DefaultHttpClient();

    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", port),
            new UsernamePasswordCredentials(username, password));

    HttpPut put = new HttpPut("http://" + host + ":" + port + "/v1/config/resources/bootstrap?method=POST");

    put.setEntity(new FileEntity(new File("src/test/resources/bootstrap.xqy"), "application/xquery"));
    HttpResponse response = client.execute(put);
    @SuppressWarnings("unused")
    HttpEntity entity = response.getEntity();
    System.out.println("Installed bootstrap extension.  Response is " + response.toString());

}

From source file:webrequester.CouchDBWebRequest.java

public String requestWithPut(String content) {
    String s = "";
    try {//  w  w  w. ja  v a2s  . c  om

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut httpput = new HttpPut(buildURI().toString());

        StringEntity se = new StringEntity(content);
        httpput.setEntity(se);

        CloseableHttpResponse response = httpclient.execute(httpput);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                s = EntityUtils.toString(entity, "UTF-8");
            }
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        return null;
    }
    return s;
}

From source file:se.svt.helios.serviceregistration.consul.ConsulClient.java

public Future<HttpResponse> register(final Service record) throws JsonProcessingException {
    final String value = OBJECT_MAPPER.writeValueAsString(record);

    final URI uri = URI.create(baseUri + REGISTER_ENDPOINT);
    final HttpPut request = new HttpPut(uri);
    request.setEntity(new StringEntity(value, "UTF-8"));

    return httpClient.execute(request, new RequestCallback("register service " + record.getId()));
}

From source file:org.deviceconnect.android.manager.test.MultipartTest.java

/**
 * PUT????????.//from  w w  w .j a  v  a  2s  . com
 */
public void testParsingMutilpartAsRequestParametersMethodPut() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(DeviceOrientationProfileConstants.PROFILE_NAME);
    builder.setAttribute(DeviceOrientationProfileConstants.ATTRIBUTE_ON_DEVICE_ORIENTATION);
    try {
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(DConnectProfileConstants.PARAM_DEVICE_ID, new StringBody(getDeviceId()));
        entity.addPart(DConnectProfileConstants.PARAM_SESSION_KEY, new StringBody(getClientId()));
        entity.addPart(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, new StringBody(getAccessToken()));
        HttpPut request = new HttpPut(builder.toString());
        request.setEntity(entity);
        JSONObject response = sendRequest(request);
        assertResultOK(response);
    } catch (UnsupportedEncodingException e) {
        fail(e.getMessage());
    } catch (JSONException e) {
        fail(e.getMessage());
    }
}

From source file:org.gradle.cache.tasks.http.HttpTaskOutputCache.java

@Override
public void store(TaskCacheKey key, final TaskOutputWriter output) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {/*from ww  w  . ja va  2  s  .co  m*/
        final URI uri = root.resolve(key.getHashCode());
        HttpPut httpPut = new HttpPut(uri);
        httpPut.setEntity(new AbstractHttpEntity() {
            @Override
            public boolean isRepeatable() {
                return true;
            }

            @Override
            public long getContentLength() {
                return -1;
            }

            @Override
            public InputStream getContent() throws IOException, UnsupportedOperationException {
                throw new UnsupportedOperationException();
            }

            @Override
            public void writeTo(OutputStream outstream) throws IOException {
                output.writeTo(outstream);
            }

            @Override
            public boolean isStreaming() {
                return false;
            }
        });
        CloseableHttpResponse response = httpClient.execute(httpPut);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Response for PUT {}: {}", uri, response.getStatusLine());
        }
    } finally {
        httpClient.close();
    }
}

From source file:dk.i2m.drupal.resource.NodeResource.java

public NodeMessage update(Long id, UrlEncodedFormEntity uefe) throws HttpResponseException, IOException {
    URLBuilder builder = new URLBuilder(getDc().getHostname());
    builder.add(getDc().getEndpoint());//from w w  w  . j a va 2 s  .co m
    builder.add(getAlias());
    builder.add(id);

    HttpPut method = new HttpPut(builder.toURI());
    method.setEntity(uefe);

    ResponseHandler<String> handler = new BasicResponseHandler();
    String response = getDc().getHttpClient().execute(method, handler);

    return new Gson().fromJson(response, NodeMessage.class);
}

From source file:nl.esciencecenter.osmium.mac.MacITCase.java

/**
 * Submit status to a callback server using MAC Access authentication.
 * //from   w  w  w.  ja v a 2s .com
 * @throws URISyntaxException
 * @throws ClientProtocolException
 * @throws IOException
 */
@Test
public void test() throws URISyntaxException, ClientProtocolException, IOException {
    URI url = getServerURI();

    // TODO throw better exception than NullPointerException when property can not be found.

    String state = "STOPPED";
    HttpPut request = new HttpPut(url);
    request.setEntity(new StringEntity(state));

    String mac_id = "eyJzYWx0IjogIjU3MjY0NCIsICJleHBpcmVzIjogMTM4Njc3MjEwOC4yOTIyNTUsICJ1c2VyaWQiOiAiam9ibWFuYWdlciJ9KBJRMeTW2G9I6jlYwRj6j8koAek=";
    String mac_key = "_B1YfcqEYpZxyTx_-411-QdBOSI=";
    URI scope = new URI(url.getScheme(), null, url.getHost(), url.getPort(), null, null, null);
    ImmutableList<MacCredential> macs = ImmutableList.of(new MacCredential(mac_id, mac_key, scope));
    HttpClient httpClient = new DefaultHttpClient();
    httpClient = JobLauncherService.macifyHttpClient((AbstractHttpClient) httpClient, macs);

    HttpResponse response = httpClient.execute(request);

    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
    assertThat(EntityUtils.toString(response.getEntity())).startsWith("MAC id=\"" + mac_id);
    // asserting rest of request.header[AUTHORIZATION] can not be done due to embedded timestamp and changing hostname/port.
}

From source file:org.graphity.core.util.jena.HttpOp.java

/**
 * Executes a HTTP PUT operation/* w ww  .ja  v a2  s .com*/
 * 
 * @param url
 *            URL
 * @param entity
 *            HTTP Entity to PUT
 * @param httpClient
 *            HTTP Client
 * @param httpContext
 *            HTTP Context
 * @param authenticator
 *            HTTP Authenticator
 */
public static void execHttpPut(String url, HttpEntity entity, HttpClient httpClient, HttpContext httpContext,
        HttpAuthenticator authenticator) {
    String requestURI = determineRequestURI(url);
    HttpPut httpput = new HttpPut(requestURI);
    httpput.setEntity(entity);
    exec(url, httpput, null, nullHandler, httpClient, httpContext, authenticator);
}

From source file:org.wso2.carbon.identity.cloud.web.jaggery.clients.MutualSSLHttpClient.java

public String doPut(String endPoint, HttpHeaders headers, String jsonContent) {
    String responseString = null;
    try {// ww  w  .  j av  a 2  s  . c  o m
        StringEntity inputMappings = new StringEntity(jsonContent);
        inputMappings.setContentType(ApplicationJson);
        HttpPut putMethod = new HttpPut(endPoint);
        putMethod.setEntity(inputMappings);
        responseString = doHttpMethod(putMethod, headers);
    } catch (UnsupportedEncodingException e) {
        log.error("Error while creating payload for http request. Payload : " + jsonContent, e);
    }
    return responseString;
}

From source file:tech.beshu.ror.utils.integration.ElasticsearchTweetsInitializer.java

private void createMessage(RestClient client, String endpoint, String id, String user, String message) {
    try {/*from  w  w w.  j  a  v  a 2  s  .  c  om*/
        HttpPut httpPut = new HttpPut(client.from(endpoint + id));
        httpPut.setHeader("Content-Type", "application/json");
        httpPut.setEntity(new StringEntity("{\n" + "\"user\" : \"" + user + "\",\n" + "\"post_date\" : \""
                + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE) + "\",\n" + "\"message\" : \""
                + message + "\"\n" + "}"));
        EntityUtils.consume(client.execute(httpPut).getEntity());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Creating message failed", e);
    }
}