Search in sources :

Example 81 with ApiEntity

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

the class ApiResource method getBackgroundByApiId.

@GET
@Path("background")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
public Response getBackgroundByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        InlinePictureEntity image = apiService.getBackground(apiId);
        return createPictureResponse(request, image);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity)

Example 82 with ApiEntity

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

the class ApiResource method getPictureByApiId.

@GET
@Path("picture")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getPictureByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    // Do not filter on visibility to display the picture on subscription screen even if the API is no more published
    Collection<ApiEntity> userApis = apiService.findByUser(getAuthenticatedUserOrNull(), apiQuery, true);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        InlinePictureEntity image = apiService.getPicture(apiId);
        return createPictureResponse(request, image);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 83 with ApiEntity

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

the class ApiMediaResource method getApiMedia.

@GET
@Path("{mediaHash}")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
public Response getApiMedia(@Context Request request, @PathParam("apiId") String apiId, @PathParam("mediaHash") String mediaHash) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        MediaEntity mediaEntity = mediaService.findByHashAndApi(mediaHash, apiId, true);
        if (mediaEntity == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        return createMediaResponse(request, mediaHash, mediaEntity);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) MediaEntity(io.gravitee.rest.api.model.MediaEntity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 84 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity 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 85 with ApiEntity

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

the class PlanServiceImpl method updatePlanToApiDefinition.

private void updatePlanToApiDefinition(Plan updatedPlan) {
    String apiId = updatedPlan.getApi();
    if (apiId != null) {
        ApiEntity api = apiService.findById(apiId);
        if (DefinitionVersion.V2.equals(DefinitionVersion.valueOfLabel(api.getGraviteeDefinitionVersion()))) {
            api.getPlans().stream().forEach(plan -> {
                if (plan.getId().equals(updatedPlan.getId())) {
                    fillApiDefinitionPlan(plan, updatedPlan, null);
                }
            });
            apiService.update(updatedPlan.getApi(), ApiService.convert(api));
        }
    }
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UuidString(io.gravitee.rest.api.service.common.UuidString)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17