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.activiti.rest.service.demo.DemoDataGenerator.java

protected void createModelData(String name, String description, String jsonFile) {
    List<Model> modelList = repositoryService.createModelQuery().modelName("Demo model").list();

    if (modelList == null || modelList.size() == 0) {

        Model model = repositoryService.newModel();
        model.setName(name);//from w w w  . j a  v a2 s. co m

        ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
        modelObjectNode.put(MODEL_NAME, name);
        modelObjectNode.put(MODEL_DESCRIPTION, description);
        model.setMetaInfo(modelObjectNode.toString());

        repositoryService.saveModel(model);

        try {
            InputStream svgStream = this.getClass().getClassLoader()
                    .getResourceAsStream("org/activiti/rest/demo/model/test.svg");
            repositoryService.addModelEditorSourceExtra(model.getId(), IOUtils.toByteArray(svgStream));
        } catch (Exception e) {
            LOGGER.warn("Failed to read SVG", e);
        }

        try {
            InputStream editorJsonStream = this.getClass().getClassLoader().getResourceAsStream(jsonFile);
            repositoryService.addModelEditorSource(model.getId(), IOUtils.toByteArray(editorJsonStream));
        } catch (Exception e) {
            LOGGER.warn("Failed to read editor JSON", e);
        }
    }
}

From source file:org.activiti.editor.ui.ImportUploadReceiver.java

protected void deployUploadedFile() {
    try {/*from www .  ja  v  a  2s . com*/
        try {
            if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
                validFile = true;
                BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
                XMLInputFactory xif = XMLInputFactory.newInstance();
                InputStreamReader in = new InputStreamReader(
                        new ByteArrayInputStream(outputStream.toByteArray()), "UTF-8");
                XMLStreamReader xtr = xif.createXMLStreamReader(in);
                BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
                xmlConverter.convertToBpmnModel(xtr);

                if (bpmnModel.getMainProcess() == null || bpmnModel.getMainProcess().getId() == null) {
                    notificationManager.showErrorNotification(Messages.MODEL_IMPORT_FAILED,
                            i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_BPMN_EXPLANATION));
                } else {

                    if (bpmnModel.getLocationMap().size() == 0) {
                        notificationManager.showErrorNotification(Messages.MODEL_IMPORT_INVALID_BPMNDI,
                                i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_BPMNDI_EXPLANATION));
                    } else {

                        String processName = null;
                        if (StringUtils.isNotEmpty(bpmnModel.getMainProcess().getName())) {
                            processName = bpmnModel.getMainProcess().getName();
                        } else {
                            processName = bpmnModel.getMainProcess().getId();
                        }

                        modelData = repositoryService.newModel();
                        ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
                        modelObjectNode.put(MODEL_NAME, processName);
                        modelObjectNode.put(MODEL_REVISION, 1);
                        modelData.setMetaInfo(modelObjectNode.toString());
                        modelData.setName(processName);

                        repositoryService.saveModel(modelData);

                        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
                        ObjectNode editorNode = jsonConverter.convertToJson(bpmnModel);

                        repositoryService.addModelEditorSource(modelData.getId(),
                                editorNode.toString().getBytes("utf-8"));
                    }
                }
            } else {
                notificationManager.showErrorNotification(Messages.MODEL_IMPORT_INVALID_FILE,
                        i18nManager.getMessage(Messages.MODEL_IMPORT_INVALID_FILE_EXPLANATION));
            }
        } catch (Exception e) {
            String errorMsg = e.getMessage().replace(System.getProperty("line.separator"), "<br/>");
            notificationManager.showErrorNotification(Messages.MODEL_IMPORT_FAILED, errorMsg);
        }
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                notificationManager.showErrorNotification("Server-side error", e.getMessage());
            }
        }
    }
}

From source file:com.unboundid.scim2.extension.messages.externalidentity.ProviderTest.java

/**
 * Tests serialization of Provider objects.
 *
 * @throws Exception if an error occurs.
 *///w ww  .j  a va  2 s  .c  o  m
