|
| 1 | +package com.axway.apim.api.export.impl; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.apache.commons.io.FileUtils; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import com.axway.apim.adapter.APIManagerAdapter; |
| 13 | +import com.axway.apim.adapter.apis.APIFilter; |
| 14 | +import com.axway.apim.adapter.apis.APIFilter.Builder; |
| 15 | +import com.axway.apim.adapter.apis.OrgFilter; |
| 16 | +import com.axway.apim.api.API; |
| 17 | +import com.axway.apim.api.export.lib.params.APIExportParams; |
| 18 | +import com.axway.apim.lib.errorHandling.AppException; |
| 19 | +import com.axway.apim.lib.errorHandling.ErrorCode; |
| 20 | + |
| 21 | +public class DATAPIExporter extends APIResultHandler { |
| 22 | + private static Logger LOG = LoggerFactory.getLogger(DATAPIExporter.class); |
| 23 | + |
| 24 | + /** Where to store the exported API-Definition */ |
| 25 | + private String givenExportFolder = null; |
| 26 | + |
| 27 | + private String datPassword = null; |
| 28 | + |
| 29 | + APIManagerAdapter apiManager = APIManagerAdapter.getInstance(); |
| 30 | + |
| 31 | + public DATAPIExporter(APIExportParams params) throws AppException { |
| 32 | + super(params); |
| 33 | + this.givenExportFolder = params.getTarget(); |
| 34 | + this.datPassword = params.getDatPassword(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void execute(List<API> apis) throws AppException { |
| 39 | + for (API api : apis) { |
| 40 | + try { |
| 41 | + saveAPILocally(api); |
| 42 | + } catch (AppException e) { |
| 43 | + LOG.error("Can't export API: " + e.getMessage() + " as DAT-File. Please check in API-Manager UI the API is valid.", e); |
| 44 | + } |
| 45 | + } |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public APIFilter getFilter() { |
| 51 | + Builder builder = getBaseAPIFilterBuilder(); |
| 52 | + return builder.build(); |
| 53 | + } |
| 54 | + |
| 55 | + private void saveAPILocally(API api) throws AppException { |
| 56 | + String apiPath = getAPIExportFolder(api.getPath()); |
| 57 | + File localFolder = new File(this.givenExportFolder +File.separator+ getVHost(api) + apiPath); |
| 58 | + LOG.info("Going to export API into folder: " + localFolder); |
| 59 | + if(localFolder.exists()) { |
| 60 | + if(params.isDeleteTarget()) { |
| 61 | + LOG.debug("Existing local export folder: " + localFolder + " already exists and will be deleted."); |
| 62 | + try { |
| 63 | + FileUtils.deleteDirectory(localFolder); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new AppException("Error deleting local folder", ErrorCode.UNXPECTED_ERROR, e); |
| 66 | + } |
| 67 | + } else { |
| 68 | + LOG.warn("Local export folder: " + localFolder + " already exists. API will not be exported. (You may set -deleteTarget)"); |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + if (!localFolder.mkdirs()) { |
| 73 | + throw new AppException("Cant create export folder: " + localFolder, ErrorCode.UNXPECTED_ERROR); |
| 74 | + } |
| 75 | + byte[] datFileContent = apiManager.apiAdapter.getAPIDatFile(api, datPassword); |
| 76 | + |
| 77 | + String targetFile = null; |
| 78 | + try { |
| 79 | + targetFile = localFolder.getCanonicalPath() + "/" + api.getName() + ".dat"; |
| 80 | + writeBytesToFile(datFileContent, targetFile); |
| 81 | + } catch (IOException e) { |
| 82 | + throw new AppException("Can't save API-DAT file locally: " + targetFile, |
| 83 | + ErrorCode.UNXPECTED_ERROR, e); |
| 84 | + } |
| 85 | + LOG.info("Successfully exported API as DAT-File into folder: " + localFolder.getAbsolutePath()); |
| 86 | + } |
| 87 | + |
| 88 | + private String getVHost(API api) throws AppException { |
| 89 | + if(api.getVhost()!=null) return api.getVhost() + File.separator; |
| 90 | + String orgVHost = APIManagerAdapter.getInstance().orgAdapter.getOrg(new OrgFilter.Builder().hasId(api.getOrganizationId()).build()).getVirtualHost(); |
| 91 | + if(orgVHost!=null) return orgVHost+File.separator; |
| 92 | + return ""; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + private static void writeBytesToFile(byte[] bFile, String fileDest) throws AppException { |
| 97 | + |
| 98 | + try (FileOutputStream fileOuputStream = new FileOutputStream(fileDest)) { |
| 99 | + fileOuputStream.write(bFile); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new AppException("Can't write file", ErrorCode.UNXPECTED_ERROR, e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private String getAPIExportFolder(String apiExposurePath) { |
| 106 | + if (apiExposurePath.startsWith("/")) |
| 107 | + apiExposurePath = apiExposurePath.replaceFirst("/", ""); |
| 108 | + if (apiExposurePath.endsWith("/")) |
| 109 | + apiExposurePath = apiExposurePath.substring(0, apiExposurePath.length() - 1); |
| 110 | + apiExposurePath = apiExposurePath.replace("/", "-"); |
| 111 | + return apiExposurePath; |
| 112 | + } |
| 113 | +} |
0 commit comments