Search in sources :

Example 1 with XmlPrimtiveAssertion

use of org.apache.neethi.builders.xml.XmlPrimtiveAssertion in project wso2-synapse by wso2.

the class ThrottleEnguageUtils method getThrottlePolicy.

// todo - done by isuru, recheck
private static Policy getThrottlePolicy(Collection components) throws AxisFault {
    QName assertionName;
    // Finds the policy for throttling
    Iterator i = components.iterator();
    while (i.hasNext()) {
        Object comp = i.next();
        // }
        // for (Object comp : components) {
        Policy policy = (Policy) comp;
        for (Iterator iterator = policy.getAlternatives(); iterator.hasNext(); ) {
            Object object = iterator.next();
            if (object instanceof List) {
                List list = (List) object;
                Iterator j = list.iterator();
                while (j.hasNext()) {
                    // for (Object assertObj : list) {
                    Object assertObj = j.next();
                    if (assertObj instanceof XmlPrimtiveAssertion) {
                        XmlPrimtiveAssertion primitiveAssertion = (XmlPrimtiveAssertion) assertObj;
                        assertionName = primitiveAssertion.getName();
                        if (assertionName.equals(ThrottleConstants.SERVICE_THROTTLE_ASSERTION_QNAME) || assertionName.equals(ThrottleConstants.MODULE_THROTTLE_ASSERTION_QNAME)) {
                            if (log.isDebugEnabled()) {
                                log.debug("Existing ThrottleAssertion found");
                            }
                            return policy;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : Policy(org.apache.neethi.Policy) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) XmlPrimtiveAssertion(org.apache.neethi.builders.xml.XmlPrimtiveAssertion)

Example 2 with XmlPrimtiveAssertion

use of org.apache.neethi.builders.xml.XmlPrimtiveAssertion in project wso2-synapse by wso2.

the class ThrottleFactory method createThrottle.

/**
 * Factory method to create a throttle from a given throttle policy
 *
 * @param policy    Throttle policy
 * @param forceRoot Root assertion QName for select correct policy assertion
 * @return Throttle instance
 * @throws ThrottleException
 */
private static Throttle createThrottle(Policy policy, QName forceRoot) throws ThrottleException {
    if (policy == null) {
        if (log.isDebugEnabled()) {
            log.debug("Policy cannot be found");
        }
        // no policy is available in the module description
        return null;
    }
    if (forceRoot == null) {
        if (log.isDebugEnabled()) {
            log.debug("Given root assertion QName is null");
        }
        return null;
    }
    for (Iterator iterator = policy.getAlternatives(); iterator.hasNext(); ) {
        Object object = iterator.next();
        if (object instanceof List) {
            List list = (List) object;
            for (Iterator it = list.iterator(); it.hasNext(); ) {
                Object assertObj = it.next();
                if (assertObj instanceof XmlPrimtiveAssertion) {
                    XmlPrimtiveAssertion primitiveAssertion = (XmlPrimtiveAssertion) assertObj;
                    QName qName = primitiveAssertion.getName();
                    if (qName == null) {
                        handleException("Invalid Throttle Policy - QName of the " + "assertion cannot be null.");
                    }
                    // top policy must contains ThrottleAssertion
                    Policy throttlePolicy = PolicyEngine.getPolicy(primitiveAssertion.getValue());
                    if (ThrottleConstants.THROTTLE_ASSERTION_QNAME.equals(qName)) {
                        return ThrottlePolicyProcessor.processPolicy(throttlePolicy);
                    } else if (forceRoot.equals(qName)) {
                        return buildThrottle(throttlePolicy);
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("There is no throttle policy " + "for given QName : " + forceRoot);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : Policy(org.apache.neethi.Policy) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) List(java.util.List) XmlPrimtiveAssertion(org.apache.neethi.builders.xml.XmlPrimtiveAssertion)

Example 3 with XmlPrimtiveAssertion

use of org.apache.neethi.builders.xml.XmlPrimtiveAssertion in project wso2-synapse by wso2.

the class ThrottleFactory method fillControlConfiguration.

/**
 * Helper method to process control assertion
 *
 * @param policy              Policy for Control Assertion
 * @param callerConfiguration Caller to whom control need to be applied.
 * @throws ThrottleException
 */
private static void fillControlConfiguration(Policy policy, CallerConfiguration callerConfiguration) throws ThrottleException {
    boolean isFoundMaxCount = false;
    boolean isFoundUnitTime = false;
    List list = policy.getPolicyComponents();
    for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
        Object obj = iterator.next();
        if (obj instanceof Policy) {
            List controlList = ((Policy) obj).getPolicyComponents();
            for (Iterator controlIterator = controlList.iterator(); controlIterator.hasNext(); ) {
                Object object = controlIterator.next();
                if (object instanceof XmlPrimtiveAssertion) {
                    XmlPrimtiveAssertion primitiveAssertion = (XmlPrimtiveAssertion) object;
                    OMElement element = primitiveAssertion.getValue();
                    // Name of the policy assertion
                    String name = element.getLocalName();
                    // Value of the policy assertion
                    String value = element.getText();
                    // invalid policy configuration
                    if (name == null || value == null) {
                        handleException("Either Value or " + "Name of the policy cannot be null");
                    }
                    if (!value.equals("")) {
                        if (name.equals(ThrottleConstants.MAXIMUM_COUNT_PARAMETER_NAME)) {
                            isFoundMaxCount = true;
                            try {
                                callerConfiguration.setMaximumRequestPerUnitTime(Integer.parseInt(value.trim()));
                            } catch (NumberFormatException ignored) {
                                log.error("Error occurred - " + "Invalid number for maximum " + "request number ", ignored);
                                if (log.isDebugEnabled()) {
                                    log.debug("Access" + " will be fully allowed");
                                }
                                callerConfiguration.setAccessState(ThrottleConstants.ACCESS_ALLOWED);
                            }
                        } else if (name.equals(ThrottleConstants.UNIT_TIME_PARAMETER_NAME)) {
                            // TODO need to verify that value is in milisecond
                            long timeInMilliSec = 0;
                            try {
                                timeInMilliSec = Long.parseLong(value.trim());
                            } catch (NumberFormatException ignored) {
                                log.error("Error occurred " + "- Invalid number for unit time", ignored);
                            }
                            if (timeInMilliSec == 0) {
                                handleException("Unit Time cannot " + "find - invalid throttle " + "policy configuration");
                            }
                            isFoundUnitTime = true;
                            callerConfiguration.setUnitTime(timeInMilliSec);
                        } else if (name.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
                            try {
                                callerConfiguration.setProhibitTimePeriod(Long.parseLong(value.trim()));
                            } catch (NumberFormatException ignored) {
                                log.error("Error occurred - Invalid" + " number for prohibit time ", ignored);
                            }
                        } else {
                            handleException("Undefined Policy" + " property for Throttle Policy");
                        }
                    } else {
                        if (!name.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
                            handleException("The policy which have " + " defined as optional " + "should have value ");
                        }
                    }
                }
            }
        } else {
            handleException("Invalid policy - " + "Control Assertion must contain a wsp:Policy as child ");
        }
    }
    if (!isFoundUnitTime && !isFoundMaxCount) {
        handleException("Maximum Count and UnitTime are " + "Mandatory in Throttle Policy ");
    }
}
Also used : Policy(org.apache.neethi.Policy) Iterator(java.util.Iterator) List(java.util.List) OMElement(org.apache.axiom.om.OMElement) XmlPrimtiveAssertion(org.apache.neethi.builders.xml.XmlPrimtiveAssertion)

Example 4 with XmlPrimtiveAssertion

use of org.apache.neethi.builders.xml.XmlPrimtiveAssertion in project wso2-synapse by wso2.

the class ThrottleFactory method buildThrottle.

/**
 * Factory method to help to create a throttle
 *
 * @param throtlePolicy Throttle assertion policy
 * @return Throttle instance
 * @throws ThrottleException
 */
private static Throttle buildThrottle(Policy throtlePolicy) throws ThrottleException {
    // throttle instance
    Throttle throttle = new Throttle();
    // configuration data
    ThrottleConfiguration configuration = null;
    List list = throtlePolicy.getPolicyComponents();
    if (list == null || (list != null && list.isEmpty())) {
        handleException("Empty the policy components" + " as ThrottleAssertion's children");
    }
    for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
        Object object = iterator.next();
        if (object instanceof Policy) {
            // boolean isOtherConfiguration = false;
            // // To track default callerConfiguration for all ips
            CallerConfiguration callerConfiguration = null;
            Policy policy = null;
            List assertList = ((Policy) object).getAssertions();
            if (assertList != null) {
                for (Iterator assertIterator = assertList.iterator(); assertIterator.hasNext(); ) {
                    Object ca = assertIterator.next();
                    if (ca instanceof XmlPrimtiveAssertion) {
                        XmlPrimtiveAssertion id = (XmlPrimtiveAssertion) ca;
                        configuration = createThrottleConfiguration(id, throttle);
                        if (configuration == null) {
                            handleException("Invalid throttle - Throttle configuration " + "cannot be created from given policy");
                        }
                        if (configuration.getType() == ThrottleConstants.IP_BASE) {
                            // create a caller context for ip based throttle
                            callerConfiguration = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.IP_BASE);
                        } else if (configuration.getType() == ThrottleConstants.DOMAIN_BASE) {
                            // create a caller context for ip based throttle
                            callerConfiguration = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.DOMAIN_BASE);
                        } else if (configuration.getType() == ThrottleConstants.ROLE_BASE) {
                            // create a caller context for ip based throttle
                            callerConfiguration = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.ROLE_BASE);
                        } else {
                            handleException("Invalid throttle type - Only" + " support IP ,DOMAIN and ROLE as types ");
                        }
                        if (callerConfiguration != null) {
                            OMElement element = id.getValue();
                            // Name of the policy assertion
                            String name = element.getLocalName();
                            // Value of the policy assertion
                            String value = element.getText();
                            // then it is a invalid policy configuration
                            if (name == null || value == null) {
                                handleException("Either Value or" + " Name of the policy cannot be null");
                            } else if (name.equals(ThrottleConstants.ID_PARAMETER_NAME)) {
                                if (!value.equals("")) {
                                    callerConfiguration.setID(value);
                                } else {
                                    handleException("Value of ID cannot find " + "- invalid configuration");
                                }
                            } else {
                                handleException("Undefined policy property for" + " throttle - Expect ID  ");
                            }
                        }
                    } else if (ca instanceof Policy) {
                        policy = (Policy) ca;
                    }
                }
            }
            if (callerConfiguration != null) {
                if (policy != null) {
                    fillCallerConfiguration(policy, callerConfiguration);
                    configuration.addCallerConfiguration(callerConfiguration);
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Couldn't find a callerConfiguration for a throttle" + " configuration for an one caller  ");
                }
            }
        } else if (object instanceof XmlPrimtiveAssertion) {
            XmlPrimtiveAssertion xmlPrimitiveAssertion = (XmlPrimtiveAssertion) object;
            OMElement element = xmlPrimitiveAssertion.getValue();
            // Name of the policy assertion
            String name = element.getLocalName();
            // Value of the policy assertion
            String value = element.getText();
            // it is a invalid policy configuration
            if (name == null || value == null) {
                handleException("Either value or name of the policy cannot be null");
            } else if (name.equals(ThrottleConstants.MAXIMUM_CONCURRENT_ACCESS_PARAMETER_NAME)) {
                int intValue = 0;
                try {
                    intValue = Integer.parseInt(value.trim());
                } catch (NumberFormatException ignored) {
                    log.error("Error occurred - Invalid number for maximum " + "concurrent access ", ignored);
                }
                if (intValue > 0) {
                    throttle.setConcurrentAccessController(new ConcurrentAccessController(intValue));
                }
            } else {
                handleException("Invalid throttle policy configuration : unexpected policy " + "element with name " + name);
            }
        }
    }
    return throttle;
}
Also used : Policy(org.apache.neethi.Policy) OMElement(org.apache.axiom.om.OMElement) Iterator(java.util.Iterator) List(java.util.List) XmlPrimtiveAssertion(org.apache.neethi.builders.xml.XmlPrimtiveAssertion)

Example 5 with XmlPrimtiveAssertion

use of org.apache.neethi.builders.xml.XmlPrimtiveAssertion in project wso2-synapse by wso2.

the class ThrottlePolicyProcessor method processPolicy.

/**
 * @param policy - policy for throttle
 * @return Throttle        - An object which holds Mata-Data about throttle
 * @throws ThrottleException - throws for errors in policy processing - ex : invalid policy
 * @deprecated process policy and returns throttle object
 */
public static Throttle processPolicy(Policy policy) throws ThrottleException {
    if (policy == null) {
        // no policy is available in the module description
        return null;
    }
    Throttle th = new Throttle();
    // configuration data
    ThrottleConfiguration tc = null;
    List al = policy.getPolicyComponents();
    if (al == null || (al != null && al.isEmpty())) {
        handleException("Empty the policy components" + " as ThrottleAssertion's children");
    }
    for (Iterator i = al.iterator(); i.hasNext(); ) {
        Object tp = i.next();
        if (tp instanceof All) {
            // boolean isOtherConfiguration = false;
            // // To track default cn for all ips
            // To create a
            CallerConfiguration cn = null;
            // configurationbean object
            boolean isIPRangeFound = false;
            boolean isExactlyOneFound = false;
            ExactlyOne cp = null;
            List cL = ((All) tp).getAssertions();
            if (cL != null) {
                for (Iterator ci = cL.iterator(); ci.hasNext(); ) {
                    Object ca = ci.next();
                    if (ca instanceof XmlPrimtiveAssertion) {
                        XmlPrimtiveAssertion id = (XmlPrimtiveAssertion) ca;
                        OMElement el = id.getValue();
                        String t = el.getAttributeValue(ThrottleConstants.THROTTLE_TYPE_ATTRIBUTE_QNAME);
                        if (t == null) {
                            handleException("Type of Throtle " + "in the policy cannot be null");
                        }
                        if (t.equals("IP")) {
                            // create a ip based throttle context and configuration
                            tc = th.getThrottleConfiguration(ThrottleConstants.IP_BASED_THROTTLE_KEY);
                            if (tc == null) {
                                tc = ThrottleConfigurationFactory.createThrottleConfiguration(ThrottleConstants.IP_BASE);
                                th.addThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY, ThrottleContextFactory.createThrottleContext(ThrottleConstants.IP_BASE, tc));
                                th.addThrottleConfiguration(ThrottleConstants.IP_BASED_THROTTLE_KEY, tc);
                            }
                            // create a callercontext for ip based throttle
                            cn = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.IP_BASE);
                        } else if (t.equals("DOMAIN")) {
                            // create a domain based throttle context and configuration
                            tc = th.getThrottleConfiguration(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY);
                            if (tc == null) {
                                tc = ThrottleConfigurationFactory.createThrottleConfiguration(ThrottleConstants.DOMAIN_BASE);
                                th.addThrottleContext(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY, ThrottleContextFactory.createThrottleContext(ThrottleConstants.DOMAIN_BASE, tc));
                                th.addThrottleConfiguration(ThrottleConstants.DOMAIN_BASED_THROTTLE_KEY, tc);
                            }
                            // create a callercontext for domain based throttl
                            cn = CallerConfigurationFactory.createCallerConfiguration(ThrottleConstants.DOMAIN_BASE);
                        } else {
                            handleException("Unsupported throttle type : " + t);
                        }
                        if (cn != null) {
                            // Name of the policy assertion
                            String n = el.getLocalName();
                            // Value of the policy assertion
                            String v = el.getText();
                            // then it is a invalid policy config
                            if (n == null || v == null) {
                                handleException("Either Value or" + " Name of the policy cannot be null");
                            } else if (n.equals(ThrottleConstants.ID_PARAMETER_NAME)) {
                                if (!v.equals("")) {
                                    isIPRangeFound = true;
                                    cn.setID(v);
                                } else {
                                    handleException("Value of ID cannot find " + "- invalid configuration");
                                }
                            } else {
                                handleException("Undefined pocilcy property for" + " throttle - Expect ID  ");
                            }
                        }
                    } else if (ca instanceof ExactlyOne) {
                        cp = (ExactlyOne) ca;
                    }
                }
            }
            if (cn != null) {
                if (cp != null) {
                    List cal = cp.getPolicyComponents();
                    boolean haveSelectOneFromExactlyOne = false;
                    for (Iterator ci = cal.iterator(); ci.hasNext() && !haveSelectOneFromExactlyOne; ) {
                        Object co = ci.next();
                        if (co instanceof All) {
                            haveSelectOneFromExactlyOne = true;
                            boolean isFoundMaxCount = false;
                            boolean isFoundUnitTime = false;
                            All childAll = (All) co;
                            List cd = childAll.getPolicyComponents();
                            for (Iterator cdl = cd.iterator(); cdl.hasNext(); ) {
                                Object d = cdl.next();
                                if (d instanceof XmlPrimtiveAssertion) {
                                    XmlPrimtiveAssertion adx = (XmlPrimtiveAssertion) d;
                                    OMElement el = adx.getValue();
                                    // Name of the policy assertion
                                    String n = el.getLocalName();
                                    // Value of the policy assertion
                                    String v = el.getText();
                                    // invalid policy config
                                    if (n == null || v == null) {
                                        handleException("Either Value or " + "Name of the policy cannot be null");
                                    }
                                    if (!v.equals("")) {
                                        if (n.equals(ThrottleConstants.MAXIMUM_COUNT_PARAMETER_NAME)) {
                                            isFoundMaxCount = true;
                                            try {
                                                cn.setMaximumRequestPerUnitTime(Integer.parseInt(v.trim()));
                                            } catch (NumberFormatException ignored) {
                                                log.error("Error occurred - " + "Invalid number for maximum " + "request number ", ignored);
                                                if (log.isDebugEnabled()) {
                                                    log.debug("Access" + " will be fully allowed");
                                                }
                                                cn.setAccessState(ThrottleConstants.ACCESS_ALLOWED);
                                            }
                                        } else if (n.equals(ThrottleConstants.UNIT_TIME_PARAMETER_NAME)) {
                                            // TODO need to verify that value is in milisecond
                                            long timeInMiliSec = 0;
                                            try {
                                                timeInMiliSec = Long.parseLong(v.trim());
                                            } catch (NumberFormatException ignored) {
                                                log.error("Error occurred " + "- Invalid number for unit time", ignored);
                                            }
                                            if (timeInMiliSec == 0) {
                                                handleException("Unit Time cannot " + "find - invalid throttle " + "policy configuration");
                                            }
                                            isFoundUnitTime = true;
                                            cn.setUnitTime(timeInMiliSec);
                                        } else if (n.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
                                            try {
                                                cn.setProhibitTimePeriod(Long.parseLong(v.trim()));
                                            } catch (NumberFormatException ignored) {
                                                log.error("Error occurred - Invalid" + " number for prohibit time ", ignored);
                                            }
                                        } else {
                                            handleException("Undefined Policy" + " property for Throttle Policy");
                                        }
                                    } else {
                                        if (!n.equals(ThrottleConstants.PROHIBIT_TIME_PERIOD_PARAMETER_NAME)) {
                                            handleException("The policy which have " + " defined as optional " + "should have value ");
                                        }
                                    }
                                }
                            }
                            if (isFoundUnitTime && isFoundMaxCount) {
                                isExactlyOneFound = true;
                            } else {
                                handleException("Maximum Count and UnitTime are " + "Mandatory in Throttle Policy ");
                            }
                        } else if (co instanceof XmlPrimtiveAssertion) {
                            haveSelectOneFromExactlyOne = true;
                            XmlPrimtiveAssertion alx = (XmlPrimtiveAssertion) co;
                            OMElement ele = alx.getValue();
                            // Name of the policy assertion
                            String n = ele.getLocalName();
                            // Value of the policy assertion
                            String v = ele.getText();
                            // then it is a invalid policy config
                            if (n == null || v == null) {
                                handleException("Either Value or" + " Name of the policy cannot be null");
                            } else if (n.equals(ThrottleConstants.ISALLOW_PARAMETER_NAME)) {
                                if (v.equals(Boolean.toString(true))) {
                                    isExactlyOneFound = true;
                                    cn.setAccessState(ThrottleConstants.ACCESS_ALLOWED);
                                } else if (v.equals(Boolean.toString(false))) {
                                    isExactlyOneFound = true;
                                    cn.setAccessState(ThrottleConstants.ACCESS_DENIED);
                                } else {
                                    handleException("Value for isAllow " + " component is invalied");
                                }
                            } else {
                                handleException("Invalied Throttle" + " Policy configuration");
                            }
                        }
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Couldn't find a cn for a throttle configuration" + " for an one caller  ");
                }
            }
            if (isIPRangeFound && isExactlyOneFound) {
                // If the Throttle Configuration is valid
                tc.addCallerConfiguration(cn);
            } else {
                handleException("ID and one of Valid Control policy component are " + "Mandatory in Throttle Policy");
            }
        } else if (tp instanceof XmlPrimtiveAssertion) {
            XmlPrimtiveAssertion mca = (XmlPrimtiveAssertion) tp;
            OMElement ele = mca.getValue();
            // Name of the policy assertion
            String n = ele.getLocalName();
            // Value of the policy assertion
            String v = ele.getText();
            // it is a invalid policy configuration
            if (n == null || v == null) {
                handleException("Either Value or Name of the policy cannot be null");
            } else if (n.equals(ThrottleConstants.MAXIMUM_CONCURRENT_ACCESS_PARAMETER_NAME)) {
                int intvalue = 0;
                try {
                    intvalue = Integer.parseInt(v.trim());
                } catch (NumberFormatException ignored) {
                    log.error("Error occurred - Invalid number for maximum " + "concurrent access ", ignored);
                }
                if (intvalue > 0) {
                    th.setConcurrentAccessController(new ConcurrentAccessController(intvalue));
                }
            } else {
                handleException("Invalied Throttle Policy configuration");
            }
        }
    }
    return th;
}
Also used : All(org.apache.neethi.All) OMElement(org.apache.axiom.om.OMElement) ExactlyOne(org.apache.neethi.ExactlyOne) Iterator(java.util.Iterator) List(java.util.List) XmlPrimtiveAssertion(org.apache.neethi.builders.xml.XmlPrimtiveAssertion)

Aggregations

Iterator (java.util.Iterator)6 List (java.util.List)6 XmlPrimtiveAssertion (org.apache.neethi.builders.xml.XmlPrimtiveAssertion)6 Policy (org.apache.neethi.Policy)5 OMElement (org.apache.axiom.om.OMElement)4 QName (javax.xml.namespace.QName)2 ArrayList (java.util.ArrayList)1 All (org.apache.neethi.All)1 ExactlyOne (org.apache.neethi.ExactlyOne)1