Search in sources :

Example 66 with API

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

Example 67 with API

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

the class ApplicationDeletionWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (application == null) {
        // this is when complete method is executed through workflow rest api
        this.application = applicationDAO.getApplication(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Deletion workflow complete: Approved");
        }
        applicationDAO.deleteApplication(getWorkflowReference());
        try {
            getApiGateway().deleteApplication(application.getId());
        } catch (GatewayException ex) {
            // This log is not harm to therefore not rethrow
            log.warn("Failed to send the Application Deletion Event ", ex);
        }
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Deletion workflow complete: Rejected");
        }
    }
    updateWorkflowEntries(this);
    return response;
}
Also used : GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Example 68 with API

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

the class ApplicationUpdateWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    String appId = getWorkflowReference();
    String name = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME);
    String updatedUser = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY);
    String applicationId = getWorkflowReference();
    String tier = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER);
    String policyId = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID);
    String description = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION);
    String permission = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION);
    Application application = new Application(name, updatedUser);
    application.setPolicy(new ApplicationPolicy(policyId, tier));
    application.setDescription(description);
    application.setId(applicationId);
    application.setUpdatedUser(updatedUser);
    application.setPermissionString(permission);
    application.setUpdatedTime(LocalDateTime.now());
    if (existingApplication == null && updatedApplication == null) {
        // this is when complete method is executed through workflow rest api
        existingApplication = applicationDAO.getApplication(appId);
        updatedApplication = application;
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application update workflow complete: Approved");
        }
        application.setStatus(APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED);
        applicationDAO.updateApplication(appId, application);
        try {
            getApiGateway().updateApplication(application);
        } catch (GatewayException ex) {
            // This log is not harm to therefore not rethrow
            log.warn("Failed to send the Application Update Event ", ex);
        }
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application update workflow complete: Rejected");
        }
        String existingAppStatus = getAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS);
        applicationDAO.updateApplicationState(appId, existingAppStatus);
    }
    updateWorkflowEntries(this);
    return response;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 69 with API

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

the class SampleTestObjectCreator method createDefaultAPIPolicy.

/**
 * create default api policy
 *
 * @return APIPolicy object is returned
 */
public static APIPolicy createDefaultAPIPolicy() {
    APIPolicy apiPolicy = new APIPolicy(SAMPLE_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    apiPolicy.setDisplayName(SAMPLE_API_POLICY);
    apiPolicy.setDescription(SAMPLE_API_POLICY_DESCRIPTION);
    apiPolicy.setUserLevel(APIMgtConstants.ThrottlePolicyConstants.API_LEVEL);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1000, 10000);
    defaultQuotaPolicy.setLimit(requestCountLimit);
    apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    apiPolicy.setPipelines(createDefaultPipelines());
    return apiPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 70 with API

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

the class SampleTestObjectCreator method createUniqueCompositeAPI.

public static CompositeAPI.Builder createUniqueCompositeAPI() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    HashMap permissionMap = new HashMap();
    permissionMap.put(DEVELOPER_ROLE_ID, 6);
    permissionMap.put(ADMIN_ROLE_ID, 15);
    permissionMap.put(ADMIN_ROLE_ID, 7);
    Application app = createDefaultApplication();
    // generate random name for each time when generating unique composite API
    app.setName(UUID.randomUUID().toString());
    try {
        DAOFactory.getApplicationDAO().addApplication(app);
    } catch (APIMgtDAOException e) {
        log.error("Error adding application", e);
    }
    CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder().id(UUID.randomUUID().toString()).name(UUID.randomUUID().toString()).provider(UUID.randomUUID().toString()).version(API_VERSION).context(UUID.randomUUID().toString()).description("Get Food & Beverage Info").transport(transport).permissionMap(permissionMap).applicationId(app.getId()).createdTime(LocalDateTime.now()).createdBy(API_CREATOR).uriTemplates(Collections.emptyMap()).apiDefinition(apiDefinition).lastUpdatedTime(LocalDateTime.now()).threatProtectionPolicies(threatProtectionPolicies);
    return apiBuilder;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) Application(org.wso2.carbon.apimgt.core.models.Application) HashSet(java.util.HashSet)

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