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:org.apache.olingo.fit.utils.JSONUtilities.java

@Override
public InputStream addOperation(final InputStream content, final String name, final String metaAnchor,
        final String href) throws IOException {

    final ObjectNode srcNode = (ObjectNode) mapper.readTree(content);
    IOUtils.closeQuietly(content);/*www .j a va  2 s .  c  o  m*/

    final ObjectNode action = mapper.createObjectNode();
    action.set("title", new TextNode(name));
    action.set("target", new TextNode(href));

    srcNode.set(metaAnchor, action);
    return IOUtils.toInputStream(srcNode.toString(), Constants.ENCODING);
}

From source file:org.activiti.rest.content.service.api.BaseSpringContentRestTestCase.java

protected void assertResultsPresentInPostDataResponseWithStatusCheck(String url, ObjectNode body,
        int expectedStatusCode, String... expectedResourceIds) throws JsonProcessingException, IOException {
    int numberOfResultsExpected = 0;
    if (expectedResourceIds != null) {
        numberOfResultsExpected = expectedResourceIds.length;
    }//from   w ww .  ja v  a  2  s  . c  o m

    // Do the actual call
    HttpPost post = new HttpPost(SERVER_URL_PREFIX + url);
    post.setEntity(new StringEntity(body.toString()));
    CloseableHttpResponse response = executeRequest(post, expectedStatusCode);

    if (expectedStatusCode == HttpStatus.SC_OK) {
        // Check status and size
        JsonNode rootNode = objectMapper.readTree(response.getEntity().getContent());
        JsonNode dataNode = rootNode.get("data");
        assertEquals(numberOfResultsExpected, dataNode.size());

        // Check presence of ID's
        if (expectedResourceIds != null) {
            List<String> toBeFound = new ArrayList<String>(Arrays.asList(expectedResourceIds));
            Iterator<JsonNode> it = dataNode.iterator();
            while (it.hasNext()) {
                String id = it.next().get("id").textValue();
                toBeFound.remove(id);
            }
            assertTrue(
                    "Not all entries have been found in result, missing: " + StringUtils.join(toBeFound, ", "),
                    toBeFound.isEmpty());
        }
    }

    closeResponse(response);
}

From source file:org.apache.olingo.fit.utils.JSONUtilities.java

@Override
protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is,
        final Set<String> links) throws IOException {

    final ObjectNode srcNode = (ObjectNode) mapper.readTree(is);
    IOUtils.closeQuietly(is);//from   w  ww . j av a2  s .  c om

    for (String link : links) {
        srcNode.set(link + Constants.get(ConstantKey.JSON_NAVIGATION_SUFFIX),
                new TextNode(Commons.getLinksURI(entitySetName, entitykey, link)));
    }

    return IOUtils.toInputStream(srcNode.toString(), Constants.ENCODING);
}

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

