use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method findById.
@Override
public ApplicationEntity findById(String applicationId) {
try {
LOGGER.debug("Find application by ID: {}", applicationId);
Optional<Application> application = applicationRepository.findById(applicationId);
if (application.isPresent()) {
Optional<Membership> primaryOwnerMembership = membershipRepository.findByReferenceAndRole(MembershipReferenceType.APPLICATION, applicationId, RoleScope.APPLICATION, SystemRole.PRIMARY_OWNER.name()).stream().findFirst();
if (!primaryOwnerMembership.isPresent()) {
LOGGER.error("The Application {} doesn't have any primary owner.", applicationId);
throw new TechnicalException("The Application " + applicationId + " doesn't have any primary owner.");
}
return convert(application.get(), userService.findById(primaryOwnerMembership.get().getUserId()));
}
throw new ApplicationNotFoundException(applicationId);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find an application using its ID {}", applicationId, ex);
throw new TechnicalManagementException("An error occurs while trying to find an application using its ID " + applicationId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method findAll.
@Override
public Set<ApplicationEntity> findAll() {
try {
LOGGER.debug("Find all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
if (applications == null || applications.isEmpty()) {
return emptySet();
}
return this.convert(applications);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find all applications", ex);
throw new TechnicalManagementException("An error occurs while trying to find all applications", ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method archive.
@Override
public void archive(String applicationId) {
try {
LOGGER.debug("Delete application {}", applicationId);
Optional<Application> optApplication = applicationRepository.findById(applicationId);
if (!optApplication.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
Application application = optApplication.get();
Application previousApplication = new Application(application);
Collection<SubscriptionEntity> subscriptions = subscriptionService.findByApplicationAndPlan(applicationId, null);
subscriptions.forEach(subscription -> {
Set<ApiKeyEntity> apiKeys = apiKeyService.findBySubscription(subscription.getId());
apiKeys.forEach(apiKey -> {
try {
apiKeyService.delete(apiKey.getKey());
} catch (TechnicalManagementException tme) {
LOGGER.error("An error occurs while deleting API Key {}", apiKey.getKey(), tme);
}
});
try {
subscriptionService.close(subscription.getId());
} catch (SubscriptionNotClosableException snce) {
// Subscription can not be closed because it is already closed or not yet accepted
LOGGER.debug("The subscription can not be closed: {}", snce.getMessage());
}
});
// Archive the application
application.setUpdatedAt(new Date());
application.setStatus(ApplicationStatus.ARCHIVED);
applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(application.getId(), Collections.emptyMap(), APPLICATION_ARCHIVED, application.getUpdatedAt(), previousApplication, application);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to delete application %s", applicationId), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class AuditServiceImpl method create.
@Async
protected void create(Audit.AuditReferenceType referenceType, String referenceId, Map<Audit.AuditProperties, String> properties, Audit.AuditEvent event, String userId, Date createdAt, Object oldValue, Object newValue) {
Audit audit = new Audit();
audit.setId(UUID.toString(UUID.random()));
audit.setUser(userId);
audit.setCreatedAt(createdAt);
if (properties != null) {
Map<String, String> stringStringMap = new HashMap<>(properties.size());
properties.forEach((auditProperties, s) -> stringStringMap.put(auditProperties.name(), s));
audit.setProperties(stringStringMap);
}
audit.setReferenceType(referenceType);
audit.setReferenceId(referenceId);
audit.setEvent(event.name());
ObjectNode oldNode = oldValue == null ? mapper.createObjectNode() : mapper.convertValue(oldValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));
ObjectNode newNode = newValue == null ? mapper.createObjectNode() : mapper.convertValue(newValue, ObjectNode.class).remove(Arrays.asList("updatedAt", "createdAt"));
audit.setPatch(JsonDiff.asJson(oldNode, newNode).toString());
try {
auditRepository.create(audit);
} catch (TechnicalException e) {
LOGGER.error("Error occurs during the creation of an Audit Log {}.", e);
}
}
use of io.gravitee.repository.exceptions.TechnicalException 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;
}
Aggregations