Search in sources :

Example 51 with API

use of org.wso2.carbon.apimgt.core.models.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 52 with API

use of org.wso2.carbon.apimgt.core.models.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 53 with API

use of org.wso2.carbon.apimgt.core.models.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 54 with API

use of org.wso2.carbon.apimgt.core.models.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)

Example 55 with API

use of org.wso2.carbon.apimgt.core.models.API in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.

/**
 * Generate policy for api level throttling
 *
 * @return throttle policies for api level
 * @throws APITemplateException throws if generation failure occur
 */
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy pipeline and generate the template
    Map<String, String> policyArray = new HashMap<String, String>();
    StringWriter writer;
    VelocityContext context;
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPI());
    // Generate template for pipeline conditions if pipelines not null
    if (apiPolicy.getPipelines() != null) {
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            // set values for velocity context
            context = new VelocityContext();
            setConstantContext(context);
            context.put(PIPELINE_ITEM, pipeline);
            context.put(POLICY, apiPolicy);
            context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
            context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
            String conditionString = getPolicyCondition(pipeline.getConditions());
            context.put(CONDITION, AND + conditionString);
            writer = new StringWriter();
            template.merge(context, writer);
            if (log.isDebugEnabled()) {
                log.debug("Generated Siddhi App : " + writer.toString());
            }
            String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
            policyArray.put(policyName, writer.toString());
        }
    }
    return policyArray;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)320 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)132 HashMap (java.util.HashMap)129 ArrayList (java.util.ArrayList)112 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)106 Test (org.junit.Test)83 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)83 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)82 SQLException (java.sql.SQLException)75 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)70 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)65 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)61 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)60 Connection (java.sql.Connection)58 Response (javax.ws.rs.core.Response)58