Search in sources :

Example 1 with ExportArchive

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;
}
Also used : Response(javax.ws.rs.core.Response) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) ExportArchive(org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FileBasedServicesImportExportManager(org.wso2.carbon.apimgt.rest.api.service.catalog.utils.FileBasedServicesImportExportManager) File(java.io.File) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 2 with ExportArchive

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;
}
Also used : ExportArchive(org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ZipFile(java.util.zip.ZipFile)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 ExportArchive (org.wso2.carbon.apimgt.rest.api.service.catalog.model.ExportArchive)2 File (java.io.File)1 ZipFile (java.util.zip.ZipFile)1 Response (javax.ws.rs.core.Response)1 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)1 ServiceEntry (org.wso2.carbon.apimgt.api.model.ServiceEntry)1 FileBasedServicesImportExportManager (org.wso2.carbon.apimgt.rest.api.service.catalog.utils.FileBasedServicesImportExportManager)1