Search in sources :

Example 6 with Feature

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

the class FeaturesController method post.

/**
 * {@inheritDoc}
 */
public void post(HttpServletRequest req, HttpServletResponse res, WebContext ctx) throws IOException {
    String msg = null;
    String msgType = "success";
    String operation = req.getParameter(WebConstants.OPERATION);
    String featureId = req.getParameter(WebConstants.FEATID);
    if (OP_EDIT_FEATURE.equalsIgnoreCase(operation)) {
        this.updateFeature(req, featureId);
        msg = featureId + " has been UPDATED";
    } else if (OP_CREATE_FEATURE.equalsIgnoreCase(operation)) {
        ConsoleOperations.createFeature(getFf4j(), req);
        msg = featureId + " has been CREATED";
    } else if (OP_RMV_FEATURE.equalsIgnoreCase(operation)) {
        getFf4j().getFeatureStore().delete(featureId);
        msg = featureId + " has been DELETED";
    } else if (OP_RENAME_FEATURE.equalsIgnoreCase(operation)) {
        String newName = req.getParameter(NEW_NAME);
        Set<String> featureNames = getFf4j().getFeatureStore().readAll().keySet();
        if (featureNames.contains(newName)) {
            msgType = "warning";
            msg = "Cannot rename " + featureId + " to " + newName + " : it already exists";
        } else {
            Feature newFeature = getFf4j().getFeatureStore().read(featureId);
            newFeature.setUid(newName);
            getFf4j().getFeatureStore().delete(featureId);
            getFf4j().getFeatureStore().create(newFeature);
            msg = "Feature " + featureId + " has been renamed to " + newName;
        }
    } else if (OP_COPY_FEATURE.equalsIgnoreCase(operation)) {
        String newName = req.getParameter(NEW_NAME);
        Set<String> featureNames = getFf4j().getFeatureStore().readAll().keySet();
        if (featureNames.contains(newName)) {
            msgType = "warning";
            msg = "Cannot copy " + featureId + " with name " + newName + " : it already exists";
        } else {
            Feature newFeature = new Feature(getFf4j().getFeatureStore().read(featureId));
            newFeature.setUid(newName);
            getFf4j().getFeatureStore().create(newFeature);
            msg = "Feature " + featureId + " has been copied to " + newName;
        }
    } 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);
                msg = groupName + " has been ENABLED";
                LOGGER.info("Group '" + groupName + "' has been ENABLED.");
            } else if (OP_DISABLE.equalsIgnoreCase(operationGroup)) {
                getFf4j().getFeatureStore().disableGroup(groupName);
                msg = groupName + " has been DISABLED";
                LOGGER.info("Group '" + groupName + "' has been DISABLED.");
            }
        }
    }
    ctx.setVariable("msgType", msgType);
    ctx.setVariable("msgInfo", msg);
    renderPage(ctx);
}
Also used : Set(java.util.Set) Feature(org.ff4j.core.Feature)

Example 7 with Feature

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

the class FeaturesController method get.

/**
 * {@inheritDoc}
 */
