Java Code Examples for org.codehaus.jackson.JsonNode

The following examples show how to use org.codehaus.jackson.JsonNode. These examples are extracted from open source projects. 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 Project: kardio   Source File: APIDashboardTask.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param envInfo
 * @param appName
 * @return
 * @throws Exception
 */
private static String getMarathonAppLaunchDate(EnvironmentVO envInfo,String appName) throws Exception {
	String appVerUrl = envInfo.getMarathonUrl()+"//" + appName + "/versions";
	String appVersionJson = null;
	try{
		appVersionJson = RestCommunicationHandler.getResponse(appVerUrl, true, "Basic ", envInfo.getMarathonCred());
	}catch(FileNotFoundException ex){
		logger.error("Unable to get Version of API : AppName: " + appName + "; URL :"+ appVerUrl, ex);
	}

	if(appVersionJson == null){
		return null;
	}
	ObjectMapper mapper = new ObjectMapper();
	JsonNode rootNode = mapper.readTree(appVersionJson).get("versions");
	String launchDate = null; 
	if(rootNode.isArray()){
		launchDate = rootNode.get(rootNode.size() - 1).asText();
	} else {
		logger.info("Launch Date for "+ appName +" is null");
	}
	return launchDate;
}
 
Example 2
Source Project: helix   Source File: TestWorkflowAccessor.java    License: Apache License 2.0 6 votes vote down vote up
@Test(dependsOnMethods = "testGetWorkflows")
public void testGetWorkflow() throws IOException {
  System.out.println("Start test :" + TestHelper.getTestMethodName());
  String body = get("clusters/" + CLUSTER_NAME + "/workflows/" + WORKFLOW_NAME, null,
      Response.Status.OK.getStatusCode(), true);
  JsonNode node = OBJECT_MAPPER.readTree(body);
  Assert.assertNotNull(node.get(WorkflowAccessor.WorkflowProperties.WorkflowConfig.name()));
  Assert.assertNotNull(node.get(WorkflowAccessor.WorkflowProperties.WorkflowContext.name()));

  TaskExecutionInfo lastScheduledTask = OBJECT_MAPPER
      .treeToValue(node.get(WorkflowAccessor.WorkflowProperties.LastScheduledTask.name()),
          TaskExecutionInfo.class);
  Assert.assertTrue(lastScheduledTask
      .equals(new TaskExecutionInfo(null, null, null, TaskExecutionInfo.TIMESTAMP_NOT_SET)));
  String workflowId =
      node.get(WorkflowAccessor.WorkflowProperties.WorkflowConfig.name()).get("WorkflowID")
          .getTextValue();
  Assert.assertEquals(workflowId, WORKFLOW_NAME);
  System.out.println("End test :" + TestHelper.getTestMethodName());
}
 
Example 3
Source Project: Cubert   Source File: MaxAggregation.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BlockSchema outputSchema(BlockSchema inputSchema, JsonNode json) throws PreconditionException
{
    String[] inputColNames = JsonUtils.asArray(json, "input");
    if (inputColNames.length != 1)
        throw new PreconditionException(PreconditionExceptionType.INVALID_CONFIG,
                                        "Only one column expected for MAX. Found: "
                                                + JsonUtils.get(json, "input"));

    String inputColName = inputColNames[0];
    if (!inputSchema.hasIndex(inputColName))
        throw new PreconditionException(PreconditionExceptionType.COLUMN_NOT_PRESENT,
                                        inputColName);

    String outputColName = JsonUtils.getText(json, "output");
    DataType inputType = inputSchema.getType(inputSchema.getIndex(inputColName));

    return new BlockSchema(inputType + " " + outputColName);
}
 
Example 4
Source Project: DataSphereStudio   Source File: FlowRestfulApi.java    License: Apache License 2.0 6 votes vote down vote up
@POST
@Path("/updateFlowBaseInfo")
public Response updateFlowBaseInfo(@Context HttpServletRequest req, JsonNode json) throws DSSErrorException {
    Long flowID = json.get("id").getLongValue();
    String name = json.get("name")==null?null:json.get("name").getTextValue();
    String description = json.get("description") == null?null:json.get("description").getTextValue();
    Long taxonomyID = json.get("taxonomyID") == null?null:json.get("taxonomyID").getLongValue();
    Long projectVersionID = json.get("projectVersionID").getLongValue();
    String uses = json.get("uses") == null?null:json.get("uses").getTextValue();
    publishManager.checkeIsPublishing(projectVersionID);
    // TODO: 2019/6/13  projectVersionID的更新校验
    //这里可以不做事务
    DWSFlow dwsFlow = new DWSFlow();
    dwsFlow.setId(flowID);
    dwsFlow.setName(name);
    dwsFlow.setDescription(description);
    dwsFlow.setUses(uses);
    flowService.updateFlowBaseInfo(dwsFlow,projectVersionID,taxonomyID);
    return Message.messageToResponse(Message.ok());
}
 
