/** * Redacts the values stored for keys that contain the following: password, * secret, key, token, *credentials.*. * @param yml String containing a yaml. * @return redacted yaml String. */ public static String sanitizeYml(String yml) { String result = ""; try { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setPrettyFlow(true); Yaml yaml = new Yaml(options); Iterator<Object> iter = yaml.loadAll(yml).iterator(); while (iter.hasNext()) { Object o = iter.next(); if (o instanceof LinkedHashMap) { iterateLinkedHashMap((LinkedHashMap<String, Object>) o); } result += yaml.dump(o); } } catch (Throwable throwable) { logger.error("Unable to redact data from Manifest debug entry", throwable); } if (result == null || result.length() == 0) { result = yml; } return result; }
private YamlConversionResult convert(Map<String, Collection<String>> properties) { if (properties.isEmpty()) { return YamlConversionResult.EMPTY; } YamlBuilder root = new YamlBuilder(mode, keyspaceList, status, YamlPath.EMPTY); for (Entry<String, Collection<String>> e : properties.entrySet()) { for (String v : e.getValue()) { root.addProperty(YamlPath.fromProperty(e.getKey()), v); } } Object object = root.build(); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setPrettyFlow(true); Yaml yaml = new Yaml(options); String output = yaml.dump(object); return new YamlConversionResult(status, output); }
@Override protected Yaml initialValue() { Representer representer = new Representer() { { representers.put(Configuration.class, new Represent() { @Override public Node representData(Object data) { return represent(((Configuration) data).self); } }); } }; DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); return new Yaml(new Constructor(), representer, options); }
public YamlSnakeYaml() { // Representer ExtensibleRepresenter representer = new ExtensibleRepresenter(); // Install Java / Apache Cassandra serializers addDefaultSerializers(representer); // Install MongoDB / BSON serializers tryToAddSerializers("io.datatree.dom.adapters.YamlSnakeYamlBsonSerializers", representer); // Create flow-style YAML mapper DumperOptions optionsNormal = new DumperOptions(); optionsNormal.setDefaultFlowStyle(FlowStyle.FLOW); mapper = new Yaml(representer, optionsNormal); // Create "pretty" YAML mapper DumperOptions optionsPretty = new DumperOptions(); optionsPretty.setDefaultFlowStyle(FlowStyle.BLOCK); prettyMapper = new Yaml(representer, optionsPretty); }
private void dumpConfiguration(Path configFilePath) { try (FileWriter fileWriter = new FileWriter(configFilePath.toFile())) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Representer representer = new Representer() { @Override protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,Tag customTag) { // if value of property is null, ignore it. if (propertyValue == null) { return null; } else { return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); } } }; representer.addClassTag(Configuration.class, Tag.MAP); Yaml yaml = new Yaml(representer, options); yaml.dump(configuration, fileWriter); } catch (IOException e) { throw new RuntimeException("Failed to dump configuration in file " + configFilePath, e); } }
@SuppressWarnings("unchecked") @Override public void reload() { Config.createConfigFile(this.file); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); try { this.list = yaml.loadAs(Utils.readFile(file), Map.class); } catch (IOException e) { e.printStackTrace(); } if (this.list == null) { this.list = useSynchronization ? new Hashtable<>() : new HashMap<>(); } else { this.list = useSynchronization ? new Hashtable<>(this.list) : new HashMap<>(this.list); } }
@Override public String saveToString() { yamlOptions.setIndent(options().indent()); yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); yamlOptions.setAllowUnicode(SYSTEM_UTF); yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); String header = buildHeader(); String dump = yaml.dump(getValues(false)); if (dump.equals(BLANK_CONFIG)) { dump = ""; } return header + dump; }
public static Yaml newYaml() { PropertyUtils propertyUtils = new AdvancedPropertyUtils(); propertyUtils.setSkipMissingProperties(true); Constructor constructor = new Constructor(Federations.class); TypeDescription federationDescription = new TypeDescription(Federations.class); federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class); constructor.addTypeDescription(federationDescription); constructor.setPropertyUtils(propertyUtils); Representer representer = new AdvancedRepresenter(); representer.setPropertyUtils(new FieldOrderPropertyUtils()); representer.addClassTag(Federations.class, Tag.MAP); representer.addClassTag(AbstractMetaStore.class, Tag.MAP); representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP); representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP); representer.addClassTag(GraphiteConfiguration.class, Tag.MAP); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); return new Yaml(constructor, representer, dumperOptions); }
private static Yaml newYaml() { return new Yaml(new Constructor(), new Representer(), new DumperOptions(), new Resolver() { @Override public Tag resolve(NodeId kind, String value, boolean implicit) { if (value != null) { if (value.equalsIgnoreCase("on") || value.equalsIgnoreCase("off") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("no")) { return Tag.STR; } } return super.resolve(kind, value, implicit); } }); }
/** * !!timestamp must be used, without it the implicit tag will be ignored * because 'date' is the JavaBean property. * * Since the timestamp contains ':' character it cannot use plain scalar * style in the FLOW mapping style. Emitter suggests single quoted scalar * style and that is why the explicit '!!timestamp' is present in the YAML * document. * * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=128"></a> * */ public void testLoadBeanWithAutoFlow() { MyBean bean = new MyBean(); bean.setId("id123"); DateTime etalon = new DateTime(timestamp, DateTimeZone.UTC); bean.setDate(etalon); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); Yaml dumper = new Yaml(new JodaTimeRepresenter(), options); String doc = dumper.dump(bean); // System.out.println(doc); assertEquals( "!!examples.jodatime.MyBean {date: !!timestamp '2001-09-09T01:46:40Z', id: id123}\n", doc); Yaml loader = new Yaml(new JodaTimeImplicitContructor()); MyBean parsed = (MyBean) loader.load(doc); assertEquals(etalon, parsed.getDate()); }
@Override public String toString() { Map<String,Object> data = new LinkedHashMap<String,Object>() {{ if (asset != null) { put(ASSET, asset); } put(MAIN_CLASS, mainClass); put(HOLLOW, hollow); put(PROPERTIES, properties); put(MODULES, bootstrapModules); put(BOOTSTRAP_ARTIFACTS, bootstrapArtifacts); put(BUNDLE_DEPENDENCIES, bundleDependencies); put(DEPENDENCIES, dependencies); }}; DumperOptions options = new DumperOptions(); options.setPrettyFlow(true); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); return yaml.dump(data); }
public void testEmitWithTags() { TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml")); DumperOptions options = new DumperOptions(); options.setExplicitStart(true); Representer repr = new Representer(); repr.addClassTag(TestObject.class, new Tag("!ruby/object:Test::Module::Object")); repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1")); repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2")); Yaml yaml2 = new Yaml(repr, options); String output = yaml2.dump(result); // System.out.println(output); assertTrue("Tags must be present.", output.startsWith("--- !ruby/object:Test::Module::Object")); assertTrue("Tags must be present: " + output, output.contains("!ruby/object:Test::Module::Sub1")); assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2")); // parse back. TestObject result2 = parseObject(output); assertEquals(0, result2.getSub1().getAtt2()); assertEquals("MyString", result2.getSub2().getAtt1()); assertEquals(1, result2.getSub2().getAtt2().size()); assertEquals(12345, result2.getSub2().getAtt3()); }
public void testDate() { List<Date> list = new ArrayList<Date>(); list.add(new Date(1229684761159L)); list.add(new Date(1229684761059L)); list.add(new Date(1229684761009L)); list.add(new Date(1229684761150L)); list.add(new Date(1229684761100L)); list.add(new Date(1229684761000L)); list.add(new Date(1229684760000L)); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); String output = yaml.dump(list); assertEquals( "- 2008-12-19T11:06:01.159Z\n- 2008-12-19T11:06:01.059Z\n- 2008-12-19T11:06:01.009Z\n- 2008-12-19T11:06:01.150Z\n- 2008-12-19T11:06:01.100Z\n- 2008-12-19T11:06:01Z\n- 2008-12-19T11:06:00Z\n", output); }
public void testStyle2() { List<Integer> list = new ArrayList<Integer>(); list.add(new Integer(1)); list.add(new Integer(1)); Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("age", 5); map.put("name", "Ubuntu"); map.put("list", list); DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); Yaml yaml = new Yaml(options); String output = yaml.dump(map); assertEquals("{'age': !!int '5', 'name': 'Ubuntu', 'list': [!!int '1', !!int '1']}\n", output); }
public void testStyle2Pretty() { List<Integer> list = new ArrayList<Integer>(); list.add(new Integer(1)); list.add(new Integer(1)); Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("age", 5); map.put("name", "Ubuntu"); map.put("list", list); DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setPrettyFlow(true); Yaml yaml = new Yaml(options); String output = yaml.dump(map); assertEquals( "{\n 'age': !!int '5',\n 'name': 'Ubuntu',\n 'list': [\n !!int '1',\n !!int '1']\n \n}\n", output); }
/** * http://pyyaml.org/ticket/196 */ public void testEmitQuoted() { List<String> list = new ArrayList<String>(3); list.add("This is an 'example'."); list.add("This is an \"example\"."); list.add("123"); String output = dump(list); assertEquals("[This is an 'example'., This is an \"example\"., '123']\n", output); // single quoted DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED); Yaml yaml = new Yaml(options); String output2 = yaml.dump(list); // System.out.println(output2); assertEquals("- 'This is an ''example''.'\n- 'This is an \"example\".'\n- '123'\n", output2); // double quoted DumperOptions options2 = new DumperOptions(); options2.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); yaml = new Yaml(options2); String output3 = yaml.dump(list); // System.out.println(output2); assertEquals("- \"This is an 'example'.\"\n- \"This is an \\\"example\\\".\"\n- \"123\"\n", output3); }
public void testCustomGenerator() { List<Object> list = new ArrayList<Object>(); list.add("data123"); list.add(list); Yaml yaml1 = new Yaml(); String output = yaml1.dump(list); assertEquals("&id001\n" + "- data123\n" + "- *id001\n", output); DumperOptions options = new DumperOptions(); Yaml yaml2 = new Yaml(options); options.setAnchorGenerator(new Gener(3)); String output2 = yaml2.dump(list); assertEquals("&list-id004\n" + "- data123\n" + "- *list-id004\n", output2); }
public Yaml createYamlReader() { return new Yaml(new Constructor(), new Representer(), new DumperOptions(), new Resolver() { @Override protected void addImplicitResolvers() { // Intentionally left TIMESTAMP as string to let DBUnit deal with the conversion addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO"); addImplicitResolver(Tag.INT, INT, "-+0123456789"); addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789."); addImplicitResolver(Tag.MERGE, MERGE, "<"); addImplicitResolver(Tag.NULL, NULL, "~nN\0"); addImplicitResolver(Tag.NULL, EMPTY, null); addImplicitResolver(Tag.VALUE, VALUE, "="); addImplicitResolver(Tag.YAML, YAML, "!&*"); } }); }
public void testSplitLineExpectFirstFlowSequenceItem() { DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setWidth(8); Yaml yaml; String output; Map<String, Object> map = new TreeMap<String, Object>(); map.put("12345", Arrays.asList("1111111111")); // Split lines enabled (default) yaml = new Yaml(options); output = yaml.dump(map); assertEquals("{\"12345\": [\n \"1111111111\"]}\n", output); // Split lines disabled options.setSplitLines(false); assertFalse(options.getSplitLines()); yaml = new Yaml(options); output = yaml.dump(map); assertEquals("{\"12345\": [\"1111111111\"]}\n", output); }
public void testSplitLineExpectFlowSequenceItem() { DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setWidth(8); Yaml yaml; String output; // Split lines enabled (default) yaml = new Yaml(options); output = yaml.dump(Arrays.asList("1111111111", "2222222222")); assertEquals("[\"1111111111\",\n \"2222222222\"]\n", output); output = yaml.dump(Arrays.asList("1", "2")); assertEquals("[\"1\", \"2\"]\n", output); // Split lines disabled options.setSplitLines(false); assertFalse(options.getSplitLines()); yaml = new Yaml(options); output = yaml.dump(Arrays.asList("1111111111", "2222222222")); assertEquals("[\"1111111111\", \"2222222222\"]\n", output); output = yaml.dump(Arrays.asList("1", "2")); assertEquals("[\"1\", \"2\"]\n", output); }
@SuppressWarnings("deprecation") @Override public String saveToString() { yamlOptions.setIndent(options().indent()); yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); yamlOptions.setAllowUnicode(SYSTEM_UTF); yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); String header = buildHeader(); String dump = yaml.dump(getValues(false)); if (dump.equals(BLANK_CONFIG)) dump = ""; return header + dump; }
protected final String dumpMap(LinkedHashMap<Object, Object> toStore, String header) { DumperOptions dumperOptions = new DumperOptions(); toStore = cleanMap(toStore); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); dumperOptions.setAllowUnicode(true); Yaml rl = new Yaml(dumperOptions); StringWriter stringWriter = new StringWriter(); BufferedWriter ws = new BufferedWriter(stringWriter); try { ws.write(header); ws.newLine(); rl.dump(toStore, ws); return stringWriter.toString().replaceAll(NOVALUE, ""); } catch (Exception e) { throw new RuntimeException(e); } }
@Autowired public ManifestCommands(SkipperClient skipperClient) { this.skipperClient = skipperClient; DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); dumperOptions.setPrettyFlow(true); this.yaml = new Yaml(dumperOptions); }
@Test public void testYamlMerge() throws IOException { DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); dumperOptions.setPrettyFlow(true); Yaml yaml = new Yaml(dumperOptions); Resource resource = new ClassPathResource("/org/springframework/cloud/skipper/server/service/ticktock-1.0.0"); Package pkg = this.packageReader.read(resource.getFile()); ConfigValues configValues = new ConfigValues(); Map<String, Object> configValuesMap = new TreeMap<>(); Map<String, Object> logMap = new TreeMap<>(); logMap.put("appVersion", "1.2.1.RELEASE"); configValuesMap.put("log", logMap); configValuesMap.put("hello", "universe"); String configYaml = yaml.dump(configValuesMap); configValues.setRaw(configYaml); Map<String, Object> mergedMap = ConfigValueUtils.mergeConfigValues(pkg, configValues); String mergedYaml = yaml.dump(mergedMap); String expectedYaml = StreamUtils.copyToString( TestResourceUtils.qualifiedResource(getClass(), "merged.yaml").getInputStream(), Charset.defaultCharset()); assertThat(mergedYaml).isEqualTo(expectedYaml); }
private Package createSimplePackage() throws IOException { Package pkg = new Package(); // Add package metadata PackageMetadata packageMetadata = new PackageMetadata(); packageMetadata.setName("myapp"); packageMetadata.setVersion("1.0.0"); packageMetadata.setMaintainer("bob"); pkg.setMetadata(packageMetadata); // Add ConfigValues Map<String, String> map = new HashMap<>(); map.put("foo", "bar"); map.put("fiz", "faz"); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); dumperOptions.setPrettyFlow(true); Yaml yaml = new Yaml(dumperOptions); ConfigValues configValues = new ConfigValues(); configValues.setRaw(yaml.dump(map)); pkg.setConfigValues(configValues); // Add template Resource resource = new ClassPathResource("/org/springframework/cloud/skipper/io/generic-template.yml"); String genericTempateData = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); Template template = new Template(); template.setData(genericTempateData); template.setName(resource.getURL().toString()); List<Template> templateList = new ArrayList<>(); templateList.add(template); pkg.setTemplates(templateList); return pkg; }
@Override public void reloadConfig() { this.config = new Config(this.configFile); InputStream configStream = this.getResource("config.yml"); if (configStream != null) { DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); try { this.config.setDefault(yaml.loadAs(Utils.readFile(this.configFile), LinkedHashMap.class)); } catch (IOException e) { Server.getInstance().getLogger().logException(e); } } }
public boolean save(Boolean async) { if (this.file == null) throw new IllegalStateException("Failed to save Config. File object is undefined."); if (this.correct) { String content = ""; switch (this.type) { case Config.PROPERTIES: content = this.writeProperties(); break; case Config.JSON: content = new GsonBuilder().setPrettyPrinting().create().toJson(this.config); break; case Config.YAML: DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); content = yaml.dump(this.config); break; case Config.ENUM: for (Object o : this.config.entrySet()) { Map.Entry entry = (Map.Entry) o; content += String.valueOf(entry.getKey()) + "\r\n"; } break; } if (async) { Server.getInstance().getScheduler().scheduleAsyncTask(new FileWriteTask(this.file, content)); } else { try { Utils.writeFile(this.file, content); } catch (IOException e) { Server.getInstance().getLogger().logException(e); } } return true; } else { return false; } }
private void parseContent(String content) { switch (this.type) { case Config.PROPERTIES: this.parseProperties(content); break; case Config.JSON: GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); this.config = new ConfigSection(gson.fromJson(content, new TypeToken<LinkedHashMap<String, Object>>() { }.getType())); break; case Config.YAML: DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); this.config = new ConfigSection(yaml.loadAs(content, LinkedHashMap.class)); if (this.config == null) { this.config = new ConfigSection(); } break; // case Config.SERIALIZED case Config.ENUM: this.parseList(content); break; default: this.correct = false; } }
@Nonnull public static YAMLConfigurationLoader yaml(@Nonnull Path path) { return YAMLConfigurationLoader.builder() .setFlowStyle(DumperOptions.FlowStyle.BLOCK) .setIndent(2) .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8)) .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8)) .build(); }
@Nonnull public static YAMLConfigurationLoader yaml(@Nonnull File file) { Path path = file.toPath(); return YAMLConfigurationLoader.builder() .setFlowStyle(DumperOptions.FlowStyle.BLOCK) .setIndent(2) .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8)) .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8)) .build(); }
public void save() throws IOException { FileWriter writer = new FileWriter(getConfigFile()); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); options.setIndent(4); Representer representer = new Representer(); representer.getPropertyUtils().setBeanAccess(BeanAccess.DEFAULT); new Yaml(options).dump(this, writer); }
protected DumperOptions getDumperOptions() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setPrettyFlow(true); options.setIndent(2); return options; }
public Serializer(Emitable emitter, Resolver resolver, DumperOptions opts, Tag rootTag) { this.emitter = emitter; this.resolver = resolver; this.explicitStart = opts.isExplicitStart(); this.explicitEnd = opts.isExplicitEnd(); if (opts.getVersion() != null) { this.useVersion = opts.getVersion(); } this.useTags = opts.getTags(); this.serializedNodes = new HashSet<Node>(); this.anchors = new HashMap<Node, String>(); this.anchorGenerator = opts.getAnchorGenerator(); this.closed = null; this.explicitRoot = rootTag; }
private static Yaml getYaml() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); StringExRepresent representer = new StringExRepresent(); PropertyUtils propertyUtils = representer.getPropertyUtils(); propertyUtils.setSkipMissingProperties(true); return new Yaml(new StringExConstructor(), representer, options); }