Search in sources :

Example 1 with PropertyApiBean

use of org.ff4j.web.api.resources.domain.PropertyApiBean in project ff4j by ff4j.

the class PropertiesResourceTestIT method testGet.

/**
 * TDD.
 */
@Test
public void testGet() {
    // Given
    // When
    WebResource wResFeatures = rscProperties();
    ClientResponse httpResponse = wResFeatures.get(ClientResponse.class);
    List<PropertyApiBean> fList = httpResponse.getEntity(new GenericType<List<PropertyApiBean>>() {
    });
    // Then, HTTPResponse
    Assert.assertEquals("Expected status is 200", Status.OK.getStatusCode(), httpResponse.getStatus());
    Assert.assertNotNull(fList);
    // Then, Entity Object
    Assert.assertFalse(fList.isEmpty());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) WebResource(com.sun.jersey.api.client.WebResource) List(java.util.List) Test(org.junit.Test)

Example 2 with PropertyApiBean

use of org.ff4j.web.api.resources.domain.PropertyApiBean in project ff4j by ff4j.

the class PropertyStoreHttp method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> value) {
    Util.assertNotNull(value);
    Util.assertHasLength(value.getName());
    if (existProperty(value.getName())) {
        throw new PropertyAlreadyExistException("Property already exist");
    }
    /* Now can process upsert through PUT HTTP method
        Response cRes = getStore().path(value.getName())//
                .request(MediaType.APPLICATION_JSON)
                .put(Entity.entity(new PropertyApiBean(value), MediaType.APPLICATION_JSON));*/
    Response cRes = ClientHttpUtils.createRequest(getStore().path(value.getName()), authorization, null).put(Entity.entity(new PropertyApiBean(value), MediaType.APPLICATION_JSON));
    // Check response code CREATED or raised error
    if (Status.CREATED.getStatusCode() != cRes.getStatus()) {
        throw new FeatureAccessException("Cannot create properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
}
Also used : Response(javax.ws.rs.core.Response) PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) FeatureAccessException(org.ff4j.exception.FeatureAccessException) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Example 3 with PropertyApiBean

use of org.ff4j.web.api.resources.domain.PropertyApiBean in project ff4j by ff4j.

the class PropertyStoreResource method readProperties.

@GET
@Path("/" + RESOURCE_PROPERTIES)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Display information regarding <b>Properties</b>", response = PropertyApiBean.class)
@ApiResponses(@ApiResponse(code = 200, message = "get all Properties"))
public List<PropertyApiBean> readProperties() {
    List<PropertyApiBean> apiBean = new ArrayList<PropertyApiBean>();
    getPropertyStore().readAllProperties();
    for (Property<?> prop : getPropertyStore().readAllProperties().values()) {
        apiBean.add(new PropertyApiBean(prop));
    }
    return apiBean;
}
Also used : PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with PropertyApiBean

use of org.ff4j.web.api.resources.domain.PropertyApiBean in project ff4j by ff4j.

the class PropertyStoreHttp method createProperty.

/**
 * {@inheritDoc}
 */
public <T> void createProperty(Property<T> value) {
    Util.assertNotNull(value);
    Util.assertHasLength(value.getName());
    if (existProperty(value.getName())) {
        throw new PropertyAlreadyExistException("Property already exist");
    }
    ClientResponse cRes = // 
    getStore().path(value.getName()).type(// 
    MediaType.APPLICATION_JSON).put(ClientResponse.class, new PropertyApiBean(value));
    // Check response code CREATED or raised error
    if (Status.CREATED.getStatusCode() != cRes.getStatus()) {
        throw new FeatureAccessException("Cannot create properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) FeatureAccessException(org.ff4j.exception.FeatureAccessException) PropertyAlreadyExistException(org.ff4j.exception.PropertyAlreadyExistException)

Example 5 with PropertyApiBean

use of org.ff4j.web.api.resources.domain.PropertyApiBean 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();
}
Also used : PropertyApiBean(org.ff4j.web.api.resources.domain.PropertyApiBean) FlippingStrategyApiBean(org.ff4j.web.api.resources.domain.FlippingStrategyApiBean) URISyntaxException(java.net.URISyntaxException) Feature(org.ff4j.core.Feature) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) FeatureNotFoundException(org.ff4j.exception.FeatureNotFoundException) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

PropertyApiBean (org.ff4j.web.api.resources.domain.PropertyApiBean)5 ClientResponse (com.sun.jersey.api.client.ClientResponse)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Produces (javax.ws.rs.Produces)2 FeatureAccessException (org.ff4j.exception.FeatureAccessException)2 PropertyAlreadyExistException (org.ff4j.exception.PropertyAlreadyExistException)2 WebResource (com.sun.jersey.api.client.WebResource)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1 Feature (org.ff4j.core.Feature)1 FeatureNotFoundException (org.ff4j.exception.FeatureNotFoundException)1