use of org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager in project carbon-apimgt by wso2.
the class FileBasedApplicationImportExportManager method exportApplication.
/**
* Export a given Application to a file system as zip archive.
* The export root location is given by {@link FileBasedApplicationImportExportManager#path}/exported-application.
*
* @param application Application{@link Application} to be exported
* @param exportDirectoryName Name of directory to be exported
* @return path to the exported directory with exported artifacts
* @throws APIMgtEntityImportExportException
*/
public String exportApplication(Application application, String exportDirectoryName) throws APIMgtEntityImportExportException {
String applicationArtifactBaseDirectoryPath = path + File.separator + exportDirectoryName;
try {
Files.createDirectories(Paths.get(applicationArtifactBaseDirectoryPath));
} catch (IOException e) {
String errorMsg = "Unable to create the directory for export Application at :" + applicationArtifactBaseDirectoryPath;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e);
}
Application exportApplication = application;
String applicationExportDirectory = applicationArtifactBaseDirectoryPath + File.separator + exportApplication.getName();
try {
// create directory per application
Files.createDirectories(Paths.get(applicationExportDirectory));
// export application details
exportApplicationDetailsToFileSystem(exportApplication, applicationExportDirectory);
} catch (IOException e) {
log.error("Error while exporting Application: " + exportApplication.getName(), e);
}
// Check if no application is exported
try {
if (APIFileUtils.getDirectoryList(applicationArtifactBaseDirectoryPath).isEmpty()) {
// cleanup the archive root directory
APIFileUtils.deleteDirectory(path);
}
} catch (APIManagementException e) {
String errorMsg = "Unable to find Application Details at: " + applicationArtifactBaseDirectoryPath;
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.APPLICATION_EXPORT_ERROR);
}
return applicationArtifactBaseDirectoryPath;
}
use of org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager in project carbon-apimgt by wso2.
the class ExportApiServiceImpl method exportApplicationsGet.
/**
* Export an existing Application
*
* @param appId Search query
* @param request msf4j request object
* @return Zip file containing exported Applications
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response exportApplicationsGet(String appId, Request request) throws NotFoundException {
APIStore consumer = null;
String exportedFilePath, zippedFilePath = null;
Application applicationDetails;
String exportedAppDirName = "exported-application";
String pathToExportDir = System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + // creates a directory in default temporary-file directory
UUID.randomUUID().toString();
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, pathToExportDir);
applicationDetails = importExportManager.getApplicationDetails(appId, username);
if (applicationDetails == null) {
// 404
String errorMsg = "No application found for query " + appId;
log.error(errorMsg);
HashMap<String, String> paramList = new HashMap<>();
paramList.put("query", appId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.APPLICATION_NOT_FOUND, paramList);
return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
}
exportedFilePath = importExportManager.exportApplication(applicationDetails, exportedAppDirName);
zippedFilePath = importExportManager.createArchiveFromExportedAppArtifacts(exportedFilePath, pathToExportDir, exportedAppDirName);
} catch (APIManagementException e) {
String errorMessage = "Error while exporting Application";
log.error(errorMessage, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
File exportedApplicationArchiveFile = new File(zippedFilePath);
Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedApplicationArchiveFile);
responseBuilder.header("Content-Disposition", "attachment; filename=\"" + exportedApplicationArchiveFile.getName() + "\"");
Response response = responseBuilder.build();
return response;
}
use of org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPut.
@Override
public Response importApplicationsPut(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(RestApiUtil.getLoggedInUsername(request));
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
Application updatedApplication = importExportManager.updateApplication(applicationDetails, username);
return Response.status(Response.Status.OK).entity(updatedApplication).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPost.
/**
* Import an Application which has been exported to a zip file
*
* @param fileInputStream content stream of the zip file which contains exported Application
* @param fileDetail meta information of the zip file
* @param request msf4j request object
* @return Application that was imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
return Response.status(Response.Status.OK).entity(response).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager in project carbon-apimgt by wso2.
the class ExportApiServiceImplTestCase method testExportApplicationsGet.
@Test
public void testExportApplicationsGet() throws Exception {
printTestMethodName();
ExportApiServiceImpl exportApiService = new ExportApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
Application testApp = new Application("testApp", USER);
testApp.setName("testApp");
testApp.setId("testId");
testApp.setDescription("testDesc");
testApp.setPolicy(new Policy("50PerMin"));
testApp.setCreatedUser("admin");
testApp.setUpdatedUser("admin");
testApp.setStatus("APPROVED");
testApp.setCreatedTime(LocalDateTime.now());
testApp.setUpdatedTime(LocalDateTime.now());
testApp.setApplicationKeys(null);
testApp.setPermissionString("7");
testApp.setPermissionMap(null);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
Mockito.when(apiStore.getApplication("test", USER)).thenReturn(testApp);
FileBasedApplicationImportExportManager importExportManager = Mockito.mock(FileBasedApplicationImportExportManager.class);
Mockito.when(importExportManager.exportApplication(testApp, "testDir")).thenReturn("testPath");
Response response = exportApiService.exportApplicationsGet("test", request);
Assert.assertEquals(response.getStatus(), 200);
}
Aggregations