Search in sources :

Example 1 with ApplicationNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException 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 2 with ApplicationNotFoundException

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

the class ApplicationMetadataResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApplicationMetadataEntity applicationMetadataEntity1 = new ApplicationMetadataEntity();
    applicationMetadataEntity1.setKey(METADATA_1);
    ApplicationMetadataEntity applicationMetadataEntity2 = new ApplicationMetadataEntity();
    applicationMetadataEntity2.setKey(METADATA_2);
    when(referenceMetadataMapper.convert(any())).thenCallRealMethod();
    when(referenceMetadataMapper.convert(any(), any())).thenCallRealMethod();
    when(referenceMetadataMapper.convert(any(), any(), any())).thenCallRealMethod();
    doReturn(Arrays.asList(applicationMetadataEntity1, applicationMetadataEntity2)).when(applicationMetadataService).findAllByApplication(APPLICATION);
    doReturn(applicationMetadataEntity1).when(applicationMetadataService).findByIdAndApplication(METADATA_1, APPLICATION);
    doReturn(null).when(applicationMetadataService).findByIdAndApplication(METADATA_2, APPLICATION);
    when(applicationMetadataService.create(any())).thenAnswer(invocation -> {
        NewApplicationMetadataEntity newApplicationMetadataEntity = invocation.getArgument(0);
        if (newApplicationMetadataEntity.getApplicationId().equals(UNKNOWN_APPLICATION)) {
            throw new ApplicationNotFoundException(UNKNOWN_APPLICATION);
        }
        return applicationMetadataEntity1;
    });
    when(applicationMetadataService.update(any())).thenAnswer(invocation -> {
        UpdateApplicationMetadataEntity updateApplicationMetadataEntity = invocation.getArgument(0);
        if (updateApplicationMetadataEntity.getApplicationId().equals(UNKNOWN_APPLICATION)) {
            throw new ApplicationNotFoundException(UNKNOWN_APPLICATION);
        }
        if (updateApplicationMetadataEntity.getKey().equals(UNKNOWN_METADATA)) {
            throw new ApplicationMetadataNotFoundException(updateApplicationMetadataEntity.getApplicationId(), UNKNOWN_METADATA);
        }
        return applicationMetadataEntity1;
    });
    doThrow(ApplicationNotFoundException.class).when(applicationMetadataService).findAllByApplication(UNKNOWN_APPLICATION);
    doThrow(ApplicationNotFoundException.class).when(applicationMetadataService).findByIdAndApplication(any(), eq(UNKNOWN_APPLICATION));
    doThrow(ApplicationNotFoundException.class).when(applicationMetadataService).delete(any(), eq(UNKNOWN_APPLICATION));
    doThrow(ApplicationMetadataNotFoundException.class).when(applicationMetadataService).findByIdAndApplication(UNKNOWN_METADATA, APPLICATION);
    doThrow(ApplicationMetadataNotFoundException.class).when(applicationMetadataService).delete(eq(UNKNOWN_METADATA), any());
}
Also used : NewApplicationMetadataEntity(io.gravitee.rest.api.model.NewApplicationMetadataEntity) UpdateApplicationMetadataEntity(io.gravitee.rest.api.model.UpdateApplicationMetadataEntity) ApplicationMetadataEntity(io.gravitee.rest.api.model.ApplicationMetadataEntity) UpdateApplicationMetadataEntity(io.gravitee.rest.api.model.UpdateApplicationMetadataEntity) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) NewApplicationMetadataEntity(io.gravitee.rest.api.model.NewApplicationMetadataEntity) ApplicationMetadataNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationMetadataNotFoundException) Before(org.junit.Before)

Example 3 with ApplicationNotFoundException

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

the class ApplicationSubscribersResource method getSubscriberApisByApplicationId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApisByApplicationId(@BeanParam PaginationParam paginationParam, @PathParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
    String currentUser = getAuthenticatedUserOrNull();
    Collection<ApplicationListItem> userApplications = applicationService.findByUser(currentUser);
    Optional<ApplicationListItem> optionalApplication = userApplications.stream().filter(a -> a.getId().equals(applicationId)).findFirst();
    if (optionalApplication.isPresent()) {
        SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
        subscriptionQuery.setApplication(applicationId);
        subscriptionQuery.setStatuses(statuses);
        ApplicationListItem application = optionalApplication.get();
        if (!application.getPrimaryOwner().getId().equals(currentUser)) {
            Set<ApiEntity> userApis = this.apiService.findPublishedByUser(currentUser);
            if (userApis == null || userApis.isEmpty()) {
                return createListResponse(Collections.emptyList(), paginationParam);
            }
            subscriptionQuery.setApis(userApis.stream().map(ApiEntity::getId).collect(Collectors.toList()));
        }
        Map<String, Long> nbHitsByApp = getNbHitsByApplication(applicationId);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
        List<Api> subscribersApis = subscriptions.stream().map(SubscriptionEntity::getApi).distinct().map(api -> apiService.findById(api)).map(apiMapper::convert).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
        return createListResponse(subscribersApis, paginationParam);
    }
    throw new ApplicationNotFoundException(applicationId);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ApiMapper(io.gravitee.rest.api.portal.rest.mapper.ApiMapper) Api(io.gravitee.rest.api.portal.rest.model.Api) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) 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) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Aggregations

ApplicationNotFoundException (io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException)3 MediaType (io.gravitee.common.http.MediaType)2 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)2 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)2 ApplicationService (io.gravitee.rest.api.service.ApplicationService)2 Inject (javax.inject.Inject)2 Response (javax.ws.rs.core.Response)2 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)1 ApplicationMetadataEntity (io.gravitee.rest.api.model.ApplicationMetadataEntity)1 NewApplicationMetadataEntity (io.gravitee.rest.api.model.NewApplicationMetadataEntity)1 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)1 SubscriptionStatus (io.gravitee.rest.api.model.SubscriptionStatus)1 UpdateApplicationMetadataEntity (io.gravitee.rest.api.model.UpdateApplicationMetadataEntity)1 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)1 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)1 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1 ApiMapper (io.gravitee.rest.api.portal.rest.mapper.ApiMapper)1 Api (io.gravitee.rest.api.portal.rest.model.Api)1 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)1