Java Code Examples for org.wso2.carbon.apimgt.impl.APIConstants#ZIP_FILE_EXTENSION

The following examples show how to use org.wso2.carbon.apimgt.impl.APIConstants#ZIP_FILE_EXTENSION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Import a given Application from an InputStream
 *
 * @param uploadedAppArchiveInputStream Content stream of the zip file which contains exported Application
 * @return the imported application
 * @throws APIManagementException if an error occurs while importing an application archive
 */
public Application importApplication(InputStream uploadedAppArchiveInputStream) throws APIManagementException {
    String appArchiveLocation = path + File.separator + IMPORTED_APPLICATIONS_DIRECTORY_NAME +
            APIConstants.ZIP_FILE_EXTENSION;
    String archiveExtractLocation;
    try {
        archiveExtractLocation = extractUploadedArchiveApplication(uploadedAppArchiveInputStream,
                IMPORTED_APPLICATIONS_DIRECTORY_NAME,
                appArchiveLocation, path);
        return parseApplicationFile(archiveExtractLocation);
    } catch (IOException e) {
        String errorMsg = "Error occurred while importing Application archive" + appArchiveLocation;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e);
    }
}
 
Example 2
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an archive of the contained application 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 String createArchiveFromExportedAppArtifacts(String sourceDirectory, String archiveLocation,
                                                    String archiveName) throws APIManagementException {
    String archivedFilePath;
    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;
    return archivedFilePath;
}
 
Example 3
Source File: APIImportExportManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to export the given API as an archive (zip file).
 *
 * @param apiTypeWrapper    Requested API or API Product to export
 * @param isStatusPreserved Is API or API Product  status preserved or not
 * @param exportFormat      Export file format of the API or the API Product
 * @return Archive file for the requested API or API Product
 * @throws APIImportExportException If an error occurs while exporting the API or the API Product and creating the archive
 */
public File exportAPIOrAPIProductArchive(ApiTypeWrapper apiTypeWrapper, boolean isStatusPreserved, ExportFormat exportFormat)
        throws APIImportExportException {
    String archiveBasePath;
    if (!apiTypeWrapper.isAPIProduct()) {
        APIIdentifier apiIdentifier = apiTypeWrapper.getApi().getId();
        archiveBasePath = exportAPIArtifacts(apiTypeWrapper.getApi(), isStatusPreserved, exportFormat);
        if (log.isDebugEnabled()) {
            log.debug("API" + apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion() + " exported successfully");
        }
    } else {
        APIProductIdentifier apiProductIdentifier = apiTypeWrapper.getApiProduct().getId();
        archiveBasePath = exportAPIProductArtifacts(apiTypeWrapper.getApiProduct(), isStatusPreserved, exportFormat);
        if (log.isDebugEnabled()) {
            log.info("API Product" + apiProductIdentifier.getName() + "-" + apiProductIdentifier.getVersion() + " exported successfully");
        }
    }
    CommonUtil.archiveDirectory(archiveBasePath);
    FileUtils.deleteQuietly(new File(archiveBasePath));
    return new File(archiveBasePath + APIConstants.ZIP_FILE_EXTENSION);
}
 
Example 4
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Import a given Application from an InputStream
 *
 * @param uploadedAppArchiveInputStream Content stream of the zip file which contains exported Application
 * @return the imported application
 * @throws APIManagementException if an error occurs while importing an application archive
 */
public Application importApplication(InputStream uploadedAppArchiveInputStream) throws APIManagementException {
    String appArchiveLocation = path + File.separator + IMPORTED_APPLICATIONS_DIRECTORY_NAME +
            APIConstants.ZIP_FILE_EXTENSION;
    String archiveExtractLocation;
    try {
        archiveExtractLocation = extractUploadedArchiveApplication(uploadedAppArchiveInputStream,
                IMPORTED_APPLICATIONS_DIRECTORY_NAME,
                appArchiveLocation, path);
        return parseApplicationFile(archiveExtractLocation);
    } catch (IOException e) {
        String errorMsg = "Error occurred while importing Application archive" + appArchiveLocation;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e);
    }
}
 
Example 5
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an archive of the contained application 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 String createArchiveFromExportedAppArtifacts(String sourceDirectory, String archiveLocation,
                                                    String archiveName) throws APIManagementException {
    String archivedFilePath;
    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;
    return archivedFilePath;
}
 
Example 6
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * @param directoryToZip  directory to create zip archive
 * @param fileList        list of files
 * @param archiveLocation path to the archive location, excluding archive name
 * @param archiveName     name of the archive to create
 * @throws IOException if an error occurs while writing to the archive file
 */
private void writeArchiveFile(File directoryToZip, List<File> fileList, String archiveLocation,
                              String archiveName) throws IOException {
    try (FileOutputStream fileOutputStream = new FileOutputStream(archiveLocation + File.separator +
            archiveName
            + APIConstants.ZIP_FILE_EXTENSION);
         ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
        for (File file : fileList) {
            if (!file.isDirectory()) {
                addToArchive(directoryToZip, file, zipOutputStream);
            }
        }
    }
}
 
Example 7
Source File: RestApiUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JAXRS Response object based on the provided ResourceFile
 *
 * @param fileNameWithoutExtension Filename without the extension. The extension is determined from the method
 * @param resourceFile ResourceFile object
 * @return JAXRS Response object
 */
public static Response getResponseFromResourceFile(String fileNameWithoutExtension, ResourceFile resourceFile) {
    String contentType;
    String extension;
    if (resourceFile.getContentType().contains(APIConstants.APPLICATION_ZIP)) {
        contentType = APIConstants.APPLICATION_ZIP;
        extension = APIConstants.ZIP_FILE_EXTENSION;
    } else {
        contentType = APIConstants.APPLICATION_WSDL_MEDIA_TYPE;
        extension = APIConstants.WSDL_FILE_EXTENSION;
    }
    String filename = fileNameWithoutExtension + extension;
    return Response.ok(resourceFile.getContent(), contentType).header("Content-Disposition",
            "attachment; filename=\"" + filename + "\"" ).build();
}
 
Example 8
Source File: FileBasedApplicationImportExportManager.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * @param directoryToZip  directory to create zip archive
 * @param fileList        list of files
 * @param archiveLocation path to the archive location, excluding archive name
 * @param archiveName     name of the archive to create
 * @throws IOException if an error occurs while writing to the archive file
 */
private void writeArchiveFile(File directoryToZip, List<File> fileList, String archiveLocation,
                              String archiveName) throws IOException {
    try (FileOutputStream fileOutputStream = new FileOutputStream(archiveLocation + File.separator +
            archiveName
            + APIConstants.ZIP_FILE_EXTENSION);
         ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
        for (File file : fileList) {
            if (!file.isDirectory()) {
                addToArchive(directoryToZip, file, zipOutputStream);
            }
        }
    }
}