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:org.flowable.rest.service.api.runtime.ExecutionResourceTest.java

/**
 * Test signalling a single execution, without signal name.
 *///from   w w  w. j a v  a 2 s  .  c  om
@Deployment(resources = {
        "org/flowable/rest/service/api/runtime/ExecutionResourceTest.process-with-signal-event.bpmn20.xml" })
public void testSignalEventExecution() throws Exception {
    Execution signalExecution = runtimeService.startProcessInstanceByKey("processOne");
    assertNotNull(signalExecution);

    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("action", "signalEventReceived");
    requestNode.put("signalName", "unexisting");

    Execution waitingExecution = runtimeService.createExecutionQuery().activityId("waitState").singleResult();
    assertNotNull(waitingExecution);

    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
            + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION, waitingExecution.getId()));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_INTERNAL_SERVER_ERROR);
    closeResponse(response);

    requestNode.put("signalName", "alert");

    // Sending signal event causes the execution to end (scope-execution for
    // the catching event)
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    response = executeRequest(httpPut, HttpStatus.SC_OK);
    closeResponse(response);

    // Check if process is moved on to the other wait-state
    waitingExecution = runtimeService.createExecutionQuery().activityId("anotherWaitState").singleResult();
    assertNotNull(waitingExecution);

}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static boolean putFile(String remoteUrlStr, File localFile, String contentType, int timeout)
        throws BAGException {
    HttpPut httpput = null;
    DefaultHttpClient httpclient = null;
    try {/*from   w w w .  ja v  a 2s .  com*/
        BasicHttpParams httpParams = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);

        httpclient = new DefaultHttpClient(httpParams);
        URI remoteUrl = new URI(remoteUrlStr);
        String userInfo = remoteUrl.getUserInfo();
        if ((userInfo != null) && !userInfo.isEmpty()) {
            int colonIx = userInfo.indexOf(':');
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(remoteUrl.getHost(), remoteUrl.getPort()), new UsernamePasswordCredentials(
                            userInfo.substring(0, colonIx), userInfo.substring(colonIx + 1)));
        }

        if ((contentType == null) || contentType.isEmpty()) {
            contentType = "text/plain; charset=\"UTF-8\"";
        }

        FileEntity entity = new FileEntity(localFile, contentType);

        httpput = new HttpPut(remoteUrlStr);
        httpput.setEntity(entity);

        HttpResponse response = httpclient.execute(httpput);
        return response.getStatusLine().getStatusCode() - 200 < 100;

    } catch (IOException ex) {

        // In case of an IOException the connection will be released
        // back to the connection manager automatically
        throw new BAGException(ex);

    } catch (RuntimeException ex) {

        // In case of an unexpected exception you may want to abort
        // the HTTP request in order to shut down the underlying
        // connection and release it back to the connection manager.
        httpput.abort();
        throw ex;

    } catch (URISyntaxException e) {
        // will be still null: httpput.abort();
        throw new BAGException(e);
    } finally {

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();

    }
}

From source file:org.cloudml.connectors.PyHrapiConnector.java

private String invoke(URI uri, String method, String body) throws UnsupportedEncodingException, IOException {
    HttpUriRequest request = null;/*from  w w w . java 2  s.  c o m*/

    if ("GET".equals(method)) {
        request = new HttpGet(uri);
    } else if ("POST".equals(method)) {
        HttpPost post = new HttpPost(uri);
        if (body != null && !body.isEmpty()) {
            post.setHeader("Content-type", "application/json");
            post.setEntity(new StringEntity(body));
        }
        request = post;
    } else if ("PUT".equals(method)) {
        HttpPut put = new HttpPut(uri);
        if (body != null && !body.isEmpty()) {
            put.setHeader("Content-type", "application/json");
            put.setEntity(new StringEntity(body));
        }
        request = put;
    } else if ("DELETE".equals(method)) {
        request = new HttpDelete(uri);
    }
    HttpResponse response = httpclient.execute(request);
    String s = IOUtils.toString(response.getEntity().getContent());
    //TODO: will be removed after debug
    System.out.println(s);
    return s;
}

From source file:com.microsoft.projectoxford.face.rest.WebServiceRequest.java

private Object put(String url, Map<String, Object> data) throws ClientException, IOException {
    HttpPut request = new HttpPut(url);

    request.setHeader(HEADER_KEY, mSubscriptionKey);
    String json = mGson.toJson(data).toString();
    StringEntity entity = new StringEntity(json);
    request.setEntity(entity);
    request.setHeader(CONTENT_TYPE, APPLICATION_JSON);
    HttpResponse response = mClient.execute(request);

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
        return readInput(response.getEntity().getContent());
    } else {/*www  .  j a  v  a  2s .  c o m*/
        json = readInput(response.getEntity().getContent());
        if (json != null) {
            ServiceError error = mGson.fromJson(json, ServiceError.class);
            if (error != null) {
                throw new ClientException(error.error);
            }
        }

        throw new ClientException("Error executing PUT request!", statusCode);
    }
}

