Search in sources :

Example 61 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method addApiPolicy.

@Override
public String addApiPolicy(APIPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApiPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add API policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 62 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method getAPIsByStatus.

@Override
public List<API> getAPIsByStatus(List<String> gatewayLabels, String status) throws APIManagementException {
    List<API> apiList;
    try {
        if (gatewayLabels != null && status != null) {
            apiList = apiDAO.getAPIsByStatus(gatewayLabels, status);
        } else {
            if (gatewayLabels == null) {
                String msg = "Gateway labels cannot be null";
                log.error(msg);
                throw new APIManagementException(msg, ExceptionCodes.GATEWAY_LABELS_CANNOT_BE_NULL);
            } else {
                String msg = "Status cannot be null";
                log.error(msg);
                throw new APIManagementException(msg, ExceptionCodes.STATUS_CANNOT_BE_NULL);
            }
        }
    } catch (APIMgtDAOException e) {
        String msg = "Error occurred while getting the API list in given states";
        log.error(msg, e);
        throw new APIManagementException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
    return apiList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API)

Example 63 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class KubernetesGatewayImpl method createContainerGateway.

/**
 * @see ContainerBasedGatewayGenerator#createContainerGateway(String, API)
 */
@Override
public void createContainerGateway(String label, API api) throws ContainerBasedGatewayException {
    Map<String, String> templateValues = new HashMap<>();
    String serviceName = label + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    String deploymentName = label + ContainerBasedGatewayConstants.CMS_DEPLOYMENT_SUFFIX;
    String ingressName = label + ContainerBasedGatewayConstants.CMS_INGRESS_SUFFIX;
    templateValues.put(ContainerBasedGatewayConstants.NAMESPACE, namespace);
    templateValues.put(ContainerBasedGatewayConstants.GATEWAY_LABEL, label);
    templateValues.put(ContainerBasedGatewayConstants.SERVICE_NAME, serviceName);
    templateValues.put(ContainerBasedGatewayConstants.DEPLOYMENT_NAME, deploymentName);
    templateValues.put(ContainerBasedGatewayConstants.INGRESS_NAME, ingressName);
    templateValues.put(ContainerBasedGatewayConstants.CONTAINER_NAME, label + ContainerBasedGatewayConstants.CMS_CONTAINER_SUFFIX);
    templateValues.put(ContainerBasedGatewayConstants.API_CORE_URL, apiCoreUrl);
    templateValues.put(ContainerBasedGatewayConstants.BROKER_HOST, brokerHost);
    templateValues.put(ContainerBasedGatewayConstants.GATEWAY_HOSTNAME, generateSubDomain(api) + "." + gatewayHostname);
    ContainerBasedGatewayTemplateBuilder builder = new ContainerBasedGatewayTemplateBuilder();
    // Create gateway service resource
    createServiceResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_SERVICE_TEMPLATE), serviceName);
    // Create gateway deployment resource
    createDeploymentResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_DEPLOYMENT_TEMPLATE), deploymentName);
    // Create gateway ingress resource
    createIngressResource(builder.generateTemplate(templateValues, ContainerBasedGatewayConstants.GATEWAY_INGRESS_TEMPLATE), ingressName);
}
Also used : HashMap(java.util.HashMap) ContainerBasedGatewayTemplateBuilder(org.wso2.carbon.apimgt.core.template.ContainerBasedGatewayTemplateBuilder)

Example 64 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIFileUtils method archiveDirectory.

/**
 * Creates a zip archive from of a directory
 *
 * @param sourceDirectory directory to create zip archive from
 * @param archiveLocation path to the archive location, excluding archive name
 * @param archiveName     name of the archive to create
 * @throws APIMgtDAOException if an error occurs while creating the archive
 */
public static void archiveDirectory(String sourceDirectory, String archiveLocation, String archiveName) throws APIMgtDAOException {
    File directoryToZip = new File(sourceDirectory);
    List<File> fileList = new ArrayList<>();
    getAllFiles(directoryToZip, fileList);
    try {
        writeArchiveFile(directoryToZip, fileList, archiveLocation, archiveName);
    } catch (IOException e) {
        String errorMsg = "Error while writing archive file " + directoryToZip.getPath() + " to archive " + archiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Archived API generated successfully" + archiveName);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 65 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIFileUtils method extractUploadedArchive.

/**
 * Extracts the APIs to the file system by reading the incoming {@link InputStream} object
 * uploadedApiArchiveInputStream
 *
 * @param uploadedApiArchiveInputStream Incoming {@link InputStream}
 * @param importedDirectoryName         directory to extract the archive
 * @param apiArchiveLocation            full path of the archive location
 * @param extractLocation               full path to the location to which the archive will be written
 * @return location to which APIs were extracted
 * @throws APIMgtDAOException if an error occurs while extracting the archive
 */
public static String extractUploadedArchive(InputStream uploadedApiArchiveInputStream, String importedDirectoryName, String apiArchiveLocation, String extractLocation) throws APIMgtDAOException {
    String archiveExtractLocation;
    try {
        // create api import directory structure
        APIFileUtils.createDirectory(extractLocation);
        // create archive
        createArchiveFromInputStream(uploadedApiArchiveInputStream, apiArchiveLocation);
        // extract the archive
        archiveExtractLocation = extractLocation + File.separator + importedDirectoryName;
        extractArchive(apiArchiveLocation, archiveExtractLocation);
    } catch (APIMgtDAOException e) {
        APIFileUtils.deleteDirectory(extractLocation);
        String errorMsg = "Error in accessing uploaded API archive";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
    return archiveExtractLocation;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)582 ArrayList (java.util.ArrayList)374 API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)350 HashMap (java.util.HashMap)318 Test (org.junit.Test)316 API (org.wso2.carbon.apimgt.api.model.API)307 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)255 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)234 SQLException (java.sql.SQLException)190 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)186 IOException (java.io.IOException)181 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 PreparedStatement (java.sql.PreparedStatement)169 Connection (java.sql.Connection)158 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)149 JSONObject (org.json.simple.JSONObject)142 Resource (org.wso2.carbon.registry.core.Resource)139