Example usage for com.fasterxml.jackson.databind JsonNode toString

List of usage examples for com.fasterxml.jackson.databind JsonNode toString

Introduction

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

Prototype

public abstract String toString();

Source Link

Usage

From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_Buckets.java

/** Builds a V2 bucket out of a V1 source
 * @param src_json//from   w  w  w .ja  v  a  2s .c  o  m
 * @return
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 * @throws ParseException
 */
protected static DataBucketBean getBucketFromV1Source(final JsonNode src_json)
        throws JsonParseException, JsonMappingException, IOException, ParseException {
    // (think we'll use key instead of _id):
    //final String _id = safeJsonGet(JsonUtils._ID, src_json).asText(); 
    final String key = safeJsonGet("key", src_json).asText();
    final String created = safeJsonGet("created", src_json).asText();
    final String modified = safeJsonGet("modified", src_json).asText();
    final String title = safeJsonGet("title", src_json).asText();
    final String description = safeJsonGet("description", src_json).asText();
    final String owner_id = safeJsonGet("ownerId", src_json).asText();

    final JsonNode tags = safeJsonGet("tags", src_json); // collection of strings
    //final JsonNode comm_ids = safeJsonGet("communityIds", src_json); // collection of strings
    final JsonNode px_pipeline = safeJsonGet("processingPipeline", src_json); // collection of JSON objects, first one should have data_bucket
    final JsonNode px_pipeline_first_el = ((ObjectNode) px_pipeline.get(0))
            .without(Arrays.asList("test_params"));
    final JsonNode data_bucket_tmp = safeJsonGet("data_bucket", px_pipeline_first_el);// (WARNING: mutable, see below)
    final JsonNode scripting = safeJsonGet("scripting", data_bucket_tmp);

    // HANDLE SUBSTITUTION
    final String sub_prefix = Optional.ofNullable(scripting.get("sub_prefix")).map(x -> x.asText())
            .orElse("$$SCRIPT_");
    final String sub_suffix = Optional.ofNullable(scripting.get("sub_suffix")).map(x -> x.asText())
            .orElse("$$");
    final List<UnaryOperator<String>> search_replace = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(scripting.fieldNames(), Spliterator.ORDERED), false)
            .filter(f -> !f.equals("sub_prefix") && !f.equals("sub_suffix")) // (remove non language fields)
            .map(lang -> Tuples._2T(scripting.get(lang), lang))
            // Get (separator regex, entire script, sub prefix)
            .map(scriptobj_lang -> Tuples._3T(safeJsonGet("separator_regex", scriptobj_lang._1()).asText(),
                    safeJsonGet("script", scriptobj_lang._1()).asText(), sub_prefix + scriptobj_lang._2()))
            // Split each "entire script" up into blocks of format (bloc, lang)
            .<Stream<Tuple2<String, String>>>map(regex_script_lang -> Stream.concat(
                    Stream.of(Tuples._2T(regex_script_lang._2(), regex_script_lang._3())),
                    regex_script_lang._1().isEmpty()
                            ? Stream.of(Tuples._2T(regex_script_lang._2(), regex_script_lang._3()))
                            : Arrays.stream(regex_script_lang._2().split(regex_script_lang._1()))
                                    .<Tuple2<String, String>>map(s -> Tuples._2T(s, regex_script_lang._3()))))
            // Associate a per-lang index with each  script block -> (replacement, string_sub)
            .<Tuple2<String, String>>flatMap(stream -> StreamUtils.zip(stream, Stream.iterate(0, i -> i + 1),
                    (script_lang, i) -> Tuples._2T(
                            script_lang._1().replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r"),
                            i == 0 ? script_lang._2() + sub_suffix // (entire thing)
                                    : script_lang._2() + "_" + i + sub_suffix))) //(broken down components)

            .<UnaryOperator<String>>map(t2 -> (String s) -> s.replace(t2._2(), t2._1()))
            //(need to escape "s and newlines)
            .collect(Collectors.toList());

    // Apply the list of transforms to the string
    ((ObjectNode) data_bucket_tmp).remove("scripting"); // (WARNING: mutable)
    final String data_bucket_str = search_replace.stream().reduce(data_bucket_tmp.toString(),
            (acc, s) -> s.apply(acc), (acc1, acc2) -> acc1);

    // Convert back to the bucket JSON
    final JsonNode data_bucket = ((ObjectNode) _mapper.readTree(data_bucket_str))
            .without(Arrays.asList("test_params"));

    final DataBucketBean bucket = BeanTemplateUtils.build(data_bucket, DataBucketBean.class)
            .with(DataBucketBean::_id, getBucketIdFromV1SourceKey(key))
            .with(DataBucketBean::created, parseJavaDate(created))
            .with(DataBucketBean::modified, parseJavaDate(modified)).with(DataBucketBean::display_name, title)
            .with(DataBucketBean::description, description).with(DataBucketBean::owner_id, owner_id)
            .with(DataBucketBean::tags, StreamSupport.stream(tags.spliterator(), false).map(jt -> jt.asText())
                    .collect(Collectors.toSet()))
            .done().get();

    return bucket;

}

