Search in sources :

Example 31 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ExportUtils method addOperationPoliciesToArchive.

/**
 * Retrieve the operation policies and store those in the archive directory.
 *
 * @param archivePath  File path to export the endpoint certificates
 * @param apiID        UUID of the API/ API Product
 * @param exportFormat Export format of file
 * @param apiProvider  API Provider
 * @throws APIImportExportException If an error occurs while exporting operation policies
 */
public static void addOperationPoliciesToArchive(String archivePath, String apiID, String tenantDomain, ExportFormat exportFormat, APIProvider apiProvider, API api) throws APIManagementException {
    try {
        CommonUtil.createDirectory(archivePath + File.separator + ImportExportConstants.POLICIES_DIRECTORY);
        Set<URITemplate> uriTemplates = api.getUriTemplates();
        for (URITemplate uriTemplate : uriTemplates) {
            List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
            if (operationPolicies != null && !operationPolicies.isEmpty()) {
                for (OperationPolicy policy : operationPolicies) {
                    OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(), apiID, tenantDomain, true);
                    if (policyData != null) {
                        String policyName = archivePath + File.separator + ImportExportConstants.POLICIES_DIRECTORY + File.separator + policyData.getSpecification().getName();
                        // Policy specification and definition will have the same name
                        if (policyData.getSpecification() != null) {
                            CommonUtil.writeDtoToFile(policyName, exportFormat, ImportExportConstants.TYPE_POLICY_SPECIFICATION, policyData.getSpecification());
                        }
                        if (policyData.getSynapsePolicyDefinition() != null) {
                            CommonUtil.writeFile(policyName + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION, policyData.getSynapsePolicyDefinition().getContent());
                        }
                        if (policyData.getCcPolicyDefinition() != null) {
                            CommonUtil.writeFile(policyName + APIConstants.CC_POLICY_DEFINITION_EXTENSION, policyData.getCcPolicyDefinition().getContent());
                        }
                    }
                }
            }
        }
    } catch (IOException | APIImportExportException e) {
        throw new APIManagementException("Error while adding operation policy details for API: " + apiID, e);
    }
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) IOException(java.io.IOException)

Example 32 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ImportUtils method validateWSDLFromArchive.

/**
 * Validate WSDL definition from the archive directory and return it.
 *
 * @param pathToArchive Path to API archive
 * @throws APIImportExportException If an error due to an invalid WSDL definition
 */
