Search in sources :

Example 51 with Policy

use of org.apache.neethi.Policy in project wso2-synapse by wso2.

the class ThrottleEnguageUtils method enguage.

public static void enguage(AxisDescription axisDescription, ConfigurationContext configctx, Throttle defaultThrottle) throws AxisFault {
    String currentServiceName;
    if (axisDescription instanceof AxisService) {
        Throttle throttle = null;
        AxisService currentService = ((AxisService) axisDescription);
        PolicySubject policySubject = currentService.getPolicySubject();
        if (policySubject != null) {
            try {
                List policies = new ArrayList(policySubject.getAttachedPolicyComponents());
                Policy currentPolicy = PolicyUtil.getMergedPolicy(policies, currentService);
                if (currentPolicy != null) {
                    throttle = ThrottleFactory.createServiceThrottle(currentPolicy);
                    if (throttle == null) {
                        // this is for the scenario when throttle policy is empty rather than
                        // null (eg: removing the throttle policy via policy editor)
                        throttle = defaultThrottle;
                    }
                // todo - done by isuru, recheck
                } else {
                    AxisConfiguration axisConfig = configctx.getAxisConfiguration();
                    AxisModule throttleModule = axisConfig.getModule(ThrottleConstants.THROTTLE_MODULE_NAME);
                    policySubject = throttleModule.getPolicySubject();
                    if (policySubject != null) {
                        currentPolicy = ThrottleEnguageUtils.getThrottlePolicy(policySubject.getAttachedPolicyComponents());
                        if (currentPolicy != null) {
                            throttle = ThrottleFactory.createModuleThrottle(currentPolicy);
                        }
                    }
                // todo - done by isuru
                }
            } catch (ThrottleException e) {
                log.error("Error was occurred when engaging throttle module for" + " the service :" + currentService.getName() + e.getMessage());
                log.info("Throttling will occur using default module policy");
                throttle = defaultThrottle;
            }
            if (throttle != null) {
                Map throttles = (Map) configctx.getPropertyNonReplicable(ThrottleConstants.THROTTLES_MAP);
                if (throttles == null) {
                    throttles = new HashMap();
                    configctx.setNonReplicableProperty(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);
                    }
                }
            }
        }
    }
}
Also used : Policy(org.apache.neethi.Policy) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Throttle(org.apache.synapse.commons.throttle.core.Throttle) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentAccessController(org.apache.synapse.commons.throttle.core.ConcurrentAccessController) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with Policy

use of org.apache.neethi.Policy in project wso2-synapse by wso2.

the class ThrottleEnguageUtils method readExternalGlobalPolicy.

/**
 * An external policy can be configured using a parameter in the axis2.xml which points to the
 * absolute path of the policy file. This method reads the policy file and creates a
 * PolicySubject.
 *
 * @param axisConfig - AxisConfiguration instance
 * @return - PolicySubject instance if the file found. Otherwise null..
 */
public static PolicySubject readExternalGlobalPolicy(AxisConfiguration axisConfig) {
    PolicySubject extPolicySubject = null;
    // read the global throttle parameter from axisConfig
    Parameter globalThrottlePolicyParam = axisConfig.getParameter(ThrottleConstants.GLOBAL_THROTTLE_PATH_PARAM);
    if (globalThrottlePolicyParam != null && !"".equals(globalThrottlePolicyParam.getValue())) {
        // If the path found, try to read the file
        String policyPath = (String) globalThrottlePolicyParam.getValue();
        File policyFile = new File(policyPath);
        if (policyFile.exists()) {
            try {
                // If the file exists, try to build the policy
                Policy globalPolicy = PolicyEngine.getPolicy(new FileInputStream(policyFile));
                extPolicySubject = new PolicySubject();
                extPolicySubject.attachPolicy(globalPolicy);
            } catch (FileNotFoundException e) {
                log.error("Error while reading global policy file..", e);
            }
        }
    }
    return extPolicySubject;
}
Also used : Policy(org.apache.neethi.Policy) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 53 with Policy

use of org.apache.neethi.Policy 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 54 with Policy

use of org.apache.neethi.Policy 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 55 with Policy

use of org.apache.neethi.Policy 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)

Aggregations

Policy (org.apache.neethi.Policy)122 Test (org.junit.Test)47 Assertion (org.apache.neethi.Assertion)27 QName (javax.xml.namespace.QName)23 ArrayList (java.util.ArrayList)21 All (org.apache.neethi.All)18 ExactlyOne (org.apache.neethi.ExactlyOne)18 Message (org.apache.cxf.message.Message)15 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)14 Element (org.w3c.dom.Element)13 Bus (org.apache.cxf.Bus)12 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)12 List (java.util.List)9 Interceptor (org.apache.cxf.interceptor.Interceptor)9 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)9 HashMap (java.util.HashMap)7 OMElement (org.apache.axiom.om.OMElement)7 MessageImpl (org.apache.cxf.message.MessageImpl)7 ReferenceResolver (org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver)6 InputStream (java.io.InputStream)5