Example usage for org.apache.http.client.methods HttpPatch setEntity

List of usage examples for org.apache.http.client.methods HttpPatch setEntity

Introduction

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

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:org.fcrepo.integration.AbstractResourceIT.java

protected HttpResponse setProperty(final String pid, final String txId, final String propertyUri,
        final String value) throws IOException {
    final HttpPatch postProp = new HttpPatch(serverAddress + (txId != null ? txId + "/" : "") + pid);
    postProp.setHeader(CONTENT_TYPE, "application/sparql-update");
    final String updateString = "INSERT { <" + serverAddress + pid + "> <" + propertyUri + "> \"" + value
            + "\" } WHERE { }";
    postProp.setEntity(new StringEntity(updateString));
    final HttpResponse dcResp = execute(postProp);
    assertEquals(dcResp.getStatusLine().toString(), 204, dcResp.getStatusLine().getStatusCode());
    postProp.releaseConnection();/*w  ww. ja v a2s .c o m*/
    return dcResp;
}

From source file:org.fcrepo.integration.generator.DublinCoreGeneratorIT.java

@Test
public void testJcrPropertiesBasedOaiDc() throws Exception {
    final int status = getStatus(postObjMethod("DublinCoreTest1"));
    assertEquals(201, status);//ww  w . j av  a 2s  . c  om
    final HttpPatch post = new HttpPatch(serverAddress + "DublinCoreTest1");
    post.setHeader("Content-Type", "application/sparql-update");
    final BasicHttpEntity entity = new BasicHttpEntity();
    final String subjectURI = serverAddress + "DublinCoreTest1";
    entity.setContent(new ByteArrayInputStream(("INSERT { <" + subjectURI
            + "> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\" } WHERE {}")
                    .getBytes()));
    post.setEntity(entity);
    assertEquals(204, getStatus(post));
    final HttpGet getWorstCaseOaiMethod = new HttpGet(serverOAIAddress + "DublinCoreTest1/oai:dc");
    getWorstCaseOaiMethod.setHeader("Accept", TEXT_XML);
    final HttpResponse response = client.execute(getWorstCaseOaiMethod);

    assertEquals(200, response.getStatusLine().getStatusCode());

    final String content = EntityUtils.toString(response.getEntity());
    logger.debug("Got content: {}", content);
    assertTrue("Didn't find oai_dc!", compile("oai_dc", DOTALL).matcher(content).find());

    assertTrue("Didn't find dc:identifier!", compile("dc:identifier", DOTALL).matcher(content).find());
}

From source file:org.fcrepo.integration.http.api.html.FedoraHtmlResponsesIT.java

License:asdf

private void postSparqlUpdateUsingHttpClient(final String sparql, final String pid) throws IOException {
    final HttpPatch method = new HttpPatch(serverAddress + pid);
    method.addHeader("Content-Type", "application/sparql-update");
    final BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(sparql.getBytes()));
    method.setEntity(entity);
    final HttpResponse response = client.execute(method);
    assertEquals("Expected successful response.", 204, response.getStatusLine().getStatusCode());
}

From source file:com.precioustech.fxtrading.oanda.restapi.trade.OandaTradeManagementProvider.java

HttpPatch createPatchCommand(Long accountId, Long tradeId, double stopLoss, double takeProfit)
        throws Exception {
    HttpPatch httpPatch = new HttpPatch(getTradeForAccountUrl(tradeId, accountId));
    httpPatch.setHeader(this.authHeader);
    List<NameValuePair> params = Lists.newArrayList();
    params.add(new BasicNameValuePair(OandaJsonKeys.takeProfit, String.valueOf(takeProfit)));
    params.add(new BasicNameValuePair(OandaJsonKeys.stopLoss, String.valueOf(stopLoss)));
    httpPatch.setEntity(new UrlEncodedFormEntity(params));
    return httpPatch;
}

From source file:com.github.restdriver.clientdriver.integration.ClientDriverSuccessTest.java

@Test
public void testJettyWorkingWithPatchBody() throws Exception {

    String baseUrl = driver.getBaseUrl();
    driver.addExpectation(/*  w ww. ja  v a2s.  c  om*/
            onRequestTo("/blah2").withMethod(Method.custom("PATCH")).withBody("Jack your body!", "text/plain"),
            giveResponse("___", "text/plain").withStatus(501));

    HttpClient client = new DefaultHttpClient();
    HttpPatch patcher = new HttpPatch(baseUrl + "/blah2");
    patcher.setEntity(new StringEntity("Jack your body!", ContentType.TEXT_PLAIN));
    HttpResponse response = client.execute(patcher);

    assertThat(response.getStatusLine().getStatusCode(), is(501));
    assertThat(IOUtils.toString(response.getEntity().getContent()), equalTo("___"));
}

From source file:com.github.restdriver.clientdriver.integration.ClientDriverSuccessTest.java

