Search in sources :

Example 66 with Subscription

use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project wso2-synapse by wso2.

the class SynapseEventSource method receive.

/**
 * Override the Message receiver method to accept subscriptions and events
 *
 * @param mc message context
 * @throws AxisFault
 */
public void receive(MessageContext mc) throws AxisFault {
    // Create synapse message context from the axis2 message context
    SynapseConfiguration synCfg = (SynapseConfiguration) mc.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_CONFIG).getValue();
    SynapseEnvironment synEnv = (SynapseEnvironment) mc.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV).getValue();
    org.apache.synapse.MessageContext smc = new Axis2MessageContext(mc, synCfg, synEnv);
    // initialize the response message builder using the message context
    ResponseMessageBuilder messageBuilder = new ResponseMessageBuilder(mc);
    try {
        if (EventingConstants.WSE_SUBSCRIBE.equals(mc.getWSAAction())) {
            // add new subscription to the SynapseSubscription store through subscription manager
            processSubscriptionRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_UNSUBSCRIBE.equals(mc.getWSAAction())) {
            // Unsubscribe the matching subscription
            processUnSubscribeRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_GET_STATUS.equals(mc.getWSAAction())) {
            // Response with the status of the subscription
            processGetStatusRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_RENEW.equals(mc.getWSAAction())) {
            // Renew subscription
            processReNewRequest(mc, messageBuilder);
        } else {
            // Treat as an Event
            if (log.isDebugEnabled()) {
                log.debug("Event received");
            }
            dispatchEvents(smc);
        }
    } catch (EventException e) {
        handleException("Subscription manager processing error", e);
    }
}
Also used : EventException(org.wso2.eventing.exceptions.EventException) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ResponseMessageBuilder(org.apache.synapse.eventing.builders.ResponseMessageBuilder) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 67 with Subscription

use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project wso2-synapse by wso2.

the class SynapseEventSource method processGetStatusRequest.

/**
 * Process the GetStatus message request
 *
 * @param mc             axis2 message context
 * @param messageBuilder respose message builder
 * @throws AxisFault     axis fault
 * @throws EventException event
 */
private void processGetStatusRequest(MessageContext mc, ResponseMessageBuilder messageBuilder) throws AxisFault, EventException {
    Subscription subscription = SubscriptionMessageBuilder.createGetStatusMessage(mc);
    if (log.isDebugEnabled()) {
        log.debug("GetStatus request recived for SynapseSubscription ID : " + subscription.getId());
    }
    subscription = subscriptionManager.getSubscription(subscription.getId());
    if (subscription != null) {
        if (log.isDebugEnabled()) {
            log.debug("Sending GetStatus responce for SynapseSubscription ID : " + subscription.getId());
        }
        // send the responce
        SOAPEnvelope soapEnvelope = messageBuilder.genGetStatusResponse(subscription);
        dispatchResponse(soapEnvelope, EventingConstants.WSE_GET_STATUS_RESPONSE, mc, false);
    } else {
        // Send the Fault responce
        if (log.isDebugEnabled()) {
            log.debug("GetStatus failed, sending fault response");
        }
        SOAPEnvelope soapEnvelope = messageBuilder.genFaultResponse(mc, EventingConstants.WSE_FAULT_CODE_RECEIVER, "EventSourceUnableToProcess", "Subscription Not Found", "");
        dispatchResponse(soapEnvelope, EventingConstants.WSA_FAULT, mc, true);
    }
}
Also used : SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Subscription(org.wso2.eventing.Subscription)

Example 68 with Subscription

use of org.wso2.carbon.apimgt.api.model.subscription.Subscription in project wso2-synapse by wso2.

the class EventSourceFactory method createEventSource.

@SuppressWarnings({ "UnusedDeclaration" })
public static SynapseEventSource createEventSource(OMElement elem, Properties properties) {
    SynapseEventSource eventSource = null;
    OMAttribute name = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name == null) {
        handleException("The 'name' attribute is required for a event source de");
    } else {
        eventSource = new SynapseEventSource(name.getAttributeValue());
    }
    OMElement subscriptionManagerElem = elem.getFirstChildWithName(SUBSCRIPTION_MANAGER_QNAME);
    if (eventSource != null && subscriptionManagerElem != null) {
        OMAttribute clazz = subscriptionManagerElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "class"));
        if (clazz != null) {
            String className = clazz.getAttributeValue();
            try {
                Class subscriptionManagerClass = Class.forName(className);
                SubscriptionManager manager = (SubscriptionManager) subscriptionManagerClass.newInstance();
                Iterator itr = subscriptionManagerElem.getChildrenWithName(PROPERTIES_QNAME);
                while (itr.hasNext()) {
                    OMElement propElem = (OMElement) itr.next();
                    String propName = propElem.getAttribute(new QName("name")).getAttributeValue();
                    String propValue = propElem.getAttribute(new QName("value")).getAttributeValue();
                    if (propName != null && !"".equals(propName.trim()) && propValue != null && !"".equals(propValue.trim())) {
                        propName = propName.trim();
                        propValue = propValue.trim();
                        PasswordManager passwordManager = PasswordManager.getInstance();
                        String key = eventSource.getName() + "." + propName;
                        if (passwordManager.isInitialized() && passwordManager.isTokenProtected(key)) {
                            eventSource.putConfigurationProperty(propName, propValue);
                            propValue = passwordManager.resolve(propValue);
                        }
                        manager.addProperty(propName, propValue);
                    }
                }
                eventSource.setSubscriptionManager(manager);
                eventSource.getSubscriptionManager().init();
            } catch (ClassNotFoundException e) {
                handleException("SubscriptionManager class not found", e);
            } catch (IllegalAccessException e) {
                handleException("Unable to access the SubscriptionManager object", e);
            } catch (InstantiationException e) {
                handleException("Unable to instantiate the SubscriptionManager object", e);
            }
        } else {
            handleException("SynapseSubscription manager class is a required attribute");
        }
    } else {
        handleException("SynapseSubscription Manager has not been specified for the event source");
    }
    try {
        createStaticSubscriptions(elem, eventSource);
    } catch (EventException e) {
        handleException("Static subscription creation failure", e);
    }
    return eventSource;
}
Also used : SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) EventException(org.wso2.eventing.exceptions.EventException) QName(javax.xml.namespace.QName) PasswordManager(org.wso2.securevault.PasswordManager) OMElement(org.apache.axiom.om.OMElement) SubscriptionManager(org.wso2.eventing.SubscriptionManager) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 69 with Subscription

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

