Search in sources :

Example 1 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class PlatformEventsResource method getPlatformEvents.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List platform events", notes = "User must have the MANAGEMENT_PLATFORM[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Platform events", response = EventEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_PLATFORM, acls = RolePermissionAction.READ) })
public Page<EventEntity> getPlatformEvents(@BeanParam EventSearchParam eventSearchParam) {
    eventSearchParam.validate();
    Map<String, Object> properties = new HashMap<>();
    if (eventSearchParam.getApiIdsParam() != null && eventSearchParam.getApiIdsParam().getIds() != null && !eventSearchParam.getApiIdsParam().getIds().isEmpty()) {
        properties.put(Event.EventProperties.API_ID.getValue(), eventSearchParam.getApiIdsParam().getIds());
    } else if (!isAdmin()) {
        properties.put(Event.EventProperties.API_ID.getValue(), apiService.findByUser(getAuthenticatedUser(), null, false).stream().filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ)).map(ApiEntity::getId).collect(Collectors.joining(",")));
    }
    Page<EventEntity> events = eventService.search(eventSearchParam.getEventTypeListParam().getEventTypes(), properties, eventSearchParam.getFrom(), eventSearchParam.getTo(), eventSearchParam.getPage(), eventSearchParam.getSize());
    events.getContent().forEach(event -> {
        Map<String, String> properties1 = event.getProperties();
        // Event relative to API
        if (properties1 != null && properties1.containsKey(Event.EventProperties.API_ID.getValue())) {
            // Remove payload content from response since it's not required anymore
            event.setPayload(null);
            // Retrieve additional data
            String apiId = properties1.get(Event.EventProperties.API_ID.getValue());
            try {
                ApiEntity api = apiService.findById(apiId);
                properties1.put("api_name", api.getName());
                properties1.put("api_version", api.getVersion());
            } catch (ApiNotFoundException anfe) {
                properties1.put("deleted", Boolean.TRUE.toString());
                properties1.put("api_name", "Deleted API");
            }
        }
    });
    return events;
}
Also used : HashMap(java.util.HashMap) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) EventEntity(io.gravitee.rest.api.model.EventEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class PermissionsResource method getCurrentUserPermissions.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCurrentUserPermissions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId) {
    final String userId = getAuthenticatedUser();
    if (apiId != null) {
        ApiQuery apiQuery = new ApiQuery();
        apiQuery.setIds(Collections.singletonList(apiId));
        Set<ApiEntity> publishedByUser = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
        ApiEntity apiEntity = publishedByUser.stream().filter(a -> a.getId().equals(apiId)).findFirst().orElseThrow(() -> new ApiNotFoundException(apiId));
        Map<String, char[]> permissions;
        permissions = membershipService.getUserMemberPermissions(apiEntity, userId);
        return Response.ok(permissions).build();
    } else if (applicationId != null) {
        ApplicationListItem applicationListItem = applicationService.findByUser(getAuthenticatedUser()).stream().filter(a -> a.getId().equals(applicationId)).findFirst().orElseThrow(() -> new ApplicationNotFoundException(applicationId));
        ApplicationEntity application = applicationService.findById(applicationListItem.getId());
        Map<String, char[]> permissions;
        permissions = membershipService.getUserMemberPermissions(application, userId);
        return Response.ok(permissions).build();
    }
    throw new BadRequestException("One of the two parameters appId or applicationId must not be null.");
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) MembershipService(io.gravitee.rest.api.service.MembershipService) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Set(java.util.Set) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) MediaType(io.gravitee.common.http.MediaType) QueryParam(javax.ws.rs.QueryParam) Response(javax.ws.rs.core.Response) Map(java.util.Map) ApplicationService(io.gravitee.rest.api.service.ApplicationService) BadRequestException(javax.ws.rs.BadRequestException) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) BadRequestException(javax.ws.rs.BadRequestException) Map(java.util.Map) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiPlansResource method getApiPlansByApiId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getApiPlansByApiId(@PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam) {
    String username = getAuthenticatedUserOrNull();
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(username, apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        ApiEntity apiEntity = apiService.findById(apiId);
        if (Visibility.PUBLIC.equals(apiEntity.getVisibility()) || hasPermission(API_PLAN, apiId, READ)) {
            List<Plan> plans = planService.findByApi(apiId).stream().filter(plan -> PlanStatus.PUBLISHED.equals(plan.getStatus())).filter(plan -> groupService.isUserAuthorizedToAccessApiData(apiEntity, plan.getExcludedGroups(), username)).sorted(Comparator.comparingInt(PlanEntity::getOrder)).map(p -> planMapper.convert(p)).collect(Collectors.toList());
            return createListResponse(plans, paginationParam);
        } else {
            return createListResponse(emptyList(), paginationParam);
        }
    }
    throw new ApiNotFoundException(apiId);
}
Also used : PlanMapper(io.gravitee.rest.api.portal.rest.mapper.PlanMapper) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Plan(io.gravitee.rest.api.portal.rest.model.Plan) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) PlanStatus(io.gravitee.rest.api.model.PlanStatus) Inject(javax.inject.Inject) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) API_PLAN(io.gravitee.rest.api.model.permissions.RolePermission.API_PLAN) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) READ(io.gravitee.rest.api.model.permissions.RolePermissionAction.READ) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GroupService(io.gravitee.rest.api.service.GroupService) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) Visibility(io.gravitee.rest.api.model.Visibility) PlanService(io.gravitee.rest.api.service.PlanService) Comparator(java.util.Comparator) Collections(java.util.Collections) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 4 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingsResource method createApiRating.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.CREATE) })
public Response createApiRating(@PathParam("apiId") String apiId, @Valid RatingInput ratingInput) {
    if (ratingInput == null) {
        throw new BadRequestException("Input must not be null.");
    }
    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))) {
        NewRatingEntity rating = new NewRatingEntity();
        rating.setApi(apiId);
        rating.setComment(ratingInput.getComment());
        rating.setTitle(ratingInput.getTitle());
        rating.setRate(ratingInput.getValue().byteValue());
        RatingEntity createdRating = ratingService.create(rating);
        return Response.status(Status.CREATED).entity(ratingMapper.convert(createdRating, uriInfo)).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 5 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException 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);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) ApplicationMapper(io.gravitee.rest.api.portal.rest.mapper.ApplicationMapper) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Inject(javax.inject.Inject) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) MediaType(io.gravitee.common.http.MediaType) ChronoUnit(java.time.temporal.ChronoUnit) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Application(io.gravitee.rest.api.portal.rest.model.Application)

Aggregations

ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)19 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)18 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)16 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)9 Produces (javax.ws.rs.Produces)7 MediaType (io.gravitee.common.http.MediaType)6 RatingEntity (io.gravitee.rest.api.model.RatingEntity)6 Inject (javax.inject.Inject)6 GET (javax.ws.rs.GET)6 Response (javax.ws.rs.core.Response)6 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)5 Collectors (java.util.stream.Collectors)5 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)4 Collections (java.util.Collections)4 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)3 List (java.util.List)3 javax.ws.rs (javax.ws.rs)3 Context (javax.ws.rs.core.Context)3 NewRatingEntity (io.gravitee.rest.api.model.NewRatingEntity)2 PageEntity (io.gravitee.rest.api.model.PageEntity)2