List of usage examples for com.fasterxml.jackson.databind JsonNode size
public int size()
From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java
@Test public void testAuthenticateForTokenByIdAnsProjectNameScope() throws JsonGenerationException, JsonMappingException, IOException { user = new User(); user.setId("e7912c2225e84ac5905d8cf0b5040a6d"); user.setPassword("f00@bar"); identity = new Identity(); identity.getMethods().add("password"); password = new Password(); password.setUser(user);//from w w w . jav a 2s. c om identity.getAuthMethods().put("password", password); Scope scope = new Scope(); AuthV3 auth = new AuthV3(); auth.setScope(scope); Project project = new Project(); project.setName("admin"); Domain d = new Domain(); d.setId("default"); project.setDomain(d); scope.setProject(project); auth.setIdentity(identity); AuthV3Wrapper wrapper = new AuthV3Wrapper(auth); String json = JsonUtils.toJson(wrapper); JsonNode node = JsonUtils.convertToJsonNode(json); JsonNode authJson = node.get("auth"); assertNotNull(authJson); JsonNode identityJson = authJson.get("identity"); assertNotNull(identityJson); JsonNode methodsJson = identityJson.get("methods"); assertEquals(1, methodsJson.size()); assertEquals("password", methodsJson.get(0).asText()); JsonNode passwordJson = identityJson.get("password"); assertNotNull(passwordJson); JsonNode userJson = passwordJson.get("user"); assertNotNull(userJson); assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText()); assertEquals("f00@bar", userJson.get("password").asText()); // authenticate Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request() .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE)); assertEquals(201, response.getStatus()); System.err.println(response.readEntity(String.class)); }
From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java
@Test public void testAuthenticateForTokenByIdAnsProjectNameDomainNameScope() throws JsonGenerationException, JsonMappingException, IOException { user = new User(); user.setId("e7912c2225e84ac5905d8cf0b5040a6d"); user.setPassword("f00@bar"); identity = new Identity(); identity.getMethods().add("password"); password = new Password(); password.setUser(user);/*from ww w . j a v a 2 s.co m*/ identity.getAuthMethods().put("password", password); Scope scope = new Scope(); AuthV3 auth = new AuthV3(); auth.setScope(scope); Project project = new Project(); project.setName("admin"); Domain d = new Domain(); d.setName("Default"); project.setDomain(d); scope.setProject(project); auth.setIdentity(identity); AuthV3Wrapper wrapper = new AuthV3Wrapper(auth); String json = JsonUtils.toJson(wrapper); JsonNode node = JsonUtils.convertToJsonNode(json); JsonNode authJson = node.get("auth"); assertNotNull(authJson); JsonNode identityJson = authJson.get("identity"); assertNotNull(identityJson); JsonNode methodsJson = identityJson.get("methods"); assertEquals(1, methodsJson.size()); assertEquals("password", methodsJson.get(0).asText()); JsonNode passwordJson = identityJson.get("password"); assertNotNull(passwordJson); JsonNode userJson = passwordJson.get("user"); assertNotNull(userJson); assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText()); assertEquals("f00@bar", userJson.get("password").asText()); // authenticate Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request() .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE)); assertEquals(201, response.getStatus()); System.err.println(response.readEntity(String.class)); }
From source file:com.appdynamics.extensions.solr.SolrHelper.java
/** * Fetches the solr-mbeans node from JsonResponse and puts it into a map * with key as Category name and its values as JsonNode * // w w w . j a va 2 s. com * @param mbeansUri * * @param resource * @return * @throws IOException */ public Map<String, JsonNode> getSolrMBeansHandlersMap(String core, String mbeansUri) throws IOException { String uri = String.format(mbeansUri, core); InputStream inputStream = null; Map<String, JsonNode> solrStatsMap = new HashMap<String, JsonNode>(); try { inputStream = httpClient.target().path(uri).get().inputStream(); JsonNode jsonNode = getJsonNode(inputStream); if (jsonNode != null) { JsonNode solrMBeansNode = jsonNode.path("solr-mbeans"); if (solrMBeansNode.isMissingNode()) { throw new IllegalArgumentException( "Missing node while parsing solr-mbeans node json string for " + core + uri); } for (int i = 1; i <= solrMBeansNode.size(); i += 2) { solrStatsMap.put(solrMBeansNode.get(i - 1).asText(), solrMBeansNode.get(i)); } } } catch (Exception e) { LOG.error(e); throw new RuntimeException(e); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { // Ignore } } return solrStatsMap; }
From source file:org.apache.parquet.cli.json.AvroJson.java
private static <T> T visit(JsonNode node, JsonTreeVisitor<T> visitor) { switch (node.getNodeType()) { case OBJECT:/*from w w w . j a va 2 s .c o m*/ Preconditions.checkArgument(node instanceof ObjectNode, "Expected instance of ObjectNode: " + node); // use LinkedHashMap to preserve field order Map<String, T> fields = Maps.newLinkedHashMap(); Iterator<Map.Entry<String, JsonNode>> iter = node.fields(); while (iter.hasNext()) { Map.Entry<String, JsonNode> entry = iter.next(); visitor.recordLevels.push(entry.getKey()); fields.put(entry.getKey(), visit(entry.getValue(), visitor)); visitor.recordLevels.pop(); } return visitor.object((ObjectNode) node, fields); case ARRAY: Preconditions.checkArgument(node instanceof ArrayNode, "Expected instance of ArrayNode: " + node); List<T> elements = Lists.newArrayListWithExpectedSize(node.size()); for (JsonNode element : node) { elements.add(visit(element, visitor)); } return visitor.array((ArrayNode) node, elements); case BINARY: Preconditions.checkArgument(node instanceof BinaryNode, "Expected instance of BinaryNode: " + node); return visitor.binary((BinaryNode) node); case STRING: Preconditions.checkArgument(node instanceof TextNode, "Expected instance of TextNode: " + node); return visitor.text((TextNode) node); case NUMBER: Preconditions.checkArgument(node instanceof NumericNode, "Expected instance of NumericNode: " + node); return visitor.number((NumericNode) node); case BOOLEAN: Preconditions.checkArgument(node instanceof BooleanNode, "Expected instance of BooleanNode: " + node); return visitor.bool((BooleanNode) node); case MISSING: Preconditions.checkArgument(node instanceof MissingNode, "Expected instance of MissingNode: " + node); return visitor.missing((MissingNode) node); case NULL: Preconditions.checkArgument(node instanceof NullNode, "Expected instance of NullNode: " + node); return visitor.nullNode((NullNode) node); default: throw new IllegalArgumentException("Unknown node type: " + node.getNodeType() + ": " + node); } }
From source file:com.google.api.server.spi.config.jsonwriter.JsonConfigWriterTest.java
@Test public void testEnumType() throws Exception { ObjectMapper mapper = new ObjectMapper(); ObjectNode schemasConfig = mapper.createObjectNode(); writer.addTypeToSchema(schemasConfig, TypeToken.of(Outcome.class), apiConfig, null); JsonNode outcome = schemasConfig.path("Outcome"); assertEquals("Outcome", outcome.path("id").asText()); assertEquals("string", outcome.path("type").asText()); JsonNode enumConfig = outcome.path("enum"); assertTrue(enumConfig.isArray());/*from www . ja v a 2s . c o m*/ assertEquals(3, enumConfig.size()); assertEquals(Outcome.WON.toString(), enumConfig.get(0).asText()); assertEquals(Outcome.LOST.toString(), enumConfig.get(1).asText()); assertEquals(Outcome.TIE.toString(), enumConfig.get(2).asText()); }
From source file:jsonbrowse.JsonBrowse.java
/** * Builds a tree of TreeNode objects using the tree under the * given JsonNode./*from w ww . ja v a 2 s . com*/ * * @param name Text to be associated with node * @param node * @return root TreeNode */ private DefaultMutableTreeNode buildTree(String name, JsonNode node) { DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(name); Iterator<Entry<String, JsonNode>> it = node.fields(); while (it.hasNext()) { Entry<String, JsonNode> entry = it.next(); treeNode.add(buildTree(entry.getKey(), entry.getValue())); } if (node.isArray()) { for (int i = 0; i < node.size(); i++) { JsonNode child = node.get(i); if (child.isValueNode()) treeNode.add(new DefaultMutableTreeNode(child.asText())); else treeNode.add(buildTree(String.format("[%d]", i), child)); } } else if (node.isValueNode()) { treeNode.add(new DefaultMutableTreeNode(node.asText())); } return treeNode; }
From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java
@Test public void testAuthenticateForTokenAndCheckAndValidateAndRevokeAndCheck() throws JsonGenerationException, JsonMappingException, IOException { domain = new Domain(); domain.setId("default"); AuthV3 auth = new AuthV3(); auth.setIdentity(identity);/* ww w . j a v a 2 s. com*/ AuthV3Wrapper wrapper = new AuthV3Wrapper(auth); String json = JsonUtils.toJson(wrapper); JsonNode node = JsonUtils.convertToJsonNode(json); JsonNode authJson = node.get("auth"); assertNotNull(authJson); JsonNode identityJson = authJson.get("identity"); assertNotNull(identityJson); JsonNode methodsJson = identityJson.get("methods"); assertEquals(1, methodsJson.size()); assertEquals("password", methodsJson.get(0).asText()); JsonNode passwordJson = identityJson.get("password"); assertNotNull(passwordJson); JsonNode userJson = passwordJson.get("user"); assertNotNull(userJson); assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText()); assertEquals("f00@bar", userJson.get("password").asText()); // authenticate Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request() .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE)); assertEquals(201, response.getStatus()); String tokenid = response.getHeaderString("X-Subject-Token"); assertNotNull(tokenid); node = JsonUtils.convertToJsonNode(response.readEntity(String.class)); System.err.println(node); JsonNode tokenJ = node.get("token"); assertNotNull(tokenJ); assertNotNull(tokenJ.get("expires_at").asText()); assertNotNull(tokenJ.get("issued_at").asText()); assertEquals("password", tokenJ.get("methods").get(0).asText()); JsonNode userJ = tokenJ.get("user"); assertEquals(user.getId(), userJ.get("id").asText()); assertEquals(domain.getId(), userJ.get("domain").get("id").asText()); // check response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class) .register(ObjectMapperResolver.class).request() .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()) .header("X-Subject-Token", tokenid).head(); assertEquals(200, response.getStatus()); // validate response = target("/v3/auth/tokens").register(JacksonFeature.class).register(ObjectMapperResolver.class) .request() .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()) .header("X-Subject-Token", tokenid).get(); assertEquals(200, response.getStatus()); assertEquals(tokenid, response.getHeaderString("X-Subject-Token")); node = JsonUtils.convertToJsonNode(response.readEntity(String.class)); tokenJ = node.get("token"); assertNotNull(tokenJ); assertNotNull(tokenJ.get("expires_at").asText()); assertNotNull(tokenJ.get("issued_at").asText()); assertEquals("password", tokenJ.get("methods").get(0).asText()); userJ = tokenJ.get("user"); assertEquals(user.getId(), userJ.get("id").asText()); assertEquals(domain.getId(), userJ.get("domain").get("id").asText()); System.err.println(node.toString()); // revoke response = target("/v3/auth/tokens").register(JacksonFeature.class).register(ObjectMapperResolver.class) .request() .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()) .header("X-Subject-Token", tokenid).delete(); assertEquals(204, response.getStatus()); response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class) .register(ObjectMapperResolver.class).request() .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()) .header("X-Subject-Token", tokenid).head(); assertEquals(404, response.getStatus()); }
From source file:com.tda.sitefilm.allocine.JSONAllocineAPIHelper.java
/** * bla//from w w w . j ava2 s.co m * @param query nom du film recherch * @return la rehcerche * @exception Exception exception */ //@Override public Search searchMovieInfos(String query) throws Exception { JsonNode rootNode = null; InputStream inputStream = connectSearchMovieInfos(query).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } Search search = new Search(); if (rootNode == null) { return search; } JsonNode feedNode = rootNode.get("feed"); if (feedNode != null) { JsonNode moviesNode = feedNode.get("movie"); if (moviesNode != null && moviesNode.size() > 0) { for (int i = 0; i < moviesNode.size(); i++) { JsonNode movieNode = moviesNode.get(i); Movie movie = new Movie(); movie.setCode(getValueAsInt(movieNode.get("code"))); movie.setTitle(getValueAsString(movieNode.get("title"))); movie.setOriginalTitle(getValueAsString(movieNode.get("originalTitle"))); movie.setProductionYear(getValueAsString(movieNode.get("productionYear"))); // parse poster movie.setPoster(parsePosterType(movieNode)); // enough values for search result search.getMovie().add(movie); } // NOTE: pagination not supported; so totalsResults may be higher than count search.setTotalResults(getValueAsInt(feedNode.get("totalResults"))); } } return search; }
From source file:com.tda.sitefilm.allocine.JSONAllocineAPIHelper.java
public Search searchTvseriesInfos(String query) throws Exception { JsonNode rootNode = null;/* w w w . j a va2 s . c o m*/ InputStream inputStream = connectSearchTvseriesInfos(query).getInputStream(); try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(inputStream); } finally { closeInputStream(inputStream); } Search search = new Search(); if (rootNode == null) { return search; } JsonNode feedNode = rootNode.get("feed"); if (feedNode != null) { JsonNode tvseriesNode = feedNode.get("tvseries"); if (tvseriesNode != null && tvseriesNode.size() > 0) { for (int i = 0; i < tvseriesNode.size(); i++) { JsonNode tvserieNode = tvseriesNode.get(i); Tvseries tvseries = new Tvseries(); tvseries.setCode(getValueAsInt(tvserieNode.get("code"))); tvseries.setTitle(getValueAsString(tvserieNode.get("title"))); tvseries.setOriginalTitle(getValueAsString(tvserieNode.get("originalTitle"))); tvseries.setYearStart(getValueAsString(tvserieNode.get("yearStart"))); tvseries.setYearEnd(getValueAsString(tvserieNode.get("yearEnd"))); // enough values for search result search.getTvseries().add(tvseries); } // NOTE: pagination not supported; so totalsResults may be higher than count search.setTotalResults(getValueAsInt(feedNode.get("totalResults"))); } } return search; }