use of org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method getFileFromPrefix.
/**
* Return the file with given prefix
*
* @param apiDirectoryPath Path to find the file
* @param prefix
* @return File with given prefix
* @throws APIMgtEntityImportExportException if file not found or more than one file is present with given prefix
*/
private File getFileFromPrefix(String apiDirectoryPath, String prefix) throws APIMgtEntityImportExportException {
File dir = new File(apiDirectoryPath);
File[] files = dir.listFiles((d, name) -> name.startsWith(prefix));
if (files == null) {
String errorMsg = "Unable find file with prefix: " + prefix + " at path: " + apiDirectoryPath;
log.error(errorMsg);
throw new APIMgtEntityImportExportException(errorMsg);
}
if (files.length != 1) {
String errorMsg = "More than one file with prefix: " + prefix + " found at path: " + apiDirectoryPath;
log.error(errorMsg);
throw new APIMgtEntityImportExportException(errorMsg);
}
return files[0];
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method decodeApiInformationFromDirectoryStructure.
/**
* Reads and decodes APIs and relevant information from the given set of paths
*
* @param apiArtifactsBasePath path to the directory with API related artifacts
* @param newApiProvider API newApiProvider to be updated
* @return Set of {@link APIDetails} objects
* @throws APIMgtEntityImportExportException if any error occurs while decoding the APIs
*/
public Set<APIDetails> decodeApiInformationFromDirectoryStructure(String apiArtifactsBasePath, String newApiProvider) throws APIMgtEntityImportExportException {
Set<String> apiDefinitionsRootDirectoryPaths = null;
try {
apiDefinitionsRootDirectoryPaths = APIFileUtils.getDirectoryList(apiArtifactsBasePath);
} catch (APIMgtDAOException e) {
String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
if (apiDefinitionsRootDirectoryPaths.isEmpty()) {
try {
APIFileUtils.deleteDirectory(path);
} catch (APIMgtDAOException e) {
log.warn("Unable to remove directory " + path);
}
String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
Set<APIDetails> apiDetailsSet = new HashSet<>();
for (String apiDefinitionDirectoryPath : apiDefinitionsRootDirectoryPaths) {
File apiDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX);
File swaggerDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.SWAGGER_DEFINITION_FILE_PREFIX);
API api;
String swaggerDefinition, gatewayConfiguration;
Set<Endpoint> endpoints;
try {
api = getApiDefinitionFromExtractedArchive(apiDefinitionFile.getPath());
swaggerDefinition = getSwaggerDefinitionFromExtractedArchive(swaggerDefinitionFile.getPath());
gatewayConfiguration = getGatewayConfigurationFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.GATEWAY_CONFIGURATION_DEFINITION_FILE);
endpoints = getEndpointsFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + ENDPOINTS_ROOT_DIRECTORY, api.getName(), api.getVersion());
} catch (APIManagementException e) {
log.error("Error occurred while importing api from path: " + apiDefinitionDirectoryPath, e);
// skip this API
continue;
}
if (newApiProvider != null && !newApiProvider.isEmpty()) {
// update the newApiProvider
api = new API.APIBuilder(api).provider(newApiProvider).build();
}
String documentsRootDirectory = apiDefinitionDirectoryPath + File.separator + DOCUMENTS_ROOT_DIRECTORY;
Set<DocumentInfo> documentInfoSet = getDocumentInfoFromExtractedArchive(documentsRootDirectory, api.getName(), api.getVersion());
Set<DocumentContent> documentContents = new HashSet<>();
for (DocumentInfo aDocumentInfo : documentInfoSet) {
DocumentContent aDocumentContent = getDocumentContentFromExtractedArchive(aDocumentInfo, documentsRootDirectory + File.separator + aDocumentInfo.getId());
if (aDocumentContent != null) {
documentContents.add(aDocumentContent);
}
}
InputStream thumbnailStream = null;
try {
thumbnailStream = APIFileUtils.getThumbnailImage(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME);
} catch (APIMgtDAOException e) {
// log and ignore
log.error("Error occurred while reading thumbnail image.", e);
}
APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
apiDetails.setGatewayConfiguration(gatewayConfiguration);
apiDetails.setEndpoints(endpoints);
if (!documentInfoSet.isEmpty()) {
apiDetails.addDocumentInformation(documentInfoSet);
}
if (!documentContents.isEmpty()) {
apiDetails.addDocumentContents(documentContents);
}
if (thumbnailStream != null) {
apiDetails.setThumbnailStream(thumbnailStream);
}
apiDetailsSet.add(apiDetails);
}
return apiDetailsSet;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method getEndpointsFromExtractedArchive.
private Set<Endpoint> getEndpointsFromExtractedArchive(String endpointLocation, String apiName, String version) throws APIMgtEntityImportExportException {
File endpointsRootDirectory = new File(endpointLocation);
Set<Endpoint> endpoints = new HashSet<>();
if (endpointsRootDirectory.isDirectory()) {
File[] endpointFiles = endpointsRootDirectory.listFiles(File::isFile);
if (endpointFiles == null) {
// no endpoints in the given location, can't continue
String errorMsg = "No endpoints found at " + endpointsRootDirectory;
log.error(errorMsg);
throw new APIMgtEntityImportExportException(errorMsg);
}
Gson gson = new GsonBuilder().create();
for (File endpointFile : endpointFiles) {
// read everything
String content = null;
try {
content = APIFileUtils.readFileContentAsText(endpointFile.getPath());
} catch (APIMgtDAOException e) {
String errorMsg = "Unable to read endpoints from " + endpointFile.getPath();
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e);
}
endpoints.add(gson.fromJson(content, Endpoint.class));
}
}
return endpoints;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method importAndCreateAPIs.
/**
* Imports and creates a set of new APIs to API Manager by reading and decoding the
* input stream. Will fail if the APIs already exists
*
* @param uploadedApiArchiveInputStream InputStream to be read ana decoded to a set of APIs
* @param provider API provider, if needs to be updated
* @return {@link APIListDTO} object comprising of successfully imported APIs
* @throws APIMgtEntityImportExportException if any error occurs while importing or no APIs are imported successfully
*/
public APIListDTO importAndCreateAPIs(InputStream uploadedApiArchiveInputStream, String provider) throws APIMgtEntityImportExportException {
String apiArchiveLocation = path + File.separator + IMPORTED_APIS_DIRECTORY_NAME + ".zip";
String archiveExtractLocation = null;
try {
archiveExtractLocation = APIFileUtils.extractUploadedArchive(uploadedApiArchiveInputStream, IMPORTED_APIS_DIRECTORY_NAME, apiArchiveLocation, path);
} catch (APIMgtDAOException e) {
String errorMsg = "Error in accessing uploaded API archive" + apiArchiveLocation;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.API_IMPORT_ERROR);
}
// List to contain newly created/updated APIs
Set<APIDetails> apiDetailsSet = decodeApiInformationFromDirectoryStructure(archiveExtractLocation, provider);
List<API> apis = new ArrayList<>();
for (APIDetails apiDetails : apiDetailsSet) {
try {
apis.add(importAndCreateApi(apiDetails));
} catch (APIManagementException e) {
log.error("Error while importing API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
// skip importing the API
continue;
}
log.info("Successfully imported API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
}
try {
APIFileUtils.deleteDirectory(path);
} catch (APIMgtDAOException e) {
log.warn("Unable to remove directory " + path);
}
// if no APIs are corrected exported, throw an error
if (apis.isEmpty()) {
String errorMsg = "No APIs imported successfully";
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
return MappingUtil.toAPIListDTO(apis);
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException in project carbon-apimgt by wso2.
the class FileBasedApplicationImportExportManager method importApplication.
/**
* Import a given Application from an InputStream
*
* @param uploadedAppArchiveInputStream Content stream of the zip file which contains exported Application
* @return the imported application
* @throws APIMgtEntityImportExportException
*/
public Application importApplication(InputStream uploadedAppArchiveInputStream) throws APIMgtEntityImportExportException {
String appArchiveLocation = path + File.separator + IMPORTED_APPLICATIONS_DIRECTORY_NAME + ".zip";
String archiveExtractLocation = null;
try {
archiveExtractLocation = extractUploadedArchiveApplication(uploadedAppArchiveInputStream, IMPORTED_APPLICATIONS_DIRECTORY_NAME, appArchiveLocation, path);
} catch (APIManagementException e) {
String errorMsg = "Error in accessing uploaded Application archive" + appArchiveLocation;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_IMPORT_ERROR);
}
Application applicationDetails = parseApplicationFile(archiveExtractLocation);
return applicationDetails;
}
Aggregations