Search in sources :

Example 1 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method createWithImportedDefinition.

@Override
public ApiEntity createWithImportedDefinition(String apiDefinitionOrURL, String userId, String organizationId, String environmentId) {
    String apiDefinition = fetchApiDefinitionContentFromURL(apiDefinitionOrURL);
    try {
        // Read the whole definition
        final JsonNode jsonNode = objectMapper.readTree(apiDefinition);
        UpdateApiEntity importedApi = convertToEntity(apiDefinition, jsonNode);
        ApiEntity createdApiEntity = apiService.createWithApiDefinition(importedApi, userId, jsonNode);
        createPageAndMedia(createdApiEntity, jsonNode, environmentId);
        updateApiReferences(createdApiEntity, jsonNode, organizationId, environmentId, false);
        return createdApiEntity;
    } catch (JsonProcessingException e) {
        LOGGER.error("An error occurs while trying to JSON deserialize the API {}", apiDefinition, e);
        throw new TechnicalManagementException("An error occurs while trying to JSON deserialize the API definition.");
    }
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DuplicateApiEntity(io.gravitee.rest.api.model.api.DuplicateApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UuidString(io.gravitee.rest.api.service.common.UuidString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method updateWithImportedDefinition.

@Override
public ApiEntity updateWithImportedDefinition(String apiId, String apiDefinitionOrURL, String userId, String organizationId, String environmentId) {
    String apiDefinition = fetchApiDefinitionContentFromURL(apiDefinitionOrURL);
    try {
        // Read the whole definition
        final JsonNode jsonNode = objectMapper.readTree(apiDefinition);
        UpdateApiEntity importedApi = convertToEntity(apiDefinition, jsonNode);
        ApiEntity updatedApiEntity = apiService.update(apiId, importedApi, false);
        updateApiReferences(updatedApiEntity, jsonNode, organizationId, environmentId, true);
        return updatedApiEntity;
    } catch (JsonProcessingException e) {
        LOGGER.error("An error occurs while trying to JSON deserialize the API {}", apiDefinition, e);
        throw new TechnicalManagementException("An error occurs while trying to JSON deserialize the API definition.");
    }
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DuplicateApiEntity(io.gravitee.rest.api.model.api.DuplicateApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UuidString(io.gravitee.rest.api.service.common.UuidString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method convert.

private UpdateApiEntity convert(final ApiEntity apiEntity) {
    final UpdateApiEntity updateApiEntity = new UpdateApiEntity();
    updateApiEntity.setDescription(apiEntity.getDescription());
    updateApiEntity.setName(apiEntity.getName());
    updateApiEntity.setVersion(apiEntity.getVersion());
    updateApiEntity.setGraviteeDefinitionVersion(apiEntity.getGraviteeDefinitionVersion());
    updateApiEntity.setGroups(apiEntity.getGroups());
    updateApiEntity.setLabels(apiEntity.getLabels());
    updateApiEntity.setLifecycleState(apiEntity.getLifecycleState());
    updateApiEntity.setPicture(apiEntity.getPicture());
    updateApiEntity.setBackground(apiEntity.getBackground());
    updateApiEntity.setProperties(apiEntity.getProperties());
    updateApiEntity.setProxy(apiEntity.getProxy());
    updateApiEntity.setResources(apiEntity.getResources());
    updateApiEntity.setResponseTemplates(apiEntity.getResponseTemplates());
    updateApiEntity.setServices(apiEntity.getServices());
    updateApiEntity.setTags(apiEntity.getTags());
    updateApiEntity.setCategories(apiEntity.getCategories());
    updateApiEntity.setVisibility(apiEntity.getVisibility());
    updateApiEntity.setPaths(apiEntity.getPaths());
    updateApiEntity.setFlows(apiEntity.getFlows());
    updateApiEntity.setPathMappings(apiEntity.getPathMappings());
    updateApiEntity.setDisableMembershipNotifications(apiEntity.isDisableMembershipNotifications());
    updateApiEntity.setPlans(apiEntity.getPlans());
    return updateApiEntity;
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity)

Example 4 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method create.

@Override
public PlanEntity create(NewPlanEntity newPlan) {
    try {
        logger.debug("Create a new plan {} for API {}", newPlan.getName(), newPlan.getApi());
        assertPlanSecurityIsAllowed(newPlan.getSecurity());
        final ApiEntity api = apiService.findById(newPlan.getApi());
        if (ApiLifecycleState.DEPRECATED.equals(api.getLifecycleState())) {
            throw new ApiDeprecatedException(api.getName());
        }
        String id = newPlan.getId() != null && UUID.fromString(newPlan.getId()) != null ? newPlan.getId() : UuidString.generateRandom();
        Plan plan = new Plan();
        plan.setId(id);
        plan.setApi(newPlan.getApi());
        plan.setName(newPlan.getName());
        plan.setDescription(newPlan.getDescription());
        plan.setCreatedAt(new Date());
        plan.setUpdatedAt(plan.getCreatedAt());
        plan.setNeedRedeployAt(plan.getCreatedAt());
        plan.setType(Plan.PlanType.valueOf(newPlan.getType().name()));
        plan.setSecurity(Plan.PlanSecurityType.valueOf(newPlan.getSecurity().name()));
        plan.setSecurityDefinition(newPlan.getSecurityDefinition());
        plan.setStatus(Plan.Status.valueOf(newPlan.getStatus().name()));
        plan.setExcludedGroups(newPlan.getExcludedGroups());
        plan.setCommentRequired(newPlan.isCommentRequired());
        plan.setCommentMessage(newPlan.getCommentMessage());
        plan.setTags(newPlan.getTags());
        plan.setSelectionRule(newPlan.getSelectionRule());
        plan.setGeneralConditions(newPlan.getGeneralConditions());
        if (plan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // There is no need for a validation when authentication is KEY_LESS, force to AUTO
            plan.setValidation(Plan.PlanValidationType.AUTO);
        } else {
            plan.setValidation(Plan.PlanValidationType.valueOf(newPlan.getValidation().name()));
        }
        plan.setCharacteristics(newPlan.getCharacteristics());
        if (!DefinitionVersion.V2.equals(DefinitionVersion.valueOfLabel(api.getGraviteeDefinitionVersion()))) {
            String planPolicies = objectMapper.writeValueAsString(newPlan.getPaths());
            plan.setDefinition(planPolicies);
        }
        plan = planRepository.create(plan);
        if (DefinitionVersion.V2.equals(DefinitionVersion.valueOfLabel(api.getGraviteeDefinitionVersion()))) {
            UpdateApiEntity updateApi = ApiService.convert(api);
            updateApi.addPlan(fillApiDefinitionPlan(new io.gravitee.definition.model.Plan(), plan, newPlan.getFlows()));
            apiService.update(api.getId(), updateApi);
        }
        auditService.createApiAuditLog(newPlan.getApi(), Collections.singletonMap(PLAN, plan.getId()), PLAN_CREATED, plan.getCreatedAt(), null, plan);
        return convert(plan);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to create a plan {} for API {}", newPlan.getName(), newPlan.getApi(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), ex);
    } catch (JsonProcessingException jse) {
        logger.error("Unexpected error while generating plan definition", jse);
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a plan %s for API %s", newPlan.getName(), newPlan.getApi()), jse);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Plan(io.gravitee.repository.management.model.Plan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with UpdateApiEntity

use of io.gravitee.rest.api.model.api.UpdateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiPlansResource method removePlanFromApiDefinition.

private void removePlanFromApiDefinition(String planId, String apiId) {
    // Remove plan from api definition
    if (apiId != null) {
        ApiEntity api = apiService.findById(apiId);
        if (DefinitionVersion.V2.equals(DefinitionVersion.valueOfLabel(api.getGraviteeDefinitionVersion()))) {
            List<io.gravitee.definition.model.Plan> plans = api.getPlans().stream().filter(plan1 -> plan1.getId() != null && !plan1.getId().equals(planId)).collect(Collectors.toList());
            UpdateApiEntity updateApiEntity = ApiService.convert(api);
            updateApiEntity.setPlans(plans);
            if (api.getPlans().size() != updateApiEntity.getPlans().size()) {
                apiService.update(apiId, updateApiEntity);
            }
        }
    }
}
Also used : PlanSecurityParam(io.gravitee.rest.api.management.rest.resource.param.PlanSecurityParam) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) Valid(javax.validation.Valid) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) io.gravitee.rest.api.model(io.gravitee.rest.api.model) io.swagger.annotations(io.swagger.annotations) API_PLAN(io.gravitee.rest.api.model.permissions.RolePermission.API_PLAN) PlanStatusParam(io.gravitee.rest.api.management.rest.resource.param.PlanStatusParam) DELETE(javax.ws.rs.DELETE) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) Comparator.comparingInt(java.util.Comparator.comparingInt) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GroupService(io.gravitee.rest.api.service.GroupService) Context(javax.ws.rs.core.Context) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Permission(io.gravitee.rest.api.management.rest.security.Permission) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) DefinitionVersion(io.gravitee.definition.model.DefinitionVersion) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ResourceContext(javax.ws.rs.container.ResourceContext) API_GATEWAY_DEFINITION(io.gravitee.rest.api.model.permissions.RolePermission.API_GATEWAY_DEFINITION) PlanService(io.gravitee.rest.api.service.PlanService) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity)

Aggregations

UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)9 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)6 UuidString (io.gravitee.rest.api.service.common.UuidString)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DuplicateApiEntity (io.gravitee.rest.api.model.api.DuplicateApiEntity)3 Proxy (io.gravitee.definition.model.Proxy)2 VirtualHost (io.gravitee.definition.model.VirtualHost)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 Before (org.junit.Before)2 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 MediaType (io.gravitee.common.http.MediaType)1 DefinitionVersion (io.gravitee.definition.model.DefinitionVersion)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Plan (io.gravitee.repository.management.model.Plan)1 PlanSecurityParam (io.gravitee.rest.api.management.rest.resource.param.PlanSecurityParam)1 PlanStatusParam (io.gravitee.rest.api.management.rest.resource.param.PlanStatusParam)1 Permission (io.gravitee.rest.api.management.rest.security.Permission)1