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