From source file:com.appdynamics.analytics.processor.event.ElasticSearchEventService.java

private String[] arrayOfStringsFromNode(ArrayNode sourceNode) {
    /* 1595 */ String[] ary = new String[sourceNode.size()];
    /* 1596 */ Iterator<JsonNode> elements = sourceNode.elements();
    /* 1597 */ int i = 0;
    /* 1598 */ while (elements.hasNext()) {
        /* 1599 */ JsonNode elem = (JsonNode) elements.next();
        /* 1600 */ ary[(i++)] = (elem.isTextual() ? elem.asText() : elem.toString());
        /*      */ }
    /* 1602 */ return ary;
    /*      */ }/*from  w  w w. j  av  a 2  s. co m*/

From source file:com.baasbox.controllers.Admin.java

public static Result updateUser(String username) {
    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method Start");
    Http.RequestBody body = request().body();

    JsonNode bodyJson = body.asJson();
    if (BaasBoxLogger.isDebugEnabled())
        BaasBoxLogger.debug("signUp bodyJson: " + bodyJson);

    //extract fields
    String missingField = null;//  w ww .  ja  v  a  2 s . c  o  m
    JsonNode nonAppUserAttributes;
    JsonNode privateAttributes;
    JsonNode friendsAttributes;
    JsonNode appUsersAttributes;
    try {
        missingField = UserDao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER;
        nonAppUserAttributes = bodyJson.get(UserDao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER);
        if (nonAppUserAttributes == null) {
            throw new IllegalArgumentException(missingField);
        }
        missingField = UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER;
        privateAttributes = bodyJson.get(UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER);
        if (privateAttributes == null) {
            throw new IllegalArgumentException(missingField);
        }
        missingField = UserDao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER;
        friendsAttributes = bodyJson.get(UserDao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER);
        if (friendsAttributes == null) {
            throw new IllegalArgumentException(missingField);
        }
        missingField = UserDao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER;
        appUsersAttributes = bodyJson.get(UserDao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER);
        if (appUsersAttributes == null) {
            throw new IllegalArgumentException(missingField);
        }

    } catch (Exception npe) {
        return badRequest("The '" + missingField + "' field is missing");
    }
    String role = null;
    if (bodyJson.has("role"))
        role = (String) bodyJson.findValuesAsText("role").get(0);

    if (privateAttributes.has("email")) {
        //check if email address is valid
        if (!Util.validateEmail((String) (String) privateAttributes.findValuesAsText("email").get(0)))
            return badRequest("The email address must be valid.");
    }

    ODocument user = null;
    //try to update new user
    try {
        user = UserService.updateProfile(username, role, nonAppUserAttributes, privateAttributes,
                friendsAttributes, appUsersAttributes);
    } catch (InvalidParameterException e) {
        return badRequest(ExceptionUtils.getMessage(e));
    } catch (InvalidJsonException e) {
        return badRequest("Body is not a valid JSON: " + ExceptionUtils.getMessage(e) + "\nyou sent:\n"
                + bodyJson.toString() + "\nHint: check the fields "
                + UserDao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER + ", "
                + UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER + ", "
                + UserDao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER + ", "
                + UserDao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER + " they must be an object, not a value.");
    } catch (AdminCannotChangeRoleException e) {
        return badRequest("User 'admin' cannot change role");
    } catch (Throwable e) {
        BaasBoxLogger.warn("signUp", e);
        if (Play.isDev())
            return internalServerError(ExceptionUtils.getFullStackTrace(e));
        else
            return internalServerError(ExceptionUtils.getMessage(e));
    }
    if (BaasBoxLogger.isTraceEnabled())
        BaasBoxLogger.trace("Method End");
    return ok(user.toJSON(Formats.USER.toString()));
}

