Package org.wso2.throttle

Examples of org.wso2.throttle.ConcurrentAccessController


* TODO: class description
*/
public final class Util {

    public static void checkAccess(String ownerTenantDomain, int ownerTenantId) {
        CarbonContext carbonContext = getCarbonContext();
        String callerTenantDomain = carbonContext.getTenantDomain();
        if (callerTenantDomain == null) {
            throw new IllegalStateException("Caller tenant domain cannot be null");
        }
        int callerTenantId = carbonContext.getTenantId();
        if (callerTenantId == MultitenantConstants.INVALID_TENANT_ID) {
            throw new IllegalStateException("Caller Tenant ID cannot be " + MultitenantConstants.INVALID_TENANT_ID);
        }

        if (callerTenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME) &&
View Full Code Here


                                        "],[" + callerTenantId + "]}");
        }
    }

    private static CarbonContext getCarbonContext() {
        CarbonContext carbonContext = CarbonContext.getThreadLocalCarbonContext();
        if (carbonContext == null) {
            throw new IllegalStateException("CarbonContext cannot be null");
        }
        return carbonContext;
    }
View Full Code Here

            PrivilegedCarbonContext.endTenantFlow();
        }
    }

    private void setTenantCredentialsInCarbonContext() {
        PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        cc.setTenantId(ownerTenantId);
        cc.setTenantDomain(ownerTenantDomain);
    }
View Full Code Here

        }

        public void run() {
            try {
                MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                Event<MessageContext> event = new Event(msgCtx);
                subscriptions = subscriptionManager.getMatchingSubscriptions(event);
            } catch (EventException e) {
                handleException("Matching subscriptions fetching error", e);
            }
View Full Code Here

     * @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);
