Search in sources :

Example 1 with Feature

use of org.ff4j.core.Feature in project ff4j by ff4j.

the class ConsoleOperations method createProperty.

/**
 * Create new property in store.
 *
 * @param ff4j
 *      current ff4j instance.
 * @param req
 *      current http request
 */
public static void createProperty(FF4j ff4j, HttpServletRequest req) {
    String name = req.getParameter("name");
    String type = req.getParameter("pType");
    String description = req.getParameter("desc");
    String value = req.getParameter("pValue");
    String featureId = req.getParameter(WebConstants.FEATURE_UID);
    Property<?> ap = PropertyFactory.createProperty(name, type, value);
    ap.setDescription(description);
    if (Util.hasLength(featureId)) {
        Feature current = ff4j.getFeatureStore().read(featureId);
        current.addProperty(ap);
        ff4j.getFeatureStore().update(current);
    } else {
        ff4j.getPropertiesStore().createProperty(ap);
    }
}
Also used : Feature(org.ff4j.core.Feature)

Example 2 with Feature

use of org.ff4j.core.Feature in project ff4j by ff4j.

the class ConsoleOperations method updateProperty.

/**
 * Sample Element should be updated like name, description, value
 * @param ff4j
 * @param req
 */
public static void updateProperty(FF4j ff4j, HttpServletRequest req) {
    String name = req.getParameter("name");
    String type = req.getParameter("pType");
    String description = req.getParameter("desc");
    String value = req.getParameter("pValue");
    String uid = req.getParameter("uid");
    String featureId = req.getParameter(WebConstants.FEATURE_UID);
    Property<?> ap;
    // To update the core the uid is the name (rename, edit)
    if (uid == null) {
        uid = name;
    }
    // Update Feature property
    if (Util.hasLength(featureId)) {
        Feature current = ff4j.getFeatureStore().read(featureId);
        ap = current.getProperty(uid);
        ap.setDescription(description);
        if (ap.getType().equalsIgnoreCase(type)) {
            ap.setValueFromString(value);
        } else {
            ap = PropertyFactory.createProperty(name, type, value);
            LOGGER.warn("By changing property type you loose the fixedValues, cannot evaluate ? at runtime");
        }
        ff4j.getFeatureStore().update(current);
    } else if (ff4j.getPropertiesStore().existProperty(uid)) {
        // Do not change name, just and update
        if (uid.equalsIgnoreCase(name)) {
            ap = ff4j.getPropertiesStore().readProperty(uid);
            // just an update for the value
            if (ap.getType().equalsIgnoreCase(type)) {
                ap.setDescription(description);
                ap.setValueFromString(value);
                ff4j.getPropertiesStore().updateProperty(ap);
            } else {
                ap = PropertyFactory.createProperty(name, type, value);
                ap.setDescription(description);
                // Note : Fixed Values are LOST if type changed => cannot cast ? to T
                LOGGER.warn("By changing property type you loose the fixedValues, cannot evaluate ? at runtime");
                ff4j.getPropertiesStore().deleteProperty(name);
                ff4j.getPropertiesStore().createProperty(ap);
            }
        } else {
            // Name change delete and create a new
            ap = PropertyFactory.createProperty(name, type, value);
            ap.setDescription(description);
            // Note : Fixed Values are LOST if name changed => cannot cast ? to T
            LOGGER.warn("By changing property name you loose the fixedValues, cannot evaluate generics at runtime (type inference)");
            ff4j.getPropertiesStore().deleteProperty(uid);
            ff4j.getPropertiesStore().createProperty(ap);
        }
    }
}
Also used : Feature(org.ff4j.core.Feature)

Example 3 with Feature

use of org.ff4j.core.Feature 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 4 with Feature

use of org.ff4j.core.Feature in project ff4j by ff4j.

the class ConsoleOperations method updateFeatureDescription.

/**
 * User action to update a target feature's description.
 *
 * @param req
 *            http request containing operation parameters
 */
public static void updateFeatureDescription(FF4j ff4j, HttpServletRequest req) {
    // uid
    final String featureId = req.getParameter(FEATID);
    if (featureId != null && !featureId.isEmpty()) {
        // https://github.com/clun/ff4j/issues/66
        Feature old = ff4j.getFeatureStore().read(featureId);
        Feature fp = new Feature(featureId, old.isEnable());
        // <--
        // Description
        final String featureDesc = req.getParameter(DESCRIPTION);
        if (null != featureDesc && !featureDesc.isEmpty()) {
            fp.setDescription(featureDesc);
        }
        // GroupName
        final String groupName = req.getParameter(GROUPNAME);
        if (null != groupName && !groupName.isEmpty()) {
            fp.setGroup(groupName);
        }
        // Strategy
        updateFlippingStrategy(fp, req.getParameter(STRATEGY), req.getParameter(STRATEGY_INIT));
        // Permissions
        final String permission = req.getParameter(PERMISSION);
        if (null != permission && PERMISSION_RESTRICTED.equals(permission)) {
            @SuppressWarnings("unchecked") Map<String, Object> parameters = req.getParameterMap();
            Set<String> permissions = new HashSet<String>();
            for (String key : parameters.keySet()) {
                if (key.startsWith(PREFIX_CHECKBOX)) {
                    permissions.add(key.replace(PREFIX_CHECKBOX, ""));
                }
            }
            fp.setPermissions(permissions);
        }
        // Creation
        ff4j.getFeatureStore().update(fp);
        LOGGER.info(featureId + " has been updated");
    }
}
Also used : Feature(org.ff4j.core.Feature) HashSet(java.util.HashSet)

Example 5 with Feature

use of org.ff4j.core.Feature in project ff4j by ff4j.

the class ConsoleRenderer method renderFeatureRows.

