use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method findAllByApi.
@Override
public List<ApiMetadataEntity> findAllByApi(final String apiId) {
try {
LOGGER.debug("Find all metadata by api ID {}", apiId);
final List<MetadataEntity> defaultMetadata = metadataService.findAllDefault();
final List<String> defaultMetadataKeys = defaultMetadata.stream().map(MetadataEntity::getKey).collect(toList());
final List<Metadata> apiMetadata = metadataRepository.findByReferenceTypeAndReferenceId(MetadataReferenceType.API, apiId);
final List<ApiMetadataEntity> allMetadata = new ArrayList<>();
allMetadata.addAll(defaultMetadata.stream().map(metadata -> {
final Optional<Metadata> optApiMetadata = apiMetadata.stream().filter(metadataEntity -> metadata.getKey().equals(metadataEntity.getKey())).findAny();
return convert(optApiMetadata, metadata, null);
}).collect(toList()));
allMetadata.addAll(apiMetadata.stream().filter(metadata -> !defaultMetadataKeys.contains(metadata.getKey())).map(metadata -> convert(metadata, apiId)).collect(toList()));
return allMetadata;
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find all metadata by API", ex);
throw new TechnicalManagementException("An error occurred while trying to find all metadata by API", ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApiMetadataServiceImpl method create.
@Override
public ApiMetadataEntity create(final NewApiMetadataEntity metadataEntity) {
final ApiEntity apiEntity = apiService.findById(metadataEntity.getApiId());
// if no format defined, we just set String format
if (metadataEntity.getFormat() == null) {
metadataEntity.setFormat(MetadataFormat.STRING);
}
// First we prevent the duplicate metadata name
final Optional<ApiMetadataEntity> optionalMetadata = findAllByApi(apiEntity.getId()).stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
if (optionalMetadata.isPresent()) {
throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
}
metadataService.checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
try {
final Metadata metadata = convertForAPI(metadataEntity);
final Date now = new Date();
metadata.setCreatedAt(now);
metadata.setUpdatedAt(now);
metadataRepository.create(metadata);
// Audit
auditService.createApiAuditLog(apiEntity.getId(), Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
return convert(metadata, metadataEntity.getApiId());
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to create metadata {} on API {}", metadataEntity.getName(), metadataEntity.getApiId(), ex);
throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName() + " on API " + metadataEntity.getApiId(), ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method findByUser.
@Override
public Set<ApplicationEntity> findByUser(String username) {
try {
LOGGER.debug("Find applications for user {}", username);
// find applications where the user is a member
List<String> appIds = membershipRepository.findByUserAndReferenceType(username, MembershipReferenceType.APPLICATION).stream().map(Membership::getReferenceId).collect(Collectors.toList());
final Set<Application> applications = applicationRepository.findByIds(appIds).stream().filter(app -> ApplicationStatus.ACTIVE.equals(app.getStatus())).collect(Collectors.toSet());
// find applications which be part of the same group as the user
List<String> groupIds = membershipRepository.findByUserAndReferenceType(username, MembershipReferenceType.GROUP).stream().filter(m -> m.getRoles().keySet().contains(RoleScope.APPLICATION.getId())).map(Membership::getReferenceId).collect(Collectors.toList());
applications.addAll(applicationRepository.findByGroups(groupIds, ApplicationStatus.ACTIVE));
if (applications.isEmpty()) {
return emptySet();
}
return this.convert(applications);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find applications for user {}", username, ex);
throw new TechnicalManagementException("An error occurs while trying to find applications for user " + username, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method create.
@Override
public ApplicationEntity create(NewApplicationEntity newApplicationEntity, String userId) {
try {
LOGGER.debug("Create {} for user {}", newApplicationEntity, userId);
// If clientId is set, check for uniqueness
String clientId = newApplicationEntity.getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
final boolean alreadyExistingApp = applications.stream().anyMatch(application -> clientId.equals(application.getClientId()));
if (alreadyExistingApp) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
if (newApplicationEntity.getGroups() != null && !newApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(newApplicationEntity.getGroups());
}
Application application = convert(newApplicationEntity);
application.setId(UUID.toString(UUID.random()));
application.setStatus(ApplicationStatus.ACTIVE);
// Add Default groups
Set<String> defaultGroups = groupService.findByEvent(GroupEvent.APPLICATION_CREATE).stream().map(GroupEntity::getId).collect(Collectors.toSet());
if (!defaultGroups.isEmpty() && application.getGroups() == null) {
application.setGroups(defaultGroups);
} else if (!defaultGroups.isEmpty()) {
application.getGroups().addAll(defaultGroups);
}
// Set date fields
application.setCreatedAt(new Date());
application.setUpdatedAt(application.getCreatedAt());
Application createdApplication = applicationRepository.create(application);
// Audit
auditService.createApplicationAuditLog(createdApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, isAuthenticated() ? getAuthenticatedUsername() : userId, createdApplication.getCreatedAt(), null, createdApplication);
// Add the primary owner of the newly created API
Membership membership = new Membership(userId, createdApplication.getId(), MembershipReferenceType.APPLICATION);
membership.setRoles(singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
membership.setCreatedAt(application.getCreatedAt());
membership.setUpdatedAt(application.getCreatedAt());
membershipRepository.create(membership);
// create the default mail notification
UserEntity userEntity = userService.findById(userId);
if (userEntity.getEmail() != null && !userEntity.getEmail().isEmpty()) {
GenericNotificationConfigEntity notificationConfigEntity = new GenericNotificationConfigEntity();
notificationConfigEntity.setName("Default Mail Notifications");
notificationConfigEntity.setReferenceType(HookScope.APPLICATION.name());
notificationConfigEntity.setReferenceId(createdApplication.getId());
notificationConfigEntity.setHooks(Arrays.stream(ApplicationHook.values()).map(Enum::name).collect(Collectors.toList()));
notificationConfigEntity.setNotifier(NotifierServiceImpl.DEFAULT_EMAIL_NOTIFIER_ID);
notificationConfigEntity.setConfig(userEntity.getEmail());
genericNotificationConfigService.create(notificationConfigEntity);
}
// TODO add membership log
return convert(createdApplication, userEntity);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create {} for user {}", newApplicationEntity, userId, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newApplicationEntity + " for user " + userId, ex);
}
}
use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.
the class EventServiceImpl method findById.
@Override
public EventEntity findById(String id) {
try {
LOGGER.debug("Find event by ID: {}", id);
Optional<Event> event = eventRepository.findById(id);
if (event.isPresent()) {
return convert(event.get());
}
throw new EventNotFoundException(id);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find an event using its ID {}", id, ex);
throw new TechnicalManagementException("An error occurs while trying to find an event using its ID " + id, ex);
}
}
Aggregations