/** * Fetches the global configuration from Zookeeper. * @param zkClient The Zookeeper client. * @return The global configuration retrieved from Zookeeper. * @throws Exception */ private Map<String, Object> fetchGlobalConfig(CuratorFramework zkClient) throws Exception { byte[] raw = readGlobalConfigBytesFromZookeeper(zkClient); return JSONUtils.INSTANCE.load( new ByteArrayInputStream(raw), JSONUtils.MAP_SUPPLIER); }
/** * Transforms a bean (aka POJO) to a JSONObject. */ public JSONObject toJSONObject(Object o) throws JsonProcessingException, ParseException { return (JSONObject) _parser.get().parse(toJSON(o, false)); } }
/** * Loads any variables defined in an external file. * @param commandLine The command line arguments. * @param executor The stellar executor. * @throws IOException */ private static void loadVariables( CommandLine commandLine, StellarShellExecutor executor) throws IOException { if(commandLine.hasOption("v")) { // load variables defined in a file String variablePath = commandLine.getOptionValue("v"); Map<String, Object> variables = JSONUtils.INSTANCE.load( new File(variablePath), JSONUtils.MAP_SUPPLIER); // for each variable... for(Map.Entry<String, Object> kv : variables.entrySet()) { String variable = kv.getKey(); Object value = kv.getValue(); // define the variable - no expression available executor.assign(variable, value, Optional.empty()); } } }
public static void writeConfigToZookeeper(String name, Map<String, Object> config, String zookeeperUrl) throws Exception { writeConfigToZookeeper(name, JSONUtils.INSTANCE.toJSON(config), zookeeperUrl); }
@Override public Object apply(List<Object> strings) { if (strings == null || strings.size() == 0) { throw new IllegalArgumentException("[TO_JSON_OBJECT] incorrect arguments. Usage: TO_JSON_OBJECT <String>"); } String var = (strings.get(0) == null) ? null : (String) strings.get(0); if (var == null) { return null; } else if (var.length() == 0) { return var; } else { if (!(strings.get(0) instanceof String)) { throw new ParseException("Valid JSON string not supplied"); } // Return JSON Object try { return JSONUtils.INSTANCE.load((String) strings.get(0), Object.class); } catch (JsonProcessingException ex) { throw new ParseException("Valid JSON string not supplied", ex); } catch (IOException e) { e.printStackTrace(); } } return new ParseException("Unable to parse JSON string"); } }
public static void writeGlobalConfigToZookeeper(Map<String, Object> globalConfig, CuratorFramework client) throws Exception { writeGlobalConfigToZookeeper(JSONUtils.INSTANCE.toJSON(globalConfig), client); }
/** * Executes a given expression on a message. * * @param expr The filter expression to execute. * @param message The message that the expression is executed on. * @return Returns true, only if the expression returns true. If the expression * returns false or fails to execute, false is returned. */ public boolean isSatisfied(LambdaExpression expr, String message) { boolean result = false; Map<String, Object> messageAsMap; try { // transform the message to a map of fields messageAsMap = JSONUtils.INSTANCE.load(message, JSONUtils.MAP_SUPPLIER); // apply the filter expression Object out = expr.apply(Collections.singletonList(messageAsMap)); if(out instanceof Boolean) { result = (Boolean) out; } else { LOG.error("Expected boolean from filter expression, got {}", ClassUtils.getShortClassName(out, "null")); } } catch(IOException e) { LOG.error("Unable to parse message", e); } return result; }
@GET @Path("/apply") @Produces("application/json") public Response apply(@QueryParam("host") String host ) throws JsonProcessingException { Boolean b = isMalicious.get(host); boolean isMalicious = b != null && b; Map<String, Boolean> ret = new HashMap<String, Boolean>(); ret.put("is_malicious", isMalicious ); String resp = JSONUtils.INSTANCE.toJSON(ret, true); return Response.ok(resp, MediaType.APPLICATION_JSON_TYPE).build(); }
@Override public Object apply(List<Object> strings) { if (strings == null || strings.size() == 0) { throw new IllegalArgumentException("[TO_JSON_MAP] incorrect arguments. Usage: TO_JSON_MAP <JSON String>"); } String var = (strings.get(0) == null) ? null : (String) strings.get(0); if (var == null) { return null; } else if (var.length() == 0) { return var; } else { if (!(strings.get(0) instanceof String)) { throw new ParseException("Valid JSON string not supplied"); } // Return parsed JSON Object as a HashMap String in = (String)strings.get(0); try { return (Map)JSONUtils.INSTANCE.load(in, JSONUtils.MAP_SUPPLIER); } catch (JsonProcessingException ex) { throw new ParseException(String.format("%s is not a valid JSON string", in), ex); } catch (IOException ex) { throw new ParseException(String.format("%s is not a valid JSON string", in), ex); } catch (ClassCastException ex) { throw new ParseException(String.format("%s is not a valid JSON string, expected a map", in), ex); } } } }
@Override public Object apply(List<Object> strings) { if (strings == null || strings.size() == 0) { throw new IllegalArgumentException("[TO_JSON_LIST] incorrect arguments. Usage: TO_JSON_LIST <JSON String>"); } String var = (strings.get(0) == null) ? null : (String) strings.get(0); if (var == null) { return null; } else if (var.length() == 0) { return var; } else { if (!(strings.get(0) instanceof String)) { throw new ParseException("Valid JSON string not supplied"); } // Return parsed JSON Object as a List String in = (String)strings.get(0); try { return (List) JSONUtils.INSTANCE.load(in, JSONUtils.LIST_SUPPLIER); } catch (JsonProcessingException ex) { throw new ParseException(String.format("%s is not a valid JSON string", in), ex); } catch (IOException ex) { throw new ParseException(String.format("%s is not a valid JSON string", in), ex); } catch (ClassCastException ex) { throw new ParseException(String.format("%s is not a valid JSON string, expected a list", in), ex); } } } }
ret = JSONUtils.INSTANCE.load(results, JSONUtils.MAP_SUPPLIER); resultCache.put(cacheKey, ret); return ret;
return JSONUtils.INSTANCE.load(json, JSONUtils.MAP_SUPPLIER);
Map<String, Object> variables = new HashMap<>(); if (variablesFile.isPresent()) { variables = JSONUtils.INSTANCE.load(new FileInputStream(variablesFile.get()), JSONUtils.MAP_SUPPLIER);
@Test public void loads_file_with_typeref() throws Exception { Map<String, Object> expected = new HashMap<String, Object>() {{ put("a", "hello"); put("b", "world"); }}; Map<String, Object> actual = JSONUtils.INSTANCE.load(configFile, JSONUtils.MAP_SUPPLIER); Assert.assertThat("config not equal", actual, equalTo(expected)); }
@Test @SuppressWarnings("unchecked") public void loads_file_with_map_class() throws Exception { Map<String, Object> expected = new HashMap<String, Object>() {{ put("a", "hello"); put("b", "world"); }}; Map<String, Object> actual = JSONUtils.INSTANCE.load(configFile, Map.class); Assert.assertThat("config not equal", actual, equalTo(expected)); }
@Test public void loads_file_with_custom_class() throws Exception { TestConfig expected = new TestConfig().setA("hello").setB("world"); TestConfig actual = JSONUtils.INSTANCE.load(configFile, TestConfig.class); Assert.assertThat("a not equal", actual.getA(), equalTo(expected.getA())); Assert.assertThat("b not equal", actual.getB(), equalTo(expected.getB())); }