Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Nov 22, 2024
1 parent 3e1c21d commit ed2c806
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public synchronized void deleteInstance() {
}

private void setApiManagerVersion() throws AppException {
Config config = configAdapter.getConfig(false);
Config config = configAdapter.getConfig();
apiManagerVersion = config.getProductVersion();
if (usingOrgAdmin)
LOG.info("Organization Administrator Self Service Enabled : {}", config.getOadminSelfServiceEnabled());
Expand Down Expand Up @@ -539,15 +539,15 @@ public String getApiManagerVersion() throws AppException {
if (apiManagerVersion != null) {
return apiManagerVersion;
}
apiManagerVersion = APIManagerAdapter.getInstance().configAdapter.getConfig(false).getProductVersion();
apiManagerVersion = APIManagerAdapter.getInstance().configAdapter.getConfig().getProductVersion();
return apiManagerVersion;
}

public String getApiManagerName() throws AppException {
if (apiManagerName != null) {
return apiManagerName;
}
apiManagerName = APIManagerAdapter.getInstance().configAdapter.getConfig(false).getPortalName();
apiManagerName = APIManagerAdapter.getInstance().configAdapter.getConfig().getPortalName();
return apiManagerName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URIBuilder;
Expand All @@ -28,8 +24,6 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class APIManagerConfigAdapter {

Expand All @@ -43,50 +37,7 @@ public APIManagerConfigAdapter() {
cmd = CoreParameters.getInstance();
}

Map<Boolean, String> apiManagerResponse = new HashMap<>();

Map<Boolean, Config> managerConfig = new HashMap<>();


/**
* Config fields that were introduced with a certain API-Manager version.
* This list is mainly used to filter out fields, when using an older API-Manager version.
*/
protected enum ConfigFields {
VERSION_7720200130("7.7.20200530", new String[]{"apiImportTimeout", "apiImportMimeValidation", "apiImportEditable", "lockUserAccount"}),
VERSION_77("7.7.0", new String[]{
"userNameRegex", "changePasswordOnFirstLogin", "passwordExpiryEnabled", "passwordLifetimeDays",
"applicationScopeRestrictions", "strictCertificateChecking", "serverCertificateVerification", "advisoryBannerEnabled", "advisoryBannerText"
});

private final String[] ignoreFields;
private final String managerVersion;

ConfigFields(String managerVersion, String[] ignoreFields) {
this.ignoreFields = ignoreFields;
this.managerVersion = managerVersion;
}

public String getManagerVersion() {
return managerVersion;
}

public static String[] getIgnoredFields() {
String[] restrictedFields = new String[]{};
for (ConfigFields fields : values()) {
// Add all ignore fields until we reached the used API-Manager version
if (APIManagerAdapter.hasAPIManagerVersion(fields.getManagerVersion())) {
break;
}
restrictedFields = ArrayUtils.addAll(restrictedFields, fields.ignoreFields);
}
return restrictedFields;
}

}

private void readConfigFromAPIManager(boolean useAdmin) throws AppException {
if (apiManagerResponse.get(useAdmin) != null) return;
public Config getConfig() throws AppException {
try {
URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + "/config").build();
RestAPICall getRequest = new GETRequest(uri);
Expand All @@ -97,22 +48,10 @@ private void readConfigFromAPIManager(boolean useAdmin) throws AppException {
LOG.error("Error loading configuration from API-Manager. Response-Code: {} Got response: {}", statusCode, response);
throw new AppException("Error loading configuration from API-Manager. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
}
apiManagerResponse.put(useAdmin, response);
return mapper.readValue(response, Config.class);
}
} catch (Exception e) {
throw new AppException("Can't read configuration from API-Manager", ErrorCode.API_MANAGER_COMMUNICATION, e);
}
}

public Config getConfig(boolean useAdmin) throws AppException {
if (managerConfig.get(useAdmin) != null) return managerConfig.get(useAdmin);
readConfigFromAPIManager(useAdmin);
try {
Config config = mapper.readValue(apiManagerResponse.get(useAdmin), Config.class);
managerConfig.put(useAdmin, config);
return config;
} catch (IOException e) {
throw new AppException("Error parsing API-Manager configuration", ErrorCode.API_MANAGER_COMMUNICATION_ERR, e);
throw new AppException("Can't read configuration from API-Manager", ErrorCode.API_MANAGER_COMMUNICATION_ERR, e);
}
}

Expand All @@ -122,9 +61,6 @@ public void updateConfiguration(Config desiredConfig) throws AppException {
throw new AppException("An Admin Account is required to update the API-Manager configuration.", ErrorCode.NO_ADMIN_ROLE_USER);
}
URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + "/config").build();
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
SimpleBeanPropertyFilter.serializeAllExcept(ConfigFields.getIgnoredFields()));
mapper.setFilterProvider(filter);
mapper.registerModule(new SimpleModule().setSerializerModifier(new PolicySerializerModifier(false)));
mapper.setSerializationInclusion(Include.NON_NULL);
RestAPICall request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class APIMgrAppsAdapter {

Expand All @@ -44,8 +45,8 @@ public class APIMgrAppsAdapter {
public static final String API_KEY = "apiKey";
public static final String CREDENTIAL_TYPE = "credentialType";

Map<ClientAppFilter, String> apiManagerResponse = new HashMap<>();
Map<String, String> subscribedAppAPIManagerResponse = new HashMap<>();
Map<ClientAppFilter, String> apiManagerResponse = new ConcurrentHashMap<>();
Map<String, String> subscribedAppAPIManagerResponse = new ConcurrentHashMap<>();
CoreParameters cmd = CoreParameters.getInstance();
ObjectMapper mapper = APIManagerAdapter.mapper;
Cache<String, String> applicationsCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ public static void validateCustomProperties(Map<String, String> customProperties
}
}

/**
* Read custom properties from payload and create in a different format.
* @param entities api, app, org and user
* @param json raw json message
* @param filter filter
* @throws IOException
*/

public static void addCustomPropertiesForEntity(List<? extends CustomPropertiesEntity> entities, String json, CustomPropertiesFilter filter) throws IOException {
ObjectMapper mapper = new ObjectMapper();
// Custom-Properties will be added depending on the given Properties in the filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,12 @@ public void close() {
super.close();
}

@Test
public void testCorrectFieldsAreIgnored() throws AppException {
apiManagerAdapter.setApiManagerVersion("7.7.20200930");
String[] ignoreFields = APIManagerConfigAdapter.ConfigFields.getIgnoredFields();
// No field should be ignored for the Sept-Release
Assert.assertNotNull(ignoreFields);
Assert.assertEquals(ignoreFields.length, 0);

apiManagerAdapter.setApiManagerVersion("7.7.20200730");
ignoreFields = APIManagerConfigAdapter.ConfigFields.getIgnoredFields();
// Same for July, as fields have been added in January release
Assert.assertNotNull(ignoreFields);
Assert.assertEquals(ignoreFields.length, 0);

apiManagerAdapter.setApiManagerVersion("7.7.20200331");
ignoreFields = APIManagerConfigAdapter.ConfigFields.getIgnoredFields();
// March release doesn't support new parameters introduced with the May release
Assert.assertNotNull(ignoreFields);
Assert.assertEquals(ignoreFields.length, 4);

apiManagerAdapter.setApiManagerVersion("7.7.0");
ignoreFields = APIManagerConfigAdapter.ConfigFields.getIgnoredFields();
// 7.7.0 plain already supports some new settings
Assert.assertNotNull(ignoreFields);
Assert.assertEquals(ignoreFields.length, 4); // Only brand new fields are returned to be ignored
}

@Test
public void updateConfiguration() {
try {
Config config = apiManagerConfigAdapter.getConfig(true);
Config config = apiManagerConfigAdapter.getConfig();
config.setApiPortalHostname("api.axway.com");
apiManagerConfigAdapter.updateConfiguration(config);
}catch (AppException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public int importAPI(APIImportParams params) {
// If we don't have an AdminAccount available, we ignore published APIs - For OrgAdmins
// the unpublished or pending APIs become the actual API
boolean isAdminAccount = APIManagerAdapter.getInstance().hasAdminAccount();
boolean orgAdminSelfService = APIManagerAdapter.getInstance().getConfigAdapter().getConfig(isAdminAccount).getOadminSelfServiceEnabled();
boolean orgAdminSelfService = APIManagerAdapter.getInstance().getConfigAdapter().getConfig().getOadminSelfServiceEnabled();
if (!isAdminAccount && !orgAdminSelfService) {
filters.add(new BasicNameValuePair("field", "state"));
filters.add(new BasicNameValuePair("op", "ne"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public APIImportParams getApiImportParams() {


public boolean isAdminAccountNeeded() throws AppException {
boolean orgAdminSelfServiceEnabled = APIManagerAdapter.getInstance().getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()).getOadminSelfServiceEnabled();
boolean orgAdminSelfServiceEnabled = APIManagerAdapter.getInstance().getConfigAdapter().getConfig().getOadminSelfServiceEnabled();
if (orgAdminSelfServiceEnabled) return false;
return (!getDesiredAPI().getState().equals(Constants.API_UNPUBLISHED) && !getDesiredAPI().getState().equals(Constants.API_DELETED)) ||
(getActualAPI() != null && !getActualAPI().getState().equals(Constants.API_UNPUBLISHED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ private void handleOutboundSSLAuthN(AuthenticationProfile authnProfile) throws A
private void validateHasQueryStringKey(API importApi) throws AppException {
if (importApi.getApiRoutingKey() == null) return; // Nothing to check
if (APIManagerAdapter.getInstance().hasAdminAccount()) {
boolean apiRoutingKeyEnabled = APIManagerAdapter.getInstance().getConfigAdapter().getConfig(true).getApiRoutingKeyEnabled();
boolean apiRoutingKeyEnabled = APIManagerAdapter.getInstance().getConfigAdapter().getConfig().getApiRoutingKeyEnabled();
if (!apiRoutingKeyEnabled) {
throw new AppException("API-Manager Query-String Routing option is disabled. Please turn it on to use apiRoutingKey.", ErrorCode.QUERY_STRING_ROUTING_DISABLED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class APIImportManager {
*/
public void applyChanges(APIChangeState changeState, boolean forceUpdate, boolean updateOnly) throws AppException {
boolean enforceBreakingChange = CoreParameters.getInstance().isForce();
boolean orgAdminSelfService = APIManagerAdapter.getInstance().getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()).getOadminSelfServiceEnabled();
boolean orgAdminSelfService = APIManagerAdapter.getInstance().getConfigAdapter().getConfig().getOadminSelfServiceEnabled();
if (!APIManagerAdapter.getInstance().hasAdminAccount() && changeState.isAdminAccountNeeded()) {
if (orgAdminSelfService) {
LOG.info("Desired API-State set to published using OrgAdmin account only. Going to create a publish request.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private ExportResult exportAPIManagerSetup(APIManagerSetupExportParams params, R
APIManagerSetupResultHandler exporter = APIManagerSetupResultHandler.create(exportImpl, params, result);
APIManagerConfig apiManagerConfig = new APIManagerConfig();
if (params.isExportConfig()) {
apiManagerConfig.setConfig(adapter.getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()));
apiManagerConfig.setConfig(adapter.getConfigAdapter().getConfig());
}
if (params.isExportAlerts()) {
apiManagerConfig.setAlerts(adapter.getAlertsAdapter().getAlerts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void exportToFile(ObjectMapper mapper, APIManagerConfig apimanagerConfig,
private String getExportFolder(Config config) {
try {
if (config == null) {
config = APIManagerAdapter.getInstance().getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount());
config = APIManagerAdapter.getInstance().getConfigAdapter().getConfig();
}
String name = config.getPortalName().toLowerCase();
name = name.replace(" ", "-");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testConsoleExportConfigStandard() throws AppException {
APIManagerAdapter apimanagerAdapter = APIManagerAdapter.getInstance();
APIManagerSetupResultHandler exporter = APIManagerSetupResultHandler.create(ResultHandler.CONSOLE_EXPORTER, params, result);
APIManagerConfig apiManagerConfig = new APIManagerConfig();
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()));
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig());
exporter.export(apiManagerConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testJsonExport() throws AppException {
APIManagerAdapter apimanagerAdapter = APIManagerAdapter.getInstance();
APIManagerSetupResultHandler exporter = APIManagerSetupResultHandler.create(ResultHandler.JSON_EXPORTER, params, result);
APIManagerConfig apiManagerConfig = new APIManagerConfig();
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()));
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig());
exporter.export(apiManagerConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testYamlExport() throws AppException {
APIManagerAdapter apimanagerAdapter = APIManagerAdapter.getInstance();
APIManagerSetupResultHandler exporter = APIManagerSetupResultHandler.create(ResultHandler.YAML_EXPORTER, params, result);
APIManagerConfig apiManagerConfig = new APIManagerConfig();
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig(APIManagerAdapter.getInstance().hasAdminAccount()));
apiManagerConfig.setConfig(apimanagerAdapter.getConfigAdapter().getConfig());
exporter.export(apiManagerConfig);
}
}

0 comments on commit ed2c806

Please sign in to comment.