private static void validateWSDLFromArchive(String pathToArchive, APIDTO apiDto) throws APIManagementException {
    try {
        byte[] wsdlDefinition = loadWsdlFile(pathToArchive, apiDto);
        WSDLValidationResponse wsdlValidationResponse = APIMWSDLReader.getWsdlValidationResponse(APIMWSDLReader.getWSDLProcessor(wsdlDefinition));
        if (!wsdlValidationResponse.isValid()) {
            throw new APIManagementException("Error occurred while importing the API. Invalid WSDL definition found. " + wsdlValidationResponse.getError());
        }
    } catch (IOException | APIManagementException e) {
        throw new APIManagementException("Error while reading API meta information from path: " + pathToArchive, e, ExceptionCodes.ERROR_READING_META_DATA);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) IOException(java.io.IOException)

Example 33 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ImportUtils method preprocessImportedArtifact.

/**
 * Process the extracted temporary directory in order to detect the flow and alter the directory structure
 * according to the flow.
 *
 * @param tempDirectory String of the temporary directory path value
 * @return Path to the extracted directory
 * @throws APIImportExportException If an error occurs while creating the directory, transferring files or
 *                                  extracting the content
 */
public static String preprocessImportedArtifact(String tempDirectory) throws APIImportExportException {
    String tempDirectoryAbsolutePath = tempDirectory + File.separator;
    String paramsFileName = ImportExportConstants.INTERMEDIATE_PARAMS_FILE_LOCATION + ImportExportConstants.YAML_EXTENSION;
    boolean isParamsFileAvailable = CommonUtil.checkFileExistence(tempDirectoryAbsolutePath + paramsFileName);
    boolean isDeploymentDirectoryAvailable = CommonUtil.checkFileExistence(tempDirectoryAbsolutePath + ImportExportConstants.DEPLOYMENT_DIRECTORY_NAME);
    // When API controller is provided with params file
    if (isParamsFileAvailable) {
        if (!CommonUtil.checkFileExistence(tempDirectoryAbsolutePath + ImportExportConstants.SOURCE_ZIP_DIRECTORY_NAME)) {
            throw new APIImportExportException("The source artifact is not provided properly");
        } else {
            String newExtractedFolderName = CommonUtil.extractArchive(new File(tempDirectoryAbsolutePath + ImportExportConstants.SOURCE_ZIP_DIRECTORY_NAME), tempDirectoryAbsolutePath);
            // Copy the params file to working directory
            String srcParamsFilePath = tempDirectoryAbsolutePath + paramsFileName;
            String destParamsFilePath = tempDirectoryAbsolutePath + newExtractedFolderName + File.separator + paramsFileName;
            CommonUtil.copyFile(srcParamsFilePath, destParamsFilePath);
            return tempDirectoryAbsolutePath + newExtractedFolderName;
        }
    }
    // When API controller is provided with the "Deployment" directory
    if (isDeploymentDirectoryAvailable) {
        if (!CommonUtil.checkFileExistence(tempDirectoryAbsolutePath + ImportExportConstants.SOURCE_ZIP_DIRECTORY_NAME)) {
            throw new APIImportExportException("The source artifact is not provided properly");
        } else {
            String newExtractedFolderName = CommonUtil.extractArchive(new File(tempDirectoryAbsolutePath + ImportExportConstants.SOURCE_ZIP_DIRECTORY_NAME), tempDirectoryAbsolutePath);
            // Copy the params file to working directory
            String srcParamsFilePath = tempDirectoryAbsolutePath + ImportExportConstants.DEPLOYMENT_DIRECTORY_NAME + File.separator + paramsFileName;
            String destParamsFilePath = tempDirectoryAbsolutePath + newExtractedFolderName + File.separator + paramsFileName;
            CommonUtil.copyFile(srcParamsFilePath, destParamsFilePath);
            // move deployment directory into working directory
            String srcDeploymentDirectoryPath = tempDirectoryAbsolutePath + ImportExportConstants.DEPLOYMENT_DIRECTORY_NAME;
            String destDeploymentDirectoryPath = tempDirectoryAbsolutePath + newExtractedFolderName + File.separator + ImportExportConstants.DEPLOYMENT_DIRECTORY_NAME;
            CommonUtil.copyDirectory(srcDeploymentDirectoryPath, destDeploymentDirectoryPath);
            return tempDirectoryAbsolutePath + newExtractedFolderName;
        }
    }
    return tempDirectory;
}
Also used : APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) File(java.io.File)

Example 34 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class ImportUtils method importMediationLogic.

/**
 * Method created to add inflow and outflow mediation logic.
 *
 * @param sequenceData       Inflow and outflow directory
 * @param registry           Registry
 * @param soapToRestLocation Folder location
 * @throws APIImportExportException If an error occurs while importing/storing SOAP to REST mediation logic
 */
