Example usage for com.fasterxml.jackson.databind.node ObjectNode toString

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode toString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ObjectNode toString.

Prototype

public String toString() 

Source Link

Usage

From source file:easyrpc.client.serialization.jsonrpc.JSONCaller.java

@Override
public byte[] serializeCall(Object theProxy, Method thisMethod, Object[] args) throws Throwable {
    ObjectNode sc = JsonNodeFactory.instance.objectNode();
    sc.put("jsonrpc", "2.0");
    sc.put("method", thisMethod.getName());

    if (args != null && args.length > 0) {
        ArrayNode params = JsonNodeFactory.instance.arrayNode();
        for (Object arg : args) {
            addType(arg, params);/*from www . j  a  va2  s . c  o  m*/
        }
        sc.set("params", params);
    }
    sc.put("id", UUID.randomUUID().toString());

    String json = sc.toString();
    return json.getBytes();
}

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

/**
 * Test querying process instance based on variables. POST query/process-instances
 *///from w w  w  .j a  v  a 2  s  .  c o m
@Deployment
public void testQueryProcessInstancesPagingAndSorting() throws Exception {
    ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("aOneTaskProcess");
    ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("bOneTaskProcess");
    ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey("cOneTaskProcess");

    // Create request node
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("order", "desc");
    requestNode.put("sort", "processDefinitionKey");

    String url = RestUrls.createRelativeResourceUrl(RestUrls.URL_PROCESS_INSTANCE_QUERY);
    HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + url);
    httpPost.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_OK);

    // Check order
    JsonNode rootNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);
    JsonNode dataNode = rootNode.get("data");
    assertEquals(3, dataNode.size());

    assertEquals(processInstance3.getId(), dataNode.get(0).get("id").asText());
    assertEquals(processInstance2.getId(), dataNode.get(1).get("id").asText());
    assertEquals(processInstance1.getId(), dataNode.get(2).get("id").asText());

    // Check paging size
    requestNode = objectMapper.createObjectNode();
    requestNode.put("start", 0);
    requestNode.put("size", 1);

    httpPost.setEntity(new StringEntity(requestNode.toString()));
    response = executeRequest(httpPost, HttpStatus.SC_OK);
    rootNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);
    dataNode = rootNode.get("data");
    assertEquals(1, dataNode.size());

    // Check paging start and size
    requestNode = objectMapper.createObjectNode();
    requestNode.put("start", 1);
    requestNode.put("size", 1);
    requestNode.put("order", "desc");
    requestNode.put("sort", "processDefinitionKey");

    httpPost.setEntity(new StringEntity(requestNode.toString()));
    response = executeRequest(httpPost, HttpStatus.SC_OK);
    rootNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);
    dataNode = rootNode.get("data");
    assertEquals(1, dataNode.size());
    assertEquals(processInstance2.getId(), dataNode.get(0).get("id").asText());
}

From source file:org.jetbrains.webdemo.handlers.ServerHandler.java

private void sendSessionInfo(HttpServletRequest request, HttpServletResponse response,
        SessionInfo sessionInfo) {/*from  www  .j a v  a  2s . c o  m*/
    try {
        String id = sessionInfo.getId();
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        responseBody.put("id", id);
        responseBody.put("isLoggedIn", sessionInfo.getUserInfo().isLogin());
        writeResponse(request, response, responseBody.toString(), HttpServletResponse.SC_OK);
    } catch (Throwable e) {
        ErrorWriter.ERROR_WRITER.writeExceptionToExceptionAnalyzer(e, "UNKNOWN", sessionInfo.getOriginUrl(),
                request.getRequestURI() + "?" + request.getQueryString());
    }
}

From source file:org.activiti.rest.service.api.identity.GroupMembershipResourceTest.java

public void testCreatemembership() throws Exception {
    try {/*from  w w  w  .  j  ava2 s . c  o  m*/
        Group testGroup = identityService.newGroup("testgroup");
        testGroup.setName("Test group");
        testGroup.setType("Test type");
        identityService.saveGroup(testGroup);

        User testUser = identityService.newUser("testuser");
        identityService.saveUser(testUser);

        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("userId", "testuser");

        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX
                + RestUrls.createRelativeResourceUrl(RestUrls.URL_GROUP_MEMBERSHIP_COLLECTION, "testgroup"));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_CREATED);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testuser", responseNode.get("userId").textValue());
        assertEquals("testgroup", responseNode.get("groupId").textValue());
        assertTrue(responseNode.get("url").textValue().endsWith(RestUrls.createRelativeResourceUrl(
                RestUrls.URL_GROUP_MEMBERSHIP, testGroup.getId(), testUser.getId())));

        Group createdGroup = identityService.createGroupQuery().groupId("testgroup").singleResult();
        assertNotNull(createdGroup);
        assertEquals("Test group", createdGroup.getName());
        assertEquals("Test type", createdGroup.getType());

        assertNotNull(identityService.createUserQuery().memberOfGroup("testgroup").singleResult());
        assertEquals("testuser",
                identityService.createUserQuery().memberOfGroup("testgroup").singleResult().getId());
    } finally {
        try {
            identityService.deleteGroup("testgroup");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }

        try {
            identityService.deleteUser("testuser");
        } catch (Throwable ignore) {
            // Ignore, since the group may not have been created in the test
            // or already deleted
        }
    }
}

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

@Deployment(resources = {
        "org/flowable/rest/service/api/runtime/SignalsResourceTest.process-signal-start.bpmn20.xml" })
