use of org.ff4j.web.api.resources.domain.FlippingStrategyApiBean in project ff4j by ff4j.
the class FeatureResource method upsertFeature.
/**
* Create the feature if not exist or update it
*
* @param headers
* current request header
* @param data
* feature serialized as JSON
* @return 204 or 201
*/
@PUT
@RolesAllowed({ ROLE_WRITE })
@ApiOperation(value = "Create of update a feature", response = Response.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = 201, message = "Feature has been created"), @ApiResponse(code = 204, message = "No content, feature is updated") })
public Response upsertFeature(@Context HttpHeaders headers, @PathParam("uid") String id, FeatureApiBean fApiBean) {
// Parameter validations
if ("".equals(id) || !id.equals(fApiBean.getUid())) {
String errMsg = "Invalid identifier expected " + id;
return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build();
}
Feature feat = new Feature(id);
feat.setDescription(fApiBean.getDescription());
feat.setEnable(fApiBean.isEnable());
feat.setGroup(fApiBean.getGroup());
feat.setPermissions(new HashSet<String>(fApiBean.getPermissions()));
// Flipping Strategy
FlippingStrategyApiBean flipApiBean = fApiBean.getFlippingStrategy();
if (flipApiBean != null) {
try {
Map<String, String> initparams = flipApiBean.getInitParams();
feat.setFlippingStrategy(MappingUtil.instanceFlippingStrategy(id, flipApiBean.getType(), initparams));
} catch (Exception e) {
String errMsg = "Cannot read Flipping Strategy, does not seems to have a DEFAULT constructor, " + e.getMessage();
return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build();
}
}
// Properties
Map<String, PropertyApiBean> mapProperties = fApiBean.getCustomProperties();
if (mapProperties != null) {
for (PropertyApiBean propertyBean : mapProperties.values()) {
feat.addProperty(propertyBean.asProperty());
}
}
// Update or create ?
if (!getFeatureStore().exist(feat.getUid())) {
getFeatureStore().create(feat);
String location = String.format("%s", uriInfo.getAbsolutePath().toString());
try {
return Response.created(new URI(location)).build();
} catch (URISyntaxException e) {
return Response.status(Response.Status.CREATED).header(LOCATION, location).entity(id).build();
}
}
// Create
getFeatureStore().update(feat);
return Response.noContent().build();
}
Aggregations