use of org.ff4j.conf.FF4jConfiguration in project ff4j by ff4j.
the class PropertiesParser method parseConfiguration.
/**
* Load properties from environments.
*/
public FF4jConfiguration parseConfiguration(Map<String, String> mapProperties) {
Util.assertNotNull(mapProperties, "Cannot parse null properties");
FF4jConfiguration ff4jConfig = new FF4jConfiguration();
// Audit
if (mapProperties.containsKey(FF4J_TAG + "." + GLOBAL_AUDIT_TAG)) {
ff4jConfig.setAudit(Boolean.valueOf(mapProperties.get(FF4J_TAG + "." + GLOBAL_AUDIT_TAG)));
}
// AutoCreate
if (mapProperties.containsKey(FF4J_TAG + "." + GLOBAL_AUTOCREATE)) {
ff4jConfig.setAutoCreate(Boolean.valueOf(mapProperties.get(FF4J_TAG + "." + GLOBAL_AUTOCREATE)));
}
// Properties
ff4jConfig.getProperties().putAll(parseProperties(FF4J_TAG + "." + PROPERTIES_TAG, mapProperties));
// Features
parseFeatures(ff4jConfig, mapProperties);
return ff4jConfig;
}
use of org.ff4j.conf.FF4jConfiguration in project ff4j by ff4j.
the class PropertiesParserTest method importProperties_should_be_same_asXMLImport.
@Test
public void importProperties_should_be_same_asXMLImport() {
// Give XML an YAML files
InputStream xmlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.xml");
InputStream ymlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.properties");
// When parsing those files
XmlConfig xmlConfig = new XmlParser().parseConfigurationFile(xmlFile);
FF4jConfiguration propsConfig = new PropertiesParser().parseConfigurationFile(ymlFile);
// Than both config are even
assertEquals(xmlConfig.getFeatures().size(), propsConfig.getFeatures().size());
assertEquals(xmlConfig.getProperties().size(), propsConfig.getProperties().size());
// Custom-properties
Feature f1Xml = xmlConfig.getFeatures().get("first");
Feature f1props = propsConfig.getFeatures().get("first");
assertEquals(f1Xml.getDescription(), f1props.getDescription());
assertEquals(f1Xml.getCustomProperties().size(), f1props.getCustomProperties().size());
// FlipStrategy & Permission
Feature f3Xml = xmlConfig.getFeatures().get("third");
Feature f3Props = propsConfig.getFeatures().get("third");
assertEquals(f3Xml.getFlippingStrategy().getClass(), f3Props.getFlippingStrategy().getClass());
assertEquals(f3Xml.getFlippingStrategy().getInitParams().get("expression"), f3Props.getFlippingStrategy().getInitParams().get("expression"));
assertEquals(f3Xml.getPermissions(), f3Props.getPermissions());
}
use of org.ff4j.conf.FF4jConfiguration in project ff4j by ff4j.
the class YamlParser method parseConfigurationFile.
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public FF4jConfiguration parseConfigurationFile(InputStream inputStream) {
Util.assertNotNull(inputStream, "Cannot read file stream is empty, check readability and path.");
// Strengthen serialization
Map<?, ?> yamlConfigFile = safeYaml.load(inputStream);
Map<?, ?> ff4jYamlMap = (Map<?, ?>) yamlConfigFile.get(FF4J_TAG);
FF4jConfiguration ff4jConfig = new FF4jConfiguration();
if (ff4jYamlMap != null) {
// Audit
if (ff4jYamlMap.containsKey(GLOBAL_AUDIT_TAG)) {
ff4jConfig.setAudit(Boolean.valueOf(ff4jYamlMap.get(GLOBAL_AUDIT_TAG).toString()));
}
// AutoCreate
if (ff4jYamlMap.containsKey(GLOBAL_AUTOCREATE)) {
ff4jConfig.setAutoCreate(Boolean.valueOf(ff4jYamlMap.get(GLOBAL_AUTOCREATE).toString()));
}
// Properties
ff4jConfig.getProperties().putAll(parseProperties((List<Map<String, Object>>) ff4jYamlMap.get(PROPERTIES_TAG)));
// Features
parseFeatures(ff4jConfig, (List<Map<String, Object>>) ff4jYamlMap.get(FEATURES_TAG));
}
return ff4jConfig;
}
use of org.ff4j.conf.FF4jConfiguration in project ff4j by ff4j.
the class YamlParserTest method importYaml_should_be_same_asXMLImport.
@Test
public void importYaml_should_be_same_asXMLImport() {
// Give XML an YAML files
InputStream xmlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.xml");
InputStream ymlFile = getClass().getClassLoader().getResourceAsStream("test-ff4j-features.yml");
// When parsing those files
XmlConfig xmlConfig = new XmlParser().parseConfigurationFile(xmlFile);
FF4jConfiguration ymlConfig = new YamlParser().parseConfigurationFile(ymlFile);
// Than both config are even
assertEquals(xmlConfig.getFeatures().size(), ymlConfig.getFeatures().size());
assertEquals(xmlConfig.getProperties().size(), ymlConfig.getProperties().size());
// Custom-properties
Feature f1Xml = xmlConfig.getFeatures().get("first");
Feature f1Yml = ymlConfig.getFeatures().get("first");
assertEquals(f1Xml.getDescription(), f1Yml.getDescription());
assertEquals(f1Xml.getCustomProperties().size(), f1Yml.getCustomProperties().size());
// FlipStrategy & Permission
Feature f3Xml = xmlConfig.getFeatures().get("third");
Feature f3Yml = ymlConfig.getFeatures().get("third");
assertEquals(f3Xml.getFlippingStrategy().getClass(), f3Yml.getFlippingStrategy().getClass());
assertEquals(f3Xml.getFlippingStrategy().getInitParams().get("expression"), f3Yml.getFlippingStrategy().getInitParams().get("expression"));
assertEquals(f3Xml.getPermissions(), f3Yml.getPermissions());
}
use of org.ff4j.conf.FF4jConfiguration in project ff4j by ff4j.
the class ConsoleOperations method exportConfiguration.
/**
* Export configuration.
*
* @param ff4j
* feature flags for java
* @param res
* http response
* @param format
* format
* @throws IOException
* error occured when exporting
*/
public static void exportConfiguration(FF4j ff4j, HttpServletResponse res, String format) throws IOException {
if (format != null) {
InputStream in = null;
ServletOutputStream sos = null;
String fileName = "ff4j-" + SDF.format(new Date()) + ".";
try {
sos = res.getOutputStream();
if (FORMAT_XML.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_XML);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_XML + "\"");
in = new XmlParser().exportAll(new XmlConfig(ff4j));
} else if (FORMAT_YML.equals(format.toLowerCase()) || FORMAT_YAML.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_YAML);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_YML + "\"");
in = new YamlParser().export(new FF4jConfiguration(ff4j));
} else if (FORMAT_PROPERTIES.equals(format.toLowerCase())) {
res.setContentType(CONTENT_TYPE_PROPERTIES);
res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + FORMAT_PROPERTIES + "\"");
in = new YamlParser().export(new FF4jConfiguration(ff4j));
}
if (in != null) {
org.apache.commons.io.IOUtils.copy(in, sos);
}
} finally {
if (in != null) {
in.close();
}
if (sos != null) {
sos.flush();
sos.close();
}
}
}
}
Aggregations