Search in sources :

Example 21 with XmlParser

use of org.ff4j.conf.XmlParser in project ff4j by ff4j.

the class InvalidParameter method testInvalidParameter.

@Test(expected = IllegalArgumentException.class)
public void testInvalidParameter() throws Throwable {
    final IDoIt service = new IDoItImpl();
    service.doIt("");
    FeatureAdvisor fa = new FeatureAdvisor();
    fa.setFf4j(new FF4j(new XmlParser(), "test-ff4j-features.xml"));
    MethodInvocation mi = new MethodInvocation() {

        public Object proceed() throws Throwable {
            return null;
        }

        public Object getThis() {
            return service;
        }

        public AccessibleObject getStaticPart() {
            return null;
        }

        public Object[] getArguments() {
            return null;
        }

        public Method getMethod() {
            try {
                Method m = IDoIt.class.getMethod("doIt", String.class);
                return m;
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    fa.invoke(mi);
}
Also used : XmlParser(org.ff4j.conf.XmlParser) FF4j(org.ff4j.FF4j) FeatureAdvisor(org.ff4j.aop.FeatureAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 22 with XmlParser

use of org.ff4j.conf.XmlParser in project ff4j by ff4j.

the class ConsoleOperations method exportFile.

/**
 * Build Http response when invoking export features.
 *
 * @param res
 *            http response
 * @throws IOException
 *             error when building response
 */
public static void exportFile(FF4j ff4j, HttpServletResponse res) throws IOException {
    Map<String, Feature> features = ff4j.getFeatureStore().readAll();
    InputStream in = new XmlParser().exportFeatures(features);
    ServletOutputStream sos = null;
    try {
        sos = res.getOutputStream();
        res.setContentType("text/xml");
        res.setHeader("Content-Disposition", "attachment; filename=\"ff4j.xml\"");
        // res.setContentLength()
        org.apache.commons.io.IOUtils.copy(in, sos);
        LOGGER.info(features.size() + " features have been exported.");
    } finally {
        if (in != null) {
            in.close();
        }
        if (sos != null) {
            sos.flush();
            sos.close();
        }
    }
}
Also used : XmlParser(org.ff4j.conf.XmlParser) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) Feature(org.ff4j.core.Feature)

Example 23 with XmlParser

use of org.ff4j.conf.XmlParser 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();
            }
        }
    }
}
Also used : XmlParser(org.ff4j.conf.XmlParser) YamlParser(org.ff4j.parser.yaml.YamlParser) XmlConfig(org.ff4j.conf.XmlConfig) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) Date(java.util.Date) FF4jConfiguration(org.ff4j.conf.FF4jConfiguration)

Example 24 with XmlParser

use of org.ff4j.conf.XmlParser in project ff4j by ff4j.

the class ConsoleServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String message = null;
    String messagetype = "info";
    try {
        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());
                    if (filename.toLowerCase().endsWith("xml")) {
                        importFile(getFf4j(), new XmlParser().parseConfigurationFile(item.getInputStream()));
                        message = "The file <b>" + filename + "</b> has been successfully imported";
                    } else {
                        messagetype = ERROR;
                        message = "Invalid FILE, must be CSV, XML or PROPERTIES files";
                    }
                }
            }
        } else {
            String operation = req.getParameter(OPERATION);
            String uid = req.getParameter(FEATID);
            LOGGER.info("POST - op=" + operation + " feat=" + uid);
            if (operation != null && !operation.isEmpty()) {
                if (OP_EDIT_FEATURE.equalsIgnoreCase(operation)) {
                    updateFeatureDescription(getFf4j(), req);
                    message = msg(uid, "UPDATED");
                } else if (OP_EDIT_PROPERTY.equalsIgnoreCase(operation)) {
                    updateProperty(getFf4j(), req);
                    message = renderMsgProperty(uid, "UPDATED");
                } else if (OP_CREATE_PROPERTY.equalsIgnoreCase(operation)) {
                    createProperty(getFf4j(), req);
                    message = renderMsgProperty(req.getParameter(NAME), "ADDED");
                } else if (OP_CREATE_FEATURE.equalsIgnoreCase(operation)) {
                    createFeature(getFf4j(), req);
                    message = msg(uid, "ADDED");
                } else if (OP_TOGGLE_GROUP.equalsIgnoreCase(operation)) {
                    String groupName = req.getParameter(GROUPNAME);
                    if (groupName != null && !groupName.isEmpty()) {
                        String operationGroup = req.getParameter(SUBOPERATION);
                        if (OP_ENABLE.equalsIgnoreCase(operationGroup)) {
                            getFf4j().getFeatureStore().enableGroup(groupName);
                            message = renderMsgGroup(groupName, "ENABLED");
                            LOGGER.info("Group '" + groupName + "' has been ENABLED.");
                        } else if (OP_DISABLE.equalsIgnoreCase(operationGroup)) {
                            getFf4j().getFeatureStore().disableGroup(groupName);
                            message = renderMsgGroup(groupName, "DISABLED");
                            LOGGER.info("Group '" + groupName + "' has been DISABLED.");
                        }
                    }
                } else {
                    LOGGER.error("Invalid POST OPERATION" + operation);
                    messagetype = ERROR;
                    message = "Invalid REQUEST";
                }
            } else {
                LOGGER.error("No ID provided" + operation);
                messagetype = ERROR;
                message = "Invalid UID";
            }
        }
    } catch (Exception e) {
        messagetype = ERROR;
        message = e.getMessage();
        LOGGER.error("An error occured ", e);
    }
    // Update FF4J in Session (distributed)
    getServletContext().setAttribute(FF4J_SESSIONATTRIBUTE_NAME, ff4j);
    renderPage(ff4j, req, res, message, messagetype);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) XmlParser(org.ff4j.conf.XmlParser) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 25 with XmlParser

use of org.ff4j.conf.XmlParser 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

XmlParser (org.ff4j.conf.XmlParser)44 Test (org.junit.Test)29 InputStream (java.io.InputStream)22 FF4j (org.ff4j.FF4j)17 XmlConfig (org.ff4j.conf.XmlConfig)14 Feature (org.ff4j.core.Feature)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Property (org.ff4j.property.Property)7 FF4jConfiguration (org.ff4j.conf.FF4jConfiguration)4 Date (java.util.Date)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 YamlParser (org.ff4j.parser.yaml.YamlParser)2 PropertyDate (org.ff4j.property.PropertyDate)2 SpringSecurityAuthorisationManager (org.ff4j.security.SpringSecurityAuthorisationManager)2 TestConstantsFF4j (org.ff4j.test.TestConstantsFF4j)2