refactor
This commit is contained in:
parent
b614988674
commit
bde6a00a71
@ -1,8 +1,5 @@
|
||||
package cz.trask.apioperator.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -10,8 +7,6 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -19,13 +14,12 @@ import org.apache.logging.log4j.Logger;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import cz.trask.apioperator.AbstractProcess;
|
||||
import cz.trask.apioperator.model.FileType;
|
||||
import cz.trask.apioperator.model.HttpResponse;
|
||||
import cz.trask.apioperator.model.RegisterResponse;
|
||||
import cz.trask.apioperator.model.TokenResponse;
|
||||
import cz.trask.apioperator.utils.ZipUtils;
|
||||
import io.apicurio.registry.rest.client.RegistryClient;
|
||||
import io.apicurio.registry.rest.client.RegistryClientFactory;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactReference;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactSearchResults;
|
||||
import io.apicurio.registry.rest.v2.beans.SearchedArtifact;
|
||||
@ -110,7 +104,7 @@ public class ExportToWso2 extends AbstractProcess {
|
||||
api.getId(), ver.getVersion());
|
||||
if (ref != null && !ref.isEmpty()) {
|
||||
log.info("Artifact has {} references", ref.size());
|
||||
byte[] data = prepareApiZipFile(ver, ref);
|
||||
byte[] data = ZipUtils.prepareApiZipFile(client, ver, ref);
|
||||
String fileName = api.getName() + "-" + ver.getVersion() + ".zip";
|
||||
if (data != null && data.length > 0 && fileName != null && !fileName.isEmpty()) {
|
||||
int responseCode = publishApiToWso2(fileName, data, tokenResponse);
|
||||
@ -158,41 +152,4 @@ public class ExportToWso2 extends AbstractProcess {
|
||||
}
|
||||
return responseCode;
|
||||
}
|
||||
|
||||
private byte[] prepareApiZipFile(SearchedVersion ver, List<ArtifactReference> ref) throws IOException {
|
||||
|
||||
String baseDir = ver.getName() + "-" + ver.getVersion() + "/";
|
||||
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
|
||||
for (ArtifactReference r : ref) {
|
||||
log.info(" - Reference: {} {} {}", r.getGroupId(), r.getArtifactId(), r.getVersion());
|
||||
|
||||
ArtifactMetaData amd = client.getArtifactMetaData(r.getGroupId(), r.getArtifactId());
|
||||
|
||||
String subDir = "";
|
||||
|
||||
if (FileType.OPENAPI.toString().equals(amd.getGroupId())) {
|
||||
subDir = "Definitions/";
|
||||
} else if (FileType.POLICY.toString().equals(amd.getGroupId())) {
|
||||
subDir = "Policies/";
|
||||
}
|
||||
|
||||
String fileName = baseDir + subDir + r.getName();
|
||||
log.info(" - Adding file: {}", fileName);
|
||||
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
|
||||
try (InputStream is = client.getContentByGlobalId(amd.getGlobalId())) {
|
||||
zos.write(is.readAllBytes());
|
||||
}
|
||||
|
||||
zos.closeEntry();
|
||||
}
|
||||
zos.finish();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ import cz.trask.apioperator.model.HttpResponse;
|
||||
import cz.trask.apioperator.model.RegisterResponse;
|
||||
import cz.trask.apioperator.model.TokenResponse;
|
||||
import cz.trask.apioperator.model.ZipEntryData;
|
||||
import cz.trask.apioperator.utils.ZipExtractor;
|
||||
import cz.trask.apioperator.utils.ZipUtils;
|
||||
import io.apicurio.registry.rest.client.RegistryClient;
|
||||
import io.apicurio.registry.rest.client.RegistryClientFactory;
|
||||
import io.apicurio.registry.rest.client.exception.VersionAlreadyExistsException;
|
||||
@ -137,7 +137,7 @@ public class ImportToApicurio extends AbstractProcess {
|
||||
config.getSourcePublisherApiUrl() + "/apis/export?apiId=" + api.getId(), httpHeaders,
|
||||
Collections.emptyMap(), true);
|
||||
|
||||
List<ZipEntryData> zipEntries = ZipExtractor.extractFilesFromZip(exportedZip.getResponseBytes());
|
||||
List<ZipEntryData> zipEntries = ZipUtils.extractFilesFromZip(exportedZip.getResponseBytes());
|
||||
|
||||
String swagger = null;
|
||||
|
||||
|
||||
@ -3,15 +3,26 @@ package cz.trask.apioperator.utils;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import cz.trask.apioperator.model.FileType;
|
||||
import cz.trask.apioperator.model.ZipEntryData;
|
||||
import io.apicurio.registry.rest.client.RegistryClient;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
|
||||
import io.apicurio.registry.rest.v2.beans.ArtifactReference;
|
||||
import io.apicurio.registry.rest.v2.beans.SearchedVersion;
|
||||
|
||||
public class ZipExtractor {
|
||||
public class ZipUtils {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(ZipUtils.class);
|
||||
|
||||
public static List<ZipEntryData> extractFilesFromZip(byte[] data) throws IOException {
|
||||
List<ZipEntryData> fileList = new ArrayList<>();
|
||||
@ -49,10 +60,6 @@ public class ZipExtractor {
|
||||
return FileType.UNKNOWN;
|
||||
}
|
||||
|
||||
private static boolean isWsdLFile(String fileName) {
|
||||
return fileName.toLowerCase().endsWith(".wsdl");
|
||||
}
|
||||
|
||||
private static byte[] readAllBytes(ZipInputStream zis) throws IOException {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
@ -63,4 +70,41 @@ public class ZipExtractor {
|
||||
buffer.flush();
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] prepareApiZipFile(RegistryClient client, SearchedVersion ver, List<ArtifactReference> ref)
|
||||
throws IOException {
|
||||
|
||||
String baseDir = ver.getName() + "-" + ver.getVersion() + "/";
|
||||
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
|
||||
for (ArtifactReference r : ref) {
|
||||
log.info(" - Reference: {} {} {}", r.getGroupId(), r.getArtifactId(), r.getVersion());
|
||||
|
||||
ArtifactMetaData amd = client.getArtifactMetaData(r.getGroupId(), r.getArtifactId());
|
||||
|
||||
String subDir = "";
|
||||
|
||||
if (FileType.OPENAPI.toString().equals(amd.getGroupId())) {
|
||||
subDir = "Definitions/";
|
||||
} else if (FileType.POLICY.toString().equals(amd.getGroupId())) {
|
||||
subDir = "Policies/";
|
||||
}
|
||||
|
||||
String fileName = baseDir + subDir + r.getName();
|
||||
log.info(" - Adding file: {}", fileName);
|
||||
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
|
||||
try (InputStream is = client.getContentByGlobalId(amd.getGlobalId())) {
|
||||
zos.write(is.readAllBytes());
|
||||
}
|
||||
|
||||
zos.closeEntry();
|
||||
}
|
||||
zos.finish();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user