public void testCreateUserInfo() throws Exception {
    User savedUser = null;/*from   w w w . j  a va  2s . c o  m*/
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;

        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("value", "Value 1");
        requestNode.put("key", "key1");

        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX
                + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO_COLLECTION, "testuser"));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_CREATED);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertEquals("key1", responseNode.get("key").textValue());
        assertEquals("Value 1", responseNode.get("value").textValue());

        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, newUser.getId(), "key1")));

    } finally {

        // Delete user after test passes or fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}

From source file:edu.nwpu.gemfire.monitor.controllers.PulseController.java

@RequestMapping(value = "/acknowledgeAlert", method = RequestMethod.GET)
public void acknowledgeAlert(HttpServletRequest request, HttpServletResponse response) throws IOException {
    int alertId;/*from w  w  w.  jav  a2 s .  co m*/
    ObjectNode responseJSON = mapper.createObjectNode();

    try {
        alertId = Integer.valueOf(request.getParameter("alertId"));
    } catch (NumberFormatException e) {
        // Empty json response
        response.getOutputStream().write(responseJSON.toString().getBytes());
        if (LOGGER.finerEnabled()) {
            LOGGER.finer(e.getMessage());
        }
        return;
    }

    try {
        // get cluster object
        Cluster cluster = Repository.get().getCluster();

        // set alert is acknowledged
        cluster.acknowledgeAlert(alertId);
        responseJSON.put("status", "deleted");
    } catch (Exception e) {
        if (LOGGER.fineEnabled()) {
            LOGGER.fine("Exception Occured : " + e.getMessage());
        }
    }

    // Send json response
    response.getOutputStream().write(responseJSON.toString().getBytes());
}

From source file:org.jetbrains.webdemo.sessions.MyHttpSession.java

private void sendFileExistenceResult() {
    try {// w  ww  .j  av  a2 s .c om
        ObjectNode response = new ObjectNode(JsonNodeFactory.instance);
        if (MySqlConnector.getInstance().getFile(request.getParameter("publicId")) != null) {
            response.put("exists", true);
        } else {
            response.put("exists", false);
        }
        writeResponse(response.toString(), HttpServletResponse.SC_OK);
    } catch (NullPointerException e) {
        writeResponse("Can't get parameters", HttpServletResponse.SC_BAD_REQUEST);
    } catch (DatabaseOperationException e) {
        writeResponse(e.getMessage(), HttpServletResponse.SC_BAD_REQUEST);
    }
}

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

/**
 * Test updating a single user./*from   w w  w  . j  a  v a2  s  . co m*/
 */
public void testUpdateUser() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;

        ObjectNode taskUpdateRequest = objectMapper.createObjectNode();
        taskUpdateRequest.put("firstName", "Tijs");
        taskUpdateRequest.put("lastName", "Barrez");
        taskUpdateRequest.put("email", "no-reply@alfresco.org");
        taskUpdateRequest.put("password", "updatedpassword");

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()));
        httpPut.setEntity(new StringEntity(taskUpdateRequest.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testuser", responseNode.get("id").textValue());
        assertEquals("Tijs", responseNode.get("firstName").textValue());
        assertEquals("Barrez", responseNode.get("lastName").textValue());
        assertEquals("no-reply@alfresco.org", responseNode.get("email").textValue());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())));

        // Check user is updated in activiti
        newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult();
        assertEquals("Barrez", newUser.getLastName());
        assertEquals("Tijs", newUser.getFirstName());
        assertEquals("no-reply@alfresco.org", newUser.getEmail());
        assertEquals("updatedpassword", newUser.getPassword());

    } finally {

        // Delete user after test fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}

From source file:com.mirth.connect.client.ui.components.rsta.RSTAPreferences.java

String getKeyStrokesJSON() {
    ObjectNode keyStrokesNode = JsonNodeFactory.instance.objectNode();

    for (Entry<String, KeyStroke> entry : getKeyStrokeMap().entrySet()) {
        if (entry.getValue() != null) {
            ArrayNode arrayNode = keyStrokesNode.putArray(entry.getKey());
            arrayNode.add(entry.getValue().getKeyCode());
            arrayNode.add(entry.getValue().getModifiers());
        } else {/*w ww .j av  a 2s .  c  o m*/
            keyStrokesNode.putNull(entry.getKey());
        }
    }

    return keyStrokesNode.toString();
}

From source file:edu.nwpu.gemfire.monitor.controllers.PulseController.java

@RequestMapping(value = "/clearAlerts", method = RequestMethod.GET)
public void clearAlerts(HttpServletRequest request, HttpServletResponse response) throws IOException {
    int alertType;
    ObjectNode responseJSON = mapper.createObjectNode();

    try {/*ww  w  . ja  v  a2 s . com*/
        alertType = Integer.valueOf(request.getParameter("alertType"));
    } catch (NumberFormatException e) {
        // Empty json response
        response.getOutputStream().write(responseJSON.toString().getBytes());
        if (LOGGER.finerEnabled()) {
            LOGGER.finer(e.getMessage());
        }
        return;
    }

    try {
        boolean isClearAll = Boolean.valueOf(request.getParameter("clearAll"));
        // get cluster object
        Cluster cluster = Repository.get().getCluster();
        cluster.clearAlerts(alertType, isClearAll);
        responseJSON.put("status", "deleted");
        responseJSON.put("systemAlerts",
                SystemAlertsService.getAlertsJson(cluster, cluster.getNotificationPageNumber()));
        responseJSON.put("pageNumber", cluster.getNotificationPageNumber());

        boolean isGFConnected = cluster.isConnectedFlag();
        if (isGFConnected) {
            responseJSON.put("connectedFlag", isGFConnected);
        } else {
            responseJSON.put("connectedFlag", isGFConnected);
            responseJSON.put("connectedErrorMsg", cluster.getConnectionErrorMsg());
        }
    } catch (Exception e) {
        if (LOGGER.fineEnabled()) {
            LOGGER.fine("Exception Occurred : " + e.getMessage());
        }
    }

    // Send json response
    response.getOutputStream().write(responseJSON.toString().getBytes());
}