use of org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive in project carbon-apimgt by wso2.
the class ServicesApiServiceImpl method exportService.
@Override
public Response exportService(String name, String version, MessageContext messageContext) {
File exportedServiceArchiveFile = null;
// creates a directory in default temporary-file directory
String pathToExportDir = FileBasedServicesImportExportManager.createDir(RestApiConstants.JAVA_IO_TMPDIR);
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
String archiveName = name + APIConstants.KEY_SEPARATOR + version;
ServiceEntry serviceEntry;
String exportedFileName = null;
ExportArchive exportArchive;
if (StringUtils.isBlank(name) || StringUtils.isBlank(version)) {
RestApiUtil.handleBadRequest("Service name or owner should not be empty or null.", log);
}
try {
serviceEntry = serviceCatalog.getServiceByNameAndVersion(name, version, tenantId);
if (serviceEntry != null) {
FileBasedServicesImportExportManager importExportManager = new FileBasedServicesImportExportManager(pathToExportDir);
exportArchive = importExportManager.createArchiveFromExportedServices(ServiceEntryMappingUtil.generateServiceFiles(serviceEntry), pathToExportDir, archiveName);
exportedServiceArchiveFile = new File(exportArchive.getArchiveName());
exportedFileName = exportedServiceArchiveFile.getName();
Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedServiceArchiveFile).type(MediaType.APPLICATION_OCTET_STREAM);
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + exportedFileName + "\"");
return responseBuilder.build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while exporting Services: " + archiveName, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive in project carbon-apimgt by wso2.
the class FileBasedServicesImportExportManager method createArchiveFromExportedServices.
/**
* Creates an archive of the contained service details.
*
* @param sourceDirectory Directory which contains source file
* @param archiveLocation Directory to generate the zip archive
* @param archiveName Name of the zip archive
* @return path to the created archive file
* @throws APIManagementException if an error occurs while creating an archive from app details
*/
public ExportArchive createArchiveFromExportedServices(String sourceDirectory, String archiveLocation, String archiveName) throws APIManagementException {
String archivedFilePath;
ExportArchive exportArchive = new ExportArchive();
try {
archiveDirectory(sourceDirectory, archiveLocation, archiveName);
} catch (IOException e) {
// cleanup the archive root directory
try {
FileUtils.deleteDirectory(new File(path));
} catch (IOException e1) {
log.warn("Unable to remove directory " + path);
}
String errorMsg = "Error while archiving directory " + sourceDirectory;
throw new APIManagementException(errorMsg);
}
archivedFilePath = archiveLocation + File.separator + archiveName + APIConstants.ZIP_FILE_EXTENSION;
exportArchive.setArchiveName(archivedFilePath);
return exportArchive;
}
Aggregations