From source file:org.dd4t.databind.serializers.json.ComponentPresentationDeserializer.java

private void renderComponentData(final ComponentPresentation componentPresentation,
        final JsonNode rawComponentData, final String viewModelName, final String rootElementName)
        throws IOException, SerializationException {
    if (StringUtils.isEmpty(viewModelName) || DataBindFactory.renderGenericComponentsOnly()) {
        LOG.debug("No view name set on Component Template or only rendering to Generic Component");
        try {//  ww  w.  j a v  a  2s .  c o  m
            componentPresentation.setComponent(
                    DataBindFactory.buildComponent(rawComponentData, this.concreteComponentClass));
        } catch (SerializationException e) {
            throw new IOException(e.getLocalizedMessage(), e);
        }
    } else {

        final Set<String> modelNames = new HashSet<>();
        modelNames.add(viewModelName);
        if (!rootElementName.equals(viewModelName)) {
            modelNames.add(rootElementName);
        }

        final Map<String, BaseViewModel> models = DataBindFactory.buildModels(rawComponentData, modelNames,
                componentPresentation.getComponentTemplate().getId());

        if (models == null || models.isEmpty()) {
            if (DataBindFactory.renderDefaultComponentsIfNoModelFound()) {
                componentPresentation.setComponent(
                        DataBindFactory.buildComponent(rawComponentData, this.concreteComponentClass));
            } else {
                LOG.warn(
                        "No model found for CT {}, with component: {}. Fall back deserialization is also turned off.",
                        componentPresentation.getComponentTemplate().getId(),
                        componentPresentation.getComponent().getId());
            }

        } else {
            for (BaseViewModel model : models.values()) {
                if (model instanceof TridionViewModel
                        && ((TridionViewModel) model).setGenericComponentOnComponentPresentation()) {
                    LOG.debug("Also setting a Component object on the CP.");
                    componentPresentation.setComponent(
                            DataBindFactory.buildComponent(rawComponentData, this.concreteComponentClass));
                }
                if (model.setRawDataOnModel()) {
                    LOG.debug("Setting raw string data on model.");
                    model.setRawData(rawComponentData.toString());
                }
            }
            componentPresentation.setViewModel(models);
        }
    }
}

From source file:dao.AdvSearchDAO.java

public static ObjectNode elasticSearchFlowJobs(JsonNode searchOpt, int page, int size) {
    ObjectNode resultNode = Json.newObject();
    Long count = 0L;/*  ww  w.java2 s  . c o m*/
    List<FlowJob> pagedFlows = new ArrayList<>();
    ObjectNode queryNode = Json.newObject();
    queryNode.put("from", (page - 1) * size);
    queryNode.put("size", size);

    JsonNode searchNode = utils.Search.generateFlowJobAdvSearchQueryString(searchOpt);

    if (searchNode != null && searchNode.isContainerNode()) {
        queryNode.set("query", searchNode);
    }

    Promise<WSResponse> responsePromise = WS
            .url(Play.application().configuration().getString(SearchDAO.ELASTICSEARCH_FLOW_URL_KEY))
            .post(queryNode);
    JsonNode responseNode = responsePromise.get(1000).asJson();

    resultNode.put("page", page);
    resultNode.put("category", "Flows");
    resultNode.put("isFlowJob", true);
    resultNode.put("itemsPerPage", size);

    if (responseNode != null && responseNode.isContainerNode() && responseNode.has("hits")) {
        JsonNode hitsNode = responseNode.get("hits");
        if (hitsNode != null) {
            if (hitsNode.has("total")) {
                count = hitsNode.get("total").asLong();
            }
            if (hitsNode.has("hits")) {
                JsonNode dataNode = hitsNode.get("hits");
                if (dataNode != null && dataNode.isArray()) {
                    Iterator<JsonNode> arrayIterator = dataNode.elements();
                    if (arrayIterator != null) {
                        while (arrayIterator.hasNext()) {
                            JsonNode node = arrayIterator.next();
                            if (node.isContainerNode() && node.has("_id")) {
                                FlowJob flowJob = new FlowJob();
                                if (node.has("_source")) {
                                    JsonNode sourceNode = node.get("_source");
                                    if (sourceNode != null) {
                                        if (sourceNode.has("app_code")) {
                                            flowJob.appCode = sourceNode.get("app_code").asText();
                                        }
                                        if (sourceNode.has("app_id")) {
                                            flowJob.appId = sourceNode.get("app_id").asInt();
                                        }
                                        if (sourceNode.has("flow_id")) {
                                            flowJob.flowId = sourceNode.get("flow_id").asLong();
                                        }
                                        if (sourceNode.has("flow_name")) {
                                            flowJob.flowName = sourceNode.get("flow_name").asText();
                                            flowJob.displayName = flowJob.flowName;
                                        }
                                        if (sourceNode.has("flow_path")) {
                                            flowJob.flowPath = sourceNode.get("flow_path").asText();
                                        }
                                        if (sourceNode.has("flow_group")) {
                                            flowJob.flowGroup = sourceNode.get("flow_group").asText();
                                        }
                                        flowJob.link = "#/flows/name/" + flowJob.appCode + "/"
                                                + Long.toString(flowJob.flowId) + "/page/1?urn="
                                                + flowJob.flowGroup;
                                        flowJob.path = flowJob.appCode + "/" + flowJob.flowPath;

                                        flowJob.schema = sourceNode.toString();
                                    }
                                }
                                pagedFlows.add(flowJob);
                            }
                        }
                    }
                }
            }
        }
    }
    resultNode.put("count", count);
    resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
    resultNode.set("data", Json.toJson(pagedFlows));
    return resultNode;
}

