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);
}
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);
}
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);
}
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);
}
}
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));
}
}
}
Aggregations