the class APIUtil method getTiersFromPolicies.

public static Map<String, Tier> getTiersFromPolicies(String policyLevel, int tenantId) throws APIManagementException {
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    Policy[] policies;
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getSubscriptionPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getAPIPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getApplicationPolicies(tenantId);
    } else {
        throw new APIManagementException("No such a policy type : " + policyLevel);
    }
    for (Policy policy : policies) {
        if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
            Tier tier = new Tier(policy.getPolicyName());
            tier.setDescription(policy.getDescription());
            tier.setDisplayName(policy.getDisplayName());
            Limit limit = policy.getDefaultQuotaPolicy().getLimit();
            tier.setTimeUnit(limit.getTimeUnit());
            tier.setUnitTime(limit.getUnitTime());
            tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
            // If the policy is a subscription policy
            if (policy instanceof SubscriptionPolicy) {
                SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
                tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
                tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
                setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
                if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
                    tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
                }
            }
            if (limit instanceof RequestCountLimit) {
                RequestCountLimit countLimit = (RequestCountLimit) limit;
                tier.setRequestsPerMin(countLimit.getRequestCount());
                tier.setRequestCount(countLimit.getRequestCount());
            } else if (limit instanceof BandwidthLimit) {
                BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
                tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
                tier.setRequestCount(bandwidthLimit.getDataAmount());
                tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
            } else {
                EventCountLimit eventCountLimit = (EventCountLimit) limit;
                tier.setRequestCount(eventCountLimit.getEventCount());
                tier.setRequestsPerMin(eventCountLimit.getEventCount());
            }
            if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
                tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
            }
            tierMap.put(policy.getPolicyName(), tier);
        } else {
            if (APIUtil.isEnabledUnlimitedTier()) {
                Tier tier = new Tier(policy.getPolicyName());
                tier.setDescription(policy.getDescription());
                tier.setDisplayName(policy.getDisplayName());
                tier.setRequestsPerMin(Integer.MAX_VALUE);
                tier.setRequestCount(Integer.MAX_VALUE);
                if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
                    tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
                } else {
                    tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
                }
                tierMap.put(policy.getPolicyName(), tier);
            }
        }
    }
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        tierMap.remove(APIConstants.UNAUTHENTICATED_TIER);
    }
    return tierMap;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) TreeMap(java.util.TreeMap) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Limit(org.wso2.carbon.apimgt.api.model.policy.Limit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 70 with Subscription

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

the class SubscriptionCreationApprovalWorkflowExecutor method execute.

/**
 * Execute the Application Creation workflow approval process.
 *
 * @param workflowDTO
 */
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing Subscription Creation Webservice Workflow.. ");
    }
    SubscriptionWorkflowDTO subsWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    String message = "Approve API " + subsWorkflowDTO.getApiName() + " - " + subsWorkflowDTO.getApiVersion() + " subscription creation request from subscriber - " + subsWorkflowDTO.getSubscriber() + " for the application - " + subsWorkflowDTO.getApplicationName();
    workflowDTO.setWorkflowDescription(message);
    workflowDTO.setProperties("apiName", subsWorkflowDTO.getApiName());
    workflowDTO.setProperties("apiVersion", subsWorkflowDTO.getApiVersion());
    workflowDTO.setProperties("subscriber", subsWorkflowDTO.getSubscriber());
    workflowDTO.setProperties("applicationName", subsWorkflowDTO.getApplicationName());
    super.execute(workflowDTO);
    return new GeneralWorkflowResponse();
}
Also used : SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 ArrayList (java.util.ArrayList)69 Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 Test (org.junit.Test)56 SQLException (java.sql.SQLException)55 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)51 Connection (java.sql.Connection)49 PreparedStatement (java.sql.PreparedStatement)48 ResultSet (java.sql.ResultSet)39 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)31 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 Response (javax.ws.rs.core.Response)24 Application (org.wso2.carbon.apimgt.api.model.Application)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22