use of org.ff4j.parser.yaml.YamlParser 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();
}
}
}
}
use of org.ff4j.parser.yaml.YamlParser in project ff4j by ff4j.
the class HomeController method post.
/**
* {@inheritDoc}
*/
public void post(HttpServletRequest req, HttpServletResponse res, WebContext ctx) throws Exception {
String msg = null;
String msgType = "success";
String operation = req.getParameter(WebConstants.OPERATION);
// Upload Configuration File
if (ServletFileUpload.isMultipartContent(req)) {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
for (FileItem item : items) {
if (item.isFormField()) {
if (OPERATION.equalsIgnoreCase(item.getFieldName())) {
LOGGER.info("Processing action : " + item.getString());
}
} else if (FLIPFILE.equalsIgnoreCase(item.getFieldName())) {
String filename = FilenameUtils.getName(item.getName());
try {
FF4jConfiguration ff4jConfig = null;
if (filename.toLowerCase().endsWith(ConsoleConstants.FORMAT_XML)) {
ff4jConfig = new XmlParser().parseConfigurationFile(item.getInputStream());
} else if (filename.toLowerCase().endsWith(ConsoleConstants.FORMAT_YML) || filename.toLowerCase().endsWith(ConsoleConstants.FORMAT_YAML)) {
ff4jConfig = new YamlParser().parseConfigurationFile(item.getInputStream());
} else if (filename.toLowerCase().endsWith(ConsoleConstants.FORMAT_PROPERTIES)) {
ff4jConfig = new PropertiesParser().parseConfigurationFile(item.getInputStream());
}
if (ff4jConfig != null) {
importFile(getFf4j(), ff4jConfig);
msg = "The file <b>" + filename + "</b> has been successfully imported";
} else {
msgType = ERROR;
msg = "Invalid FILE, must be XML, YAML or PROPERTIES files";
}
} catch (RuntimeException re) {
msgType = ERROR;
msg = "Cannot Import Config:" + re.getMessage();
break;
}
}
}
ctx.setVariable("msgType", msgType);
ctx.setVariable("msgInfo", msg);
get(req, res, ctx);
} else if (WebConstants.OP_CREATE_SCHEMA.equalsIgnoreCase(operation)) {
try {
getFf4j().createSchema();
msg = "Schema has been created in DB (if required).";
ctx.setVariable("msgType", msgType);
ctx.setVariable("msgInfo", msg);
get(req, res, ctx);
} catch (RuntimeException re) {
ctx.setVariable("msgType", ERROR);
ctx.setVariable("msgInfo", "Cannot create Schema:" + re.getMessage());
ctx.setVariable(KEY_TITLE, "Home");
ctx.setVariable("today", Calendar.getInstance());
ctx.setVariable("homebean", new HomeBean());
}
} else if (WebConstants.OP_CLEAR_CACHE.equalsIgnoreCase(operation)) {
FF4jCacheProxy cacheProxy = getFf4j().getCacheProxy();
if (cacheProxy != null) {
cacheProxy.getCacheManager().clearFeatures();
cacheProxy.getCacheManager().clearProperties();
msg = "Cache Cleared!";
} else {
msg = "Cache not present: it cannot be cleared!";
}
ctx.setVariable("msgType", msgType);
ctx.setVariable("msgInfo", msg);
get(req, res, ctx);
}
}
Aggregations