Example usage for com.fasterxml.jackson.databind.node ObjectNode get

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode get

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ObjectNode get.

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:org.illalabs.rss.RssStreamProviderTask.java

/**
 * Reads the url and queues the data//  www. j a v  a2s.  c om
 * 
 * @param feedUrl
 *            rss feed url
 * @return set of all article urls that were read from the feed
 * @throws IOException
 *             when it cannot connect to the url or the url is malformed
 * @throws FeedException
 *             when it cannot reed the feed.
 */
@VisibleForTesting
protected Set<String> queueFeedEntries(URL feedUrl) throws IOException, FeedException {
    Set<String> batch = Sets.newConcurrentHashSet();
    URLConnection connection = feedUrl.openConnection();
    connection.setConnectTimeout(this.timeOut);
    connection.setConnectTimeout(this.timeOut);
    SyndFeedInput input = new SyndFeedInput();
    SyndFeed feed = input.build(new InputStreamReader(connection.getInputStream()));
    for (Object entryObj : feed.getEntries()) {
        SyndEntry entry = (SyndEntry) entryObj;
        ObjectNode nodeEntry = this.serializer.deserialize(entry);
        nodeEntry.put(RSS_KEY, this.feedDetails.getUrl());
        String entryId = determineId(nodeEntry);
        batch.add(entryId);
        Datum datum = new Datum(nodeEntry, entryId, DateTime.now());
        try {
            JsonNode published = nodeEntry.get(DATE_KEY);
            if (published != null) {
                try {
                    DateTime date = RFC3339Utils.parseToUTC(published.asText());
                    if (date.isAfter(this.publishedSince)
                            && (!seenBefore(entryId, this.feedDetails.getUrl()))) {
                        this.dataQueue.put(datum);
                        LOGGER.debug("Added entry, {}, to provider queue.", entryId);
                    }
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    LOGGER.trace(
                            "Failed to parse date from object node, attempting to add node to queue by default.");
                    if (!seenBefore(entryId, this.feedDetails.getUrl())) {
                        this.dataQueue.put(datum);
                        LOGGER.debug("Added entry, {}, to provider queue.", entryId);
                    }
                }
            } else {
                LOGGER.debug("No published date present, attempting to add node to queue by default.");
                if (!seenBefore(entryId, this.feedDetails.getUrl())) {
                    this.dataQueue.put(datum);
                    LOGGER.debug("Added entry, {}, to provider queue.", entryId);
                }
            }
        } catch (InterruptedException ie) {
            LOGGER.error("Interupted Exception.");
            Thread.currentThread().interrupt();
        }
    }
    return batch;
}

From source file:org.apache.streams.twitter.converter.TwitterDocumentClassifier.java

public List<Class> detectClasses(Object document) {

    Preconditions.checkNotNull(document);
    Preconditions.checkArgument(document instanceof String || document instanceof ObjectNode);

    mapper = new StreamsJacksonMapper(Lists.newArrayList(StreamsTwitterMapper.TWITTER_FORMAT));

    ObjectNode objectNode;
    try {//from  www . jav  a 2  s .  c  o m
        if (document instanceof String)
            objectNode = mapper.readValue((String) document, ObjectNode.class);
        else if (document instanceof ObjectNode)
            objectNode = (ObjectNode) document;
        else
            return Lists.newArrayList();
    } catch (IOException e) {
        return Lists.newArrayList();
    }

    List<Class> classList = Lists.newArrayList();

    if (objectNode.findValue("retweeted_status") != null && objectNode.get("retweeted_status") != null)
        classList.add(Retweet.class);
    else if (objectNode.findValue("delete") != null)
        classList.add(Delete.class);
    else if (objectNode.findValue("friends") != null || objectNode.findValue("friends_str") != null)
        classList.add(FriendList.class);
    else if (objectNode.findValue("target_object") != null)
        classList.add(UserstreamEvent.class);
    else if (objectNode.findValue("follower") != null && objectNode.findValue("followee") != null)
        classList.add(Follow.class);
    else if (objectNode.findValue("location") != null && objectNode.findValue("user") == null)
        classList.add(User.class);
    else
        classList.add(Tweet.class);

    return classList;
}

From source file:com.unboundid.scim2.common.GenericScimResourceObjectTest.java

