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);
}
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);
}
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;
}
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();
}
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());
}
Aggregations