big refactor
This commit is contained in:
parent
742e848040
commit
ca01fb657f
@ -1,20 +1,25 @@
|
|||||||
package cz.trask.apioperator.impl;
|
package cz.trask.apioperator.impl;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import cz.trask.apioperator.AbstractProcess;
|
import cz.trask.apioperator.AbstractProcess;
|
||||||
import cz.trask.apioperator.model.APIInfo;
|
import cz.trask.apioperator.model.APIInfo;
|
||||||
@ -32,218 +37,327 @@ import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
|||||||
import io.apicurio.registry.rest.v2.beans.ArtifactReference;
|
import io.apicurio.registry.rest.v2.beans.ArtifactReference;
|
||||||
import io.apicurio.registry.rest.v2.beans.EditableMetaData;
|
import io.apicurio.registry.rest.v2.beans.EditableMetaData;
|
||||||
import io.apicurio.registry.rest.v2.beans.Rule;
|
import io.apicurio.registry.rest.v2.beans.Rule;
|
||||||
import io.apicurio.registry.rest.v2.beans.VersionMetaData;
|
|
||||||
import io.apicurio.registry.rest.v2.beans.VersionSearchResults;
|
import io.apicurio.registry.rest.v2.beans.VersionSearchResults;
|
||||||
import io.apicurio.registry.types.RuleType;
|
import io.apicurio.registry.types.RuleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import class – reads APIs from WSO2 APIM and publishes them to Apicurio.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* All major improvements from the original code:
|
||||||
|
* <ul>
|
||||||
|
* <li>Thread‑safe counter using {@link AtomicInteger}</li>
|
||||||
|
* <li>Parameterized Log4j messages</li>
|
||||||
|
* <li>Safe JSON deserialization with {@code TypeToken}</li>
|
||||||
|
* <li>Try‑with‑resources for all streams</li>
|
||||||
|
* <li>Artifact creation – references first, then parent</li>
|
||||||
|
* <li>Cleaner metadata handling (no trailing spaces)</li>
|
||||||
|
* <li>Specific exception handling and wrapping in a runtime exception</li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
public class Import extends AbstractProcess {
|
public class Import extends AbstractProcess {
|
||||||
|
|
||||||
private static Logger log = LogManager.getLogger(Import.class);
|
private static final Logger log = LogManager.getLogger(Import.class);
|
||||||
|
|
||||||
StartParameters sp;
|
private final AtomicInteger apiCounter = new AtomicInteger(1);
|
||||||
RegistryClient client;
|
private final Gson gson = new Gson();
|
||||||
static int i = 1;
|
|
||||||
|
private final StartParameters sp;
|
||||||
|
private final RegistryClient client;
|
||||||
|
|
||||||
public Import(StartParameters sp) throws Exception {
|
public Import(StartParameters sp) throws Exception {
|
||||||
this.sp = sp;
|
this.sp = sp;
|
||||||
client = RegistryClientFactory.create(config.getApicurioApiUrl());
|
this.client = RegistryClientFactory.create(config.getApicurioApiUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process Export API
|
* Main entry point for the import process.
|
||||||
*
|
*
|
||||||
* @throws Exception - all exceptions in the code are thrown for handling in
|
* @throws RuntimeException if any error occurs
|
||||||
* CICD purpose
|
|
||||||
*/
|
*/
|
||||||
public void process() throws Exception {
|
public void process() {
|
||||||
log.info("exporting APIs...");
|
|
||||||
try {
|
try {
|
||||||
log.info("Register user for calling Admins APIs...");
|
log.info("Starting API export…");
|
||||||
RegisterResponse registerResponse = register(config.getSourceRegistrationApiUrl(),
|
|
||||||
config.getSourceWso2User());
|
|
||||||
log.info("Registered with clientId: " + registerResponse.getClientId());
|
|
||||||
|
|
||||||
log.info("Getting token for clientId:" + registerResponse.getClientId());
|
RegisterResponse register = register(config.getSourceRegistrationApiUrl(), config.getSourceWso2User());
|
||||||
TokenResponse tokenResponse = getToken(config.getSourcePublisherTokenUrl(), config.getSourceWso2User(),
|
|
||||||
registerResponse,
|
|
||||||
"apim:api_view apim:api_create apim:api_manage apim:api_delete apim:api_publish apim:subscription_view apim:subscription_block apim:subscription_manage apim:external_services_discover apim:threat_protection_policy_create apim:threat_protection_policy_manage apim:document_create apim:document_manage apim:mediation_policy_view apim:mediation_policy_create apim:mediation_policy_manage apim:client_certificates_view apim:client_certificates_add apim:client_certificates_update apim:ep_certificates_view apim:ep_certificates_add apim:ep_certificates_update apim:publisher_settings apim:pub_alert_manage apim:shared_scope_manage apim:app_import_export apim:api_import_export apim:api_product_import_export apim:api_generate_key apim:common_operation_policy_view apim:common_operation_policy_manage apim:comment_write apim:comment_view apim:admin");
|
|
||||||
log.debug("Token for clientId is: " + tokenResponse.getAccess_token());
|
|
||||||
|
|
||||||
log.info("Getting list of APIs ...");
|
String clientId = register.getClientId();
|
||||||
APIList apis = getList(config.getSourcePublisherApiUrl(), tokenResponse);
|
log.info("Registered with clientId: {}", clientId);
|
||||||
|
|
||||||
|
TokenResponse token = getToken(config.getSourcePublisherTokenUrl(), config.getSourceWso2User(), register,
|
||||||
|
"apim:api_view apim:api_create apim:api_manage apim:api_delete apim:api_publish "
|
||||||
|
+ "apim:subscription_view apim:subscription_block apim:subscription_manage apim:external_services_discover "
|
||||||
|
+ "apim:threat_protection_policy_create apim:threat_protection_policy_manage apim:document_create apim:document_manage "
|
||||||
|
+ "apim:mediation_policy_view apim:mediation_policy_create apim:mediation_policy_manage apim:client_certificates_view "
|
||||||
|
+ "apim:client_certificates_add apim:client_certificates_update apim:ep_certificates_view apim:ep_certificates_add "
|
||||||
|
+ "apim:ep_certificates_update apim:publisher_settings apim:pub_alert_manage apim:shared_scope_manage apim:app_import_export "
|
||||||
|
+ "apim:api_import_export apim:api_product_import_export apim:api_generate_key apim:common_operation_policy_view "
|
||||||
|
+ "apim:common_operation_policy_manage apim:comment_write apim:comment_view apim:admin");
|
||||||
|
|
||||||
|
log.debug("Access token received – {}", token.getAccess_token());
|
||||||
|
|
||||||
|
APIList apis = getList(config.getSourcePublisherApiUrl(), token);
|
||||||
if (apis == null || apis.getList() == null || apis.getList().length == 0) {
|
if (apis == null || apis.getList() == null || apis.getList().length == 0) {
|
||||||
throw new Exception(
|
throw new IllegalStateException(
|
||||||
"There is no APIs to export that meets yours criteria! Please, check the name of API you want to export and try again!");
|
"No APIs to export that match your criteria! Check the name of the API you want to export.");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Found " + apis.getCount() + " APIs");
|
log.info("Found {} APIs", apis.getCount());
|
||||||
|
|
||||||
int maxThreads = config.getMaxThreads();
|
int maxThreads = config.getMaxThreads();
|
||||||
log.info("Starting API processing with " + maxThreads + " threads");
|
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
|
ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
|
||||||
|
|
||||||
for (APIInfo api : apis.getList()) {
|
for (APIInfo api : apis.getList()) {
|
||||||
executor.execute(new Runnable() {
|
final int index = apiCounter.getAndIncrement();
|
||||||
@Override
|
executor.submit(() -> processApi(api, token, index, apis.getCount()));
|
||||||
public void run() {
|
|
||||||
processApi(api, tokenResponse, i++, apis.getCount());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
while (!executor.isTerminated()) {
|
if (!executor.awaitTermination(10, TimeUnit.MINUTES)) {
|
||||||
Thread.sleep(500);
|
log.warn("Timeout waiting for API import tasks to finish");
|
||||||
}
|
}
|
||||||
log.info("Finished processing APIs.");
|
log.info("Finished processing APIs.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error while exporting APIs.", e);
|
log.error("Error while exporting APIs.", e);
|
||||||
throw new Exception("Error while exporting APIs!");
|
throw new RuntimeException("Export failed", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processApi(APIInfo api, TokenResponse tokenResponse, int i, int apiCount) {
|
/**
|
||||||
|
* Process a single API – fetches the data, creates or updates the corresponding
|
||||||
|
* artifact in Apicurio.
|
||||||
|
*/
|
||||||
|
private void processApi(APIInfo api, TokenResponse tokenResponse, int index, int total) {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
String apiStatus = api.getLifeCycleStatus();
|
String status = api.getLifeCycleStatus();
|
||||||
if (!apiStatus.contains("PUBLISHED") && !apiStatus.contains("DEPRECATED")) {
|
|
||||||
log.info("Skipping API " + i + " of " + apiCount + "with ID " + api.getId()
|
if (!status.contains("PUBLISHED") && !status.contains("DEPRECATED")) {
|
||||||
+ " because it is not published.");
|
log.info("Skipping API {} of {} – not published (ID={})", index, total, api.getId());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.info("Processing API " + i + " of " + apiCount);
|
log.info("Processing API {} of {}", index, total);
|
||||||
|
|
||||||
Map<String, String> httpHeaders = new HashMap<>();
|
Map<String, String> httpHeaders = Collections.singletonMap("Authorization",
|
||||||
Map<String, String> params = new HashMap<>();
|
"Bearer " + tokenResponse.getAccess_token());
|
||||||
|
|
||||||
httpHeaders.put("Authorization", "Bearer ".concat(tokenResponse.getAccess_token()));
|
String type = mapApiType(api.getType());
|
||||||
|
|
||||||
String type = api.getType();
|
// 1) Retrieve basic information
|
||||||
|
HttpResponse apiInfoResp = makeRequest("GET", config.getSourceDevportalApiUrl() + "/apis/" + api.getId(),
|
||||||
|
httpHeaders, Collections.emptyMap());
|
||||||
|
|
||||||
|
HttpResponse subsResp = makeRequest("GET",
|
||||||
|
config.getSourcePublisherApiUrl() + "/subscriptions?apiId=" + api.getId(), httpHeaders,
|
||||||
|
Collections.emptyMap());
|
||||||
|
|
||||||
|
HttpResponse swaggerResp = makeRequest("GET",
|
||||||
|
config.getSourcePublisherApiUrl() + "/apis/" + api.getId() + "/swagger", httpHeaders,
|
||||||
|
Collections.emptyMap());
|
||||||
|
|
||||||
|
// 2) Export the API as a zip
|
||||||
|
HttpResponse exportedZip = makeRequest("GET",
|
||||||
|
config.getSourcePublisherApiUrl() + "/apis/export?apiId=" + api.getId(), httpHeaders,
|
||||||
|
Collections.emptyMap(), true);
|
||||||
|
|
||||||
|
List<ZipEntryData> zipEntries = ZipExtractor.extractFilesFromZip(exportedZip.getResponseBytes());
|
||||||
|
|
||||||
|
// 3) Deserialize JSON responses
|
||||||
|
TypeToken<Map<String, Object>> mapType = new TypeToken<>() {
|
||||||
|
};
|
||||||
|
Map<String, Object> apiMap = gson.fromJson(apiInfoResp.getResponse(), mapType.getType());
|
||||||
|
Map<String, Object> subsMap = gson.fromJson(subsResp.getResponse(), mapType.getType());
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<String> tagsList = (List<String>) apiMap.get("tags");
|
||||||
|
|
||||||
|
// 4) Build the properties map
|
||||||
|
Map<String, String> props = new LinkedHashMap<>();
|
||||||
|
props.put("version", api.getVersion());
|
||||||
|
props.put("status", status);
|
||||||
|
addSubscriptionsToProps(props, subsMap);
|
||||||
|
addEndpointsToProps(props, apiMap);
|
||||||
|
addTagsToProps(props, tagsList);
|
||||||
|
|
||||||
|
// 5) Build the description that contains the publisher & devportal URLs
|
||||||
|
String baseDesc = api.getDescription() != null ? api.getDescription() : "";
|
||||||
|
String pubUrl = config.getPublisherUrlPattern().replace("{API_ID}", api.getId());
|
||||||
|
String devPortUrl = config.getDevportalUrlPattern().replace("{API_ID}", api.getId());
|
||||||
|
|
||||||
|
String fullDesc = baseDesc + " ***** PUBLISHER URL ***** " + pubUrl + " ***** DEVPORTAL URL ***** "
|
||||||
|
+ devPortUrl;
|
||||||
|
|
||||||
|
// 6) Update the swagger with the description and servers
|
||||||
|
JsonObject swaggerObj = JsonParser.parseString(swaggerResp.getResponse()).getAsJsonObject();
|
||||||
|
updateSwagger(swaggerObj, apiMap, fullDesc);
|
||||||
|
|
||||||
|
// 7) Prepare artifact creation/update
|
||||||
|
String group = config.getDefaultApiGroup();
|
||||||
|
String mainArtifactId = api.getName() + api.getContext();
|
||||||
|
|
||||||
|
VersionSearchResults existingArtifacts;
|
||||||
|
try {
|
||||||
|
existingArtifacts = client.listArtifactVersions(group, mainArtifactId, 0, Integer.MAX_VALUE);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("No API {} exists – will create it", api.getContext());
|
||||||
|
existingArtifacts = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingArtifacts == null) {
|
||||||
|
// Create new artifact
|
||||||
|
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||||
|
|
||||||
|
ArtifactMetaData meta = client.createArtifact(group, mainArtifactId, api.getVersion(), null, null, null,
|
||||||
|
api.getName(), fullDesc, null, null, null,
|
||||||
|
new ByteArrayInputStream(swaggerObj.toString().getBytes()), references);
|
||||||
|
|
||||||
|
setMetaAndRules(meta, props, tagsList);
|
||||||
|
// Create the three required rules
|
||||||
|
createRule(meta, "NONE", RuleType.COMPATIBILITY);
|
||||||
|
createRule(meta, "NONE", RuleType.VALIDITY);
|
||||||
|
createRule(meta, "NONE", RuleType.INTEGRITY);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Artifact exists – check if the version exists
|
||||||
|
boolean versionExists = false;
|
||||||
|
try {
|
||||||
|
client.getArtifactVersionMetaData(group, mainArtifactId, api.getVersion());
|
||||||
|
versionExists = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Version missing – will create it below
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||||
|
|
||||||
|
if (!versionExists) {
|
||||||
|
ArtifactMetaData meta = client.updateArtifact(group, mainArtifactId, api.getVersion(),
|
||||||
|
api.getName(), fullDesc, new ByteArrayInputStream(swaggerObj.toString().getBytes()),
|
||||||
|
references);
|
||||||
|
setMetaAndRules(meta, props, tagsList);
|
||||||
|
} else {
|
||||||
|
// Version already exists – no action needed
|
||||||
|
log.warn("API {} with version {} already exists. Skipping import.", api.getContext(),
|
||||||
|
api.getVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Successfully imported API '{}' ({}). Took {} ms", api.getName(), api.getVersion(),
|
||||||
|
System.currentTimeMillis() - start);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("IO error while importing API {}: {}", api.getId(), e.getMessage(), e);
|
||||||
|
} catch (VersionAlreadyExistsException e) {
|
||||||
|
log.warn("API version already exists for {}: {}. Skipping.", api.getName(), api.getVersion());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Cannot export API '{}':{}", api.getName(), api.getVersion(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
/* Helper methods */
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
private String mapApiType(String type) {
|
||||||
|
// Java 11 does not support switch‑expressions
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "HTTP":
|
case "HTTP":
|
||||||
type = "OPENAPI";
|
return "OPENAPI";
|
||||||
break;
|
|
||||||
case "GRAPHQL":
|
case "GRAPHQL":
|
||||||
type = "GRAPHQL";
|
return "GRAPHQL";
|
||||||
break;
|
|
||||||
case "WS":
|
case "WS":
|
||||||
type = "ASYNCAPI";
|
return "ASYNCAPI";
|
||||||
break;
|
|
||||||
case "SOAP":
|
case "SOAP":
|
||||||
type = "WSDL";
|
return "WSDL";
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
private void updateSwagger(JsonObject swagger, Map<String, Object> apiMap, String description) {
|
||||||
HttpResponse responseApi = makeRequest("GET",
|
|
||||||
config.getSourceDevportalApiUrl().concat(String.format("/apis/%s", api.getId())), httpHeaders,
|
|
||||||
params);
|
|
||||||
HttpResponse responseSubs = makeRequest("GET",
|
|
||||||
config.getSourcePublisherApiUrl().concat(String.format("/subscriptions?apiId=%s", api.getId())),
|
|
||||||
httpHeaders, params);
|
|
||||||
HttpResponse responseApiSwagger = makeRequest("GET",
|
|
||||||
config.getSourcePublisherApiUrl().concat(String.format("/apis/%s/swagger", api.getId())),
|
|
||||||
httpHeaders, params);
|
|
||||||
|
|
||||||
HttpResponse exportedApiZip = makeRequest("GET",
|
|
||||||
config.getSourcePublisherApiUrl().concat(String.format("/apis/export?apiId=%s", api.getId())),
|
|
||||||
httpHeaders, params, true);
|
|
||||||
|
|
||||||
List<ZipEntryData> zipEntries = ZipExtractor.extractFilesFromZip(exportedApiZip.getResponseBytes());
|
|
||||||
|
|
||||||
Map<String, Object> mapSubs = gson.fromJson(responseSubs.getResponse(), Map.class);
|
|
||||||
Map<String, Object> mapApi = gson.fromJson(responseApi.getResponse(), Map.class);
|
|
||||||
|
|
||||||
List<String> tags = new ArrayList<String>();
|
|
||||||
|
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
|
||||||
map.put("Version ", " " + api.getVersion());
|
|
||||||
map.put("Status ", " " + apiStatus);
|
|
||||||
addSubscriptionsToMap(map, mapSubs);
|
|
||||||
addEndpointsToMap(map, mapApi);
|
|
||||||
addTagsToMap(map, (List) mapApi.get("tags"));
|
|
||||||
|
|
||||||
String group = config.getDefaultApiGroup();
|
|
||||||
|
|
||||||
String ApiDesc = api.getDescription() != null ? api.getDescription() : "";
|
|
||||||
|
|
||||||
String ApiDescNew = ApiDesc + " ***** " + "PUBLISHER URL ***** "
|
|
||||||
+ config.getPublisherUrlPattern().replace("{API_ID}", api.getId()) + " ***** "
|
|
||||||
+ "DEVPORTAL URL ***** " + config.getDevportalUrlPattern().replace("{API_ID}", api.getId());
|
|
||||||
|
|
||||||
String ApiDescSwagger = ApiDesc + "\r\n\r\n\n\n" + "PUBLISHER URL:"
|
|
||||||
+ config.getPublisherUrlPattern().replace("{API_ID}", api.getId()) + "\r\n\n\n"
|
|
||||||
+ "DEVPORTAL URL:" + config.getDevportalUrlPattern().replace("{API_ID}", api.getId());
|
|
||||||
|
|
||||||
JsonObject swagger = JsonParser.parseString(responseApiSwagger.getResponse()).getAsJsonObject();
|
|
||||||
|
|
||||||
JsonObject info = swagger.getAsJsonObject("info");
|
JsonObject info = swagger.getAsJsonObject("info");
|
||||||
info.addProperty("description", ApiDescSwagger);
|
if (info != null) {
|
||||||
|
info.addProperty("description", description);
|
||||||
JsonArray serversJsonArray = new JsonArray();
|
|
||||||
|
|
||||||
if (mapApi != null && !mapApi.isEmpty()) {
|
|
||||||
List<Map> list = (List) mapApi.get("endpointURLs");
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
for (Iterator<Map> it = list.iterator(); it.hasNext();) {
|
|
||||||
Map environment = it.next();
|
|
||||||
Map urls = (Map) environment.get("URLs");
|
|
||||||
|
|
||||||
if (urls != null && !urls.isEmpty()) {
|
|
||||||
JsonObject servers = new JsonObject();
|
|
||||||
|
|
||||||
String https = (String) urls.get("https");
|
|
||||||
String wss = (String) urls.get("wss");
|
|
||||||
|
|
||||||
if (https != null) {
|
|
||||||
servers.addProperty("url", https);
|
|
||||||
}
|
|
||||||
if (wss != null) {
|
|
||||||
servers.addProperty("url", wss);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
servers.addProperty("description",
|
// Build servers array
|
||||||
"Gateway: " + (String) environment.get("environmentName"));
|
JsonArray servers = new JsonArray();
|
||||||
serversJsonArray.add(servers);
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Map<String, Object>> endpoints = (List<Map<String, Object>>) apiMap.get("endpointURLs");
|
||||||
|
if (endpoints != null) {
|
||||||
|
for (Map<String, Object> env : endpoints) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, String> urls = (Map<String, String>) env.get("URLs");
|
||||||
|
if (urls == null || urls.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
JsonObject server = new JsonObject();
|
||||||
|
urls.forEach((k, v) -> {
|
||||||
|
if (v != null && !v.isBlank()) {
|
||||||
|
if (k.equals("https")) {
|
||||||
|
server.addProperty("url", v);
|
||||||
|
} else if (k.equals("wss")) {
|
||||||
|
server.addProperty("url", v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
server.addProperty("description", "Gateway: " + env.getOrDefault("environmentName", ""));
|
||||||
|
servers.add(server);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
swagger.remove("servers");
|
swagger.remove("servers");
|
||||||
swagger.add("servers", serversJsonArray);
|
swagger.add("servers", servers);
|
||||||
|
|
||||||
swagger.getAsJsonObject("info").addProperty("description", ApiDescSwagger);
|
|
||||||
|
|
||||||
log.info("Trying to call ApiCurio...");
|
|
||||||
|
|
||||||
VersionMetaData checkIfVersionExist = null;
|
|
||||||
VersionSearchResults checkIfApiExist = null;
|
|
||||||
|
|
||||||
String mainArtifactId = api.getName().concat(api.getContext());
|
|
||||||
|
|
||||||
try {
|
|
||||||
checkIfApiExist = client.listArtifactVersions(group, mainArtifactId, 0, 9999);
|
|
||||||
log.debug("API " + api.getContext() + " exists ...");
|
|
||||||
try {
|
|
||||||
checkIfVersionExist = client.getArtifactVersionMetaData(group, mainArtifactId,
|
|
||||||
api.getVersion());
|
|
||||||
log.debug("Version " + api.getVersion() + " for API " + api.getContext() + " exists ...");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug("No version " + api.getVersion() + " for API " + api.getContext() + " exists ...");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug("No API " + api.getContext() + " exists, so creating ...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkIfApiExist == null) {
|
private void addSubscriptionsToProps(Map<String, String> props, Map<String, Object> subsMap) {
|
||||||
|
if (subsMap == null || !subsMap.containsKey("list"))
|
||||||
|
return;
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Map<String, Object>> list = (List<Map<String, Object>>) subsMap.get("list");
|
||||||
|
int i = 1;
|
||||||
|
for (Map<String, Object> sub : list) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, String> appInfo = (Map<String, String>) sub.get("applicationInfo");
|
||||||
|
if (appInfo == null)
|
||||||
|
continue;
|
||||||
|
props.put("subscription" + i,
|
||||||
|
appInfo.getOrDefault("name", "") + " (Owner: " + appInfo.getOrDefault("subscriber", "") + ")");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addEndpointsToProps(Map<String, String> props, Map<String, Object> apiMap) {
|
||||||
|
if (apiMap == null || !apiMap.containsKey("endpointURLs"))
|
||||||
|
return;
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Map<String, Object>> envs = (List<Map<String, Object>>) apiMap.get("endpointURLs");
|
||||||
|
for (Map<String, Object> env : envs) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, String> urls = (Map<String, String>) env.get("URLs");
|
||||||
|
if (urls == null)
|
||||||
|
continue;
|
||||||
|
urls.forEach((k, v) -> props.put(k + " Endpoint", v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addTagsToProps(Map<String, String> props, List<String> tags) {
|
||||||
|
if (tags != null && !tags.isEmpty()) {
|
||||||
|
props.put("tags", String.join(", ", tags));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ArtifactReference> createReferencesFromZip(List<ZipEntryData> zipEntries, String group, APIInfo api)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
List<ArtifactReference> references = new ArrayList<>();
|
List<ArtifactReference> references = new ArrayList<>();
|
||||||
for (ZipEntryData entry : zipEntries) {
|
for (ZipEntryData entry : zipEntries) {
|
||||||
String artifactId = api.getName().concat("/").concat(api.getVersion()).concat("/")
|
String artifactId = api.getName() + "/" + api.getVersion() + "/" + entry.getName();
|
||||||
.concat(entry.getName());
|
|
||||||
ArtifactMetaData mtd = client.createArtifactWithVersion(entry.getType().toString(), artifactId,
|
// Create the artifact (versioned)
|
||||||
api.getVersion(), new ByteArrayInputStream(entry.getContent()));
|
try (ByteArrayInputStream is = new ByteArrayInputStream(entry.getContent())) {
|
||||||
|
client.createArtifactWithVersion(entry.getType().toString(), artifactId, api.getVersion(), is);
|
||||||
|
}
|
||||||
|
|
||||||
ArtifactReference ref = new ArtifactReference();
|
ArtifactReference ref = new ArtifactReference();
|
||||||
ref.setName(entry.getName());
|
ref.setName(entry.getName());
|
||||||
@ -252,115 +366,23 @@ public class Import extends AbstractProcess {
|
|||||||
ref.setVersion(api.getVersion());
|
ref.setVersion(api.getVersion());
|
||||||
references.add(ref);
|
references.add(ref);
|
||||||
}
|
}
|
||||||
|
return references;
|
||||||
ArtifactMetaData mtd = client.createArtifact(group, mainArtifactId, api.getVersion(), null, null,
|
|
||||||
null, api.getName(), ApiDescNew, null, null, null,
|
|
||||||
new ByteArrayInputStream(swagger.toString().getBytes()), references);
|
|
||||||
EditableMetaData props = new EditableMetaData();
|
|
||||||
props.setName(api.getName());
|
|
||||||
props.setDescription(ApiDescNew);
|
|
||||||
props.setProperties(map);
|
|
||||||
props.setLabels(tags);
|
|
||||||
|
|
||||||
client.updateArtifactMetaData(mtd.getGroupId(), mtd.getId(), props);
|
|
||||||
updateRules(mtd, "NONE", "COMPATIBILITY");
|
|
||||||
updateRules(mtd, "NONE", "VALIDITY");
|
|
||||||
updateRules(mtd, "NONE", "INTEGRITY");
|
|
||||||
|
|
||||||
} else if (checkIfVersionExist == null) {
|
|
||||||
List<ArtifactReference> references = new ArrayList<>();
|
|
||||||
for (ZipEntryData entry : zipEntries) {
|
|
||||||
String artifactId = api.getName().concat("/").concat(api.getVersion()).concat("/")
|
|
||||||
.concat(entry.getName());
|
|
||||||
ArtifactMetaData mtd = client.createArtifactWithVersion(entry.getType().toString(), artifactId,
|
|
||||||
api.getVersion(), new ByteArrayInputStream(entry.getContent()));
|
|
||||||
|
|
||||||
ArtifactReference ref = new ArtifactReference();
|
|
||||||
ref.setName(entry.getName());
|
|
||||||
ref.setGroupId(entry.getType().toString());
|
|
||||||
ref.setArtifactId(artifactId);
|
|
||||||
ref.setVersion(api.getVersion());
|
|
||||||
references.add(ref);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ArtifactMetaData mtd = client.updateArtifact(group, mainArtifactId, api.getVersion(), api.getName(),
|
private void setMetaAndRules(ArtifactMetaData meta, Map<String, String> props, List<String> tags) {
|
||||||
ApiDescNew, new ByteArrayInputStream(swagger.toString().getBytes()), references);
|
EditableMetaData metaData = new EditableMetaData();
|
||||||
EditableMetaData props = new EditableMetaData();
|
metaData.setName(meta.getName());
|
||||||
props.setName(api.getName());
|
metaData.setDescription(meta.getDescription());
|
||||||
props.setDescription(ApiDescNew);
|
metaData.setProperties(props);
|
||||||
props.setProperties(map);
|
metaData.setLabels(tags);
|
||||||
props.setLabels(tags);
|
|
||||||
client.updateArtifactMetaData(mtd.getGroupId(), mtd.getId(), props);
|
client.updateArtifactMetaData(meta.getGroupId(), meta.getId(), metaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("ApiCurio called successfully...");
|
private void createRule(ArtifactMetaData meta, String config, RuleType type) {
|
||||||
|
|
||||||
} catch (VersionAlreadyExistsException e) {
|
|
||||||
log.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("API successfully posted to APICurio. APIName='" + api.getName() + "'. Done in "
|
|
||||||
+ (System.currentTimeMillis() - start) + "ms");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Cannot export API: " + api.getName() + " " + api.getVersion(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addTagsToMap(Map<String, String> map, List<String> list) {
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
map.put("Tags ", " " + list.toString().replace("[", "").replace("]", ""));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateRules(ArtifactMetaData mtd, String config, String ruletype) {
|
|
||||||
Rule rule = new Rule();
|
Rule rule = new Rule();
|
||||||
rule.setConfig(config);
|
rule.setConfig(config);
|
||||||
rule.setType(RuleType.fromValue(ruletype));
|
rule.setType(type);
|
||||||
client.createArtifactRule(mtd.getGroupId(), mtd.getId(), rule);
|
client.createArtifactRule(meta.getGroupId(), meta.getId(), rule);
|
||||||
}
|
|
||||||
|
|
||||||
private void addSubscriptionsToMap(Map<String, String> map, Map mapSubs) {
|
|
||||||
if (mapSubs != null && !mapSubs.isEmpty()) {
|
|
||||||
List<Map> list = (List) mapSubs.get("list");
|
|
||||||
int i = 0;
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
for (Iterator<Map> it = list.iterator(); it.hasNext();) {
|
|
||||||
i++;
|
|
||||||
Map subscription = it.next();
|
|
||||||
Map applicationInfo = (Map) subscription.get("applicationInfo");
|
|
||||||
if (applicationInfo != null && !applicationInfo.isEmpty())
|
|
||||||
map.put("Subscription" + i + " ", " " + (String) applicationInfo.get("name") + " " + "(Owner: "
|
|
||||||
+ (String) applicationInfo.get("subscriber") + ")");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addEndpointsToMap(Map<String, String> map, Map<String, Object> mapApi) {
|
|
||||||
if (mapApi != null && !mapApi.isEmpty()) {
|
|
||||||
List<Map> list = (List) mapApi.get("endpointURLs");
|
|
||||||
if (list != null && !list.isEmpty()) {
|
|
||||||
for (Iterator<Map> it = list.iterator(); it.hasNext();) {
|
|
||||||
Map environment = it.next();
|
|
||||||
Map urls = (Map) environment.get("URLs");
|
|
||||||
if (urls != null && !urls.isEmpty()) {
|
|
||||||
String http = (String) urls.get("http");
|
|
||||||
String https = (String) urls.get("https");
|
|
||||||
String ws = (String) urls.get("ws");
|
|
||||||
String wss = (String) urls.get("wss");
|
|
||||||
if (http != null) {
|
|
||||||
map.put("HTTP Endpoint", http);
|
|
||||||
} else if (https != null) {
|
|
||||||
map.put("HTTPS Endpoint", https);
|
|
||||||
} else if (ws != null) {
|
|
||||||
map.put("WS Endpoint", ws);
|
|
||||||
} else if (wss != null) {
|
|
||||||
map.put("WSS Endpoint", wss);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user