@Test
public void testSerialization() throws Exception {
    String name = "testName";
    String description = "testDescription";
    String iconUrl = "https://localhost:12345/test/url";
    String type = "testType";
    String samlResponseBinding = "artifact";

    ObjectNode objectNode = JsonUtils.getJsonNodeFactory().objectNode();
    objectNode.put("name", name);
    objectNode.put("description", description);
    objectNode.put("iconUrl", iconUrl);
    objectNode.put("type", type);
    objectNode.put("samlResponseBinding", samlResponseBinding);

    Provider provider1 = JsonUtils.getObjectReader().forType(Provider.class).readValue(objectNode.toString());
    Assert.assertEquals(provider1.getName(), name);
    Assert.assertEquals(provider1.getDescription(), description);
    Assert.assertEquals(provider1.getType(), type);
    Assert.assertEquals(provider1.getIconUrl(), iconUrl);
    Assert.assertEquals(provider1.getSamlResponseBinding(), samlResponseBinding);

    Provider provider2 = JsonUtils.getObjectReader().forType(Provider.class)
            .readValue(JsonUtils.getObjectWriter().writeValueAsString(provider1));
    Assert.assertEquals(provider1, provider2);
}

From source file:com.unboundid.scim2.extension.messages.consent.OAuth2ClientTest.java

/**
 * Tests serialization of Client objects.
 *
 * @throws Exception if an error occurs.
 */// w w w.  j a  va2s.  c  o  m
@Test
public void testSerialization() throws Exception {
    String name = "testName";
    String description = "testDescription";
    String url = "https://localhost:12345/test/app/url";
    String iconUrl = "https://localhost:12345/test/icon/url";
    String emailAddress = "unit@test.com";

    ObjectNode objectNode = JsonUtils.getJsonNodeFactory().objectNode();
    objectNode.put("name", name);
    objectNode.put("description", description);
    objectNode.put("iconUrl", iconUrl);
    objectNode.put("url", url);
    objectNode.put("emailAddress", emailAddress);

    OAuth2Client app1 = JsonUtils.getObjectReader().forType(OAuth2Client.class)
            .readValue(objectNode.toString());
    Assert.assertEquals(app1.getName(), name);
    Assert.assertEquals(app1.getDescription(), description);
    Assert.assertEquals(app1.getIconUrl(), iconUrl);
    Assert.assertEquals(app1.getEmailAddress(), emailAddress);

    OAuth2Client app2 = JsonUtils.getObjectReader().forType(OAuth2Client.class)
            .readValue(JsonUtils.getObjectWriter().writeValueAsString(app1));
    Assert.assertEquals(app1, app2);
}

From source file:org.activiti.rest.service.api.history.HistoricVariableInstanceQueryResourceTest.java

protected void assertResultsPresentInDataResponse(String url, ObjectNode body, int numberOfResultsExpected,
        String variableName, Object variableValue) throws JsonProcessingException, IOException {

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

    // Check status and size
    JsonNode dataNode = objectMapper.readTree(response.getEntity().getContent()).get("data");
    closeResponse(response);//from   ww  w .  ja  v a2  s  . co m
    assertEquals(numberOfResultsExpected, dataNode.size());

    // Check presence of ID's
    if (variableName != null) {
        boolean variableFound = false;
        Iterator<JsonNode> it = dataNode.iterator();
        while (it.hasNext()) {
            JsonNode dataElementNode = it.next();
            JsonNode variableNode = dataElementNode.get("variable");
            String name = variableNode.get("name").textValue();
            if (variableName.equals(name)) {
                variableFound = true;
                if (variableValue instanceof Boolean) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asBoolean() == (Boolean) variableValue);
                } else if (variableValue instanceof Integer) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asInt() == (Integer) variableValue);
                } else {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asText().equals((String) variableValue));
                }
            }
        }
        assertTrue("Variable " + variableName + " is missing", variableFound);
    }
}

From source file:org.flowable.rest.service.api.history.HistoricVariableInstanceQueryResourceTest.java