public void testSignalEventReceivedSync() throws Exception {

    org.flowable.engine.repository.Deployment tenantDeployment = repositoryService.createDeployment()
            .addClasspathResource(/*w w w .j  a  v a2  s. co m*/
                    "org/flowable/rest/service/api/runtime/SignalsResourceTest.process-signal-start.bpmn20.xml")
            .tenantId("my tenant").deploy();

    try {

        // Signal without vars, without tenant
        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("signalName", "The Signal");

        HttpPost httpPost = new HttpPost(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_SIGNALS));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_NO_CONTENT));

        // Check if process is started as a result of the signal without tenant ID set
        assertEquals(1, runtimeService.createProcessInstanceQuery().processInstanceWithoutTenantId()
                .processDefinitionKey("processWithSignalStart1").count());

        // Signal with tenant
        requestNode.put("tenantId", "my tenant");
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_NO_CONTENT));

        // Check if process is started as a result of the signal, in the right tenant
        assertEquals(1, runtimeService.createProcessInstanceQuery().processInstanceTenantId("my tenant")
                .processDefinitionKey("processWithSignalStart1").count());

        // Signal with tenant AND variables
        ArrayNode vars = requestNode.putArray("variables");
        ObjectNode var = vars.addObject();
        var.put("name", "testVar");
        var.put("value", "test");

        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_NO_CONTENT));

        // Check if process is started as a result of the signal, in the right tenant and with var set
        assertEquals(1,
                runtimeService.createProcessInstanceQuery().processInstanceTenantId("my tenant")
                        .processDefinitionKey("processWithSignalStart1").variableValueEquals("testVar", "test")
                        .count());

        // Signal without tenant AND variables
        requestNode.remove("tenantId");

        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeRequest(httpPost, HttpStatus.SC_NO_CONTENT));

        // Check if process is started as a result of the signal, without tenant and with var set
        assertEquals(1,
                runtimeService.createProcessInstanceQuery().processInstanceWithoutTenantId()
                        .processDefinitionKey("processWithSignalStart1").variableValueEquals("testVar", "test")
                        .count());

    } finally {
        // Clean up tenant-specific deployment
        if (tenantDeployment != null) {
            repositoryService.deleteDeployment(tenantDeployment.getId(), true);
        }
    }
}

From source file:org.osiam.resources.helper.AttributesRemovalHelper.java

private <T extends Resource> SCIMSearchResult<T> getJsonResponseWithAdditionalFields(
        SCIMSearchResult<T> scimSearchResult, Map<String, Object> parameterMap) {

    ObjectMapper mapper = new ObjectMapper();

    String[] fieldsToReturn = (String[]) parameterMap.get("attributes");
    ObjectWriter writer = getObjectWriter(mapper, fieldsToReturn);

    try {/*from ww w  .  j  a  va 2 s. c  o m*/
        String resourcesString = writer.writeValueAsString(scimSearchResult.getResources());
        JsonNode resourcesNode = mapper.readTree(resourcesString);

        String schemasString = writer.writeValueAsString(scimSearchResult.getSchemas());
        JsonNode schemasNode = mapper.readTree(schemasString);

        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.put("totalResults", scimSearchResult.getTotalResults());
        rootNode.put("itemsPerPage", scimSearchResult.getItemsPerPage());
        rootNode.put("startIndex", scimSearchResult.getStartIndex());
        rootNode.put("schemas", schemasNode);
        rootNode.put("Resources", resourcesNode);

        return mapper.readValue(rootNode.toString(), SCIMSearchResult.class);
    } catch (IOException e) {
        LOGGER.warn("Unable to serialize search result", e);
        throw new OsiamBackendFailureException();
    }
}

From source file:org.osiam.resource_server.resources.helper.AttributesRemovalHelper.java

private <T extends Resource> SCIMSearchResult<T> getJsonResponseWithAdditionalFields(
        SCIMSearchResult<T> scimSearchResult, Map<String, Object> parameterMap) {

    ObjectMapper mapper = new ObjectMapper();

    String[] fieldsToReturn = (String[]) parameterMap.get("attributes");
    ObjectWriter writer = getObjectWriter(mapper, fieldsToReturn);

    try {/* www . jav  a  2 s . c  o  m*/
        String resourcesString = writer.writeValueAsString(scimSearchResult.getResources());
        JsonNode resourcesNode = mapper.readTree(resourcesString);

        String schemasString = writer.writeValueAsString(scimSearchResult.getSchemas());
        JsonNode schemasNode = mapper.readTree(schemasString);

        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.put("totalResults", scimSearchResult.getTotalResults());
        rootNode.put("itemsPerPage", scimSearchResult.getItemsPerPage());
        rootNode.put("startIndex", scimSearchResult.getStartIndex());
        rootNode.put("schemas", schemasNode);
        rootNode.put("Resources", resourcesNode);

        return mapper.readValue(rootNode.toString(), SCIMSearchResult.class);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.almende.demo.tuneswarm.ConductorAgent.java

/**
 * Start tune at agents.//  w  w w  .j  a v  a  2s .com
 *
 * @param id
 *            the id
 * @param delay
 *            the delay
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void startTuneAtAgents(@Name("tuneId") String id, @Name("delay") Integer delay) throws IOException {
    final Long startTime = getScheduler().now() + delay;
    for (final List<ToneAgent> list : agents.values()) {
        for (final ToneAgent agent : list) {
            if (agent.offline) {
                continue;
            }
            final ObjectNode params = JOM.createObjectNode();
            params.put("id", id);
            params.put("timestamp", startTime);
            LOG.warning("Sending:" + agent.address + " startTune:" + params.toString());
            caller.call(agent.address, "startTune", params);
        }
    }
}