Search in sources :

Example 1 with EventType

use of io.gravitee.management.model.EventType in project gravitee-management-rest-api by gravitee-io.

the class ApiServiceImpl method deployLastPublishedAPI.

private ApiEntity deployLastPublishedAPI(String apiId, String userId, EventType eventType) throws TechnicalException {
    Set<EventEntity> events = eventService.findByApi(apiId);
    Optional<EventEntity> optEvent = events.stream().filter(event -> EventType.PUBLISH_API.equals(event.getType())).sorted((e1, e2) -> e2.getCreatedAt().compareTo(e1.getCreatedAt())).findFirst();
    try {
        if (optEvent.isPresent()) {
            EventEntity event = optEvent.get();
            JsonNode node = objectMapper.readTree(event.getPayload());
            Api lastPublishedAPI = objectMapper.convertValue(node, Api.class);
            lastPublishedAPI.setLifecycleState(convert(eventType));
            lastPublishedAPI.setUpdatedAt(new Date());
            lastPublishedAPI.setDeployedAt(new Date());
            Map<String, String> properties = new HashMap<>();
            properties.put(Event.EventProperties.API_ID.getValue(), lastPublishedAPI.getId());
            properties.put(Event.EventProperties.USER.getValue(), userId);
            // Clear useless field for history
            lastPublishedAPI.setPicture(null);
            // And create event
            eventService.create(eventType, objectMapper.writeValueAsString(lastPublishedAPI), properties);
            return convert(Collections.singleton(lastPublishedAPI), true).iterator().next();
        } else {
            if (events.size() == 0) {
                // this is the first time we start the api without previously deployed id.
                // let's do it.
                this.deploy(apiId, userId, EventType.PUBLISH_API);
                return deployLastPublishedAPI(apiId, userId, eventType);
            }
            throw new TechnicalException("No event found for API " + apiId);
        }
    } catch (Exception e) {
        LOGGER.error("An error occurs while trying to deploy last published API {}", apiId, e);
        throw new TechnicalException("An error occurs while trying to deploy last published API " + apiId, e);
    }
}
Also used : PlanQuery(io.gravitee.management.model.plan.PlanQuery) java.util(java.util) HookScope(io.gravitee.management.service.notification.HookScope) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) MembershipRepository(io.gravitee.repository.management.api.MembershipRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) PageQuery(io.gravitee.management.model.documentation.PageQuery) Visibility(io.gravitee.repository.management.model.Visibility) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Proxy(io.gravitee.definition.model.Proxy) Value(org.springframework.beans.factory.annotation.Value) io.gravitee.management.service.exceptions(io.gravitee.management.service.exceptions) UUID(io.gravitee.common.utils.UUID) EventType(io.gravitee.management.model.EventType) ApiSynchronizationProcessor(io.gravitee.management.service.processor.ApiSynchronizationProcessor) AuditEvent(io.gravitee.repository.management.model.Api.AuditEvent) ApiRepository(io.gravitee.repository.management.api.ApiRepository) io.gravitee.management.model(io.gravitee.management.model) GenericNotificationConfigEntity(io.gravitee.management.model.notification.GenericNotificationConfigEntity) SystemRole(io.gravitee.management.model.permissions.SystemRole) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Lifecycle(io.gravitee.common.component.Lifecycle) SearchableUser(io.gravitee.management.idp.api.identity.SearchableUser) Logger(org.slf4j.Logger) ApiHook(io.gravitee.management.service.notification.ApiHook) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) Component(org.springframework.stereotype.Component) io.gravitee.repository.management.model(io.gravitee.repository.management.model) io.gravitee.management.service(io.gravitee.management.service) Path(io.gravitee.definition.model.Path) PageType(io.gravitee.management.model.PageType) DatatypeConverter(javax.xml.bind.DatatypeConverter) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) JsonNode(com.fasterxml.jackson.databind.JsonNode) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Lifecycle (io.gravitee.common.component.Lifecycle)1 UUID (io.gravitee.common.utils.UUID)1 Path (io.gravitee.definition.model.Path)1 Proxy (io.gravitee.definition.model.Proxy)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 SearchableUser (io.gravitee.management.idp.api.identity.SearchableUser)1 io.gravitee.management.model (io.gravitee.management.model)1 EventType (io.gravitee.management.model.EventType)1 PageType (io.gravitee.management.model.PageType)1 PageQuery (io.gravitee.management.model.documentation.PageQuery)1 GenericNotificationConfigEntity (io.gravitee.management.model.notification.GenericNotificationConfigEntity)1 SystemRole (io.gravitee.management.model.permissions.SystemRole)1 PlanQuery (io.gravitee.management.model.plan.PlanQuery)1 io.gravitee.management.service (io.gravitee.management.service)1 io.gravitee.management.service.exceptions (io.gravitee.management.service.exceptions)1