Skip to content

Commit 656437c

Browse files
author
rathnapandi
committed
- Fix issue #549
1 parent 930e850 commit 656437c

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.axway.apim.api.specification.APISpecification.APISpecType;
1414
import com.axway.apim.api.specification.APISpecificationFactory;
1515
import com.axway.apim.lib.CoreParameters;
16+
import com.axway.apim.lib.EnvironmentProperties;
1617
import com.axway.apim.lib.error.AppException;
1718
import com.axway.apim.lib.error.ErrorCode;
1819
import com.axway.apim.lib.utils.Constants;
@@ -104,8 +105,9 @@ public List<API> getAPIs(APIFilter filter, boolean logProgress) throws AppExcept
104105
addImageFromAPIM(api, filter.isIncludeImage());
105106
addRemoteHost(api, filter.isIncludeRemoteHost());
106107
addMethods(api, filter.isIncludeMethods());
107-
if (logProgress && apis.size() > 5)
108+
if (logProgress && apis.size() > 5 && !EnvironmentProperties.PRINT_CONFIG_CONSOLE) {
108109
Utils.progressPercentage(i, apis.size(), "Loading details of " + apis.size() + " APIs");
110+
}
109111
}
110112
Utils.addCustomPropertiesForEntity(apis, this.apiManagerResponse.get(filter), filter);
111113
if (logProgress && apis.size() > 5) Console.print("\n");
@@ -616,16 +618,16 @@ public String[] getSerializeAllExcept() throws AppException {
616618
} else {
617619
serializeAllExcept = new String[]{"queryStringPassThrough", "apiDefinition", "certFile", "useForInbound", "useForOutbound", "organization", "applications", "image", "clientOrganizations", "applicationQuota", "systemQuota", "backendBasepath", "remoteHost"};
618620
}
619-
LOG.debug("serializeAllExcept={}", Arrays.asList(serializeAllExcept));
621+
LOG.debug("serializeAllExcept={}", Arrays.asList(serializeAllExcept));
620622
return serializeAllExcept;
621623
}
622624

623625
private boolean isAPIManagerVersionGreaterThan7720220530(String apiManagerVersion) {
624626
try {
625627
apiManagerVersion = apiManagerVersion.replace("7.7.", "");
626-
LOG.debug("apiManagerVersion for conversion: {}",apiManagerVersion);
628+
LOG.debug("apiManagerVersion for conversion: {}", apiManagerVersion);
627629
return (Integer.parseInt(apiManagerVersion) >= 20220530);
628-
} catch (NumberFormatException nfe){
630+
} catch (NumberFormatException nfe) {
629631
LOG.warn("Failed to convert API Manager version to integer: {}, returning false", apiManagerVersion);
630632
return false;
631633
}

modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ public void saveAPILocally(ObjectMapper mapper, ExportAPI exportAPI, String conf
7575
}
7676

7777
storePrivateCerts(localFolder, exportAPI.getAuthenticationProfiles());
78+
updateMapperFilter(mapper);
79+
writeContent(mapper, exportAPI, localFolder, configFile);
80+
LOG.info("Successfully exported API: {} into folder: {}", exportAPI.getName(), localFolder.getAbsolutePath());
81+
if (!APIManagerAdapter.getInstance().hasAdminAccount()) {
82+
LOG.warn("Export has been done with an Org-Admin account only. Export is restricted by the following: ");
83+
LOG.warn("- No Quotas has been exported for the API");
84+
LOG.warn("- No Client-Organizations");
85+
LOG.warn("- Only subscribed applications from the Org-Admins organization");
86+
}
87+
}
7888

89+
public void updateMapperFilter(ObjectMapper mapper) {
7990
mapper.registerModule(new SimpleModule().setSerializerModifier(new APIExportSerializerModifier()));
8091
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
8192
FilterProvider filters = new SimpleFilterProvider()
@@ -85,14 +96,21 @@ public void saveAPILocally(ObjectMapper mapper, ExportAPI exportAPI, String conf
8596
SimpleBeanPropertyFilter.serializeAllExcept("apiMethodId"))
8697
.setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept());
8798
mapper.setFilterProvider(filters);
88-
writeContent(mapper, exportAPI, localFolder, configFile);
89-
LOG.info("Successfully exported API: {} into folder: {}", exportAPI.getName(), localFolder.getAbsolutePath());
90-
if (!APIManagerAdapter.getInstance().hasAdminAccount()) {
91-
LOG.warn("Export has been done with an Org-Admin account only. Export is restricted by the following: ");
92-
LOG.warn("- No Quotas has been exported for the API");
93-
LOG.warn("- No Client-Organizations");
94-
LOG.warn("- Only subscribed applications from the Org-Admins organization");
99+
}
100+
101+
public void writeToConsole(ObjectMapper mapper, List<ExportAPI> exportAPI) throws AppException {
102+
if (exportAPI == null || exportAPI.isEmpty()) return;
103+
try {
104+
mapper.enable(SerializationFeature.INDENT_OUTPUT);
105+
if (exportAPI.size() == 1)
106+
mapper.writeValue(System.out, exportAPI.get(0));
107+
else
108+
mapper.writeValue(System.out, exportAPI);
109+
} catch (IOException e) {
110+
throw new AppException("Problem in writing JSON / Yaml data", ErrorCode.UNXPECTED_ERROR, e);
111+
95112
}
113+
96114
}
97115