/**
 * Tests parsing a json string into a GenericScimObject.
 *
 * @throws Exception in event of an error.
 */// w  ww .  j a  va 2s .  com
@Test
public void testBasicParsing() throws Exception {
    ObjectNode node = (ObjectNode) JsonUtils.getObjectReader()
            .readTree("{\n" + "    \"externalId\": \"user:externalId\",\n" + "    \"id\": \"user:id\",\n"
                    + "    \"meta\": {\n" + "        \"created\": \"2015-02-27T11:28:39.042Z\",\n"
                    + "        \"lastModified\": \"2015-02-27T11:29:39.042Z\",\n"
                    + "        \"locAtion\": \"http://here/user\",\n"
                    + "        \"resourceType\": \"some resource type\",\n" + "        \"version\": \"1.0\"\n"
                    + "    },\n" + "    \"name\": {\n" + "        \"first\": \"name:first\",\n"
                    + "        \"last\": \"name:last\",\n" + "        \"middle\": \"name:middle\"\n"
                    + "    },\n" + "    \"shoeSize\" : \"12W\",\n" + "    \"password\": \"user:password\",\n"
                    + "    \"Schemas\": [" + "    \"urn:pingidentity:schemas:baseSchema\", "
                    + "    \"urn:pingidentity:schemas:favoriteColor\"" + "    ],\n"
                    + "    \"urn:pingidentity:schemas:favoriteColor\": {\n"
                    + "        \"favoriteColor\": \"extension:favoritecolor\"\n" + "    },\n"
                    + "    \"userName\": \"user:username\"\n" + "}");

    GenericScimResource gso = new GenericScimResource(node);

    ScimResource cso = gso;

    Set<String> schemaSet = new HashSet<String>();
    schemaSet.add("urn:pingidentity:schemas:baseSchema");
    schemaSet.add("urn:pingidentity:schemas:favoriteColor");

    Assert.assertTrue(cso.getSchemaUrns().containsAll(schemaSet));

    Assert.assertEquals(cso.getId(), "user:id");
    Assert.assertEquals(cso.getExternalId(), "user:externalId");
    Meta meta = cso.getMeta();

    Assert.assertEquals(dateFormat.format(meta.getCreated().getTime()), "2015-02-27T11:28:39.042Z");
    Assert.assertEquals(dateFormat.format(meta.getLastModified().getTime()), "2015-02-27T11:29:39.042Z");

    ObjectNode metaNode = (ObjectNode) JsonUtils.getObjectReader()
            .readTree(JsonUtils.getObjectWriter().writeValueAsString(gso.getMeta()));
    Assert.assertEquals(metaNode.get("created").asText(), "2015-02-27T11:28:39.042Z");
    Assert.assertEquals(metaNode.get("lastModified").asText(), "2015-02-27T11:29:39.042Z");

    Assert.assertEquals(meta.getLocation().toString(), "http://here/user");
    Assert.assertEquals(meta.getResourceType(), "some resource type");
    Assert.assertEquals(meta.getVersion(), "1.0");

    Assert.assertEquals(((GenericScimResource) cso).getObjectNode().path("shoeSize").asText(), "12W");
}

From source file:com.palominolabs.crm.sf.rest.RestConnectionImpl.java

@Override
@Nonnull// ww  w . j  ava2s.co m
public BasicSObjectMetadataResult getBasicObjectInfo(String sObjectType) throws IOException {
    Timer.Context context = basicSObjectInfoTimer.time();
    String jsonStr;
    try {
        jsonStr = this.getHttpApiClient().basicSObjectInfo(sObjectType);
    } finally {
        context.stop();
    }
    ObjectNode objectNode = this.objectReader.withType(ObjectNode.class).readValue(jsonStr);

    BasicSObjectMetadata metadata = this.objectReader.withType(BasicSObjectMetadata.class)
            .readValue(objectNode.get("objectDescribe"));

    ArrayNode recentItems = this.objectReader.withType(ArrayNode.class)
            .readValue(objectNode.get("recentItems"));

    List<SObject> sObjects = getSObjects(recentItems.elements());

    return new BasicSObjectMetadataResult(metadata, sObjects);
}

From source file:org.lendingclub.mercator.docker.SwarmScanner.java