@Test
public void testJettyWorkingWithPatchBodyPattern() throws Exception {

    String baseUrl = driver.getBaseUrl();
    driver.addExpectation(//from  w w  w  .jav  a 2s. c o m
            onRequestTo("/blah2").withMethod(Method.custom("PATCH"))
                    .withBody(Pattern.compile("Jack [\\w\\s]+!"), "text/plain"),
            giveResponse("___", "text/plain").withStatus(501));

    HttpClient client = new DefaultHttpClient();
    HttpPatch patcher = new HttpPatch(baseUrl + "/blah2");
    patcher.setEntity(new StringEntity("Jack your body!", ContentType.TEXT_PLAIN));
    HttpResponse response = client.execute(patcher);

    assertThat(response.getStatusLine().getStatusCode(), is(501));
    assertThat(IOUtils.toString(response.getEntity().getContent()), equalTo("___"));
}

From source file:com.cloud.network.brocade.BrocadeVcsApi.java

protected <T> boolean executeUpdateObject(T newObject, String uri) throws BrocadeVcsApiException {

    boolean result = true;

    if (_host == null || _host.isEmpty() || _adminuser == null || _adminuser.isEmpty() || _adminpass == null
            || _adminpass.isEmpty()) {//  w w w .  jav a  2 s  . co  m
        throw new BrocadeVcsApiException("Hostname/credentials are null or empty");
    }

    HttpPatch pm = (HttpPatch) createMethod("patch", uri);
    pm.setHeader("Accept", "application/vnd.configuration.resource+xml");

    pm.setEntity(new StringEntity(convertToString(newObject), ContentType.APPLICATION_XML));

    HttpResponse response = executeMethod(pm);

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {

        String errorMessage;
        try {
            errorMessage = responseToErrorMessage(response);
        } catch (IOException e) {
            s_logger.error("Failed to update object : " + e.getMessage());
            throw new BrocadeVcsApiException("Failed to update object : " + e.getMessage());
        }

        pm.releaseConnection();
        s_logger.error("Failed to update object : " + errorMessage);
        throw new BrocadeVcsApiException("Failed to update object : " + errorMessage);
    }

    pm.releaseConnection();

    return result;
}

From source file:com.hp.ov.sdk.rest.http.core.client.HttpRestClient.java

/**
 * Sends a PATCH request to HPE OneView.
 *
 * @param params//from w  w  w  .  ja v a  2  s . co  m
 *  connection parameters.
 * @param requestBody
 *  The request body information
 * @param forceReturnTask
 *  Forces the check for the Location header (task) even when the response code is not 202.
 * @return
 *  The response from HPE OneView
 * @throws SDKBadRequestException
 */
private String sendPatchRequest(RestParams params, String requestBody, final boolean forceReturnTask)
        throws SDKBadRequestException {
    try {
        HttpPatch patch = new HttpPatch(buildURI(params));
        if (requestBody == null) {
            requestBody = "";
        }
        patch.setEntity(new StringEntity(requestBody, CHAR_SET));
        setRequestHeaders(params, patch);
        patch.setConfig(createRequestTimeoutConfiguration());
        return getResponse(patch, params, forceReturnTask);
    } catch (IllegalArgumentException e) {
        throw new SDKBadRequestException(SDKErrorEnum.badRequestError, null, null, null, SdkConstants.APPLIANCE,
                e);
    }
}

From source file:org.apache.olingo.server.example.TripPinServiceTest.java

@Test
public void testUpdateEntity() throws Exception {
    String payload = "{" + "  \"Emails\":[" + "     \"Krista@example.com\"," + "     \"Krista@gmail.com\""
            + "         ]" + "}";
    HttpPatch updateRequest = new HttpPatch(baseURL + "/People('kristakemp')");
    updateRequest.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
    httpSend(updateRequest, 204);//from  w w  w  .j  a  v  a  2 s.c om

    HttpResponse response = httpGET(baseURL + "/People('kristakemp')", 200);
    JsonNode node = getJSONNode(response);
    assertEquals("$metadata#People/$entity", node.get("@odata.context").asText());
    assertEquals("Krista@example.com", node.get("Emails").get(0).asText());
    assertEquals("Krista@gmail.com", node.get("Emails").get(1).asText());
}

From source file:com.precioustech.fxtrading.oanda.restapi.order.OandaOrderManagementProvider.java

HttpPatch createPatchCommand(Order<String, Long> order, Long accountId) throws Exception {
    HttpPatch httpPatch = new HttpPatch(orderForAccountUrl(accountId, order.getOrderId()));
    httpPatch.setHeader(this.authHeader);
    List<NameValuePair> params = Lists.newArrayList();
    params.add(new BasicNameValuePair(takeProfit, String.valueOf(order.getTakeProfit())));
    params.add(new BasicNameValuePair(stopLoss, String.valueOf(order.getStopLoss())));
    params.add(new BasicNameValuePair(units, String.valueOf(order.getUnits())));
    params.add(new BasicNameValuePair(price, String.valueOf(order.getPrice())));
    httpPatch.setEntity(new UrlEncodedFormEntity(params));
    return httpPatch;
}