Search in sources :

Example 16 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project walkmod-core by walkmod.

the class SetReaderYMLAction method doAction.

@Override
public void doAction(JsonNode node) throws Exception {
    if (node.has("chains") && (chain == null || "".equals(chain.trim()))) {
        chain = "default";
    }
    ObjectNode reader = null;
    ObjectMapper mapper = provider.getObjectMapper();
    if (chain != null && !"".equals(chain.trim())) {
        if (node.has("chains")) {
            JsonNode chainsListNode = node.get("chains");
            if (chainsListNode.isArray()) {
                Iterator<JsonNode> it = chainsListNode.iterator();
                boolean found = false;
                while (it.hasNext() && !found) {
                    JsonNode current = it.next();
                    if (current.has("name")) {
                        String name = current.get("name").asText();
                        found = name.equals(chain);
                        if (found) {
                            if (current.has("reader")) {
                                reader = (ObjectNode) current.get("reader");
                            } else {
                                reader = new ObjectNode(mapper.getNodeFactory());
                            }
                            if (type != null && !"".equals(type.trim())) {
                                reader.set("type", new TextNode(type));
                            }
                            if (path != null && !"".equals(path.trim())) {
                                reader.set("path", new TextNode(path));
                            }
                            if (params != null && !params.isEmpty()) {
                                ObjectNode paramsObject = null;
                                if (reader.has("params")) {
                                    paramsObject = (ObjectNode) reader.get("params");
                                } else {
                                    paramsObject = new ObjectNode(mapper.getNodeFactory());
                                    reader.set("params", paramsObject);
                                }
                                Set<String> keys = params.keySet();
                                for (String key : keys) {
                                    paramsObject.put(key, params.get(key).toString());
                                }
                            }
                        }
                    }
                }
            }
        }
        if (reader != null) {
            provider.write(node);
        }
    } else {
        if (!node.has("chains")) {
            ArrayNode chains = new ArrayNode(mapper.getNodeFactory());
            ObjectNode defaultChain = new ObjectNode(mapper.getNodeFactory());
            defaultChain.set("name", new TextNode("default"));
            ObjectNode readerNode = new ObjectNode(mapper.getNodeFactory());
            if (type != null && !"".equals(type.trim())) {
                readerNode.set("type", new TextNode(type));
            }
            if (path != null && !"".equals(path.trim())) {
                readerNode.set("path", new TextNode(path));
            }
            if (params != null && !params.isEmpty()) {
                ObjectNode paramsObject = new ObjectNode(mapper.getNodeFactory());
                Set<String> keys = params.keySet();
                for (String key : keys) {
                    paramsObject.put(key, params.get(key).toString());
                }
                readerNode.set("params", paramsObject);
            }
            defaultChain.set("reader", readerNode);
            if (node.has("transformations")) {
                defaultChain.set("transformations", node.get("transformations"));
            }
            chains.add(defaultChain);
            provider.write(chains);
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project solr-document-store by DBCDK.

the class StatusBean method createQueueStatusNode.

private JsonNode createQueueStatusNode() {
    try (Connection connection = dataSource.getConnection();
        Statement stmt = connection.createStatement();
        PreparedStatement prepStmt = connection.prepareStatement("SELECT CAST(EXTRACT('epoch' FROM NOW() - dequeueafter) AS INTEGER) FROM queue WHERE consumer = ? ORDER BY dequeueafter LIMIT 1");
        ResultSet resultSet = stmt.executeQuery("SELECT consumer, COUNT(*) FROM queue GROUP BY consumer")) {
        ObjectNode node = O.createObjectNode();
        while (resultSet.next()) {
            int i = 0;
            String consumer = resultSet.getString(++i);
            int count = resultSet.getInt(++i);
            Integer age = null;
            prepStmt.setString(1, consumer);
            try (ResultSet preResultSet = prepStmt.executeQuery()) {
                if (preResultSet.next()) {
                    age = preResultSet.getInt(1);
                }
            }
            ObjectNode scope = node.putObject(consumer);
            scope.put("count", count);
            scope.put("age", age);
        }
        return node;
    } catch (SQLException ex) {
        log.error("Sql error counting queue entries: {}", ex.getMessage());
        log.debug("Sql error counting queue entries: ", ex);
        return new TextNode("SQL Exception");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 18 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project batfish by batfish.

the class JsonPathResult method extractValuesFromSuffix.

private void extractValuesFromSuffix(String displayVar, Extraction extraction, JsonPathExtractionHint jpeHint) {
    for (Entry<String, JsonPathResultEntry> entry : _result.entrySet()) {
        if (!_displayValues.containsKey(entry.getKey())) {
            _displayValues.put(entry.getKey(), new HashMap<>());
        }
        if (entry.getValue().getSuffix() == null) {
            throw new BatfishException("Cannot compute suffix-based display values with null suffix. " + "(Was suffix set to True in the original JsonPath Query?)");
        }
        Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE);
        Configuration c = (new ConfigurationBuilder()).build();
        Object jsonObject = JsonPath.parse(entry.getValue().getSuffix(), c).json();
        JsonPathQuery query = new JsonPathQuery(jpeHint.getFilter(), true);
        List<JsonNode> extractedList = new LinkedList<>();
        switch(jpeHint.getUse()) {
            case FUNCOFSUFFIX:
                {
                    if (!extraction.getSchemaAsObject().isIntOrIntList()) {
                        throw new BatfishException("schema must be INT(LIST) with funcofsuffix-based extraction hint");
                    }
                    Object result = JsonPathAnswerer.computePathFunction(jsonObject, query);
                    if (result != null) {
                        if (result instanceof Integer) {
                            extractedList.add(new IntNode((Integer) result));
                        } else if (result instanceof ArrayNode) {
                            for (JsonNode node : (ArrayNode) result) {
                                if (!(node instanceof IntNode)) {
                                    throw new BatfishException("Got non-integer result from path function after filter " + query.getPath());
                                }
                                extractedList.add(node);
                            }
                        } else {
                            throw new BatfishException("Unknown result type from computePathFunction");
                        }
                    }
                }
                break;
            case PREFIXOFSUFFIX:
            case SUFFIXOFSUFFIX:
                {
                    JsonPathResult filterResult = JsonPathAnswerer.computeResult(jsonObject, query);
                    Map<String, JsonPathResultEntry> filterResultEntries = filterResult.getResult();
                    for (Entry<String, JsonPathResultEntry> resultEntry : filterResultEntries.entrySet()) {
                        JsonNode value = (jpeHint.getUse() == UseType.PREFIXOFSUFFIX) ? new TextNode(resultEntry.getValue().getPrefixPart(jpeHint.getIndex())) : resultEntry.getValue().getSuffix();
                        confirmValueType(value, extraction.getSchemaAsObject().getBaseType());
                        extractedList.add(value);
                    }
                }
                break;
            default:
                throw new BatfishException("Unknown UseType " + jpeHint.getUse());
        }
        if (extractedList.size() == 0) {
            throw new BatfishException("Got no results after filtering suffix values of the answer" + "\nFilter: " + jpeHint.getFilter() + "\nJson: " + jsonObject);
        }
        if (extraction.getSchemaAsObject().isList()) {
            ArrayNode arrayNode = BatfishObjectMapper.mapper().valueToTree(extractedList);
            _displayValues.get(entry.getKey()).put(displayVar, arrayNode);
        } else {
            if (extractedList.size() > 1) {
                throw new BatfishException("Got multiple results after filtering suffix values " + " of the answer, but the display type is non-list");
            }
            _displayValues.get(entry.getKey()).put(displayVar, extractedList.get(0));
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) ConfigurationBuilder(com.jayway.jsonpath.Configuration.ConfigurationBuilder) Configuration(com.jayway.jsonpath.Configuration) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) LinkedList(java.util.LinkedList) IntNode(com.fasterxml.jackson.databind.node.IntNode) Entry(java.util.Map.Entry) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 19 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project graylog2-server by Graylog2.

the class NodeAdapterES6Test method buildVersionJsonObject.

private JsonNode buildVersionJsonObject(String foobar) {
    final ObjectNode versionObject = objectMapper.createObjectNode();
    versionObject.set("number", new TextNode(foobar));
    final ObjectNode jsonObject = objectMapper.createObjectNode();
    jsonObject.set("version", versionObject);
    return jsonObject;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 20 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithUnsuccessfulResponseAndErrorDetails.

@Test
public void executeWithUnsuccessfulResponseAndErrorDetails() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode rootCauseStub = objectMapper.createObjectNode();
    rootCauseStub.set("reason", new TextNode("foobar"));
    final ArrayNode rootCausesStub = objectMapper.createArrayNode();
    rootCausesStub.add(rootCauseStub);
    final ObjectNode errorStub = objectMapper.createObjectNode();
    errorStub.set("root_cause", rootCausesStub);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    responseStub.set("error", errorStub);
    when(resultMock.getJsonObject()).thenReturn(responseStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected ElasticsearchException to be thrown");
    } catch (ElasticsearchException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("foobar").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("foobar");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Aggregations

TextNode (com.fasterxml.jackson.databind.node.TextNode)96 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)35 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)29 JsonNode (com.fasterxml.jackson.databind.JsonNode)28 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)14 IntNode (com.fasterxml.jackson.databind.node.IntNode)14 MockScheduler (org.apache.kafka.common.utils.MockScheduler)14 MockTime (org.apache.kafka.common.utils.MockTime)14 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)14 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)13 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)12 NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)11 ArrayList (java.util.ArrayList)9 Scheduler (org.apache.kafka.common.utils.Scheduler)9 MiniTrogdorCluster (org.apache.kafka.trogdor.common.MiniTrogdorCluster)9 CreateTaskRequest (org.apache.kafka.trogdor.rest.CreateTaskRequest)9 TaskRunning (org.apache.kafka.trogdor.rest.TaskRunning)9 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)8