Search in sources :

Example 1 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method decodeApiInformationFromDirectoryStructure.

/**
 * Reads and decodes APIs and relevant information from the given set of paths
 *
 * @param apiArtifactsBasePath path to the directory with API related artifacts
 * @param newApiProvider       API newApiProvider to be updated
 * @return Set of {@link APIDetails} objects
 * @throws APIMgtEntityImportExportException if any error occurs while decoding the APIs
 */
public Set<APIDetails> decodeApiInformationFromDirectoryStructure(String apiArtifactsBasePath, String newApiProvider) throws APIMgtEntityImportExportException {
    Set<String> apiDefinitionsRootDirectoryPaths = null;
    try {
        apiDefinitionsRootDirectoryPaths = APIFileUtils.getDirectoryList(apiArtifactsBasePath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    if (apiDefinitionsRootDirectoryPaths.isEmpty()) {
        try {
            APIFileUtils.deleteDirectory(path);
        } catch (APIMgtDAOException e) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    Set<APIDetails> apiDetailsSet = new HashSet<>();
    for (String apiDefinitionDirectoryPath : apiDefinitionsRootDirectoryPaths) {
        File apiDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX);
        File swaggerDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.SWAGGER_DEFINITION_FILE_PREFIX);
        API api;
        String swaggerDefinition, gatewayConfiguration;
        Set<Endpoint> endpoints;
        try {
            api = getApiDefinitionFromExtractedArchive(apiDefinitionFile.getPath());
            swaggerDefinition = getSwaggerDefinitionFromExtractedArchive(swaggerDefinitionFile.getPath());
            gatewayConfiguration = getGatewayConfigurationFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.GATEWAY_CONFIGURATION_DEFINITION_FILE);
            endpoints = getEndpointsFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + ENDPOINTS_ROOT_DIRECTORY, api.getName(), api.getVersion());
        } catch (APIManagementException e) {
            log.error("Error occurred while importing api from path: " + apiDefinitionDirectoryPath, e);
            // skip this API
            continue;
        }
        if (newApiProvider != null && !newApiProvider.isEmpty()) {
            // update the newApiProvider
            api = new API.APIBuilder(api).provider(newApiProvider).build();
        }
        String documentsRootDirectory = apiDefinitionDirectoryPath + File.separator + DOCUMENTS_ROOT_DIRECTORY;
        Set<DocumentInfo> documentInfoSet = getDocumentInfoFromExtractedArchive(documentsRootDirectory, api.getName(), api.getVersion());
        Set<DocumentContent> documentContents = new HashSet<>();
        for (DocumentInfo aDocumentInfo : documentInfoSet) {
            DocumentContent aDocumentContent = getDocumentContentFromExtractedArchive(aDocumentInfo, documentsRootDirectory + File.separator + aDocumentInfo.getId());
            if (aDocumentContent != null) {
                documentContents.add(aDocumentContent);
            }
        }
        InputStream thumbnailStream = null;
        try {
            thumbnailStream = APIFileUtils.getThumbnailImage(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME);
        } catch (APIMgtDAOException e) {
            // log and ignore
            log.error("Error occurred while reading thumbnail image.", e);
        }
        APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
        apiDetails.setGatewayConfiguration(gatewayConfiguration);
        apiDetails.setEndpoints(endpoints);
        if (!documentInfoSet.isEmpty()) {
            apiDetails.addDocumentInformation(documentInfoSet);
        }
        if (!documentContents.isEmpty()) {
            apiDetails.addDocumentContents(documentContents);
        }
        if (thumbnailStream != null) {
            apiDetails.setThumbnailStream(thumbnailStream);
        }
        apiDetailsSet.add(apiDetails);
    }
    return apiDetailsSet;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) InputStream(java.io.InputStream) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) HashSet(java.util.HashSet) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 2 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts 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 3 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIPublisherImpl method addAPIFromWSDLArchive.