protected long saveTask(JsonNode it) {

    ObjectNode n = flattenTask(it);

    n.put("swarmClusterId", getSwarmClusterId().get());

    String taskId = n.get("taskId").asText();
    String serviceId = n.path("serviceId").asText();
    String swarmNodeId = n.path("swarmNodeId").asText();
    checkNotEmpty(taskId, "taskId");
    checkNotEmpty(serviceId, "serviceId");
    checkNotEmpty(swarmNodeId, "swarmNodeId");

    AtomicLong timestamp = new AtomicLong(Long.MAX_VALUE);
    dockerScanner.getNeoRxClient()//  w ww. ja  va  2  s. c o m
            .execCypher(
                    "merge (x:DockerTask {taskId:{taskId}}) set x+={props}, x.updateTs=timestamp() return x",
                    "taskId", taskId, "props", n)
            .forEach(tt -> {

                timestamp.set(tt.path("updateTs").asLong(Long.MAX_VALUE));

                removeDockerLabels("DockerTask", "taskId", taskId, n, it);
            });

    {
        // it might be worth it to select these relationships and only
        // update if they are missing
        dockerScanner.getNeoRxClient().execCypher(
                "match (s:DockerService {serviceId:{serviceId}}),(t:DockerTask{taskId:{taskId}}) merge (s)-[x:CONTAINS]->(t) set x.updateTs=timestamp() return t,s",
                "serviceId", serviceId, "taskId", taskId);

        dockerScanner.getNeoRxClient().execCypher(
                "match (h:DockerHost {swarmNodeId:{swarmNodeId}}), (t:DockerTask {swarmNodeId:{swarmNodeId}}) merge (h)-[x:RUNS]->(t) set x.updateTs=timestamp()",
                "swarmNodeId", swarmNodeId);
    }
    return timestamp.get();
}

From source file:com.almende.dht.rpc.DHT.java

/**
 * Iterative_node_lookup.//  w w w .j  av a2  s.c  o  m
 *
 * @param near
 *            the near
 * @return the list
 */
public List<Node> iterative_node_lookup(final Key near) {
    final TreeMap<Key, Node> shortList = new TreeMap<Key, Node>();
    final Set<Node> tried = new HashSet<Node>();

    insert(shortList, near, rt.getClosestNodes(near, Constants.A));

    final int[] noInFlight = new int[1];
    noInFlight[0] = 0;
    boolean keepGoing = true;
    while (keepGoing) {
        while (noInFlight[0] > 0) {
            synchronized (shortList) {
                try {
                    shortList.wait(100);
                } catch (InterruptedException e) {
                }
            }
        }

        List<Node> copy = Arrays.asList(shortList.values().toArray(new Node[0]));
        Collections.sort(copy);
        final Iterator<Node> iter = copy.iterator();
        int count = 0;
        while (iter.hasNext() && count < Constants.A) {
            final Node next = iter.next();
            if (tried.contains(next)) {
                continue;
            }
            count++;
            tried.add(next);
            try {
                ObjectNode params = JOM.createObjectNode();
                params.set("me", JOM.getInstance().valueToTree(rt.getMyKey()));
                params.set("near", JOM.getInstance().valueToTree(near));

                AsyncCallback<ObjectNode> callback = new AsyncCallback<ObjectNode>() {

                    @Override
                    public void onSuccess(ObjectNode res) {
                        List<Node> result = NODELIST.inject(res.get("nodes"));
                        rt.seenNode(next);
                        synchronized (shortList) {
                            insert(shortList, near, result);
                            noInFlight[0]--;
                            shortList.notifyAll();
                        }
                    }

                    @Override
                    public void onFailure(Exception exception) {
                        synchronized (shortList) {
                            shortList.remove(near.dist(next.getKey()));
                            noInFlight[0]--;
                            shortList.notifyAll();
                            LOG.log(Level.WARNING, noInFlight[0] + ":OnFailure called:" + next.getUri(),
                                    exception);
                        }
                    }

                };
                caller.call(next.getUri(), "dht.find_close_nodes", params, callback);
                synchronized (shortList) {
                    noInFlight[0]++;
                }
            } catch (IOException e) {
                synchronized (shortList) {
                    shortList.remove(near.dist(next.getKey()));
                }
                continue;
            }
        }
        if (count == 0) {
            keepGoing = false;
        }
    }
    synchronized (shortList) {
        return new ArrayList<Node>(shortList.values());
    }
}

