From bf4c4eff3ac73fa1ede60030c2ce9f9a2372c7e9 Mon Sep 17 00:00:00 2001 From: Ethan Lo Date: Wed, 18 May 2022 17:21:37 -0400 Subject: [PATCH] Fix Whitespace issue --- .../project/cli/InternalMigrationService.java | 24 +++++++++++++++++++ .../ConfigGroupsMigrationService.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-project-migration/src/main/java/org/kie/workbench/common/project/cli/InternalMigrationService.java b/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-project-migration/src/main/java/org/kie/workbench/common/project/cli/InternalMigrationService.java index 8a19117685a..ceb3d92ba49 100644 --- a/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-project-migration/src/main/java/org/kie/workbench/common/project/cli/InternalMigrationService.java +++ b/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-project-migration/src/main/java/org/kie/workbench/common/project/cli/InternalMigrationService.java @@ -16,6 +16,8 @@ package org.kie.workbench.common.project.cli; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; @@ -82,6 +84,8 @@ public InternalMigrationService(final @Migration WorkspaceProjectService project public void migrateAllProjects(Path niogitDir) { List orgUnitConfigs = configService.getConfiguration(ConfigType.ORGANIZATIONAL_UNIT); List repoConfigs = configService.getConfiguration(ConfigType.REPOSITORY); + encodeGroupNames(orgUnitConfigs, repoConfigs); + Map orgUnitByRepo = getOrgUnitsByRepo(orgUnitConfigs); if (repoConfigs.isEmpty()) { @@ -228,4 +232,24 @@ public Map getOrgUnitsByRepo(List orgUnitConfigs) { }); return orgUnitByRepo; } + + /** + * Group names with whitespace in them will fail to construct a valid URI, therefore + * failing the migration. + * URL encode all group names to handle whitespace. + */ + private void encodeGroupNames(Collection orgUnitConfigs, Collection repoConfigs) { + configService.startBatch(); + orgUnitConfigs.forEach(group -> group.setName(encodeGroupName(group.getName()))); + repoConfigs.forEach(group -> group.setName(encodeGroupName(group.getName()))); + configService.endBatch(); + } + + private String encodeGroupName(String name) { + try { + return URLEncoder.encode(name, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Unable to encode group name."); + } + } } diff --git a/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-system-to-space-configuration-migration/src/main/java/org/kie/workbench/common/system/space/configuration/ConfigGroupsMigrationService.java b/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-system-to-space-configuration-migration/src/main/java/org/kie/workbench/common/system/space/configuration/ConfigGroupsMigrationService.java index 4af591bfa69..d5847c8187d 100644 --- a/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-system-to-space-configuration-migration/src/main/java/org/kie/workbench/common/system/space/configuration/ConfigGroupsMigrationService.java +++ b/kie-wb-common-cli/kie-wb-common-cli-tools/kie-wb-common-cli-system-to-space-configuration-migration/src/main/java/org/kie/workbench/common/system/space/configuration/ConfigGroupsMigrationService.java @@ -16,7 +16,10 @@ package org.kie.workbench.common.system.space.configuration; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.Collection; +import java.util.List; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; @@ -51,6 +54,8 @@ public ConfigGroupsMigrationService(final ConfigurationService configurationServ public void moveDataToSpaceConfigRepo() { Collection groups = configurationService.getConfiguration(ConfigType.SPACE); + encodeGroupNames(groups); + if (groups != null) { for (ConfigGroup groupConfig : groups) { saveSpaceInfo(configGroupToSpaceInfoConverter.toSpaceInfo(groupConfig)); @@ -65,4 +70,23 @@ public void moveDataToSpaceConfigRepo() { void saveSpaceInfo(SpaceInfo spaceInfo) { spaceConfigStorageRegistry.get(spaceInfo.getName()).saveSpaceInfo(spaceInfo); } + + /** + * Group names with whitespace in them will fail to construct a valid URI, therefore + * failing the migration. + * URL encode all group names to handle whitespace. + */ + private void encodeGroupNames(Collection groups) { + configurationService.startBatch(); + groups.forEach(group -> group.setName(encodeGroupName(group.getName()))); + configurationService.endBatch(); + } + + private String encodeGroupName(String name) { + try { + return URLEncoder.encode(name, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Unable to encode group name."); + } + } }