Search in sources :

Example 41 with Yaml

use of org.yaml.snakeyaml.Yaml in project nifi-minifi by apache.

the class SchemaLoader method loadYamlAsMap.

public static Map<String, Object> loadYamlAsMap(InputStream sourceStream) throws IOException, SchemaLoaderException {
    try {
        Yaml yaml = new Yaml();
        // Parse the YAML file
        final Object loadedObject = yaml.load(sourceStream);
        // Verify the parsed object is a Map structure
        if (loadedObject instanceof Map) {
            return (Map<String, Object>) loadedObject;
        } else {
            throw new SchemaLoaderException("Provided YAML configuration is not a Map");
        }
    } catch (YAMLException e) {
        throw new IOException(e);
    } finally {
        sourceStream.close();
    }
}
Also used : YAMLException(org.yaml.snakeyaml.error.YAMLException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) SchemaLoaderException(org.apache.nifi.minifi.commons.schema.exception.SchemaLoaderException)

Example 42 with Yaml

use of org.yaml.snakeyaml.Yaml in project nifi-minifi by apache.

the class PullHttpChangeIngestor method run.

@Override
public void run() {
    try {
        logger.debug("Attempting to pull new config");
        HttpUrl.Builder builder = new HttpUrl.Builder().host(hostReference.get()).port(portReference.get()).encodedPath(pathReference.get());
        String query = queryReference.get();
        if (!StringUtil.isNullOrEmpty(query)) {
            builder = builder.encodedQuery(query);
        }
        final HttpUrl url = builder.scheme(connectionScheme).build();
        final Request.Builder requestBuilder = new Request.Builder().get().url(url);
        if (useEtag) {
            requestBuilder.addHeader("If-None-Match", lastEtag);
        }
        final Request request = requestBuilder.build();
        final OkHttpClient httpClient = httpClientReference.get();
        final Call call = httpClient.newCall(request);
        final Response response = call.execute();
        logger.debug("Response received: {}", response.toString());
        int code = response.code();
        if (code == NOT_MODIFIED_STATUS_CODE) {
            return;
        }
        if (code >= 400) {
            throw new IOException("Got response code " + code + " while trying to pull configuration: " + response.body().string());
        }
        ResponseBody body = response.body();
        if (body == null) {
            logger.warn("No body returned when pulling a new configuration");
            return;
        }
        ByteBuffer bodyByteBuffer = ByteBuffer.wrap(body.bytes());
        ByteBuffer readOnlyNewConfig = null;
        // checking if some parts of the configuration must be preserved
        if (overrideSecurity) {
            readOnlyNewConfig = bodyByteBuffer.asReadOnlyBuffer();
        } else {
            logger.debug("Preserving previous security properties...");
            // get the current security properties from the current configuration file
            final File configFile = new File(properties.get().getProperty(RunMiNiFi.MINIFI_CONFIG_FILE_KEY));
            ConvertableSchema<ConfigSchema> configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new FileInputStream(configFile));
            ConfigSchema currentSchema = configSchema.convert();
            SecurityPropertiesSchema secProps = currentSchema.getSecurityProperties();
            // override the security properties in the pulled configuration with the previous properties
            configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new ByteBufferInputStream(bodyByteBuffer.duplicate()));
            ConfigSchema newSchema = configSchema.convert();
            newSchema.setSecurityProperties(secProps);
            // return the updated configuration preserving the previous security configuration
            readOnlyNewConfig = ByteBuffer.wrap(new Yaml().dump(newSchema.toMap()).getBytes()).asReadOnlyBuffer();
        }
        if (differentiator.isNew(readOnlyNewConfig)) {
            logger.debug("New change received, notifying listener");
            configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
            logger.debug("Listeners notified");
        } else {
            logger.debug("Pulled config same as currently running.");
        }
        if (useEtag) {
            lastEtag = (new StringBuilder("\"")).append(response.header("ETag").trim()).append("\"").toString();
        }
    } catch (Exception e) {
        logger.warn("Hit an exception while trying to pull", e);
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) ByteBufferInputStream(org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpUrl(okhttp3.HttpUrl) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) SecurityPropertiesSchema(org.apache.nifi.minifi.commons.schema.SecurityPropertiesSchema) File(java.io.File) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Example 43 with Yaml

use of org.yaml.snakeyaml.Yaml in project mxgwd by kamax-io.

the class YamlConfigLoader method loadFromFile.

public static Config loadFromFile(String path) throws IOException {
    Representer rep = new Representer();
    rep.getPropertyUtils().setAllowReadOnlyProperties(true);
    rep.getPropertyUtils().setSkipMissingProperties(true);
    Yaml yaml = new Yaml(new Constructor(Config.class), rep);
    Object o = yaml.load(new FileInputStream(path));
    return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), Config.class);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) Constructor(org.yaml.snakeyaml.constructor.Constructor) Config(io.kamax.mxgwd.config.Config) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Example 44 with Yaml

use of org.yaml.snakeyaml.Yaml in project rxlib by RockyLOMO.

the class App method readSettings.

@SneakyThrows
public static Map<String, Object> readSettings(String yamlFile, boolean isResource) {
    Map<String, Object> result = null;
    Yaml yaml = new Yaml(new SafeConstructor());
    for (Object data : yaml.loadAll(isResource ? getClassLoader().getResourceAsStream(yamlFile) : new FileInputStream(yamlFile))) {
        Map<String, Object> map = (Map<String, Object>) data;
        if (result == null) {
            result = map;
            continue;
        }
        result.putAll(map);
    }
    if (result == null) {
        result = new HashMap<>();
    }
    return result;
}
Also used : SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) Yaml(org.yaml.snakeyaml.Yaml) SneakyThrows(lombok.SneakyThrows)

Example 45 with Yaml

use of org.yaml.snakeyaml.Yaml in project watchdog by isdream.

the class ConfigureTest method main.

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Configure config = new Configure();
    config.setKind(Constants.YAML_POD);
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(KubernetesListener.ALL_NAMESPACE, KubernetesListener.ALL_NAMESPACE);
    properties.put("test", "test");
    config.setProperties(properties);
    // List<Object> handlers = new ArrayList<Object>();
    // KubernetesNamespaceFilterHandler knf = new KubernetesNamespaceFilterHandler();
    // KubernetesKindAnalyzerHandler kka = new KubernetesKindAnalyzerHandler();
    // DefaultConsoleHandler dch = new DefaultConsoleHandler();
    // handlers.add(KubernetesNamespaceFilterHandler.class.getName());
    // handlers.add(KubernetesKindAnalyzerHandler.class.getName());
    // handlers.add(DefaultConsoleHandler.class.getName());
    // 
    // config.setHandlers(handlers);
    Yaml yaml = new Yaml();
    System.out.println(yaml.dump(config));
}
Also used : HashMap(java.util.HashMap) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)253 Map (java.util.Map)92 HashMap (java.util.HashMap)73 IOException (java.io.IOException)55 FileInputStream (java.io.FileInputStream)47 InputStream (java.io.InputStream)47 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)39 Constructor (org.yaml.snakeyaml.constructor.Constructor)27 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)21 List (java.util.List)21 Test (org.junit.Test)21 LinkedHashMap (java.util.LinkedHashMap)17 Writer (java.io.Writer)13 FileWriter (java.io.FileWriter)12 InputStreamReader (java.io.InputStreamReader)12 Path (java.nio.file.Path)12 FileReader (java.io.FileReader)10 Reader (java.io.Reader)9