Java Code Examples for com.fasterxml.jackson.core.JsonFactory#setCodec()

The following examples show how to use com.fasterxml.jackson.core.JsonFactory#setCodec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeClusterCoordinator.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void storeState() {
    final ObjectMapper mapper = new ObjectMapper();
    final JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.setCodec(mapper);

    try {
        final Map<String, String> stateMap = new HashMap<>();

        final NodeIdentifier localNodeId = getLocalNodeIdentifier();
        for (final NodeIdentifier nodeId : getNodeIdentifiers()) {
            final boolean isLocalId = nodeId.equals(localNodeId);
            final NodeIdentifierDescriptor descriptor = NodeIdentifierDescriptor.fromNodeIdentifier(nodeId, isLocalId);

            try (final StringWriter writer = new StringWriter()) {
                final JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
                jsonGenerator.writeObject(descriptor);

                final String serializedDescriptor = writer.toString();
                stateMap.put(nodeId.getId(), serializedDescriptor);
            }
        }

        stateManager.setState(stateMap, Scope.LOCAL);
        logger.debug("Stored the following state as the Cluster Topology: {}", stateMap);
    } catch (final Exception e) {
        logger.warn("Failed to store cluster topology to local State Manager. Upon restart of NiFi, the cluster topology may not be accurate until joining the cluster.", e);
    }
}
 
Example 2
Source File: JSONFormatter.java    From vespa with Apache License 2.0 4 votes vote down vote up
public JSONFormatter(final AccessLogEntry entry) {
    accessLogEntry = entry;
    generatorFactory = new JsonFactory();
    generatorFactory.setCodec(new ObjectMapper());
}
 
Example 3
Source File: JsonRenderer.java    From vespa with Apache License 2.0 4 votes vote down vote up
/** 
 * Creates a json renderer using a custom executor.
 * Using a custom executor is useful for tests to avoid creating new threads for each renderer registry.
 */
public JsonRenderer(Executor executor) {
    super(executor);
    generatorFactory = new JsonFactory();
    generatorFactory.setCodec(createJsonCodec());
}
 
Example 4
Source File: NamespaceOverrideMapper.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
private Map<String,String> getNamespaceMapping(Set<String> tenants) {
    Map<String,String> namespaceMap = new HashMap<>();

    if (tenants == null || tenants.isEmpty()) {
        return namespaceMap;
    }

    try {
        String path = "api/v1/namespaces";
        URL url = new URL(NamespaceHandler.KUBERNETES_MASTER_URL + "/" + path);

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Authorization", "Bearer " + token);

        // set to 0 which indicated an infinite timeout
        connection.setConnectTimeout(0);
        connection.setReadTimeout(0);

        connection.connect();
        int responseCode = connection.getResponseCode();

        if (responseCode == 200) {

            JsonFactory jsonFactory = new JsonFactory();
            jsonFactory.setCodec(new ObjectMapper());

            InputStream inputStream = connection.getInputStream();
            JsonParser jsonParser = jsonFactory.createParser(inputStream);

            JsonNode jsonNode = jsonParser.readValueAsTree();
            // When the connection is closed, the response is null
            if (jsonNode != null) {
                JsonNode items = jsonNode.get("items");
                if (items != null && items.isArray()) {
                    for (JsonNode project : items) {
                        JsonNode metaData = project.get("metadata");
                        String projectName = metaData.get("name").asText();
                        String projectID = metaData.get("uid").asText();

                        if (tenants.contains(projectName)) {
                            namespaceMap.put(projectID, projectName);
                        }
                    }
                }
            }

            jsonParser.close();
            inputStream.close();

        } else {
            log.error("Error getting metadata from the OpenShift Master (" + responseCode + "). This may mean the 'hawkular' user does not have permission to watch projects. Waiting 2 seconds and will try again.");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }


    } catch (IOException e) {
        log.error(e);
    }

    return namespaceMap;
}
 
Example 5
Source File: NodeClusterCoordinator.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void recoverState() throws IOException {
    final StateMap stateMap = stateManager.getState(Scope.LOCAL);
    if (stateMap == null) {
        logger.debug("No state to restore");
        return;
    }

    final ObjectMapper mapper = new ObjectMapper();
    final JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.setCodec(mapper);

    final Map<NodeIdentifier, NodeConnectionStatus> connectionStatusMap = new HashMap<>();
    NodeIdentifier localNodeId = null;

    final Map<String, String> state = stateMap.toMap();
    for (final Map.Entry<String, String> entry : state.entrySet()) {
        final String nodeUuid = entry.getKey();
        final String nodeIdentifierJson = entry.getValue();
        logger.debug("Recovering state for {} = {}", nodeUuid, nodeIdentifierJson);

        try (final JsonParser jsonParser = jsonFactory.createParser(nodeIdentifierJson)) {
            final NodeIdentifierDescriptor nodeIdDesc = jsonParser.readValueAs(NodeIdentifierDescriptor.class);
            final NodeIdentifier nodeId = nodeIdDesc.toNodeIdentifier();

            connectionStatusMap.put(nodeId, new NodeConnectionStatus(nodeId, DisconnectionCode.NOT_YET_CONNECTED));
            if (nodeIdDesc.isLocalNodeIdentifier()) {
                if (localNodeId == null) {
                    localNodeId = nodeId;
                } else {
                    logger.warn("When recovering state, determined that two Node Identifiers claim to be the local Node Identifier: {} and {}. Will ignore both of these and wait until " +
                        "connecting to cluster to determine which Node Identiifer is the local Node Identifier", localNodeId.getFullDescription(), nodeId.getFullDescription());
                    localNodeId = null;
                }
            }
        }
    }

    if (!connectionStatusMap.isEmpty()) {
        resetNodeStatuses(connectionStatusMap);
    }

    if (localNodeId != null) {
        logger.debug("Recovered state indicating that Local Node Identifier is {}", localNodeId);
        setLocalNodeIdentifier(localNodeId);
    }
}