From source file:org.activiti.rest.service.api.runtime.ExecutionResourceTest.java

/**
 * Test signalling a single execution, with signal event.
 *//*  w  ww.ja va2s  .c  om*/
@Deployment(resources = {
        "org/activiti/rest/service/api/runtime/ExecutionResourceTest.process-with-signal-event.bpmn20.xml" })
public void testSignalEventExecutionWithvariables() throws Exception {
    Execution signalExecution = runtimeService.startProcessInstanceByKey("processOne");
    assertNotNull(signalExecution);

    ArrayNode variables = objectMapper.createArrayNode();
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("action", "signalEventReceived");
    requestNode.put("signalName", "alert");
    requestNode.put("variables", variables);

    ObjectNode varNode = objectMapper.createObjectNode();
    variables.add(varNode);
    varNode.put("name", "myVar");
    varNode.put("value", "Variable set when signal event is receieved");

    Execution waitingExecution = runtimeService.createExecutionQuery().activityId("waitState").singleResult();
    assertNotNull(waitingExecution);

    // Sending signal event causes the execution to end (scope-execution for
    // the catching event)
    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
            + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION, waitingExecution.getId()));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
    closeResponse(response);

    // Check if process is moved on to the other wait-state
    waitingExecution = runtimeService.createExecutionQuery().activityId("anotherWaitState").singleResult();
    assertNotNull(waitingExecution);

    Map<String, Object> vars = runtimeService.getVariables(waitingExecution.getId());
    assertEquals(1, vars.size());

    assertEquals("Variable set when signal event is receieved", vars.get("myVar"));
}

From source file:org.activiti.rest.service.api.runtime.ExecutionResourceTest.java

/**
 * Test messaging a single execution with variables.
 */// w  ww.j  ava 2s. co m
@Deployment(resources = {
        "org/activiti/rest/service/api/runtime/ExecutionResourceTest.process-with-message-event.bpmn20.xml" })
public void testMessageEventExecutionWithvariables() throws Exception {
    Execution signalExecution = runtimeService.startProcessInstanceByKey("processOne");
    assertNotNull(signalExecution);

    ArrayNode variables = objectMapper.createArrayNode();
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("action", "messageEventReceived");
    requestNode.put("messageName", "paymentMessage");
    requestNode.put("variables", variables);

    ObjectNode varNode = objectMapper.createObjectNode();
    variables.add(varNode);
    varNode.put("name", "myVar");
    varNode.put("value", "Variable set when signal event is receieved");

    Execution waitingExecution = runtimeService.createExecutionQuery().activityId("waitState").singleResult();
    assertNotNull(waitingExecution);

    // Sending signal event causes the execution to end (scope-execution for
    // the catching event)
    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
            + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION, waitingExecution.getId()));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
    closeResponse(response);

    // Check if process is moved on to the other wait-state
    waitingExecution = runtimeService.createExecutionQuery().activityId("anotherWaitState").singleResult();
    assertNotNull(waitingExecution);

    Map<String, Object> vars = runtimeService.getVariables(waitingExecution.getId());
    assertEquals(1, vars.size());

    assertEquals("Variable set when signal event is receieved", vars.get("myVar"));
}

From source file:org.flowable.rest.service.api.runtime.ExecutionResourceTest.java

/**
 * Test signalling a single execution, with signal event.
 *//*from  w w  w . jav  a  2 s  .  c o m*/
@Deployment(resources = {
        "org/flowable/rest/service/api/runtime/ExecutionResourceTest.process-with-signal-event.bpmn20.xml" })
public void testSignalEventExecutionWithvariables() throws Exception {
    Execution signalExecution = runtimeService.startProcessInstanceByKey("processOne");
    assertNotNull(signalExecution);

    ArrayNode variables = objectMapper.createArrayNode();
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("action", "signalEventReceived");
    requestNode.put("signalName", "alert");
    requestNode.set("variables", variables);

    ObjectNode varNode = objectMapper.createObjectNode();
    variables.add(varNode);
    varNode.put("name", "myVar");
    varNode.put("value", "Variable set when signal event is received");

    Execution waitingExecution = runtimeService.createExecutionQuery().activityId("waitState").singleResult();
    assertNotNull(waitingExecution);

    // Sending signal event causes the execution to end (scope-execution for
    // the catching event)
    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
            + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION, waitingExecution.getId()));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
    closeResponse(response);

    // Check if process is moved on to the other wait-state
    waitingExecution = runtimeService.createExecutionQuery().activityId("anotherWaitState").singleResult();
    assertNotNull(waitingExecution);

    Map<String, Object> vars = runtimeService.getVariables(waitingExecution.getId());
    assertEquals(1, vars.size());

    assertEquals("Variable set when signal event is received", vars.get("myVar"));
}

