use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentServiceImpl method initialize.
@Override
public void initialize() {
Environment defaultEnvironment = new Environment();
defaultEnvironment.setId(GraviteeContext.getDefaultEnvironment());
defaultEnvironment.setName("Default environment");
defaultEnvironment.setDescription("Default environment");
defaultEnvironment.setOrganizationId(GraviteeContext.getDefaultOrganization());
try {
environmentRepository.create(defaultEnvironment);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create default environment", ex);
throw new TechnicalManagementException("An error occurs while trying to create default environment", ex);
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class FetcherServiceImpl method findAll.
@Override
public Set<FetcherEntity> findAll(boolean onlyFilesFetchers) {
try {
Set<FetcherPlugin> fetcherDefinitions = super.list();
if (onlyFilesFetchers) {
Class<?> filesFetcherClass = FilesFetcher.class;
fetcherDefinitions = fetcherDefinitions.stream().filter(fetcherPlugin -> filesFetcherClass.isAssignableFrom(fetcherPlugin.fetcher())).collect(Collectors.toSet());
}
return fetcherDefinitions.stream().map(fetcherDefinition -> convert(fetcherDefinition, false)).collect(Collectors.toSet());
} catch (Exception ex) {
logger.error("An error occurs while trying to list all fetchers", ex);
throw new TechnicalManagementException("An error occurs while trying to list all fetchers", ex);
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class ApiDuplicatorServiceImpl method updateApiReferences.
private void updateApiReferences(ApiEntity createdOrUpdatedApiEntity, JsonNode jsonNode, String organizationId, String environmentId, // FIXME: This whole method should be split in 2 (creation and update) and this flag should be removed
boolean isUpdate) throws JsonProcessingException {
// Members
final JsonNode membersToImport = jsonNode.path("members");
if (membersToImport != null && membersToImport.isArray()) {
// get current members of the api
Set<MemberToImport> membersAlreadyPresent = membershipService.getMembersByReference(MembershipReferenceType.API, createdOrUpdatedApiEntity.getId()).stream().map(member -> {
UserEntity userEntity = userService.findById(member.getId());
return new MemberToImport(userEntity.getSource(), userEntity.getSourceId(), member.getRoles().stream().map(RoleEntity::getId).collect(Collectors.toList()), null);
}).collect(toSet());
// get the current PO
RoleEntity poRole = roleService.findPrimaryOwnerRoleByOrganization(organizationId, RoleScope.API);
if (poRole != null) {
String poRoleId = poRole.getId();
MemberToImport currentPo = membersAlreadyPresent.stream().filter(memberToImport -> memberToImport.getRoles().contains(poRoleId)).findFirst().orElse(new MemberToImport());
List<String> roleUsedInTransfert = null;
MemberToImport futurePO = null;
// upsert members
for (final JsonNode memberNode : membersToImport) {
MemberToImport memberToImport = objectMapper.readValue(memberNode.toString(), MemberToImport.class);
String roleToAdd = memberToImport.getRole();
List<String> rolesToImport = memberToImport.getRoles();
if (roleToAdd != null && !roleToAdd.isEmpty()) {
if (rolesToImport == null) {
rolesToImport = new ArrayList<>();
memberToImport.setRoles(rolesToImport);
}
Optional<RoleEntity> optRoleToAddEntity = roleService.findByScopeAndName(RoleScope.API, roleToAdd);
if (optRoleToAddEntity.isPresent()) {
rolesToImport.add(optRoleToAddEntity.get().getId());
} else {
LOGGER.warn("Role {} does not exist", roleToAdd);
}
}
if (rolesToImport != null) {
rolesToImport.sort(Comparator.naturalOrder());
}
boolean presentWithSameRole = memberToImport.getRoles() != null && !memberToImport.getRoles().isEmpty() && membersAlreadyPresent.stream().anyMatch(m -> {
m.getRoles().sort(Comparator.naturalOrder());
return (m.getRoles().equals(memberToImport.getRoles()) && (m.getSourceId().equals(memberToImport.getSourceId()) && m.getSource().equals(memberToImport.getSource())));
});
// - not the current PO
if (!presentWithSameRole && (memberToImport.getRoles() != null && !memberToImport.getRoles().isEmpty() && !memberToImport.getRoles().contains(poRoleId)) && !(memberToImport.getSourceId().equals(currentPo.getSourceId()) && memberToImport.getSource().equals(currentPo.getSource()))) {
try {
UserEntity userEntity = userService.findBySource(memberToImport.getSource(), memberToImport.getSourceId(), false);
rolesToImport.forEach(role -> membershipService.addRoleToMemberOnReference(MembershipReferenceType.API, createdOrUpdatedApiEntity.getId(), MembershipMemberType.USER, userEntity.getId(), role));
} catch (UserNotFoundException unfe) {
}
}
// get the future role of the current PO
if (currentPo.getSourceId().equals(memberToImport.getSourceId()) && currentPo.getSource().equals(memberToImport.getSource()) && !rolesToImport.contains(poRoleId)) {
roleUsedInTransfert = rolesToImport;
}
if (rolesToImport.contains(poRoleId)) {
futurePO = memberToImport;
}
}
// transfer the ownership
if (futurePO != null && !(currentPo.getSource().equals(futurePO.getSource()) && currentPo.getSourceId().equals(futurePO.getSourceId()))) {
try {
UserEntity userEntity = userService.findBySource(futurePO.getSource(), futurePO.getSourceId(), false);
List<RoleEntity> roleEntity = null;
if (roleUsedInTransfert != null && !roleUsedInTransfert.isEmpty()) {
roleEntity = roleUsedInTransfert.stream().map(roleService::findById).collect(Collectors.toList());
}
membershipService.transferApiOwnership(createdOrUpdatedApiEntity.getId(), new MembershipService.MembershipMember(userEntity.getId(), null, MembershipMemberType.USER), roleEntity);
} catch (UserNotFoundException unfe) {
}
}
}
}
if (isUpdate) {
// Pages
final JsonNode pagesDefinition = jsonNode.path("pages");
if (pagesDefinition != null && pagesDefinition.isArray()) {
List<PageEntity> pagesList = objectMapper.readValue(pagesDefinition.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, PageEntity.class));
pageService.createOrUpdatePages(pagesList, environmentId, createdOrUpdatedApiEntity.getId());
}
}
// Plans
final JsonNode plansDefinition = jsonNode.path("plans");
if (plansDefinition != null && plansDefinition.isArray()) {
List<PlanEntity> plansList = objectMapper.readValue(plansDefinition.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, PlanEntity.class));
plansList.forEach(planEntity -> {
planEntity.setApi(createdOrUpdatedApiEntity.getId());
planEntity.setId(UuidString.generateForEnvironment(environmentId, createdOrUpdatedApiEntity.getId(), planEntity.getId()));
planService.createOrUpdatePlan(planEntity, environmentId);
});
}
// Metadata
final JsonNode metadataDefinition = jsonNode.path("metadata");
if (metadataDefinition != null && metadataDefinition.isArray()) {
try {
for (JsonNode metadataNode : metadataDefinition) {
UpdateApiMetadataEntity updateApiMetadataEntity = objectMapper.readValue(metadataNode.toString(), UpdateApiMetadataEntity.class);
updateApiMetadataEntity.setApiId(createdOrUpdatedApiEntity.getId());
apiMetadataService.update(updateApiMetadataEntity);
}
} catch (Exception ex) {
LOGGER.error("An error occurs while creating API metadata", ex);
throw new TechnicalManagementException("An error occurs while creating API Metadata", ex);
}
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class ApiDuplicatorServiceImpl method createWithImportedDefinition.
@Override
public ApiEntity createWithImportedDefinition(String apiDefinitionOrURL, String userId, String organizationId, String environmentId) {
String apiDefinition = fetchApiDefinitionContentFromURL(apiDefinitionOrURL);
try {
// Read the whole definition
final JsonNode jsonNode = objectMapper.readTree(apiDefinition);
UpdateApiEntity importedApi = convertToEntity(apiDefinition, jsonNode);
ApiEntity createdApiEntity = apiService.createWithApiDefinition(importedApi, userId, jsonNode);
createPageAndMedia(createdApiEntity, jsonNode, environmentId);
updateApiReferences(createdApiEntity, jsonNode, organizationId, environmentId, false);
return createdApiEntity;
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to JSON deserialize the API {}", apiDefinition, e);
throw new TechnicalManagementException("An error occurs while trying to JSON deserialize the API definition.");
}
}
use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.
the class ApiDuplicatorServiceImpl method updateWithImportedDefinition.
@Override
public ApiEntity updateWithImportedDefinition(String apiId, String apiDefinitionOrURL, String userId, String organizationId, String environmentId) {
String apiDefinition = fetchApiDefinitionContentFromURL(apiDefinitionOrURL);
try {
// Read the whole definition
final JsonNode jsonNode = objectMapper.readTree(apiDefinition);
UpdateApiEntity importedApi = convertToEntity(apiDefinition, jsonNode);
ApiEntity updatedApiEntity = apiService.update(apiId, importedApi, false);
updateApiReferences(updatedApiEntity, jsonNode, organizationId, environmentId, true);
return updatedApiEntity;
} catch (JsonProcessingException e) {
LOGGER.error("An error occurs while trying to JSON deserialize the API {}", apiDefinition, e);
throw new TechnicalManagementException("An error occurs while trying to JSON deserialize the API definition.");
}
}
Aggregations