public void get(HttpServletRequest req, HttpServletResponse res, WebContext ctx) throws IOException {
    String operation = req.getParameter(WebConstants.OPERATION);
    String featureId = req.getParameter(WebConstants.FEATID);
    String msgType = "success";
    String msg = null;
    if (Util.hasLength(operation) && Util.hasLength(featureId)) {
        if (getFf4j().getFeatureStore().exist(featureId)) {
            if (OP_DISABLE.equalsIgnoreCase(operation)) {
                getFf4j().disable(featureId);
                msg = msg(featureId, "DISABLED");
                LOGGER.info(featureId + " has been disabled");
            }
            if (OP_ENABLE.equalsIgnoreCase(operation)) {
                getFf4j().enable(featureId);
                msg = msg(featureId, "ENABLED");
                LOGGER.info(featureId + " has been enabled");
            }
            if (OP_ADD_PERMISSION.equalsIgnoreCase(operation)) {
                String permName = req.getParameter(WebConstants.PERMISSION);
                Feature feature = getFf4j().getFeatureStore().read(featureId);
                feature.getPermissions().add(permName);
                getFf4j().getFeatureStore().update(feature);
                LOGGER.info("Add new " + permName + " to " + featureId);
            }
            if (OP_RMV_PERMISSION.equalsIgnoreCase(operation)) {
                String permName = req.getParameter(WebConstants.PERMISSION);
                Feature feature = getFf4j().getFeatureStore().read(featureId);
                feature.getPermissions().remove(permName);
                getFf4j().getFeatureStore().update(feature);
                LOGGER.info("Remove " + permName + " to " + featureId);
            }
            if (OP_CLEAR_PERMISSIONS.equalsIgnoreCase(operation)) {
                Feature feature = getFf4j().getFeatureStore().read(featureId);
                feature.getPermissions().clear();
                getFf4j().getFeatureStore().update(feature);
                LOGGER.info("Clear permissions for " + featureId);
            }
            if (OP_RMV_PROPERTY.equalsIgnoreCase(operation)) {
                String propertyName = req.getParameter(WebConstants.NAME);
                Feature feature = getFf4j().getFeatureStore().read(featureId);
                feature.getCustomProperties().remove(propertyName);
                getFf4j().getFeatureStore().update(feature);
                LOGGER.info("Remove Property " + propertyName + " to " + featureId);
            }
        } else {
            msgType = "warning";
            msg = "The feature '" + featureId + "' does not exist";
        }
    }
    ctx.setVariable("msgType", msgType);
    ctx.setVariable("msgInfo", msg);
    renderPage(ctx);
}
Also used : Feature(org.ff4j.core.Feature)

Example 8 with Feature

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

the class FeatureStoreHttp method readGroup.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Feature> readGroup(String groupName) {
    if (groupName == null || groupName.isEmpty()) {
        throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY);
    }
    ClientResponse cRes = getGroups().path(groupName).get(ClientResponse.class);
    if (Status.NOT_FOUND.getStatusCode() == cRes.getStatus()) {
        throw new GroupNotFoundException(groupName);
    }
    if (Status.OK.getStatusCode() != cRes.getStatus()) {
        throw new FeatureAccessException(CANNOT_GRANT_ROLE_ON_FEATURE_AN_HTTP_ERROR + cRes.getStatus() + OCCURED);
    }
    String resEntity = cRes.getEntity(String.class);
    Feature[] fArray = parseFeatureArray(resEntity);
    Map<String, Feature> features = new HashMap<String, Feature>();
    for (Feature feature : fArray) {
        features.put(feature.getUid(), feature);
    }
    return features;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) FeatureAccessException(org.ff4j.exception.FeatureAccessException) HashMap(java.util.HashMap) GroupNotFoundException(org.ff4j.exception.GroupNotFoundException) FeatureJsonParser.parseFeature(org.ff4j.utils.json.FeatureJsonParser.parseFeature) Feature(org.ff4j.core.Feature)

Example 9 with Feature

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

the class FeatureJsonParser method featureArrayToJson.

/**
 * Convert feature array to json.
 *
 * @param features
 *            target features
 * @return json string
 */
public static String featureArrayToJson(Feature[] features) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    if (features != null) {
        boolean first = true;
        for (Feature feature : features) {
            sb.append(first ? "" : ",");
            sb.append(feature.toJson());
            first = false;
        }
    }
    sb.append("]");
    return sb.toString();
}
Also used : Feature(org.ff4j.core.Feature)

Example 10 with Feature

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

the class FeatureStoreHttpSpringTest method testUpdateFlipLessAutorisation.

/**
 * TDD.
 */
@Override
@Test
public void testUpdateFlipLessAutorisation() {
    // Given
    assertFf4j.assertThatFeatureExist(F1);
    assertFf4j.assertThatFeatureHasRole(F1, ROLE_USER);
    // When
    testedStore.update(new Feature(F1, false, null));
    // Then
    Assert.assertTrue(testedStore.read(F1).getPermissions().isEmpty());
}
Also used : Feature(org.ff4j.core.Feature) Test(org.junit.Test) JerseyTest(com.sun.jersey.test.framework.JerseyTest)

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