use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApplicationSubscribedResource method getApiSubscribed.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List APIs subscribed by the application", notes = "User must have the APPLICATION_SUBSCRIPTION permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Paged result of subscribed APIs", response = ApplicationEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.APPLICATION_SUBSCRIPTION, acls = RolePermissionAction.READ) })
public Collection<SubscribedApi> getApiSubscribed() {
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApplication(application);
Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
return subscriptions.stream().map(SubscriptionEntity::getApi).distinct().map(api -> apiService.findById(api)).map(apiEntity -> new SubscribedApi(apiEntity.getId(), apiEntity.getName())).sorted((o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName())).collect(Collectors.toList());
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscriptionsResource method getApiSubscriptions.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List subscriptions for the API", notes = "User must have the READ_SUBSCRIPTION permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Paged result of API's subscriptions", response = PagedResult.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_SUBSCRIPTION, acls = RolePermissionAction.READ) })
public PagedResult<SubscriptionEntity> getApiSubscriptions(@BeanParam SubscriptionParam subscriptionParam, @Valid @BeanParam Pageable pageable, @ApiParam(allowableValues = "keys", value = "Expansion of data to return in subscriptions") @QueryParam("expand") List<String> expand) {
// Transform query parameters to a subscription query
SubscriptionQuery subscriptionQuery = subscriptionParam.toQuery();
subscriptionQuery.setApi(api);
Page<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery, pageable.toPageable());
if (expand != null && !expand.isEmpty()) {
for (String e : expand) {
switch(e) {
case "keys":
subscriptions.getContent().forEach(subscriptionEntity -> {
final List<String> keys = apiKeyService.findBySubscription(subscriptionEntity.getId()).stream().filter(apiKeyEntity -> !apiKeyEntity.isExpired() && !apiKeyEntity.isRevoked()).map(ApiKeyEntity::getKey).collect(Collectors.toList());
subscriptionEntity.setKeys(keys);
});
break;
default:
break;
}
}
}
PagedResult<SubscriptionEntity> result = new PagedResult<>(subscriptions, pageable.getSize());
result.setMetadata(subscriptionService.getMetadata(subscriptions.getContent()).getMetadata());
return result;
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class TaskServiceImpl method findAll.
@Override
public List<TaskEntity> findAll(String userId) {
if (userId == null) {
throw new UnauthorizedAccessException();
}
try {
// because Tasks only consists on subscriptions, we can optimize the search by only look for apis where
// the user has a SUBSCRIPTION_UPDATE permission
// search for PENDING subscriptions
Set<String> apiIds = getApisForAPermission(userId, SUBSCRIPTION.getName());
final List<TaskEntity> tasks;
if (apiIds.isEmpty()) {
tasks = new ArrayList<>();
} else {
SubscriptionQuery query = new SubscriptionQuery();
query.setStatuses(singleton(PENDING));
query.setApis(apiIds);
tasks = subscriptionService.search(query).stream().map(this::convert).collect(toList());
}
// search for PENDING user registration
final Page<UserEntity> pendingUsers = userService.search(new UserCriteria.Builder().statuses(UserStatus.PENDING).build(), new PageableImpl(1, NUMBER_OF_PENDING_USERS_TO_SEARCH));
if (pendingUsers.getContent() != null && !pendingUsers.getContent().isEmpty()) {
tasks.addAll(pendingUsers.getContent().stream().map(this::convert).collect(toList()));
}
// search for IN_REVIEW apis
apiIds = getApisForAPermission(userId, REVIEWS.getName());
if (!apiIds.isEmpty()) {
apiIds.forEach(apiId -> {
final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
if (workflows != null && !workflows.isEmpty()) {
final Workflow currentWorkflow = workflows.get(0);
if (WorkflowState.IN_REVIEW.name().equals(currentWorkflow.getState())) {
tasks.add(convert(currentWorkflow));
}
}
});
}
// search for REQUEST_FOR_CHANGES apis
apiIds = getApisForAPermission(userId, DEFINITION.getName());
if (!apiIds.isEmpty()) {
apiIds.forEach(apiId -> {
final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
if (workflows != null && !workflows.isEmpty()) {
final Workflow currentWorkflow = workflows.get(0);
if (WorkflowState.REQUEST_FOR_CHANGES.name().equals(currentWorkflow.getState())) {
tasks.add(convert(currentWorkflow));
}
}
});
}
// search for TO_BE_VALIDATED promotions
tasks.addAll(promotionTasksService.getPromotionTasks(GraviteeContext.getCurrentOrganization()));
return tasks;
} catch (TechnicalException e) {
LOGGER.error("Error retrieving user tasks {}", e.getMessage());
throw new TechnicalManagementException("Error retreiving user tasks", e);
}
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class FilteringServiceImpl method getCurrentUserSubscribedApis.
private FilteredEntities<ApiEntity> getCurrentUserSubscribedApis(Collection<ApiEntity> apis, boolean excluded) {
// get Current user applications
List<String> currentUserApplicationsId = applicationService.findByUser(getAuthenticatedUser().getUsername()).stream().map(ApplicationListItem::getId).collect(Collectors.toList());
// find all subscribed apis for these applications
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApplications(currentUserApplicationsId);
List<String> subscribedApis = subscriptionService.search(subscriptionQuery).stream().map(SubscriptionEntity::getApi).distinct().collect(Collectors.toList());
// filter apis list with subscribed apis list
return new FilteredEntities<>(apis.stream().filter(api -> (!excluded && subscribedApis.contains(api.getId())) || (excluded && !subscribedApis.contains(api.getId()))).sorted((a1, a2) -> String.CASE_INSENSITIVE_ORDER.compare(a1.getName(), a2.getName())).collect(Collectors.toList()), null);
}
use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscribersResource method getSubscriberApplicationsByApiId.
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApplicationsByApiId(@BeanParam PaginationParam paginationParam, @PathParam("apiId") String apiId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
String currentUser = getAuthenticatedUserOrNull();
final ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Collection<ApiEntity> userApis = apiService.findPublishedByUser(currentUser, apiQuery);
Optional<ApiEntity> optionalApi = userApis.stream().filter(a -> a.getId().equals(apiId)).findFirst();
if (optionalApi.isPresent()) {
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApi(apiId);
subscriptionQuery.setStatuses(statuses);
ApiEntity api = optionalApi.get();
if (!api.getPrimaryOwner().getId().equals(currentUser)) {
Set<ApplicationListItem> userApplications = this.applicationService.findByUser(currentUser);
if (userApplications == null || userApplications.isEmpty()) {
return createListResponse(Collections.emptyList(), paginationParam);
}
subscriptionQuery.setApplications(userApplications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
}
Map<String, Long> nbHitsByApp = getNbHitsByApplication(apiId);
Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
List<Application> subscribersApplication = subscriptions.stream().map(SubscriptionEntity::getApplication).distinct().map(application -> applicationService.findById(application)).map(application -> applicationMapper.convert(application, uriInfo)).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
return createListResponse(subscribersApplication, paginationParam);
}
throw new ApiNotFoundException(apiId);
}
Aggregations