From ed8642f92476e96036f3a8d35f935b7d75a20aa5 Mon Sep 17 00:00:00 2001 From: rathnapandi Date: Tue, 22 Oct 2024 09:30:46 -0700 Subject: [PATCH] - fix sonar issues --- .../axway/apim/api/export/impl/ExportHelper.java | 14 ++------------ .../apim/api/export/impl/JsonAPIExporter.java | 3 ++- .../apim/api/export/impl/YamlAPIExporter.java | 6 ++++-- .../setup/impl/JsonAPIManagerSetupExporter.java | 15 ++------------- .../setup/impl/YamlAPIManagerSetupExporter.java | 6 ++++-- .../axway/apim/users/impl/JsonUserExporter.java | 14 ++------------ .../axway/apim/users/impl/YamlUserExporter.java | 9 ++++++--- 7 files changed, 22 insertions(+), 45 deletions(-) diff --git a/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java b/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java index 3d9a27b45..399f234ea 100644 --- a/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java +++ b/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java @@ -2,7 +2,6 @@ import com.axway.apim.adapter.APIManagerAdapter; import com.axway.apim.adapter.apis.OrgFilter; -import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.api.export.ExportAPI; import com.axway.apim.api.export.jackson.serializer.APIExportSerializerModifier; import com.axway.apim.api.export.lib.params.APIExportParams; @@ -44,7 +43,7 @@ public ExportHelper(APIExportParams params) { this.params = params; } - public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandler) throws AppException { + public void saveAPILocally(ObjectMapper mapper, ExportAPI exportAPI, String configFile) throws AppException { String apiPath = getAPIExportFolder(exportAPI.getPath()); File localFolder = new File(params.getTarget() + File.separator + getVHost(exportAPI) + apiPath); @@ -61,7 +60,6 @@ public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandle return; } String targetFile = null; - String configFile; try { String fileName = Utils.replaceSpecialChars(exportAPI.getName()) + apiDef.getAPIDefinitionType().getFileExtension(); targetFile = localFolder.getCanonicalPath() + "/" + fileName; @@ -72,14 +70,6 @@ public void saveAPILocally(ExportAPI exportAPI, APIResultHandler apiResultHandle } catch (IOException e) { throw new AppException("Can't save API-Definition locally to file: " + targetFile, ErrorCode.UNXPECTED_ERROR, e); } - ObjectMapper mapper; - if (apiResultHandler instanceof YamlAPIExporter) { - mapper = new ObjectMapper(CustomYamlFactory.createYamlFactory()); - configFile = "/api-config.yaml"; - } else { - mapper = new ObjectMapper(); - configFile = "/api-config.json"; - } Image image = exportAPI.getAPIImage(); if (image != null && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) { writeBytesToFile(image.getImageContent(), localFolder + File.separator + image.getBaseFilename()); @@ -147,7 +137,7 @@ private void storePrivateCerts(File localFolder, List aut Map parameters = profile.getParameters(); String pfx = (String) parameters.get("pfx"); String content = pfx.replaceFirst("data:.+,", ""); - byte[] data = Base64.getMimeDecoder().decode(content.replace("\\r\\n","\n")); + byte[] data = Base64.getMimeDecoder().decode(content.replace("\\r\\n", "\n")); String fileName = "backend_cert.pfx"; writeBytesToFile(data, localFolder + "/" + fileName); parameters.remove("pfx"); diff --git a/modules/apis/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java b/modules/apis/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java index e2b1ca3fe..d05d546ec 100644 --- a/modules/apis/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java +++ b/modules/apis/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java @@ -6,6 +6,7 @@ import com.axway.apim.api.export.ExportAPI; import com.axway.apim.api.export.lib.params.APIExportParams; import com.axway.apim.lib.error.AppException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,7 +30,7 @@ public void execute(List apis) throws AppException { LOG.info("Exporting API and configuration as JSON format"); for (API api : apis) { ExportAPI exportAPI = new ExportAPI(api); - exportHelper.saveAPILocally(exportAPI, this); + exportHelper.saveAPILocally(new ObjectMapper(), exportAPI, "/api-config.json"); } } diff --git a/modules/apis/src/main/java/com/axway/apim/api/export/impl/YamlAPIExporter.java b/modules/apis/src/main/java/com/axway/apim/api/export/impl/YamlAPIExporter.java index 025de15c8..91d7713dd 100644 --- a/modules/apis/src/main/java/com/axway/apim/api/export/impl/YamlAPIExporter.java +++ b/modules/apis/src/main/java/com/axway/apim/api/export/impl/YamlAPIExporter.java @@ -1,15 +1,17 @@ package com.axway.apim.api.export.impl; +import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.api.API; import com.axway.apim.api.export.ExportAPI; import com.axway.apim.api.export.lib.params.APIExportParams; import com.axway.apim.lib.error.AppException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; -public class YamlAPIExporter extends JsonAPIExporter{ +public class YamlAPIExporter extends JsonAPIExporter { private static final Logger LOG = LoggerFactory.getLogger(YamlAPIExporter.class); @@ -22,7 +24,7 @@ public void execute(List apis) throws AppException { LOG.info("Export API and configuration as Yaml format"); for (API api : apis) { ExportAPI exportAPI = new ExportAPI(api); - exportHelper.saveAPILocally(exportAPI, this); + exportHelper.saveAPILocally(new ObjectMapper(CustomYamlFactory.createYamlFactory()), exportAPI, "/api-config.yaml"); } } } diff --git a/modules/settings/src/main/java/com/axway/apim/setup/impl/JsonAPIManagerSetupExporter.java b/modules/settings/src/main/java/com/axway/apim/setup/impl/JsonAPIManagerSetupExporter.java index 771c15847..fd25acea5 100644 --- a/modules/settings/src/main/java/com/axway/apim/setup/impl/JsonAPIManagerSetupExporter.java +++ b/modules/settings/src/main/java/com/axway/apim/setup/impl/JsonAPIManagerSetupExporter.java @@ -2,7 +2,6 @@ import com.axway.apim.adapter.APIManagerAdapter; import com.axway.apim.adapter.apis.RemoteHostFilter; -import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.adapter.jackson.PolicySerializerModifier; import com.axway.apim.adapter.jackson.UserSerializerModifier; import com.axway.apim.api.model.Config; @@ -38,14 +37,12 @@ public RemoteHostFilter getRemoteHostFilter() { @Override public void export(APIManagerConfig apimanagerConfig) throws AppException { - exportToFile(apimanagerConfig, this); + exportToFile(new ObjectMapper(), apimanagerConfig, "/apimanager-config.json"); } - public void exportToFile(APIManagerConfig apimanagerConfig, APIManagerSetupResultHandler apiManagerSetupResultHandler) throws AppException { + public void exportToFile(ObjectMapper mapper, APIManagerConfig apimanagerConfig, String configFile) throws AppException { File localFolder = null; - ObjectMapper mapper; - String configFile; if (!EnvironmentProperties.PRINT_CONFIG_CONSOLE) { String folderName = getExportFolder(apimanagerConfig.getConfig()); String targetFolder = params.getTarget(); @@ -64,14 +61,6 @@ public void exportToFile(APIManagerConfig apimanagerConfig, APIManagerSetupResul throw new AppException("Cannot create export folder: " + localFolder, ErrorCode.UNXPECTED_ERROR); } } - - if (apiManagerSetupResultHandler instanceof YamlAPIManagerSetupExporter) { - mapper = new ObjectMapper(CustomYamlFactory.createYamlFactory()); - configFile = "/apimanager-config.yaml"; - } else { - mapper = new ObjectMapper(); - configFile = "/apimanager-config.json"; - } try { mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.registerModule(new SimpleModule().setSerializerModifier(new PolicySerializerModifier(true))); diff --git a/modules/settings/src/main/java/com/axway/apim/setup/impl/YamlAPIManagerSetupExporter.java b/modules/settings/src/main/java/com/axway/apim/setup/impl/YamlAPIManagerSetupExporter.java index 9508ae36a..c443c5df8 100644 --- a/modules/settings/src/main/java/com/axway/apim/setup/impl/YamlAPIManagerSetupExporter.java +++ b/modules/settings/src/main/java/com/axway/apim/setup/impl/YamlAPIManagerSetupExporter.java @@ -1,13 +1,15 @@ package com.axway.apim.setup.impl; +import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.lib.ExportResult; import com.axway.apim.lib.error.AppException; import com.axway.apim.setup.lib.APIManagerSetupExportParams; import com.axway.apim.setup.model.APIManagerConfig; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class YamlAPIManagerSetupExporter extends JsonAPIManagerSetupExporter{ +public class YamlAPIManagerSetupExporter extends JsonAPIManagerSetupExporter { private static final Logger LOG = LoggerFactory.getLogger(YamlAPIManagerSetupExporter.class); public YamlAPIManagerSetupExporter(APIManagerSetupExportParams params, ExportResult result) { @@ -17,6 +19,6 @@ public YamlAPIManagerSetupExporter(APIManagerSetupExportParams params, ExportRes @Override public void export(APIManagerConfig apimanagerConfig) throws AppException { LOG.info("Exporting API Manager Configuration in yaml"); - exportToFile(apimanagerConfig, this); + exportToFile(new ObjectMapper(CustomYamlFactory.createYamlFactory()), apimanagerConfig, "/apimanager-config.yaml"); } } diff --git a/modules/users/src/main/java/com/axway/apim/users/impl/JsonUserExporter.java b/modules/users/src/main/java/com/axway/apim/users/impl/JsonUserExporter.java index 98eac5d1c..217cf234a 100644 --- a/modules/users/src/main/java/com/axway/apim/users/impl/JsonUserExporter.java +++ b/modules/users/src/main/java/com/axway/apim/users/impl/JsonUserExporter.java @@ -1,6 +1,5 @@ package com.axway.apim.users.impl; -import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.adapter.jackson.ImageSerializer; import com.axway.apim.adapter.user.UserFilter; import com.axway.apim.api.model.Image; @@ -37,14 +36,12 @@ public JsonUserExporter(UserExportParams params, ExportResult result) { @Override public void export(List users) throws AppException { for (User user : users) { - saveUserLocally(new ExportUser(user), this); + saveUserLocally(new ObjectMapper(), new ExportUser(user), "/user-config.json"); } } - public void saveUserLocally(ExportUser user, UserResultHandler userResultHandler) throws AppException { + public void saveUserLocally(ObjectMapper mapper, ExportUser user, String configFile) throws AppException { File localFolder = null; - ObjectMapper mapper; - String configFile; if (!EnvironmentProperties.PRINT_CONFIG_CONSOLE) { String folderName = getExportFolder(user); String targetFolder = params.getTarget(); @@ -63,13 +60,6 @@ public void saveUserLocally(ExportUser user, UserResultHandler userResultHandler throw new AppException("Cannot create export folder: " + localFolder, ErrorCode.UNXPECTED_ERROR); } } - if (userResultHandler instanceof YamlUserExporter) { - mapper = new ObjectMapper(CustomYamlFactory.createYamlFactory()); - configFile = "/user-config.yaml"; - } else { - mapper = new ObjectMapper(); - configFile = "/user-config.json"; - } mapper.registerModule(new SimpleModule().addSerializer(Image.class, new ImageSerializer())); FilterProvider filters = new SimpleFilterProvider() .addFilter("UserFilter", diff --git a/modules/users/src/main/java/com/axway/apim/users/impl/YamlUserExporter.java b/modules/users/src/main/java/com/axway/apim/users/impl/YamlUserExporter.java index 087e0b862..71fd5a29a 100644 --- a/modules/users/src/main/java/com/axway/apim/users/impl/YamlUserExporter.java +++ b/modules/users/src/main/java/com/axway/apim/users/impl/YamlUserExporter.java @@ -1,27 +1,30 @@ package com.axway.apim.users.impl; +import com.axway.apim.adapter.jackson.CustomYamlFactory; import com.axway.apim.api.model.User; import com.axway.apim.lib.ExportResult; import com.axway.apim.lib.error.AppException; import com.axway.apim.users.lib.ExportUser; import com.axway.apim.users.lib.params.UserExportParams; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; -public class YamlUserExporter extends JsonUserExporter{ +public class YamlUserExporter extends JsonUserExporter { private static final Logger LOG = LoggerFactory.getLogger(YamlUserExporter.class); public YamlUserExporter(UserExportParams params, ExportResult result) { super(params, result); } + @Override public void export(List users) throws AppException { LOG.info("Exporting User in Yaml format"); - for(User user : users) { - saveUserLocally(new ExportUser(user), this); + for (User user : users) { + saveUserLocally(new ObjectMapper(CustomYamlFactory.createYamlFactory()), new ExportUser(user), "/user-config.yaml"); } } }