98116
private String getVHost(ExportAPI exportAPI) throws AppException {
@@ -146,6 +164,7 @@ private void storePrivateCerts(File localFolder, List<AuthenticationProfile> aut
146164
}
147165
}
148166

167+
149168
public void writeSpec(APISpecification apiDef, ExportAPI exportAPI, File localFolder) throws AppException {
150169
String targetFile = null;
151170
try {
@@ -174,12 +193,11 @@ public void writeSpec(APISpecification apiDef, ExportAPI exportAPI, File localFo
174193
}
175194

176195

196+
177197
public void writeContent(ObjectMapper mapper, ExportAPI exportAPI, File localFolder, String configFile) throws AppException {
178198
try {
179-
mapper.enable(SerializationFeature.INDENT_OUTPUT);
180-
if (EnvironmentProperties.PRINT_CONFIG_CONSOLE) {
181-
mapper.writeValue(System.out, exportAPI);
182-
} else {
199+
if (!EnvironmentProperties.PRINT_CONFIG_CONSOLE) {
200+
mapper.enable(SerializationFeature.INDENT_OUTPUT);
183201
mapper.writeValue(new File(localFolder.getCanonicalPath() + configFile), exportAPI);
184202
}
185203
} catch (Exception e) {

modules/apis/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.axway.apim.api.API;
66
import com.axway.apim.api.export.ExportAPI;
77
import com.axway.apim.api.export.lib.params.APIExportParams;
8+
import com.axway.apim.lib.EnvironmentProperties;
89
import com.axway.apim.lib.error.AppException;
910
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

14+
import java.util.ArrayList;
1315
import java.util.List;
1416

1517
public class JsonAPIExporter extends APIResultHandler {
@@ -29,12 +31,20 @@ public JsonAPIExporter(APIExportParams params) {
2931
public void execute(List<API> apis) throws AppException {
3032
LOG.info("Exporting API and configuration as JSON format");
3133
int size = apis.size();
34+
List<ExportAPI> exportAPIs = new ArrayList<>(size);
35+
ObjectMapper mapper = new ObjectMapper();
3236
for (API api : apis) {
3337
ExportAPI exportAPI = new ExportAPI(api);
34-
exportHelper.saveAPILocally(new ObjectMapper(), exportAPI, "/api-config.json", size);
38+
exportHelper.saveAPILocally(mapper, exportAPI, "/api-config.json", size);
39+
exportAPIs.add(exportAPI);
40+
}
41+
if (EnvironmentProperties.PRINT_CONFIG_CONSOLE) {
42+
exportHelper.writeToConsole(mapper, exportAPIs);
3543
}
3644
}
3745

46+
47+
3848
@Override
3949
public APIFilter getFilter() {
4050
Builder builder = getBaseAPIFilterBuilder()

modules/apis/src/main/java/com/axway/apim/api/export/impl/YamlAPIExporter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import com.axway.apim.api.API;
55
import com.axway.apim.api.export.ExportAPI;
66
import com.axway.apim.api.export.lib.params.APIExportParams;
7+
import com.axway.apim.lib.EnvironmentProperties;
78
import com.axway.apim.lib.error.AppException;
89
import com.fasterxml.jackson.databind.ObjectMapper;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

13+
import java.util.ArrayList;
1214
import java.util.List;
1315

1416
public class YamlAPIExporter extends JsonAPIExporter {
@@ -23,9 +25,15 @@ public YamlAPIExporter(APIExportParams params) {
2325
public void execute(List<API> apis) throws AppException {
2426
LOG.info("Export API and configuration as Yaml format");
2527
int size = apis.size();
28+
List<ExportAPI> exportAPIs = new ArrayList<>(size);
29+
ObjectMapper mapper = new ObjectMapper(CustomYamlFactory.createYamlFactory());
2630
for (API api : apis) {
2731
ExportAPI exportAPI = new ExportAPI(api);
28-
exportHelper.saveAPILocally(new ObjectMapper(CustomYamlFactory.createYamlFactory()), exportAPI, "/api-config.yaml", size);
32+
exportHelper.saveAPILocally(mapper, exportAPI, "/api-config.yaml", size);
33+
exportAPIs.add(exportAPI);
34+
}
35+
if (EnvironmentProperties.PRINT_CONFIG_CONSOLE) {
36+
exportHelper.writeToConsole(mapper, exportAPIs);
2937
}
3038
}
3139
}

0 commit comments

Comments
 (0)