Search in sources :

Example 6 with FF4jConfiguration

use of org.ff4j.conf.FF4jConfiguration 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);
    }
}
Also used : PropertiesParser(org.ff4j.parser.properties.PropertiesParser) FileItem(org.apache.commons.fileupload.FileItem) XmlParser(org.ff4j.conf.XmlParser) YamlParser(org.ff4j.parser.yaml.YamlParser) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HomeBean(org.ff4j.web.bean.HomeBean) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Aggregations

FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)6 XmlParser (org.ff4j.conf.XmlParser)4 InputStream (java.io.InputStream)3 XmlConfig (org.ff4j.conf.XmlConfig)3 Feature (org.ff4j.core.Feature)2 YamlParser (org.ff4j.parser.yaml.YamlParser)2 Test (org.junit.Test)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)1 PropertiesParser (org.ff4j.parser.properties.PropertiesParser)1 PropertyString (org.ff4j.property.PropertyString)1 HomeBean (org.ff4j.web.bean.HomeBean)1