private static void importMediationLogic(SoapToRestMediationDto sequenceData, Registry registry, String soapToRestLocation) throws APIManagementException {
    String fileName = sequenceData.getResource().concat("_").concat(sequenceData.getMethod()).concat(".xml");
    try {
        byte[] inSeqData = sequenceData.getContent().getBytes();
        Resource inSeqResource = registry.newResource();
        inSeqResource.setContent(inSeqData);
        inSeqResource.addProperty(SOAPToRESTConstants.METHOD, sequenceData.getMethod());
        inSeqResource.setMediaType("text/xml");
        registry.put(soapToRestLocation + RegistryConstants.PATH_SEPARATOR + fileName, inSeqResource);
    } catch (DirectoryIteratorException e) {
        throw new APIManagementException("Error in importing SOAP to REST mediation logic", e);
    } catch (RegistryException e) {
        throw new APIManagementException("Error in storing imported SOAP to REST mediation logic", e);
    }
}
Also used : DirectoryIteratorException(java.nio.file.DirectoryIteratorException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 35 with APIImportExportException

use of org.wso2.carbon.apimgt.impl.importexport.APIImportExportException in project carbon-apimgt by wso2.

the class APIControllerUtil method handleClientCertificates.

/**
 * This method will be used to generate ClientCertificates and meta information related to client certs.
 *
 * @param certificates  JsonArray of client-certificates
 * @param identifier    API Identifier/API Product Identifier of the imported API/API Product
 * @param pathToArchive String of the archive project
 * @throws IOException            If an error occurs when generating new certs and yaml file or when moving certs
 * @throws APIManagementException If an error while generating new directory
 */
private static void handleClientCertificates(JsonArray certificates, Identifier identifier, String pathToArchive) throws IOException, APIManagementException {
    APIIdentifier apiIdentifier = new APIIdentifier(identifier.getProviderName(), identifier.getName(), identifier.getVersion());
    List<ClientCertificateDTO> certs = new ArrayList<>();
    for (JsonElement certificate : certificates) {
        JsonObject certObject = certificate.getAsJsonObject();
        String alias = certObject.get(ImportExportConstants.ALIAS_JSON_KEY).getAsString();
        ClientCertificateDTO cert = new ClientCertificateDTO();
        cert.setApiIdentifier(apiIdentifier);
        cert.setAlias(alias);
        cert.setTierName(certObject.get(ImportExportConstants.CERTIFICATE_TIER_NAME_PROPERTY).getAsString());
        String certName = certObject.get(ImportExportConstants.CERTIFICATE_PATH_PROPERTY).getAsString();
        cert.setCertificate(certName);
        certs.add(cert);
        // check and create a directory
        String clientCertificatesDirectory = pathToArchive + ImportExportConstants.CLIENT_CERTIFICATES_DIRECTORY_PATH;
        if (!CommonUtil.checkFileExistence(clientCertificatesDirectory)) {
            try {
                CommonUtil.createDirectory(clientCertificatesDirectory);
            } catch (APIImportExportException e) {
                throw new APIManagementException(e);
            }
        }
        // copy certs file from certificates
        String userCertificatesTempDirectory = pathToArchive + ImportExportConstants.DEPLOYMENT_DIRECTORY + ImportExportConstants.CERTIFICATE_DIRECTORY;
        String sourcePath = userCertificatesTempDirectory + File.separator + certName;
        String destinationPath = clientCertificatesDirectory + File.separator + certName;
        if (Files.notExists(Paths.get(sourcePath))) {
            String errorMessage = "The mentioned certificate file " + certName + " is not in the certificates directory";
            throw new APIManagementException(errorMessage, ExceptionCodes.ERROR_READING_PARAMS_FILE);
        }
        CommonUtil.moveFile(sourcePath, destinationPath);
    }
    JsonElement jsonElement = new Gson().toJsonTree(certs);
    // generate meta-data yaml file
    String metadataFilePath = pathToArchive + ImportExportConstants.CLIENT_CERTIFICATES_META_DATA_FILE_PATH;
    try {
        if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.YAML_EXTENSION)) {
            File oldFile = new File(metadataFilePath + ImportExportConstants.YAML_EXTENSION);
            oldFile.delete();
        }
        if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.JSON_EXTENSION)) {
            File oldFile = new File(metadataFilePath + ImportExportConstants.JSON_EXTENSION);
            oldFile.delete();
        }
        CommonUtil.writeDtoToFile(metadataFilePath, ExportFormat.JSON, ImportExportConstants.TYPE_CLIENT_CERTIFICATES, jsonElement);
    } catch (APIImportExportException e) {
        throw new APIManagementException(e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) File(java.io.File)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)30 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)30 IOException (java.io.IOException)24 File (java.io.File)20 Gson (com.google.gson.Gson)11 JsonObject (com.google.gson.JsonObject)10 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)10 JsonArray (com.google.gson.JsonArray)9 JsonElement (com.google.gson.JsonElement)9 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)8 API (org.wso2.carbon.apimgt.api.model.API)6 GsonBuilder (com.google.gson.GsonBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)4 JsonParser (com.google.gson.JsonParser)3