Example 5
Source Project: guacamole-client   Source File: LanguageResourceService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given language resource, returning the resulting JsonNode.
 * If the resource cannot be read because it does not exist, null is
 * returned.
 *
 * @param resource
 *     The language resource to parse. Language resources must have the
 *     mimetype "application/json".
 *
 * @return
 *     A JsonNode representing the root of the parsed JSON tree, or null if
 *     the given resource does not exist.
 *
 * @throws IOException
 *     If an error occurs while parsing the resource as JSON.
 */
private JsonNode parseLanguageResource(Resource resource) throws IOException {

    // Get resource stream
    InputStream stream = resource.asStream();
    if (stream == null)
        return null;

    // Parse JSON tree
    try {
        JsonNode tree = mapper.readTree(stream);
        return tree;
    }

    // Ensure stream is always closed
    finally {
        stream.close();
    }

}
 
Example 6
Source Project: Cubert   Source File: DefaultCubeAggregator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setup(Block block, BlockSchema outputSchema, JsonNode json) throws IOException
{
    BlockSchema inputSchema = block.getProperties().getSchema();

    // determine the input column name and index within the input tuple
    String inColName = null;
    if (json.has("input") && !json.get("input").isNull())
        inColName = JsonUtils.asArray(json, "input")[0];

    if (inColName != null)
        valueIndex = inputSchema.getIndex(inColName);
    else
        valueIndex = -1;

    // determine the name of output column name the index within the output tuple
    String outColName = JsonUtils.getText(json, "output");
    outIndex = outputSchema.getIndex(outColName);
}
 
Example 7
Source Project: components   Source File: Oauth2JwtClientTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOK() throws UnsupportedEncodingException, IOException, InterruptedException, URISyntaxException {
    // start server
    JwtTestServer server = new JwtTestServer(UseCase.OK);
    Thread serverThread = new Thread(server);
    serverThread.start();

    Map<String, String> params = new HashMap<>();
    params.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");

    // Get Access Token
    JsonNode accessToken = Oauth2JwtClient.builder()//
            .withJwsHeaders(jwsHeaders())//
            .withJwtClaims(claims())//
            .signWithX509Key(x509Key(), org.talend.components.common.oauth.X509Key.Algorithm.SHA256withRSA)//
            .fromTokenEndpoint("http://" + endpoint + ":" + server.getLocalPort())//
            .withPlayloadParams(params)//
            .build()//
            .getAccessToken();

    serverThread.join(30000);

    assertNotNull(accessToken);
    assertNotNull(accessToken.get("access_token"));
}
 
Example 8
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	String[] exchanges = { "cny", "btc" };
	String addr = "http://api.btc38.com/v1/ticker.php?c=all&mk_type=";
	for(String exch : exchanges) {
		JsonNode node = readJsonFromUrl(addr + exch);
		Iterator<String> coins = node.getFieldNames();
		for(String coin; coins.hasNext();) {
			coin = coins.next();
			if(node.get(coin).get("ticker").isObject()) {
				pairs.add(new Pair(coin.toUpperCase(), exch.toUpperCase()));
			}
		}
	}
	return pairs;
}
 
Example 9
Source Project: flink-examples   Source File: TopTweet.java    License: MIT License 6 votes vote down vote up
@Override
public void flatMap(String tweetJsonStr, Collector<Tuple2<String, Integer>> collector) throws Exception {
    JsonNode tweetJson = mapper.readTree(tweetJsonStr);
    JsonNode entities = tweetJson.get("entities");
    if (entities == null) return;

    JsonNode hashtags = entities.get("hashtags");
    if (hashtags == null) return;

    for (Iterator<JsonNode> iter = hashtags.getElements(); iter.hasNext();) {
        JsonNode node = iter.next();
        String hashtag = node.get("text").getTextValue();

        if (hashtag.matches("\\w+")) {
            collector.collect(new Tuple2<>(hashtag, 1));
        }
    }
}
 
