2026-03-11 11:07:30 +01:00

87 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cz.trask.migration.impl.v45;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.atomic.AtomicInteger;
import cz.trask.migration.AbstractProcess;
import cz.trask.migration.model.TokenResponse;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class ApiFilesToWso2 extends AbstractProcess {
private final AtomicInteger apiCounter = new AtomicInteger(1);
/**
* Main entry point for the import process.
*
* @throws RuntimeException if any error occurs
*/
public void process() {
try {
log.info("Starting API import to WSO2 from directory...");
TokenResponse token = authenticateToWso2AndGetToken(config.getTarget());
File root = new File(config.getSource().getWso2ApisDir());
File[] apiFiles = root.listFiles((dir, name) -> name.endsWith(".zip"));
if (apiFiles == null || apiFiles.length == 0) {
log.warn("No API zip files found in directory: {}", config.getSource().getWso2ApisDir());
return;
}
log.info("Found {} APIs", apiFiles.length);
for (File api : apiFiles) {
final int index = apiCounter.getAndIncrement();
processApi(api, token, index, apiFiles.length);
}
log.info("Finished processing APIs.");
} catch (Exception e) {
log.error("Error while importing APIs.", e);
throw new RuntimeException("Import failed", e);
}
}
/**
* Process a single API fetches the data, creates or updates the corresponding
* artifact in WSO2.
*/
private void processApi(File apiFile, TokenResponse token, int index, int total) {
long start = System.currentTimeMillis();
try {
log.info("Processing API {} of {}", index, total);
String fileName = apiFile.getName();
byte[] data = null;
try {
data = Files.readAllBytes(apiFile.toPath());
} catch (IOException e) {
log.error("Failed to read API file '{}': {}", apiFile.getName(), e.getMessage(), e);
return;
}
if (data != null && data.length > 0 && fileName != null && !fileName.isEmpty()) {
int responseCode = publishApiToWso2(fileName, data, token);
if (responseCode == 200 || responseCode == 201) {
log.info(" - API version {} imported successfully with response code: {}", fileName, responseCode);
} else {
log.warn(" - API version {} import failed with response code: {}", fileName,
responseCode);
}
}
log.info("Successfully imported API '{}' ({}). Took {} ms", apiFile.getName(),
apiFile.getName(),
System.currentTimeMillis() - start);
} catch (Exception e) {
log.error("Cannot export API '{}': {}", apiFile.getName(), e.getMessage(), e);
}
}
}