Search in sources :

Example 71 with Feature

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

the class FeatureMapper method deserialize.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Feature deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
    String uid = json.getAsJsonObject().get(FEATURE_UID).getAsString();
    // Core
    Feature feature = new Feature(uid);
    JsonElement enable = json.getAsJsonObject().get(FEATURE_ENABLE);
    if (enable != null) {
        feature.setEnable(enable.getAsBoolean());
    }
    JsonElement description = json.getAsJsonObject().get(FEATURE_DESCRIPTION);
    if (description != null) {
        feature.setDescription(description.getAsString());
    }
    JsonElement group = json.getAsJsonObject().get(FEATURE_GROUP);
    if (group != null) {
        feature.setGroup(group.getAsString());
    }
    JsonElement permissions = json.getAsJsonObject().get(FEATURE_PERMISSIONS);
    if (permissions != null) {
        Set<String> auths = gson.fromJson(permissions, new TypeToken<HashSet<String>>() {
        }.getType());
        feature.setPermissions(auths);
    }
    // Flipping strategy
    JsonElement flippingStrategy = json.getAsJsonObject().get(FEATURE_STRATEGY);
    if (flippingStrategy != null && !flippingStrategy.isJsonNull()) {
        Map<String, ?> flippingStrategyParameters = gson.fromJson(flippingStrategy, new TypeToken<HashMap<String, ?>>() {
        }.getType());
        String flippingStrategyType = flippingStrategyParameters.get("type").toString();
        Map<String, String> initParams = (Map<String, String>) flippingStrategyParameters.get("initParams");
        // Adding flipping strategy
        feature.setFlippingStrategy(MappingUtil.instanceFlippingStrategy(uid, flippingStrategyType, initParams));
    }
    // Custom properties
    JsonElement customProperties = json.getAsJsonObject().get("customProperties");
    if (customProperties != null && !customProperties.isJsonNull()) {
        Map<String, LinkedTreeMap<String, Object>> map = new Gson().fromJson(customProperties, new TypeToken<com.google.gson.internal.LinkedTreeMap<String, LinkedTreeMap<String, Object>>>() {
        }.getType());
        for (Entry<String, LinkedTreeMap<String, Object>> element : map.entrySet()) {
            LinkedTreeMap<String, Object> propertyValues = element.getValue();
            String pName = (String) propertyValues.get("name");
            String pType = (String) propertyValues.get("type");
            String pValue = (String) propertyValues.get("value");
            String desc = (String) propertyValues.get("description");
            Object fixedValues = propertyValues.get("fixedValues");
            Set pFixedValues = fixedValues != null ? new HashSet((Collection) fixedValues) : null;
            // Creating property with it
            Property<?> property = PropertyFactory.createProperty(pName, pType, pValue, desc, pFixedValues);
            feature.addProperty(property);
        }
    }
    return feature;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Gson(com.google.gson.Gson) Feature(org.ff4j.core.Feature) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) HashSet(java.util.HashSet)

Example 72 with Feature

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

the class FeatureStoreElastic method readGroup.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Feature> readGroup(String groupName) {
    assertGroupExist(groupName);
    try {
        Map<String, Feature> mapOfFeatures = new HashMap<String, Feature>();
        Search query = findFeaturesByGroupName(indexFeatures, groupName);
        for (Hit<Feature, Void> feature : jestClient.execute(query).getHits(Feature.class)) {
            mapOfFeatures.put(feature.source.getUid(), feature.source);
        }
        return mapOfFeatures;
    } catch (IOException e) {
        throw new FeatureAccessException("Cannot read features from group '" + groupName + "'", e);
    }
}
Also used : FeatureAccessException(org.ff4j.exception.FeatureAccessException) HashMap(java.util.HashMap) Search(io.searchbox.core.Search) IOException(java.io.IOException) ElasticQueryBuilder.createFeature(org.ff4j.elastic.ElasticQueryBuilder.createFeature) Feature(org.ff4j.core.Feature) ElasticQueryBuilder.deleteFeature(org.ff4j.elastic.ElasticQueryBuilder.deleteFeature)

Example 73 with Feature

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

the class FeatureStoreElastic method grantRoleOnFeature.

/**
 * {@inheritDoc}
 */
@Override
public void grantRoleOnFeature(String flipId, String roleName) {
    assertFeatureExist(flipId);
    Util.assertHasLength(roleName);
    Feature feature = read(flipId);
    feature.getPermissions().add(roleName);
    update(feature);
}
Also used : ElasticQueryBuilder.createFeature(org.ff4j.elastic.ElasticQueryBuilder.createFeature) Feature(org.ff4j.core.Feature) ElasticQueryBuilder.deleteFeature(org.ff4j.elastic.ElasticQueryBuilder.deleteFeature)

Example 74 with Feature

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

the class FeatureStoreElastic method removeRoleFromFeature.

/**
 * {@inheritDoc}
 */
@Override
public void removeRoleFromFeature(String flipId, String roleName) {
    assertFeatureExist(flipId);
    Util.assertHasLength(roleName);
    Feature feature = read(flipId);
    feature.getPermissions().remove(roleName);
    update(feature);
}
Also used : ElasticQueryBuilder.createFeature(org.ff4j.elastic.ElasticQueryBuilder.createFeature) Feature(org.ff4j.core.Feature) ElasticQueryBuilder.deleteFeature(org.ff4j.elastic.ElasticQueryBuilder.deleteFeature)

Example 75 with Feature

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

the class FeatureStoreEhCache method removeFromGroup.

/**
 * {@inheritDoc}
 */
@Override
public void removeFromGroup(String featureId, String groupName) {
    Util.assertParamHasLength(groupName, "groupName (#2)");
    if (!existGroup(groupName)) {
        throw new GroupNotFoundException(groupName);
    }
    // retrieve
    Feature f = read(featureId);
    f.setGroup(null);
    // persist modification
    update(f);
}
Also used : GroupNotFoundException(org.ff4j.exception.GroupNotFoundException) Feature(org.ff4j.core.Feature)

Aggregations

Feature (org.ff4j.core.Feature)295 Test (org.junit.Test)144 PropertyString (org.ff4j.property.PropertyString)53 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)27 PonderationStrategy (org.ff4j.strategy.PonderationStrategy)19 Property (org.ff4j.property.Property)16 LinkedHashMap (java.util.LinkedHashMap)15 GroupNotFoundException (org.ff4j.exception.GroupNotFoundException)15 Map (java.util.Map)14 XmlParser (org.ff4j.conf.XmlParser)14 AbstractFf4jTest (org.ff4j.test.AbstractFf4jTest)14 FeatureAccessException (org.ff4j.exception.FeatureAccessException)13 InputStream (java.io.InputStream)11 FeatureApiBean (org.ff4j.web.api.resources.domain.FeatureApiBean)11 Set (java.util.Set)10 Response (javax.ws.rs.core.Response)10 FlippingStrategy (org.ff4j.core.FlippingStrategy)10 CouchDbFeature (org.ff4j.couchdb.document.CouchDbFeature)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9