Example 10
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	JsonNode node = readJsonFromUrl("https://api.kraken.com/0/public/AssetPairs");
	if(node.get("error").getElements().hasNext())
		throw new IOException(node.get("error").get(0).asText());
	else {
		String coin;
		for(JsonNode asset : node.get("result")) {
			coin = asset.get("base").asText();
			if(coin.charAt(0) == 'X')
				coin = coin.substring(1);
			pairs.add(new Pair(coin, asset.get("quote").asText().substring(1)));
		}
	}
	return pairs;
}
 
Example 11
@Test
public void testSuccessWithString()
    throws DataConversionException, IOException {
  ObjectMapper objectMapper = new ObjectMapper();
  String innerRecord = "abracadabra";
  RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata();
  RecordWithMetadata<String> record = new RecordWithMetadata<>(innerRecord, new Metadata());

  Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator =
      converter.convertRecord("", record, null).iterator();
  RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next();

  JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class);
  Assert.assertEquals(parsedElement.get("mId").getTextValue(), record.getMetadata().getGlobalMetadata().getId());
  Assert.assertEquals(parsedElement.get("r").getTextValue(), innerRecord);

  Assert
      .assertEquals(recordWithMetadata.getMetadata().getGlobalMetadata().getContentType(), "lnkd+recordWithMetadata");
  Assert.assertNull(recordWithMetadata.getMetadata().getGlobalMetadata().getInnerContentType());
}
 
Example 12
@Test
public void TestPointWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "shape");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "binary");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"properties":{},"geometry":{"type":"Point","coordinates":[15.0,5.0]}}
       addWritable(stuff, new Point(15.0, 5.0));
	Writable jsw = jserde.serialize(stuff, rowOI);
       String rslt = ((Text)jsw).toString();
	JsonNode jn = new ObjectMapper().readTree(rslt);
	jn = jn.findValue("geometry");
	Assert.assertNotNull(jn.findValue("type"));
	Assert.assertNotNull(jn.findValue("coordinates"));
}
 
Example 13
Source Project: Cubert   Source File: RankOperator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see com.linkedin.rcf.operator.TupleOperator#setInput(java.util.Map,
 *      org.codehaus.jackson.JsonNode)
 */
@Override
public void setInput(Map<String, Block> input, JsonNode json, BlockProperties props) throws IOException,
InterruptedException
{
    inputBlock = input.values().iterator().next();
    BlockSchema inputSchema = inputBlock.getProperties().getSchema();

    outputSchema = getOutputSchema(json, inputSchema);
    outputTuple = TupleFactory.getInstance().newTuple(outputSchema.getNumColumns());
    rankColumnIndex = outputSchema.getNumColumns() - 1;

    pivotBlockOp = new PivotBlockOperator();
    String[] groupByColumns = JsonUtils.asArray(JsonUtils.get(json, "groupBy"));
    pivotBlockOp.setInput(inputBlock, groupByColumns, false);

    resetState();
}
 
Example 14
Source Project: Cubert   Source File: ShuffleRewriter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public JsonNode rewrite(JsonNode plan,
                        Set<String> namesUsed,
                        boolean debugMode,
                        boolean revisit)
{
    this.namesUsed = namesUsed;

    ObjectNode newPlan = (ObjectNode) cloneNode(plan);

    ArrayNode jobs = mapper.createArrayNode();

    for (JsonNode job : plan.path("jobs"))
    {
        jobs.add(rewriteJob(job));
    }

    newPlan.remove("jobs");
    newPlan.put("jobs", jobs);

    return newPlan;
}
 
Example 15
Source Project: ambari-shell   Source File: BlueprintCommandsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAddBlueprintForFileReadPrecedence() throws IOException {
  File file = new File("src/test/resources/testBlueprint.json");
  String json = IOUtils.toString(new FileInputStream(file));
  JsonNode jsonNode = mock(JsonNode.class);
  when(objectMapper.readTree(json.getBytes())).thenReturn(jsonNode);
  when(jsonNode.get("Blueprints")).thenReturn(jsonNode);
  when(jsonNode.get("blueprint_name")).thenReturn(jsonNode);
  when(jsonNode.asText()).thenReturn("blueprintName");

  String result = blueprintCommands.addBlueprint("url", file);

  verify(ambariClient).addBlueprint(json);
  verify(context).setHint(Hints.BUILD_CLUSTER);
  verify(context).setBlueprintsAvailable(true);
  assertEquals("Blueprint: 'blueprintName' has been added", result);
}
 