From source file:com.marklogic.samplestack.database.DatabaseQnADocumentSearchIT.java

@Test
public void testTagSearch() throws JsonProcessingException {
    JsonNode query;/*from   www .  j  ava2 s  .com*/
    ObjectNode results;
    try {
        query = mapper.readValue("{\"query\":{\"qtext\":\"tag:monotouch\"}}", JsonNode.class);
        results = operations.qnaSearch(ClientRole.SAMPLESTACK_CONTRIBUTOR, query, 1, QueryView.FACETS);

        logger.debug("Query Results:" + mapper.writeValueAsString(results));
    } catch (IOException e) {
        throw new SamplestackIOException(e);
    }
    assertNotNull("JSON has facet results", results.get("facets").get("tag"));
}

From source file:org.agatom.springatom.cmp.action.DefaultActionsModelReader.java

private ActionModelReferenceMap flattenActionModels(final ObjectNode object) throws Exception {
    Assert.notNull(object, "ActionModel can not be null");

    final ActionModelReferenceMap map = new ActionModelReferenceMap();
    map.node = object;/*www  .  j a va  2  s .com*/

    if (this.hasSubmodels(object)) {
        final ArrayNode submodels = (ArrayNode) object.get("submodels");
        for (final JsonNode node : submodels) {
            map.submodel.put(node.textValue(), node);
        }
    }
    if (this.hasActions(object)) {
        final ArrayNode actions = (ArrayNode) object.get("actions");
        for (final JsonNode action : actions) {
            map.actions.add(action);
        }
    }

    return map;
}

From source file:com.almende.eve.transport.xmpp.XmppService.java

/**
 * Disconnect the agent from the connected messaging service(s) (if any).
 * //  ww w  .  j  av  a2 s.c o  m
 * @param agentId
 *            the agent id
 * @throws InvalidKeyException
 *             the invalid key exception
 * @throws InvalidAlgorithmParameterException
 *             the invalid algorithm parameter exception
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 * @throws NoSuchPaddingException
 *             the no such padding exception
 * @throws IllegalBlockSizeException
 *             the illegal block size exception
 * @throws BadPaddingException
 *             the bad padding exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
@Access(AccessType.UNAVAILABLE)
public final void disconnect(final String agentId) throws InvalidKeyException,
        InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {

    final ArrayNode conns = getConns(agentId);
    if (conns != null) {
        for (final JsonNode conn : conns) {
            final ObjectNode params = (ObjectNode) conn;

            final String encryptedUsername = params.has("username") ? params.get("username").textValue() : null;
            final String encryptedResource = params.has("resource") ? params.get("resource").textValue() : null;
            if (encryptedUsername != null) {
                final String username = EncryptionUtil.decrypt(encryptedUsername);
                String resource = null;
                if (encryptedResource != null) {
                    resource = EncryptionUtil.decrypt(encryptedResource);
                }

                final URI url = generateUrl(username, host, resource);
                final AgentConnection connection = connectionsByUrl.get(url);
                if (connection != null) {
                    connection.disconnect();
                    connectionsByUrl.remove(url);
                }
            }
        }
    }
    delConnections(agentId);
}

From source file:com.almende.eve.transport.xmpp.XmppService.java

/**
 * Get the first XMPP url of an agent from its id.
 * If no agent with given id is connected via XMPP, null is returned.
 * /*  w  w  w. j a  va2  s.  co  m*/
 * @param agentId
 *            The id of the agent
 * @return agentUrl
 */
@Override
public URI getAgentUrl(final String agentId) {
    try {
        final ArrayNode conns = getConns(agentId);
        if (conns != null) {
            for (final JsonNode conn : conns) {
                final ObjectNode params = (ObjectNode) conn;

                final String encryptedUsername = params.has("username") ? params.get("username").textValue()
                        : null;
                final String encryptedResource = params.has("resource") ? params.get("resource").textValue()
                        : null;
                if (encryptedUsername != null) {
                    final String username = EncryptionUtil.decrypt(encryptedUsername);
                    String resource = null;
                    if (encryptedResource != null) {
                        resource = EncryptionUtil.decrypt(encryptedResource);
                    }
                    return generateUrl(username, host, resource);
                }
            }
        }
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "", e);
    }
    return null;
}