From source file:dao.AdvSearchDAO.java

public static ObjectNode elasticSearchMetric(JsonNode searchOpt, int page, int size) {
    ObjectNode resultNode = Json.newObject();
    Long count = 0L;//from ww w  .ja v a2s .c o  m
    List<Metric> pagedMetrics = new ArrayList<>();
    ObjectNode queryNode = Json.newObject();
    queryNode.put("from", (page - 1) * size);
    queryNode.put("size", size);

    JsonNode searchNode = utils.Search.generateMetricAdvSearchQueryString(searchOpt);

    if (searchNode != null && searchNode.isContainerNode()) {
        queryNode.set("query", searchNode);
    }

    Promise<WSResponse> responsePromise = WS
            .url(Play.application().configuration().getString(SearchDAO.ELASTICSEARCH_METRIC_URL_KEY))
            .post(queryNode);
    JsonNode responseNode = responsePromise.get(1000).asJson();

    resultNode.put("page", page);
    resultNode.put("category", "Metrics");
    resultNode.put("isMetrics", true);
    resultNode.put("itemsPerPage", size);

    if (responseNode != null && responseNode.isContainerNode() && responseNode.has("hits")) {
        JsonNode hitsNode = responseNode.get("hits");
        if (hitsNode != null) {
            if (hitsNode.has("total")) {
                count = hitsNode.get("total").asLong();
            }
            if (hitsNode.has("hits")) {
                JsonNode dataNode = hitsNode.get("hits");
                if (dataNode != null && dataNode.isArray()) {
                    Iterator<JsonNode> arrayIterator = dataNode.elements();
                    if (arrayIterator != null) {
                        while (arrayIterator.hasNext()) {
                            JsonNode node = arrayIterator.next();
                            if (node.isContainerNode() && node.has("_id")) {
                                Metric metric = new Metric();
                                metric.id = node.get("_id").asInt();
                                if (node.has("_source")) {
                                    JsonNode sourceNode = node.get("_source");
                                    if (sourceNode != null) {
                                        if (sourceNode.has("metric_name")) {
                                            metric.name = sourceNode.get("metric_name").asText();
                                        }
                                        if (sourceNode.has("metric_description")) {
                                            metric.description = sourceNode.get("metric_description").asText();
                                        }
                                        if (sourceNode.has("dashboard_name")) {
                                            metric.dashboardName = sourceNode.get("dashboard_name").asText();
                                        }
                                        if (sourceNode.has("metric_group")) {
                                            metric.group = sourceNode.get("metric_group").asText();
                                        }
                                        if (sourceNode.has("metric_category")) {
                                            metric.category = sourceNode.get("metric_category").asText();
                                        }
                                        if (sourceNode.has("urn")) {
                                            metric.urn = sourceNode.get("urn").asText();
                                        }
                                        if (sourceNode.has("metric_source")) {
                                            metric.source = sourceNode.get("metric_source").asText();
                                            if (StringUtils.isBlank(metric.source)) {
                                                metric.source = null;
                                            }
                                        }
                                        metric.schema = sourceNode.toString();
                                    }
                                }
                                pagedMetrics.add(metric);
                            }
                        }
                    }
                }
            }
        }
    }
    resultNode.put("count", count);
    resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
    resultNode.set("data", Json.toJson(pagedMetrics));
    return resultNode;
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.TestElasticsearchIndexUtils.java

@Test
public void test_fullMapping() throws JsonProcessingException, IOException {

    final String both = Resources.toString(
            Resources/*from   www.j  a v  a2s .co  m*/
                    .getResource("com/ikanow/aleph2/search_service/elasticsearch/utils/full_mapping_test.json"),
            Charsets.UTF_8);
    final JsonNode both_json = _mapper.readTree(both);
    final String uuid = "de305d54-75b4-431b-adb2-eb6b9e546015";

    final DataBucketBean test_bucket = BeanTemplateUtils.build(DataBucketBean.class)
            .with(DataBucketBean::_id, uuid).with(DataBucketBean::full_name, "/test/full/mapping")
            .with(DataBucketBean::data_schema, BeanTemplateUtils.build(DataSchemaBean.class).with(
                    DataSchemaBean::search_index_schema,
                    BeanTemplateUtils.build(DataSchemaBean.SearchIndexSchemaBean.class).with("enabled", true)
                            .with("technology_override_schema", ImmutableMap.builder()
                                    .put("settings",
                                            ImmutableMap.builder().put("index.refresh_interval", "10s").build())
                                    .put("mappings", both_json.get("mappings")).build())
                            .done().get())
                    .with(DataSchemaBean::columnar_schema, BeanTemplateUtils
                            .build(DataSchemaBean.ColumnarSchemaBean.class)
                            .with("field_include_list",
                                    Arrays.asList("column_only_enabled", "@timestamp", "@version"))
                            .with("field_exclude_list", Arrays.asList("column_only_disabled"))
                            .with("field_include_pattern_list", Arrays.asList("test*", "column_only_enabled2*"))
                            .with("field_type_include_list", Arrays.asList("string"))
                            .with("field_exclude_pattern_list",
                                    Arrays.asList("*noindex", "column_only_disabled2*"))
                            .with("field_type_exclude_list", Arrays.asList("number")).done().get())
                    .done().get())
            .done().get();

    final String expected = Resources.toString(
            Resources.getResource(
                    "com/ikanow/aleph2/search_service/elasticsearch/utils/mapping_test_results.json"),
            Charsets.UTF_8);
    final JsonNode expected_json = _mapper.readTree(expected);

    final ElasticsearchIndexServiceConfigBean schema_config = ElasticsearchIndexConfigUtils
            .buildConfigBeanFromSchema(test_bucket, _config, _mapper);

    // 1) Sub method
    {
        final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> field_lookups = ElasticsearchIndexUtils
                .parseDefaultMapping(both_json, Optional.empty(), Optional.empty(), Optional.empty(),
                        _config.search_technology_override(), _mapper);

        final XContentBuilder test_result = ElasticsearchIndexUtils.getFullMapping(test_bucket,
                Optional.empty(), true, schema_config, field_lookups,
                _mapper.convertValue(_config.columnar_technology_override().enabled_field_data_notanalyzed(),
                        JsonNode.class),
                _mapper.convertValue(_config.columnar_technology_override().enabled_field_data_analyzed(),
                        JsonNode.class),
                _mapper.convertValue(_config.columnar_technology_override().default_field_data_notanalyzed(),
                        JsonNode.class),
                _mapper.convertValue(_config.columnar_technology_override().default_field_data_analyzed(),
                        JsonNode.class),
                Optional.empty(), _config.search_technology_override(), _mapper, "misc_test");

        assertEquals(expected_json.toString(), test_result.bytes().toUtf8());
    }

    // Final method
    {
        final XContentBuilder test_result = ElasticsearchIndexUtils.createIndexMapping(test_bucket,
                Optional.empty(), true, schema_config, _mapper, "_default_");
        assertEquals(expected_json.toString(), test_result.bytes().toUtf8());
    }
}