@Override
public String addAPIFromWSDLArchive(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
    WSDLArchiveInfo archiveInfo = extractAndValidateWSDLArchive(inputStream);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
    }
    apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(archiveInfo.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
    String uuid = addAPI(apiBuilder);
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the API. uuid: " + uuid);
    }
    try (InputStream fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath())) {
        getApiDAO().addOrUpdateWSDLArchive(uuid, fileInputStream, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully added/updated the WSDL archive. uuid: " + uuid);
        }
        if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(archiveInfo.getWsdlInfo().getVersion())) {
            log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
        }
        return uuid;
    } catch (IOException e) {
        throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    } finally {
        try {
            APIFileUtils.deleteDirectory(archiveInfo.getLocation());
        } catch (APIMgtDAOException e) {
            // This is not a blocker. Give a warning and continue
            log.warn("Error occured while deleting processed WSDL artifacts folder : " + archiveInfo.getLocation());
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 4 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class RegistryPersistenceUtil method notifyAPIStateChangeToAssociatedDocuments.

/**
 * Notify document artifacts if an api state change occured. This change is required to re-trigger the document
 * indexer so that the documnet indexes will be updated with the new associated api status.
 *
 * @param apiArtifact
 * @param registry
 * @throws RegistryException
 * @throws APIManagementException
 */
public static void notifyAPIStateChangeToAssociatedDocuments(GenericArtifact apiArtifact, Registry registry) throws RegistryException, APIManagementException {
    Association[] docAssociations = registry.getAssociations(apiArtifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
    for (Association association : docAssociations) {
        String documentResourcePath = association.getDestinationPath();
        Resource docResource = registry.get(documentResourcePath);
        String oldStateChangeIndicatorStatus = docResource.getProperty(APIConstants.API_STATE_CHANGE_INDICATOR);
        String newStateChangeIndicatorStatus = "false";
        if (oldStateChangeIndicatorStatus != null) {
            newStateChangeIndicatorStatus = String.valueOf(!Boolean.parseBoolean(oldStateChangeIndicatorStatus));
        }
        docResource.setProperty(APIConstants.API_STATE_CHANGE_INDICATOR, "false");
        registry.put(documentResourcePath, docResource);
    }
}
Also used : Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource)

Example 5 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class GatewayUtils method retrieveDeployedAPI.

public static String retrieveDeployedAPI(String apiName, String version, String tenantDomain) throws APIManagementException {
    try {
        MessageContext.setCurrentMessageContext(createAxis2MessageContext());
        RESTAPIAdminServiceProxy restapiAdminServiceProxy = new RESTAPIAdminServiceProxy(tenantDomain);
        String qualifiedName = GatewayUtils.getQualifiedApiName(apiName, version);
        OMElement api = restapiAdminServiceProxy.getApiContent(qualifiedName);
        if (api != null) {
            return api.toString();
        }
        return null;
    } catch (AxisFault axisFault) {
        throw new APIManagementException("Error while retrieving API Artifacts", axisFault, ExceptionCodes.INTERNAL_ERROR);
    } finally {
        MessageContext.destroyCurrentMessageContext();
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OMElement(org.apache.axiom.om.OMElement)

Aggregations

File (java.io.File)23 IOException (java.io.IOException)18 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)18 ArrayList (java.util.ArrayList)17 CAppArtifacts (org.wso2.ei.dashboard.core.rest.model.CAppArtifacts)16 Operation (io.swagger.v3.oas.annotations.Operation)15 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)15 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)15 Response (javax.ws.rs.core.Response)15 Artifact (org.wso2.carbon.application.deployer.config.Artifact)15 Artifacts (org.wso2.ei.dashboard.core.rest.model.Artifacts)15 DeploymentException (org.apache.axis2.deployment.DeploymentException)11 CappFile (org.wso2.carbon.application.deployer.config.CappFile)11 Deployer (org.apache.axis2.deployment.Deployer)10 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)10 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 InputStream (java.io.InputStream)9 FileInputStream (java.io.FileInputStream)7 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7