Example 16
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://api.gatecoin.com/Public/Transactions/BTCEUR?count=1
	JsonNode node = readJsonFromUrl("https://api.gatecoin.com/Public/Transactions/" + pair.getCoin() + pair.getExchange() + "?count=1");
	String message = node.get("responseStatus").get("message").asText();
	if(message.equals("OK"))
		return parseTicker(node, pair);
	else
		throw new IOException(message);
}
 
Example 17
Source Project: Cubert   Source File: SemanticAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JsonNode rewrite(JsonNode plan,
                        Set<String> namesUsed,
                        boolean debugMode,
                        boolean revisit) throws IOException
{
    this.revisit = revisit;
    this.debugMode = debugMode;
    new PhysicalPlanWalker(plan, this).walk();
    return hasErrors ? null : plan;
}
 
Example 18
Source Project: incubator-gobblin   Source File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert CSV record(List<Strings>) to JsonObject using header(column Names)
 * @param header record
 * @param data record
 * @param column Count
 * @return JsonObject
 */
public static JsonObject csvToJsonObject(List<String> bulkRecordHeader, List<String> record, int columnCount) {
  ObjectMapper mapper = new ObjectMapper();
  Map<String, String> resultInfo = new HashMap<>();
  for (int i = 0; i < columnCount; i++) {
    resultInfo.put(bulkRecordHeader.get(i), record.get(i));
  }

  JsonNode json = mapper.valueToTree(resultInfo);
  JsonElement element = GSON.fromJson(json.toString(), JsonObject.class);
  return element.getAsJsonObject();
}
 
Example 19
Source Project: kardio   Source File: KubernetesBackUpTask.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This function returns the Health check url for the service
 * @param ingressNode
 * @param healthCheckPort
 * @param pathValue
 * @return
 */
private static String getHealthCheckUrl(JsonNode ingressNode, String healthCheckPort, String pathValue) {
	// TODO Auto-generated method stub
	 JsonNode metadataNode = ingressNode.get("metadata");
     JsonNode annotNode = metadataNode.get("annotations");
     JsonNode namespaceNode = metadataNode.get("namespace");
     JsonNode nameNode = metadataNode.get("name");
     String ingressName = nameNode.asText();
     String namespace = namespaceNode.asText();
     /*The below block of code is for checking path based routing
      *CASE:: We are using nginx controller,below is the logic to find the app is using path based routing or not
      * If the "nginx.ingress.kubernetes.io/rewrite-target" is present in the annotation then app has path based routing */
     boolean isPathNeeded = false;
     String schemeValue = null;
        if(annotNode == null){
        	logger.info("The annotations node is null for the Ingress - "+ingressName+" in the namespace - "+ namespace);
        	isPathNeeded = false;
        }else{
        	isPathNeeded = annotNode.get("nginx.ingress.kubernetes.io/rewrite-target") == null? false : true;
        	schemeValue = annotNode.get("nginx.ingress.kubernetes.io/ssl-passthrough") == null ? null : "https";
        }
        
        JsonNode specIngNode = ingressNode.get("spec");
    	 JsonNode tlsNode = specIngNode.get("tls");
    	 if(schemeValue == null){
     		schemeValue = tlsNode == null ? "http" : "https";
     	 }
    	 String hostUrlPath = null;
        hostUrlPath =  getHostUrlAndPath(specIngNode, healthCheckPort, isPathNeeded);
        if(hostUrlPath == null){
       	 return null;
        }
	return schemeValue+ "://" + hostUrlPath + pathValue;
}
 
Example 20
@Test
public void testParseTicker() {
	try {
		Pair pair = new Pair("BTC", "PHP");
		JsonNode node = (new ObjectMapper().readTree(new File("src/test/json/btcexchange-ticker.json")));
		String lastValue = testExchange.parseTicker(node, pair);
		Assert.assertEquals("11794.29", lastValue);
	} catch(IOException e) {
		Assert.fail();
	}
}
 
