Search in sources :

Example 61 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class SyncManager method refresh.

public void refresh() {
    logger.debug("Refreshing state...");
    try {
        // Extract all registered APIs
        Map<String, io.gravitee.repository.management.model.Api> apis = apiRepository.findAll().stream().collect(Collectors.toMap(io.gravitee.repository.management.model.Api::getId, api -> api));
        // Get last event for each API
        Map<String, Event> events = new HashMap<>();
        apis.keySet().forEach(api -> {
            Event event = getLastEvent(api);
            events.put(api, event);
        });
        // Determine API which must be stopped and stop them
        events.entrySet().stream().filter(apiEvent -> {
            Event event = apiEvent.getValue();
            return event != null && (event.getType() == EventType.STOP_API || event.getType() == EventType.UNPUBLISH_API);
        }).forEach(apiEvent -> apiManager.undeploy(apiEvent.getKey()));
        // Determine API which must be deployed
        events.entrySet().stream().filter(apiEvent -> {
            Event event = apiEvent.getValue();
            return event != null && (event.getType() == EventType.START_API || event.getType() == EventType.PUBLISH_API);
        }).forEach(apiEvent -> {
            try {
                // Read API definition from event
                io.gravitee.repository.management.model.Api payloadApi = objectMapper.readValue(apiEvent.getValue().getPayload(), io.gravitee.repository.management.model.Api.class);
                // API to deploy
                ApiEntity apiToDeploy = convert(payloadApi);
                if (apiToDeploy != null) {
                    // Get deployed API
                    ApiEntity deployedApi = apiManager.get(apiToDeploy.getId());
                    // API is not yet deployed, so let's do it !
                    if (deployedApi == null) {
                        apiManager.deploy(apiToDeploy);
                    } else {
                        if (deployedApi.getDeployedAt().before(apiToDeploy.getDeployedAt())) {
                            apiManager.update(apiToDeploy);
                        }
                    }
                }
            } catch (IOException ioe) {
                logger.error("Error while determining deployed APIs store into events payload", ioe);
            }
        });
        lastRefreshAt = System.currentTimeMillis();
    } catch (TechnicalException te) {
        logger.error("Unable to sync instance", te);
    }
}
Also used : EventRepository(io.gravitee.repository.management.api.EventRepository) Logger(org.slf4j.Logger) Event(io.gravitee.repository.management.model.Event) ApiEntity(io.gravitee.management.model.ApiEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) LifecycleState(io.gravitee.repository.management.model.LifecycleState) List(java.util.List) Api(io.gravitee.repository.management.model.Api) EventType(io.gravitee.repository.management.model.EventType) ApiRepository(io.gravitee.repository.management.api.ApiRepository) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) Map(java.util.Map) Lifecycle(io.gravitee.common.component.Lifecycle) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) HashMap(java.util.HashMap) ApiEntity(io.gravitee.management.model.ApiEntity) IOException(java.io.IOException) Api(io.gravitee.repository.management.model.Api) Event(io.gravitee.repository.management.model.Event) Api(io.gravitee.repository.management.model.Api)

Example 62 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class PageServiceImpl method createPortalPage.

