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