Search in sources :

Example 1 with FileBasedApplicationImportExportManager

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 2 with FileBasedApplicationImportExportManager

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;
}
Also used : Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) File(java.io.File) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with FileBasedApplicationImportExportManager

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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with FileBasedApplicationImportExportManager

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();
    }
}
Also used : ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 5 with FileBasedApplicationImportExportManager

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);
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Application (org.wso2.carbon.apimgt.core.models.Application)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 FileBasedApplicationImportExportManager (org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Response (javax.ws.rs.core.Response)2 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)1 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)1 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)1 Request (org.wso2.msf4j.Request)1