@Override
public PageEntity createPortalPage(NewPageEntity newPageEntity) {
    try {
        logger.debug("Create portal page {}", newPageEntity);
        String id = UUID.toString(UUID.random());
        Optional<Page> checkPage = pageRepository.findById(id);
        if (checkPage.isPresent()) {
            throw new PageAlreadyExistsException(id);
        }
        Page page = convert(newPageEntity);
        if (page.getSource() != null) {
            String fetchedContent = this.getContentFromFetcher(page.getSource());
            if (fetchedContent != null && !fetchedContent.isEmpty()) {
                page.setContent(fetchedContent);
            }
        }
        page.setId(id);
        // Set date fields
        page.setCreatedAt(new Date());
        page.setUpdatedAt(page.getCreatedAt());
        Page createdPage = pageRepository.create(page);
        // only one homepage is allowed
        onlyOneHomepage(page);
        createAuditLog(null, PAGE_CREATED, page.getCreatedAt(), null, page);
        return convert(createdPage);
    } catch (TechnicalException | FetcherException ex) {
        logger.error("An error occurs while trying to create {}", newPageEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + newPageEntity, ex);
    }
}
Also used : PageAlreadyExistsException(io.gravitee.management.service.exceptions.PageAlreadyExistsException) FetcherException(io.gravitee.fetcher.api.FetcherException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 63 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class ParameterServiceImpl method update.

@Override
public Parameter update(final String key, final String value) {
    try {
        final Optional<Parameter> optionalParameter = parameterRepository.findById(key);
        if (!optionalParameter.isPresent()) {
            throw new ParameterNotFoundException(key);
        }
        final Parameter parameter = new Parameter();
        parameter.setKey(key);
        parameter.setValue(value);
        final Parameter updatedParameter = parameterRepository.update(parameter);
        auditService.createPortalAuditLog(singletonMap(PARAMETER, updatedParameter.getKey()), PARAMETER_UPDATED, new Date(), optionalParameter.get(), updatedParameter);
        return updatedParameter;
    } catch (final TechnicalException ex) {
        final String message = "An error occurs while trying to update parameter for key/value: " + key + '/' + value;
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Parameter(io.gravitee.repository.management.model.Parameter) ParameterNotFoundException(io.gravitee.management.service.exceptions.ParameterNotFoundException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 64 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class ParameterServiceImpl method create.

@Override
public Parameter create(final String key, final String value) {
    try {
        final Optional<Parameter> optionalParameter = parameterRepository.findById(key);
        if (optionalParameter.isPresent()) {
            throw new ParameterAlreadyExistsException(key);
        }
        final Parameter parameter = new Parameter();
        parameter.setKey(key);
        parameter.setValue(value);
        final Parameter savedParameter = parameterRepository.create(parameter);
        auditService.createPortalAuditLog(singletonMap(PARAMETER, savedParameter.getKey()), PARAMETER_CREATED, new Date(), null, savedParameter);
        return savedParameter;
    } catch (final TechnicalException ex) {
        final String message = "An error occurs while trying to create parameter for key/value: " + key + '/' + value;
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Parameter(io.gravitee.repository.management.model.Parameter) ParameterAlreadyExistsException(io.gravitee.management.service.exceptions.ParameterAlreadyExistsException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 65 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class PlanServiceImpl method update.

@Override
public PlanEntity update(UpdatePlanEntity updatePlan) {
    try {
        logger.debug("Update plan {}", updatePlan.getName());
        Optional<Plan> optPlan = planRepository.findById(updatePlan.getId());
        if (!optPlan.isPresent()) {
            throw new PlanNotFoundException(updatePlan.getId());
        }
        Plan oldPlan = optPlan.get();
        Plan newPlan = new Plan();
        // copy immutable values
        newPlan.setId(oldPlan.getId());
        newPlan.setSecurity(oldPlan.getSecurity());
        newPlan.setType(oldPlan.getType());
        newPlan.setStatus(oldPlan.getStatus());
        newPlan.setOrder(oldPlan.getOrder());
        newPlan.setApis(oldPlan.getApis());
        newPlan.setCreatedAt(oldPlan.getCreatedAt());
        newPlan.setPublishedAt(oldPlan.getPublishedAt());
        newPlan.setClosedAt(oldPlan.getClosedAt());
        // update datas
        newPlan.setName(updatePlan.getName());
        newPlan.setDescription(updatePlan.getDescription());
        newPlan.setUpdatedAt(new Date());
        newPlan.setSecurityDefinition(updatePlan.getSecurityDefinition());
        String planPolicies = objectMapper.writeValueAsString(updatePlan.getPaths());
        newPlan.setDefinition(planPolicies);
        newPlan.setExcludedGroups(updatePlan.getExcludedGroups());
        if (newPlan.getSecurity() == Plan.PlanSecurityType.KEY_LESS) {
            // There is no need for a validation when authentication is KEY_LESS, force to AUTO
            newPlan.setValidation(Plan.PlanValidationType.AUTO);
        } else {
            newPlan.setValidation(Plan.PlanValidationType.valueOf(updatePlan.getValidation().name()));
        }
        newPlan.setCharacteristics(updatePlan.getCharacteristics());
        // if order change, reorder all pages
        if (newPlan.getOrder() != updatePlan.getOrder()) {
            newPlan.setOrder(updatePlan.getOrder());
            reorderAndSavePlans(newPlan);
            return null;
        } else {
            newPlan = planRepository.update(newPlan);
            auditService.createApiAuditLog(newPlan.getApis().iterator().next(), Collections.singletonMap(PLAN, newPlan.getId()), PLAN_UPDATED, newPlan.getUpdatedAt(), oldPlan, newPlan);
            return convert(newPlan);
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to update plan {}", updatePlan.getName(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to update plan %s", updatePlan.getName()), ex);
    } catch (JsonProcessingException jse) {
        logger.error("Unexpected error while generating plan definition", jse);
        throw new TechnicalManagementException(String.format("An error occurs while trying to update a plan %s", updatePlan.getName()), jse);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Plan(io.gravitee.repository.management.model.Plan) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)102 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)80 Logger (org.slf4j.Logger)22 LoggerFactory (org.slf4j.LoggerFactory)22 Autowired (org.springframework.beans.factory.annotation.Autowired)22 Component (org.springframework.stereotype.Component)20 java.util (java.util)18 Collectors (java.util.stream.Collectors)18 io.gravitee.management.model (io.gravitee.management.model)16 AuditService (io.gravitee.management.service.AuditService)12 UUID (io.gravitee.common.utils.UUID)11 Date (java.util.Date)11 IdGenerator (io.gravitee.common.utils.IdGenerator)9 IOException (java.io.IOException)9 io.gravitee.management.service (io.gravitee.management.service)8 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7