protected void assertResultsPresentInDataResponse(String url, ObjectNode body, int numberOfResultsExpected,
        String variableName, Object variableValue) throws JsonProcessingException, IOException {

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

    // Check status and size
    JsonNode dataNode = objectMapper.readTree(response.getEntity().getContent()).get("data");
    closeResponse(response);/*w ww  . j  a va 2 s.  co m*/
    assertEquals(numberOfResultsExpected, dataNode.size());

    // Check presence of ID's
    if (variableName != null) {
        boolean variableFound = false;
        Iterator<JsonNode> it = dataNode.iterator();
        while (it.hasNext()) {
            JsonNode dataElementNode = it.next();
            JsonNode variableNode = dataElementNode.get("variable");
            String name = variableNode.get("name").textValue();
            if (variableName.equals(name)) {
                variableFound = true;
                if (variableValue instanceof Boolean) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asBoolean() == (Boolean) variableValue);
                } else if (variableValue instanceof Integer) {
                    assertEquals("Variable value is not equal", variableNode.get("value").asInt(),
                            (int) (Integer) variableValue);
                } else {
                    assertEquals("Variable value is not equal", variableNode.get("value").asText(),
                            (String) variableValue);
                }
            }
        }
        assertTrue("Variable " + variableName + " is missing", variableFound);
    }
}

From source file:com.glaf.report.web.rest.MxReportTaskResource.java

@GET
@POST/*w ww.  j  ava2s. c om*/
@Path("/view")
@ResponseBody
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
public byte[] view(@Context HttpServletRequest request, @Context UriInfo uriInfo) {
    String reportTaskId = request.getParameter("reportTaskId");
    ReportTask reportTask = null;
    if (StringUtils.isNotEmpty(reportTaskId)) {
        reportTask = reportTaskService.getReportTask(reportTaskId);
    }
    ObjectNode responseJSON = new ObjectMapper().createObjectNode();
    if (reportTask != null) {
        responseJSON = reportTask.toObjectNode();
    }
    try {
        return responseJSON.toString().getBytes("UTF-8");
    } catch (IOException e) {
        return responseJSON.toString().getBytes();
    }
}

From source file:com.redhat.lightblue.query.BasicProjection.java

/**
 * Parses a field or array projection from the given json object
 *//*from  w  w  w .  j  a  va 2 s.  c om*/
public static BasicProjection fromJson(ObjectNode node) {
    String field = node.get("field").asText();
    if (field == null) {
        throw Error.get(QueryConstants.ERR_INVALID_PROJECTION, "field");
    }

    Path path = getNonRelativePath(new Path(field));

    // Processing of optional elements. We decide on the type of
    // the final object based on what fields this object has
    JsonNode x = node.get("include");
    boolean include;
    if (x == null) {
        include = true;
    } else {
        include = x.asBoolean();
    }

    Projection projection;
    x = node.get("project");
    if (x != null) {
        projection = Projection.fromJson(x);
    } else {
        projection = null;
    }

    x = node.get("sort");
    Sort sort;
    if (x != null) {
        sort = Sort.fromJson(x);
    } else {
        sort = null;
    }

    x = node.get("range");
    if (x != null) {
        if (x instanceof ArrayNode && ((ArrayNode) x).size() == 2) {
            int from = ((ArrayNode) x).get(0).asInt();
            int to = ((ArrayNode) x).get(1).asInt();
            return new ArrayRangeProjection(path, include, projection, sort, from, to);
        } else {
            throw Error.get(QueryConstants.ERR_INVALID_ARRAY_RANGE_PROJECTION, node.toString());
        }
    }
    x = node.get("match");
    if (x != null) {
        return new ArrayQueryMatchProjection(path, include, projection, sort, QueryExpression.fromJson(x));
    }
    x = node.get("recursive");
    return new FieldProjection(path, include, x == null ? false : x.asBoolean());
}

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

public void testCreateGroupExceptions() throws Exception {
    // Create without ID
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("name", "Test group");
    requestNode.put("type", "Test type");

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

    // Create when group already exists
    requestNode = objectMapper.createObjectNode();
    requestNode.put("id", "admin");

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

From source file:easyrpc.server.serialization.jsonrpc.JSONCallee.java

byte[] returnJsonRpcError(Object id, Exception e) {
    ObjectNode object = JsonNodeFactory.instance.objectNode();
    object.put("jsonrpc", "2.0");
    object.put("id", id.toString());
    ObjectNode error = object.putObject("error");
    error.put("code", -1);
    error.put("message", e.getClass().getCanonicalName() + " : " + e.getMessage());
    object.put("error", error);

    return object.toString().getBytes();
}