List of usage examples for com.fasterxml.jackson.databind.node ObjectNode elements
public Iterator<JsonNode> elements()
From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java
/** Creates a mapping for the bucket - search service elements .. up to but not including the mapping + type * NOTE: creates an embedded object that is {{, ie must be closed twice subsequently in order to be a well formed JSON object * @param bucket// www .j a v a2 s .co m * @return * @throws IOException */ public static XContentBuilder getSearchServiceMapping(final DataBucketBean bucket, final Optional<String> secondary_buffer, final boolean is_primary, final ElasticsearchIndexServiceConfigBean schema_config, final Optional<XContentBuilder> to_embed, final ObjectMapper mapper) { try { final XContentBuilder start = to_embed.orElse(XContentFactory.jsonBuilder().startObject()); // (Nullable) final ElasticsearchIndexServiceConfigBean.SearchIndexSchemaDefaultBean search_schema = schema_config .search_technology_override(); //(very briefly Nullable) final JsonNode settings = Optional.ofNullable(search_schema).map(s -> s.settings()) .map(o -> mapper.convertValue(o, JsonNode.class)).orElse(null); //(very briefly Nullable) final ObjectNode aliases = (ObjectNode) Optional.ofNullable(search_schema).map(s -> s.aliases()) .map(o -> mapper.convertValue(o, JsonNode.class)).orElse(mapper.createObjectNode()); if (is_primary) { // add the "read only" prefix alias aliases.set( ElasticsearchContext.READ_PREFIX + ElasticsearchIndexUtils.getBaseIndexName(bucket, Optional.empty()), mapper.createObjectNode()); } // Settings final String type_key = getTypeKey(bucket, mapper); return Lambdas.wrap_u(__ -> { if (null == settings) { // nothing to do return start; } else { return start.rawField("settings", settings.toString().getBytes()); } }) // Aliases .andThen(Lambdas.wrap_u(json -> { if (!aliases.elements().hasNext()) { // nothing to do return json; } else { return start.rawField("aliases", aliases.toString().getBytes()); } })) // Mappings and overrides .andThen(Lambdas.wrap_u(json -> json.startObject("mappings").startObject(type_key))) // Add the secondary buffer name to the metadata: .andThen(Lambdas.wrap_u(json -> { return json.rawField(CUSTOM_META, createMergedMeta(Either.right(mapper), bucket, is_primary, secondary_buffer) .toString().getBytes()); })) // More mapping overrides .andThen(Lambdas.wrap_u(json -> { return Optional.ofNullable(search_schema).map(ss -> ss.mapping_overrides()) .map(m -> m.getOrDefault(type_key, m.get("*"))).orElse(Collections.emptyMap()) .entrySet().stream().reduce(json, Lambdas.wrap_u((acc, kv) -> { if (CUSTOM_META.equals(kv.getKey())) { // meta is a special case, merge my results in regardless return acc.rawField(kv.getKey(), createMergedMeta( Either.left( mapper.convertValue(kv.getValue(), JsonNode.class)), bucket, is_primary, secondary_buffer).toString() .getBytes()); } else { return acc.rawField(kv.getKey(), mapper .convertValue(kv.getValue(), JsonNode.class).toString().getBytes()); } }), (acc1, acc2) -> acc1 // (can't actually ever happen) ); })).apply(null); } catch (IOException e) { //Handle fake "IOException" return null; } }
From source file:me.tfeng.toolbox.avro.AvroHelper.java
private static JsonNode convertToSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory); if (!value.isNull()) { newNode.set(fieldName, value); }//from ww w . j ava 2s . c o m } } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory); if (value.isNull()) { newNode.set(entry.getKey(), value); } } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.set(entry.getKey(), convertToSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { return convertToSimpleRecord(type, json.get(type.getFullName()), factory); } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.play.avro.AvroHelper.java
private static JsonNode convertToSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory); if (!value.isNull()) { newNode.put(fieldName, value); }// ww w .j ava 2 s .co m } } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory); if (value.isNull()) { newNode.put(entry.getKey(), value); } } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = AvroHelper.getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.put(entry.getKey(), convertToSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { return convertToSimpleRecord(type, json.get(type.getFullName()), factory); } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.toolbox.avro.AvroHelper.java
private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { newNode.set(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory)); } else if (field.defaultValue() != null) { newNode.set(fieldName, MAPPER.readTree(field.defaultValue().toString())); } else { newNode.set(fieldName, factory.nullNode()); }/*from ww w . j ava2 s . co m*/ } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); newNode.set(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory)); } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.set(entry.getKey(), convertFromSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { ObjectNode newNode = factory.objectNode(); newNode.set(type.getFullName(), convertFromSimpleRecord(type, json, factory)); return newNode; } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.play.avro.AvroHelper.java
private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { newNode.put(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory)); } else if (field.defaultValue() != null) { newNode.put(fieldName, Json.parse(field.defaultValue().toString())); } else { newNode.put(fieldName, factory.nullNode()); }/* w w w . j a va 2 s . c om*/ } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); newNode.put(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory)); } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = AvroHelper.getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.put(entry.getKey(), convertFromSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { ObjectNode newNode = factory.objectNode(); newNode.put(type.getFullName(), convertFromSimpleRecord(type, json, factory)); return newNode; } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:de.cubeisland.engine.core.webapi.WebSocketRequestHandler.java
private void handleTextWebSocketFrame(final ChannelHandlerContext ctx, TextWebSocketFrame frame) { // TODO log exceptions!!! JsonNode jsonNode;//from w ww . ja v a2s . c o m try { jsonNode = objectMapper.readTree(frame.text()); } catch (IOException e) { this.log.info("the frame data was no valid json!"); return; } JsonNode action = jsonNode.get("action"); JsonNode msgid = jsonNode.get("msgid"); ObjectNode responseNode = objectMapper.createObjectNode(); if (action == null) { responseNode.put("response", "No action"); } else { JsonNode data = jsonNode.get("data"); switch (action.asText()) { case "http": QueryStringDecoder qsDecoder = new QueryStringDecoder(normalizePath(data.get("uri").asText()), this.UTF8, true, 100); JsonNode reqMethod = data.get("method"); RequestMethod method = reqMethod != null ? getByName(reqMethod.asText()) : GET; JsonNode reqdata = data.get("body"); ApiHandler handler = this.server.getApiHandler(normalizePath(qsDecoder.path())); if (handler == null) { responseNode.put("response", "Unknown route"); break; } Parameters params = new Parameters(qsDecoder.parameters(), core.getCommandManager().getProviderManager()); ApiRequest request = new ApiRequest((InetSocketAddress) ctx.channel().remoteAddress(), method, params, EMPTY_HEADERS, reqdata, authUser); ApiResponse response = handler.execute(request); if (msgid != null) { responseNode.set("response", objectMapper.valueToTree(response.getContent())); } break; case "subscribe": this.server.subscribe(data.asText().trim(), this); break; case "unsubscribe": this.server.unsubscribe(data.asText().trim(), this); break; default: responseNode.put("response", action.asText() + " -- " + data.asText()); } } if (msgid != null && responseNode.elements().hasNext()) { responseNode.put("msgid", msgid); ctx.writeAndFlush(responseNode); } }