From source file:org.flowable.rest.service.api.runtime.ExecutionResourceTest.java

/**
 * Test messaging a single execution with variables.
 *//*from ww  w . ja v a 2 s.co m*/
@Deployment(resources = {
        "org/flowable/rest/service/api/runtime/ExecutionResourceTest.process-with-message-event.bpmn20.xml" })
public void testMessageEventExecutionWithvariables() throws Exception {
    Execution signalExecution = runtimeService.startProcessInstanceByKey("processOne");
    assertNotNull(signalExecution);

    ArrayNode variables = objectMapper.createArrayNode();
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("action", "messageEventReceived");
    requestNode.put("messageName", "paymentMessage");
    requestNode.set("variables", variables);

    ObjectNode varNode = objectMapper.createObjectNode();
    variables.add(varNode);
    varNode.put("name", "myVar");
    varNode.put("value", "Variable set when signal event is received");

    Execution waitingExecution = runtimeService.createExecutionQuery().activityId("waitState").singleResult();
    assertNotNull(waitingExecution);

    // Sending signal event causes the execution to end (scope-execution for
    // the catching event)
    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
            + RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION, waitingExecution.getId()));
    httpPut.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
    closeResponse(response);

    // Check if process is moved on to the other wait-state
    waitingExecution = runtimeService.createExecutionQuery().activityId("anotherWaitState").singleResult();
    assertNotNull(waitingExecution);

    Map<String, Object> vars = runtimeService.getVariables(waitingExecution.getId());
    assertEquals(1, vars.size());

    assertEquals("Variable set when signal event is received", vars.get("myVar"));
}

From source file:fi.vm.sade.organisaatio.business.impl.OrganisaatioKoodistoClient.java

/**
 * Pivitt koodin koodistoon/*from  w w w .  ja  v a 2s  . c o  m*/
 *
 * @param json Pivitettv koodi json-muodossa
 * @throws NotAuthorizedException Autorisointi eponnistui
 * @throws OrganisaatioKoodistoException Koodistopalvelupyynt eponnistui
 */
public void put(String json) throws OrganisaatioKoodistoException {
    HttpContext localContext = new BasicHttpContext();
    String uri = "/rest/codeelement/save";
    LOG.debug("PUT " + koodistoServiceUrl + uri);
    LOG.debug("PUT data=" + json);
    authorize();
    HttpClient client = new DefaultHttpClient();
    HttpPut put = new HttpPut(koodistoServiceUrl + uri);
    put.addHeader("CasSecurityTicket", ticket);
    put.addHeader("Content-Type", "application/json; charset=UTF-8");
    try {
        LOG.debug("NOW json   =" + json);
        put.setEntity(new StringEntity(json, "UTF-8"));
        HttpResponse resp = client.execute(put, localContext);
        if (resp.getStatusLine().getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
            String err = "Invalid status code " + resp.getStatusLine().getStatusCode() + " from PUT "
                    + koodistoServiceUrl + uri;
            LOG.error(err);
            LOG.debug("Response body: " + EntityUtils.toString(resp.getEntity()));
            throw new OrganisaatioKoodistoException(err);
        } else {
            LOG.info("Code " + koodistoServiceUrl + uri + " succesfully updated: return code "
                    + resp.getStatusLine().getStatusCode());
        }
    } catch (IOException e) {
        String err = "Failed to PUT " + koodistoServiceUrl + uri + ": " + e.getMessage();
        LOG.error(err);
        throw new OrganisaatioKoodistoException(err);
    }
}

From source file:org.oss.bonita.utils.bonita.RestClient.java

protected HttpResponse executePutRequest(String apiURI, String payloadAsString) {
    try {//from w  w  w.  j a  v  a2 s  .co m
        HttpPut putRequest = new HttpPut(bonitaURI + apiURI);
        putRequest.addHeader("Content-Type", "application/json");

        StringEntity input = new StringEntity(payloadAsString);
        input.setContentType("application/json");
        putRequest.setEntity(input);

        return httpClient.execute(putRequest, httpContext);

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}