big refactor
This commit is contained in:
parent
742e848040
commit
ca01fb657f
@ -1,20 +1,25 @@
|
||||
package cz.trask.apioperator.impl;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
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.Logger;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import cz.trask.apioperator.AbstractProcess;
|
||||
import cz.trask.apioperator.model.APIInfo;
|
||||
@ -32,335 +37,352 @@ import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactReference;
|
||||
import io.apicurio.registry.rest.v2.beans.EditableMetaData;
|
||||
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.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 {
|
||||
|
||||
private static Logger log = LogManager.getLogger(Import.class);
|
||||
private static final Logger log = LogManager.getLogger(Import.class);
|
||||
|
||||
StartParameters sp;
|
||||
RegistryClient client;
|
||||
static int i = 1;
|
||||
private final AtomicInteger apiCounter = new AtomicInteger(1);
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
private final StartParameters sp;
|
||||
private final RegistryClient client;
|
||||
|
||||
public Import(StartParameters sp) throws Exception {
|
||||
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
|
||||
* CICD purpose
|
||||
* @throws RuntimeException if any error occurs
|
||||
*/
|
||||
public void process() throws Exception {
|
||||
log.info("exporting APIs...");
|
||||
public void process() {
|
||||
try {
|
||||
log.info("Register user for calling Admins APIs...");
|
||||
RegisterResponse registerResponse = register(config.getSourceRegistrationApiUrl(),
|
||||
config.getSourceWso2User());
|
||||
log.info("Registered with clientId: " + registerResponse.getClientId());
|
||||
log.info("Starting API export…");
|
||||
|
||||
log.info("Getting token for clientId:" + registerResponse.getClientId());
|
||||
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());
|
||||
RegisterResponse register = register(config.getSourceRegistrationApiUrl(), config.getSourceWso2User());
|
||||
|
||||
log.info("Getting list of APIs ...");
|
||||
APIList apis = getList(config.getSourcePublisherApiUrl(), tokenResponse);
|
||||
String clientId = register.getClientId();
|
||||
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) {
|
||||
throw new Exception(
|
||||
"There is no APIs to export that meets yours criteria! Please, check the name of API you want to export and try again!");
|
||||
throw new IllegalStateException(
|
||||
"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();
|
||||
log.info("Starting API processing with " + maxThreads + " threads");
|
||||
ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
|
||||
|
||||
for (APIInfo api : apis.getList()) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
processApi(api, tokenResponse, i++, apis.getCount());
|
||||
}
|
||||
});
|
||||
final int index = apiCounter.getAndIncrement();
|
||||
executor.submit(() -> processApi(api, token, index, apis.getCount()));
|
||||
}
|
||||
|
||||
executor.shutdown();
|
||||
while (!executor.isTerminated()) {
|
||||
Thread.sleep(500);
|
||||
if (!executor.awaitTermination(10, TimeUnit.MINUTES)) {
|
||||
log.warn("Timeout waiting for API import tasks to finish");
|
||||
}
|
||||
log.info("Finished processing APIs.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error while exporting APIs. ", e);
|
||||
throw new Exception("Error while exporting APIs!");
|
||||
log.error("Error while exporting APIs.", e);
|
||||
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();
|
||||
String apiStatus = api.getLifeCycleStatus();
|
||||
if (!apiStatus.contains("PUBLISHED") && !apiStatus.contains("DEPRECATED")) {
|
||||
log.info("Skipping API " + i + " of " + apiCount + "with ID " + api.getId()
|
||||
+ " because it is not published.");
|
||||
String status = api.getLifeCycleStatus();
|
||||
|
||||
if (!status.contains("PUBLISHED") && !status.contains("DEPRECATED")) {
|
||||
log.info("Skipping API {} of {} – not published (ID={})", index, total, api.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("Processing API " + i + " of " + apiCount);
|
||||
log.info("Processing API {} of {}", index, total);
|
||||
|
||||
Map<String, String> httpHeaders = new HashMap<>();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String> httpHeaders = Collections.singletonMap("Authorization",
|
||||
"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());
|
||||
|
||||
switch (type) {
|
||||
case "HTTP":
|
||||
type = "OPENAPI";
|
||||
break;
|
||||
case "GRAPHQL":
|
||||
type = "GRAPHQL";
|
||||
break;
|
||||
case "WS":
|
||||
type = "ASYNCAPI";
|
||||
break;
|
||||
case "SOAP":
|
||||
type = "WSDL";
|
||||
break;
|
||||
default:
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
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);
|
||||
if (existingArtifacts == null) {
|
||||
// Create new artifact
|
||||
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||
|
||||
HttpResponse exportedApiZip = makeRequest("GET",
|
||||
config.getSourcePublisherApiUrl().concat(String.format("/apis/export?apiId=%s", api.getId())),
|
||||
httpHeaders, params, true);
|
||||
ArtifactMetaData meta = client.createArtifact(group, mainArtifactId, api.getVersion(), null, null, null,
|
||||
api.getName(), fullDesc, null, null, null,
|
||||
new ByteArrayInputStream(swaggerObj.toString().getBytes()), references);
|
||||
|
||||
List<ZipEntryData> zipEntries = ZipExtractor.extractFilesFromZip(exportedApiZip.getResponseBytes());
|
||||
setMetaAndRules(meta, props, tagsList);
|
||||
// Create the three required rules
|
||||
createRule(meta, "NONE", RuleType.COMPATIBILITY);
|
||||
createRule(meta, "NONE", RuleType.VALIDITY);
|
||||
createRule(meta, "NONE", RuleType.INTEGRITY);
|
||||
|
||||
Map<String, Object> mapSubs = gson.fromJson(responseSubs.getResponse(), Map.class);
|
||||
Map<String, Object> mapApi = gson.fromJson(responseApi.getResponse(), Map.class);
|
||||
} 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<String> tags = new ArrayList<String>();
|
||||
List<ArtifactReference> references = createReferencesFromZip(zipEntries, group, api);
|
||||
|
||||
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"));
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
String group = config.getDefaultApiGroup();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
String ApiDesc = api.getDescription() != null ? api.getDescription() : "";
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* Helper methods */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
String ApiDescNew = ApiDesc + " ***** " + "PUBLISHER URL ***** "
|
||||
+ config.getPublisherUrlPattern().replace("{API_ID}", api.getId()) + " ***** "
|
||||
+ "DEVPORTAL URL ***** " + config.getDevportalUrlPattern().replace("{API_ID}", api.getId());
|
||||
private String mapApiType(String type) {
|
||||
// Java 11 does not support switch‑expressions
|
||||
switch (type) {
|
||||
case "HTTP":
|
||||
return "OPENAPI";
|
||||
case "GRAPHQL":
|
||||
return "GRAPHQL";
|
||||
case "WS":
|
||||
return "ASYNCAPI";
|
||||
case "SOAP":
|
||||
return "WSDL";
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
private void updateSwagger(JsonObject swagger, Map<String, Object> apiMap, String description) {
|
||||
JsonObject info = swagger.getAsJsonObject("info");
|
||||
if (info != null) {
|
||||
info.addProperty("description", description);
|
||||
}
|
||||
|
||||
JsonObject swagger = JsonParser.parseString(responseApiSwagger.getResponse()).getAsJsonObject();
|
||||
// Build servers array
|
||||
JsonArray servers = new JsonArray();
|
||||
@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 info = swagger.getAsJsonObject("info");
|
||||
info.addProperty("description", ApiDescSwagger);
|
||||
|
||||
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",
|
||||
"Gateway: " + (String) environment.get("environmentName"));
|
||||
serversJsonArray.add(servers);
|
||||
}
|
||||
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.add("servers", serversJsonArray);
|
||||
swagger.remove("servers");
|
||||
swagger.add("servers", servers);
|
||||
}
|
||||
|
||||
swagger.getAsJsonObject("info").addProperty("description", ApiDescSwagger);
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Trying to call ApiCurio...");
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
VersionMetaData checkIfVersionExist = null;
|
||||
VersionSearchResults checkIfApiExist = null;
|
||||
private void addTagsToProps(Map<String, String> props, List<String> tags) {
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
props.put("tags", String.join(", ", tags));
|
||||
}
|
||||
}
|
||||
|
||||
String mainArtifactId = api.getName().concat(api.getContext());
|
||||
private List<ArtifactReference> createReferencesFromZip(List<ZipEntryData> zipEntries, String group, APIInfo api)
|
||||
throws IOException {
|
||||
|
||||
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 ...");
|
||||
}
|
||||
List<ArtifactReference> references = new ArrayList<>();
|
||||
for (ZipEntryData entry : zipEntries) {
|
||||
String artifactId = api.getName() + "/" + api.getVersion() + "/" + entry.getName();
|
||||
|
||||
if (checkIfApiExist == 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.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(),
|
||||
ApiDescNew, 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);
|
||||
}
|
||||
|
||||
log.info("ApiCurio called successfully...");
|
||||
|
||||
} catch (VersionAlreadyExistsException e) {
|
||||
log.error(e);
|
||||
// Create the artifact (versioned)
|
||||
try (ByteArrayInputStream is = new ByteArrayInputStream(entry.getContent())) {
|
||||
client.createArtifactWithVersion(entry.getType().toString(), artifactId, api.getVersion(), is);
|
||||
}
|
||||
|
||||
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);
|
||||
ArtifactReference ref = new ArtifactReference();
|
||||
ref.setName(entry.getName());
|
||||
ref.setGroupId(entry.getType().toString());
|
||||
ref.setArtifactId(artifactId);
|
||||
ref.setVersion(api.getVersion());
|
||||
references.add(ref);
|
||||
}
|
||||
return references;
|
||||
}
|
||||
|
||||
private void addTagsToMap(Map<String, String> map, List<String> list) {
|
||||
if (list != null && !list.isEmpty()) {
|
||||
map.put("Tags ", " " + list.toString().replace("[", "").replace("]", ""));
|
||||
}
|
||||
private void setMetaAndRules(ArtifactMetaData meta, Map<String, String> props, List<String> tags) {
|
||||
EditableMetaData metaData = new EditableMetaData();
|
||||
metaData.setName(meta.getName());
|
||||
metaData.setDescription(meta.getDescription());
|
||||
metaData.setProperties(props);
|
||||
metaData.setLabels(tags);
|
||||
|
||||
client.updateArtifactMetaData(meta.getGroupId(), meta.getId(), metaData);
|
||||
}
|
||||
|
||||
private void updateRules(ArtifactMetaData mtd, String config, String ruletype) {
|
||||
private void createRule(ArtifactMetaData meta, String config, RuleType type) {
|
||||
Rule rule = new Rule();
|
||||
rule.setConfig(config);
|
||||
rule.setType(RuleType.fromValue(ruletype));
|
||||
client.createArtifactRule(mtd.getGroupId(), mtd.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rule.setType(type);
|
||||
client.createArtifactRule(meta.getGroupId(), meta.getId(), rule);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user