Search in sources :

Example 1 with Page

use of io.gravitee.common.data.domain.Page in project gravitee-management-rest-api by gravitee-io.

the class AuditServiceImpl method getMetadata.

private Map<String, String> getMetadata(List<AuditEntity> content) {
    Map<String, String> metadata = new HashMap<>();
    for (AuditEntity auditEntity : content) {
        if (auditEntity.getProperties() != null) {
            for (Map.Entry<String, String> property : auditEntity.getProperties().entrySet()) {
                String metadataKey = new StringJoiner(":").add(property.getKey()).add(property.getValue()).add("name").toString();
                if (!metadata.containsKey(metadataKey)) {
                    String name = property.getValue();
                    try {
                        switch(Audit.AuditProperties.valueOf(property.getKey())) {
                            case PAGE:
                                Optional<io.gravitee.repository.management.model.Page> optPage = pageRepository.findById(property.getValue());
                                if (optPage.isPresent()) {
                                    name = optPage.get().getName();
                                }
                                break;
                            case PLAN:
                                Optional<Plan> optPlan = planRepository.findById(property.getValue());
                                if (optPlan.isPresent()) {
                                    name = optPlan.get().getName();
                                }
                                break;
                            case METADATA:
                                MetadataReferenceType refType = (Audit.AuditReferenceType.API.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.API : (Audit.AuditReferenceType.APPLICATION.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.APPLICATION : MetadataReferenceType.DEFAULT;
                                String refId = refType.equals(MetadataReferenceType.DEFAULT) ? getDefautReferenceId() : auditEntity.getReferenceId();
                                Optional<Metadata> optMetadata = metadataRepository.findById(property.getValue(), refId, refType);
                                if (optMetadata.isPresent()) {
                                    name = optMetadata.get().getName();
                                }
                                break;
                            case GROUP:
                                Optional<Group> optGroup = groupRepository.findById(property.getValue());
                                if (optGroup.isPresent()) {
                                    name = optGroup.get().getName();
                                }
                                break;
                            case USER:
                                Optional<User> optUser = userRepository.findById(property.getValue());
                                if (optUser.isPresent()) {
                                    if (optUser.get().getFirstname() != null && optUser.get().getLastname() != null) {
                                        name = optUser.get().getFirstname() + " " + optUser.get().getLastname();
                                    } else {
                                        name = optUser.get().getUsername();
                                    }
                                }
                            default:
                                break;
                        }
                    } catch (TechnicalException e) {
                        LOGGER.error("Error finding metadata {}", metadataKey);
                        name = property.getValue();
                    }
                    metadata.put(metadataKey, name);
                }
            }
        }
    }
    return metadata;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Page(io.gravitee.common.data.domain.Page) MetadataPage(io.gravitee.common.data.domain.MetadataPage) AuditEntity(io.gravitee.management.model.audit.AuditEntity)

Example 2 with Page

use of io.gravitee.common.data.domain.Page in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findByApi.

@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Page<Rating> pageRating = ratingRepository.findByApiPageable(api, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
        final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
        return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) Page(io.gravitee.common.data.domain.Page) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with Page

use of io.gravitee.common.data.domain.Page in project gravitee-gateway by gravitee-io.

the class SyncManagerTest method shouldNotDeployBecauseWrongConfiguration.

@Test(expected = IllegalArgumentException.class)
public void shouldNotDeployBecauseWrongConfiguration() throws Exception {
    io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build();
    final Api mockApi = mockApi(api);
    when(gatewayConfiguration.shardingTags()).thenReturn(Optional.of(Arrays.asList("test", "!test")));
    when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
    when(apiManager.apis()).thenReturn(Collections.singleton(mockApi));
    final Event mockEvent = mockEvent(api, EventType.PUBLISH_API);
    when(eventRepository.search(eq(new EventCriteria.Builder().types(EventType.PUBLISH_API, EventType.UNPUBLISH_API, EventType.START_API, EventType.STOP_API).property(Event.EventProperties.API_ID.getValue(), api.getId()).build()), any())).thenReturn((new Page(Collections.singletonList(mockEvent), 0, 0, 0)));
    syncManager.refresh();
}
Also used : Event(io.gravitee.repository.management.model.Event) Page(io.gravitee.common.data.domain.Page) RepositoryApiBuilder(io.gravitee.gateway.services.sync.builder.RepositoryApiBuilder) Api(io.gravitee.gateway.handlers.api.definition.Api) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) Test(org.junit.Test)

Aggregations

Page (io.gravitee.common.data.domain.Page)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 MetadataPage (io.gravitee.common.data.domain.MetadataPage)1 Api (io.gravitee.gateway.handlers.api.definition.Api)1 RepositoryApiBuilder (io.gravitee.gateway.services.sync.builder.RepositoryApiBuilder)1 AuditEntity (io.gravitee.management.model.audit.AuditEntity)1 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 EventCriteria (io.gravitee.repository.management.api.search.EventCriteria)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 Event (io.gravitee.repository.management.model.Event)1 Rating (io.gravitee.repository.management.model.Rating)1 Test (org.junit.Test)1