Example 21
@Test
public void testParseTicker() {
	try {
		Pair pair = new Pair("BTC", "USD");
		JsonNode node = (new ObjectMapper().readTree(new File(
				"src/test/json/bittrex-ticker.json")));
		String lastValue = testExchange.parseTicker(node, pair);
		Assert.assertEquals("0.01290501", lastValue);
	}
	catch (IOException e) {
		e.printStackTrace();
		Assert.fail();
	}
}
 
Example 22
@Override
protected String getTicker(Pair pair) throws IOException {
	JsonNode node = readJsonFromUrl("https://www.btcbox.co.jp/coin/allcoin");
	if(node.has(pair.getCoin().toLowerCase()))
		return parseTicker(node, pair);
	else
		throw new IOException("Invalid pair: " + pair);
}
 
Example 23
Source Project: commerce-cif-connector   Source File: CommerceTestBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the PID of a service based on the factory PID.
 * 
 * @param osgiClient
 * @return The PID of the first configuration found for factory PID.
 * @throws ClientException
 */
private static String getConfigurationPid(OsgiConsoleClient osgiClient, String factoryPID) throws ClientException {
    SlingHttpResponse resp = osgiClient.doGet(CONFIGURATION_CONSOLE_URL + "/*.json");
    JsonNode json = JsonUtils.getJsonNodeFromString(resp.getContent());
    Iterator<JsonNode> it = json.getElements();
    while (it.hasNext()) {
        JsonNode config = it.next();
        JsonNode factoryId = config.get("factoryPid");
        if (factoryId != null && factoryPID.equals(factoryId.getTextValue())) {
            return config.get("pid").getTextValue();
        }
    }
    return null;
}
 
Example 24
@Test
public void testParseTicker() {
	try {
		Pair pair = new Pair("BTC", "EUR");
		JsonNode node = (new ObjectMapper().readTree(new File("src/test/json/zaif-ticker.json")));
		String lastValue = testExchange.parseTicker(node, pair);
		Assert.assertEquals("61235.0", lastValue);
	} catch (IOException e) {
		e.printStackTrace();
		Assert.fail();
	}
}
 
Example 25
Source Project: jwala   Source File: JsonControlGroup.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public JsonControlGroup deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException {

    final ObjectCodec obj = jp.getCodec();
    final JsonNode rootNode = obj.readTree(jp);
    final JsonNode operation = rootNode.get("controlOperation");

    return new JsonControlGroup(operation.getTextValue());
}
 
Example 26
@Override
protected String getTicker(Pair pair) throws IOException {
	if(!PAIRS.contains(pair)) {
		throw new IOException("Invalid pair: " + pair);
	}
	JsonNode node = readJsonFromUrl("https://bitex.la/api-v1/rest/btc/market/ticker");
	return parseTicker(node, pair);
}
 
Example 27
@Override
protected String getTicker(Pair pair) throws IOException {
	JsonNode node = readJsonFromUrl("https://api.bitt.com:8400/ajax/v1/GetTicker",
			"{\"productPair\":\"" + pair.getCoin() + pair.getExchange() + "\"}");
	if(node.get("isAccepted").asBoolean())
		return parseTicker(node, pair);
	else
		throw new IOException("Invalid pair: " + pair);
}
 
Example 28
Source Project: urule   Source File: ReteNodeJsonDeserializer.java    License: Apache License 2.0 5 votes vote down vote up
private SimpleArithmetic parseSimpleArithmetic(JsonNode node){
	JsonNode arithNode=node.get("arithmetic");
	if(arithNode==null){
		return null;
	}
	SimpleArithmetic arith=new SimpleArithmetic();
	arith.setType(ArithmeticType.valueOf(JsonUtils.getJsonValue(arithNode, "type")));
	arith.setValue(parseSimpleArithmeticValue(arithNode));
	return arith;
}
 
Example 29
Source Project: hbase-indexer   Source File: JsonUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] getBinary(JsonNode node, String prop) throws JsonFormatException {
    if (node.get(prop) == null) {
        throw new JsonFormatException("Missing required property: " + prop);
    }
    try {
        return node.get(prop).getBinaryValue();
    } catch (IOException e) {
        throw new JsonFormatException("Error reading binary data in property " + prop, e);
    }
}
 
Example 30
private List<Object> processArray(final String key, final ArrayNode asJsonArray) {
    List<Object> list = new LinkedList<Object>();
    
    Iterator<JsonNode> it = asJsonArray.getElements();
    while (it.hasNext()) {
        list.add(processElement(key, it.next()));
    }
    return list;
}