/**
 * Produce the rows of the Feature Table.
 *
 * @param ff4j
 *            target ff4j.
 * @param req
 *            current http request
 * @return string representing the list of features
 */
private static final String renderFeatureRows(FF4j ff4j, HttpServletRequest req) {
    StringBuilder sb = new StringBuilder();
    final Map<String, Feature> mapOfFeatures = ff4j.getFeatures();
    for (Map.Entry<String, Feature> uid : mapOfFeatures.entrySet()) {
        Feature currentFeature = uid.getValue();
        sb.append("<tr>" + END_OF_LINE);
        // Column with uid and description as tooltip
        sb.append("<td><a class=\"ff4j-tooltip\" ");
        if (null != currentFeature.getDescription()) {
            sb.append(" tooltip=\"");
            sb.append(currentFeature.getDescription());
            sb.append("\"");
        }
        sb.append(">");
        sb.append(currentFeature.getUid());
        sb.append("</a>");
        // Colonne Group
        sb.append("</td><td>");
        if (null != currentFeature.getGroup()) {
            sb.append(currentFeature.getGroup());
        } else {
            sb.append("--");
        }
        // Colonne Permissions
        sb.append("</td><td>");
        Set<String> permissions = currentFeature.getPermissions();
        if (null != permissions && !permissions.isEmpty()) {
            boolean first = true;
            for (String perm : permissions) {
                if (!first) {
                    sb.append(",");
                }
                sb.append(perm);
                first = false;
            }
        } else {
            sb.append("--");
        }
        // Colonne Strategy
        sb.append("</td><td style=\"word-break: break-all;\">");
        FlippingStrategy fs = currentFeature.getFlippingStrategy();
        if (null != fs) {
            sb.append(renderValue(fs.getClass().getName(), 50));
            if (fs.getInitParams() != null) {
                for (Map.Entry<String, String> entry : fs.getInitParams().entrySet()) {
                    sb.append("<li>" + renderValue(entry.getKey() + " =  " + entry.getValue(), 40));
                }
            }
        } else {
            sb.append("--");
        }
        // Colonne 'Holy' Toggle
        sb.append("</td><td style=\"width:8%;text-align:center\">");
        sb.append("<label class=\"switch switch-green\">");
        sb.append("<input id=\"" + currentFeature.getUid() + "\" type=\"checkbox\" class=\"switch-input\"");
        sb.append(" onclick=\"javascript:toggle(this)\" ");
        if (currentFeature.isEnable()) {
            sb.append(" checked");
        }
        sb.append(">");
        sb.append("<span class=\"switch-label\" data-on=\"On\" data-off=\"Off\"></span>");
        sb.append("<span class=\"switch-handle\"></span>");
        sb.append("</label>");
        // Colonne Button Edit
        sb.append("</td><td style=\"width:5%;text-align:center\">");
        sb.append("<a data-toggle=\"modal\" href=\"#modalEdit\" data-id=\"" + currentFeature.getUid() + "\" ");
        sb.append(" data-desc=\"" + currentFeature.getDescription() + "\"");
        sb.append(" data-group=\"" + currentFeature.getGroup() + "\"");
        sb.append(" data-strategy=\"");
        if (null != currentFeature.getFlippingStrategy()) {
            sb.append(currentFeature.getFlippingStrategy().getClass().getName());
        }
        sb.append("\" data-stratparams=\"");
        if (null != currentFeature.getFlippingStrategy()) {
            sb.append(currentFeature.getFlippingStrategy().getInitParams());
        }
        sb.append("\" data-permissions=\"");
        if (null != currentFeature.getPermissions() && !currentFeature.getPermissions().isEmpty()) {
            sb.append(currentFeature.getPermissions());
        }
        sb.append("\" style=\"width:6px;\" class=\"open-EditFlipDialog btn\">");
        sb.append("<i class=\"icon-pencil\" style=\"margin-left:-5px;\"></i></a>");
        // Colonne Button Delete
        sb.append("</td><td style=\"width:5%;text-align:center\">");
        sb.append("<a href=\"");
        sb.append(req.getContextPath());
        sb.append(req.getServletPath());
        sb.append("?op=" + OP_RMV_FEATURE + "&" + FEATID + "=" + uid.getKey());
        sb.append("\" style=\"width:6px;\" class=\"btn\">");
        sb.append("<i class=\"icon-trash\" style=\"margin-left:-5px;\"></i>");
        sb.append("</a>");
        sb.append("</td></tr>");
    }
    return sb.toString();
}
Also used : FlippingStrategy(org.ff4j.core.FlippingStrategy) PropertyString(org.ff4j.property.PropertyString) Feature(org.ff4j.core.Feature) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Feature (org.ff4j.core.Feature)258 Test (org.junit.Test)136 PropertyString (org.ff4j.property.PropertyString)49 HashMap (java.util.HashMap)29 HashSet (java.util.HashSet)29 PonderationStrategy (org.ff4j.strategy.PonderationStrategy)19 LinkedHashMap (java.util.LinkedHashMap)15 AbstractFf4jTest (org.ff4j.test.AbstractFf4jTest)14 Property (org.ff4j.property.Property)12 Set (java.util.Set)11 FeatureAccessException (org.ff4j.exception.FeatureAccessException)11 FeatureApiBean (org.ff4j.web.api.resources.domain.FeatureApiBean)11 XmlParser (org.ff4j.conf.XmlParser)10 InputStream (java.io.InputStream)9 Map (java.util.Map)9 FlippingStrategy (org.ff4j.core.FlippingStrategy)9 GroupNotFoundException (org.ff4j.exception.GroupNotFoundException)9 ClientResponse (com.sun.jersey.api.client.ClientResponse)8 ArrayList (java.util.ArrayList)8 Response (javax.ws.rs.core.Response)8