View Full Code Here

            // Adding static subscriptions
            List<Subscription> staticSubscriptionList =
                    eventSource.getSubscriptionManager().getStaticSubscriptions();
            for (Iterator<Subscription> iterator = staticSubscriptionList.iterator();
                 iterator.hasNext();) {
                Subscription staticSubscription = iterator.next();
                OMElement staticSubElem =
                        fac.createOMElement("subscription", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                staticSubElem.addAttribute(
                        fac.createOMAttribute("id", nullNS, staticSubscription.getId()));
                OMElement filterElem =
                        fac.createOMElement("filter", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                filterElem.addAttribute(fac.createOMAttribute("source", nullNS,
                        (String) staticSubscription.getFilterValue()));
                filterElem.addAttribute(fac.createOMAttribute("dialect", nullNS,
                        (String) staticSubscription.getFilterDialect()));
                staticSubElem.addChild(filterElem);
                OMElement endpointElem =
                        fac.createOMElement("endpoint", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                OMElement addressElem =
                        fac.createOMElement("address", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                addressElem.addAttribute(
                        fac.createOMAttribute("uri", nullNS, staticSubscription.getEndpointUrl()));
                endpointElem.addChild(addressElem);
                staticSubElem.addChild(endpointElem);
                if (staticSubscription.getExpires() != null) {
                    OMElement expiresElem =
                            fac.createOMElement("expires", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                    fac.createOMText(expiresElem,
                            ConverterUtil.convertToString(staticSubscription.getExpires()));
                    staticSubElem.addChild(expiresElem);
                }
                evenSourceElem.addChild(staticSubElem);
            }
View Full Code Here


    public SynapseSubscription() {
        this.setId(UIDGenerator.generateURNString());
        this.setDeliveryMode(EventingConstants.WSE_DEFAULT_DELIVERY_MODE);
        SubscriptionData subscriptionData = new SubscriptionData();
        subscriptionData.setProperty(SynapseEventingConstants.STATIC_ENTRY, "false");
        this.setSubscriptionData(subscriptionData);
    }
View Full Code Here

                    .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(); // Initialise before doing further processing, required for static subscriptions
View Full Code Here

                clusteringAgent.getStateManager() != null) {
            isClusteringEnable = true;
        }

        // Get the concurrent access controller
        ConcurrentAccessController cac;
        String key = null;
        if (isClusteringEnable) {
            // for clustered  env.,gets it from axis configuration context
            key = ThrottleConstants.THROTTLE_PROPERTY_PREFIX + throttleId
                    + ThrottleConstants.CAC_SUFFIX;
            cac = (ConcurrentAccessController) cc.getProperty(key);
        } else {
            // for non-clustered  env.,gets it from axis configuration context
            cac = throttle.getConcurrentAccessController();
        }

        // check for concurrent access
        boolean canAccess = doConcurrentThrottling(cac, messageContext);

        if (canAccess) {
            // if the concurrent access is success then
            // do the access rate based throttling

            if (messageContext.getFLOW() == MessageContext.IN_FLOW) {
                //gets the remote caller domain name
                String domain = null;
                HttpServletRequest request =
                        (HttpServletRequest) messageContext.getPropertyNonReplicable(
                                HTTPConstants.MC_HTTP_SERVLETREQUEST);
                if (request != null) {
                    domain = request.getRemoteHost();
                }

                // Domain name based throttling
                //check whether a configuration has been defined for this domain name or not
                String callerId = null;
                if (domain != null) {
                    //loads the ThrottleContext
                    ThrottleContext context =
                            throttle.getThrottleContext(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY);
                    if (context != null) {
                        //Loads the ThrottleConfiguration
                        ThrottleConfiguration config = context.getThrottleConfiguration();
                        if (config != null) {
                            //check for configuration for this caller
                            callerId = config.getConfigurationKeyOfCaller(domain);
                            if (callerId != null) {
                                // If this is a clustered env.
                                if (isClusteringEnable) {
                                    context.setConfigurationContext(cc);
                                    context.setThrottleId(throttleId);
                                }
                                AccessInformation infor =
                                        accessRateController.canAccess(context, callerId,
                                                ThrottleConstants.DOMAIN_BASE);

                                //check for the permission for access
                                if (!infor.isAccessAllowed()) {

                                    //In the case of both of concurrency throttling and
                                    //rate based throttling have enabled ,
                                    //if the access rate less than maximum concurrent access ,
                                    //then it is possible to occur death situation.To avoid that reset,
                                    //if the access has denied by rate based throttling
                                    if (cac != null) {
                                        cac.incrementAndGet();
                                        // set back if this is a clustered env
                                        if (isClusteringEnable) {
                                            cc.setProperty(key, cac);
                                            //replicate the current state of ConcurrentAccessController
                                            try {
                                                if (debugOn) {
                                                    log.debug("Going to replicates the " +
                                                            "states of the ConcurrentAccessController" +
                                                            " with key : " + key);
                                                }
                                                Replicator.replicate(cc, new String[]{key});
                                            } catch (ClusteringFault clusteringFault) {
                                                log.error("Error during replicating states ",
                                                        clusteringFault);
                                            }
                                        }
                                    }
                                    throw new AxisFault(" Access deny for a " +
                                            "caller with Domain " + domain + " " +
                                            " : Reason : " + infor.getFaultReason());
                                }
                            } else {
                                if (debugOn) {
                                    log.debug("Could not find the Throttle Context for domain-Based " +
                                            "Throttling for domain name " + domain + " Throttling for this " +
                                            "domain name may not be configured from policy");
                                }
                            }
                        }
                    }
                } else {
                    if (debugOn) {
                        log.debug("Could not find the domain of the caller - IP-based throttling may occur");
                    }
                }

                //IP based throttling - Only if there is no configuration for caller domain name

                if (callerId == null) {
                    String ip = (String) messageContext.getProperty(MessageContext.REMOTE_ADDR);
                    if (ip != null) {
                        // loads IP based throttle context
                        ThrottleContext context =
                                throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
                        if (context != null) {
                            //Loads the ThrottleConfiguration
                            ThrottleConfiguration config = context.getThrottleConfiguration();
                            if (config != null) {
                                // check for configuration for this ip
                                callerId = config.getConfigurationKeyOfCaller(ip);
                                if (callerId != null) {
                                    // for clustered env.
                                    if (isClusteringEnable) {
                                        context.setConfigurationContext(cc);
                                        context.setThrottleId(throttleId);
                                    }
                                    AccessInformation infor =
                                            accessRateController.canAccess(context, callerId,
                                                    ThrottleConstants.DOMAIN_BASE);
                                    // check for the permission for access
                                    if (!infor.isAccessAllowed()) {

                                        //In the case of both of concurrency throttling and
                                        //rate based throttling have enabled ,
                                        //if the access rate less than maximum concurrent access ,
                                        //then it is possible to occur death situation.To avoid that reset,
                                        //if the access has denied by rate based throttling
                                        if (cac != null) {
                                            cac.incrementAndGet();
                                            // set back if this is a clustered env
                                            if (isClusteringEnable) {
                                                cc.setProperty(key, cac);
                                                //replicate the current state of ConcurrentAccessController
                                                try {
View Full Code Here

                                ThrottleConstants.THROTTLES_MAP, throttles);
                    }
                    String serviceName = currentService.getName();
                    throttle.setId(serviceName);
                    throttles.put(serviceName, throttle);
                    ConcurrentAccessController cac = throttle.getConcurrentAccessController();
                    if (cac != null) {
                        String cacKey = ThrottleConstants.THROTTLE_PROPERTY_PREFIX
                                        + serviceName + ThrottleConstants.CAC_SUFFIX;
                        configctx.setProperty(cacKey, cac);
                    }
                }
            }
        } else if (axisDescription instanceof AxisOperation) {

            Throttle throttle = null;
            AxisOperation currentOperation = ((AxisOperation) axisDescription);
            AxisService axisService = (AxisService) currentOperation.getParent();
            if (axisService != null) {
                currentServiceName = axisService.getName();
                PolicySubject policySubject = currentOperation.getPolicySubject();
                if (policySubject != null) {
                    try {
                        List policies = new ArrayList(policySubject.getAttachedPolicyComponents());
                        Policy currentPolicy = PolicyUtil.getMergedPolicy(policies,
                                                                          currentOperation);
                        if (currentPolicy != null) {
                            throttle = ThrottleFactory.createOperationThrottle(currentPolicy);
                        }
                    } catch (ThrottleException e) {
                        log.error("Error was occurred when engaging throttle module " +
                                  "for operation : " +
                                  currentOperation.getName() + " in the service :" +
                                  currentServiceName + e.getMessage());
                        log.info("Throttling will occur using default module policy");
                    }

                    // if current throttle is null, use the default throttle
                    if (throttle == null) {
                        throttle = defaultThrottle;
                    }

                    Map throttles = (Map) configctx
                            .getPropertyNonReplicable(ThrottleConstants.THROTTLES_MAP);
                    if (throttles == null) {
                        throttles = new HashMap();
                        configctx.setNonReplicableProperty(ThrottleConstants.THROTTLES_MAP
                                , throttles);
                    }
                    QName opQName = currentOperation.getName();
                    if (opQName != null) {
                        String opName = opQName.getLocalPart();
                        String key = currentServiceName + opName;
                        throttle.setId(key);
                        throttles.put(key, throttle);
                        ConcurrentAccessController cac =
                                throttle.getConcurrentAccessController();
                        if (cac != null) {
                            String cacKey = ThrottleConstants.THROTTLE_PROPERTY_PREFIX
                                    + key + ThrottleConstants.CAC_SUFFIX;
                            configctx.setProperty(cacKey, cac);
View Full Code Here

TOP

Related Classes of org.wso2.throttle.ConcurrentAccessController

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.