You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by in...@apache.org on 2007/03/09 10:13:02 UTC
svn commit: r516340 - in /webservices/synapse/trunk/java: ./
modules/extensions/
modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/
modules/extensions/src/main/resources/META-INF/services/
modules/extensions/src/test/java/org/apach...
Author: indika
Date: Fri Mar 9 01:13:01 2007
New Revision: 516340
URL: http://svn.apache.org/viewvc?view=rev&rev=516340
Log:
add throttle mediator
Throttle mediator can configure using policy
The syntax for it
<throttle><policy key=""/></throttle> OR <throttle><policy>InLineXML</policy></throttle>
Added:
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java
webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java
webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/
webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializationTest.java
webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorTest.java
Modified:
webservices/synapse/trunk/java/modules/extensions/pom.xml
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
webservices/synapse/trunk/java/modules/nhttp/src/org/apache/axis2/transport/nhttp/HttpCoreNIOSender.java
webservices/synapse/trunk/java/pom.xml
Modified: webservices/synapse/trunk/java/modules/extensions/pom.xml
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/pom.xml?view=diff&rev=516340&r1=516339&r2=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/pom.xml (original)
+++ webservices/synapse/trunk/java/modules/extensions/pom.xml Fri Mar 9 01:13:01 2007
@@ -114,10 +114,18 @@
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
+ <!-- WSO2 Throttle -->
+ <dependency>
+ <groupId>org.wso2.throttle</groupId>
+ <artifactId>wso2throttle</artifactId>
+ <version>${wso2commons.version}</version>
+ <type>jar</type>
+ </dependency>
</dependencies>
<properties>
+ <wso2commons.version>1.2-SNAPSHOT</wso2commons.version>
<spring.version>1.2.6</spring.version>
<xerces.version>2.8.1</xerces.version>
<xml_apis.version>1.3.03</xml_apis.version>
Added: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java?view=auto&rev=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java (added)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediator.java Fri Mar 9 01:13:01 2007
@@ -0,0 +1,205 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.neethi.PolicyEngine;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.Entry;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.wso2.throttle.factory.AccessControllerFactory;
+import org.wso2.throttle.*;
+
+
+/**
+ * The Mediator for the throttling - Throtting will occur according to the ws-policy which is specified as
+ * the key for lookup from the registry or the inline policy
+ * Only support IP based throttling- Throotling can manage per IP using the throttle policy
+ */
+
+public class ThrottleMediator extends AbstractMediator {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediator.class);
+
+ /** The key for getting policy value - key refer to registry entry */
+ private String policyKey = null;
+ /** InLine policy object - XML */
+ private OMElement inLinePolicy = null;
+ /** The throttle - hold runtime + configuration data of throttle */
+ Throttle throttle = null;
+
+ public boolean mediate(MessageContext synCtx) {
+ //init method to init throttle
+ init(synCtx);
+ // check access allow or not
+ return canAccess(synCtx);
+
+ }
+
+ /**
+ * To check whether allow access or not for caller
+ * Current Implementaion only support IP Based Throttling
+ * @param synContext
+ * @return boolean which indicate whether this caller can or not access
+ */
+ protected boolean canAccess(MessageContext synContext) {
+
+ if (throttle == null) {
+ log.info("Can not find a throttle");
+ return true;
+ }
+ org.apache.axis2.context.MessageContext axis2MessageContext
+ = ((Axis2MessageContext) synContext).getAxis2MessageContext();
+ //IP based throttling
+ Object remoteIP = axis2MessageContext.getProperty(
+ org.apache.axis2.context.MessageContext.REMOTE_ADDR);
+ if (remoteIP == null) {
+ log.info("IP address of the caller can not find - Currently only support caller-IP base access control" +
+ "- Thottling will not happen ");
+ return true;
+ } else {
+ ThrottleContext throttleContext
+ = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
+ if (throttleContext == null) {
+ log.info("Can not find a configuartion for IP Based Throttle");
+ return true;
+ }
+ try {
+ AccessController accessControler = AccessControllerFactory.createAccessControler(ThrottleConstants.IP_BASE);
+ boolean canAccess = accessControler.canAccess(throttleContext, remoteIP);
+ if (!canAccess) {
+ log.info("Access has currently been denied by the IP_BASE throttle for IP :\t" + remoteIP);
+ }
+ return canAccess;
+ }
+ catch (ThrottleException e) {
+ log.warn(e.getMessage());
+ return true;
+ }
+ }
+ }
+
+
+ /**
+ * To init throttle with the policy
+ * If the policy is defined as a Registry key ,then Policy will only process after it has expired
+ * Any runtime changes to the policy will take effect
+ * If the policy is defined as a Inline XML ,then only one time policy will process and any runtime
+ * changes to the policy will not reflect
+ * @param synContext
+ */
+ protected void init(MessageContext synContext) {
+
+ boolean reCreate = false; // It is not need to recreate ,if property is not dyanamic
+ OMElement policyOmElement = null;
+ if (policyKey != null) {
+ Entry entry = synContext.getConfiguration().getEntryDefinition(policyKey);
+ if (entry == null) {
+ return;
+ }
+ Object entryValue = entry.getValue();
+
+ if (!(entryValue instanceof OMElement)) {
+ return;
+ }
+ // if entry is dynamic, need to check wheather updated or not
+ if ((!entry.isCached() || entry.isExpired())) {
+ reCreate = true;
+ }
+ policyOmElement = (OMElement) entryValue;
+ } else if (inLinePolicy != null) {
+ policyOmElement = inLinePolicy;
+ }
+ if (policyOmElement == null) {
+ log.warn("Cant not find a Policy - Throttling will not occur");
+ return;
+ }
+ if (!reCreate) {
+ //The first time creation
+ if (throttle == null) {
+ createThrottleMetaData(policyOmElement);
+ }
+ } else {
+ createThrottleMetaData(policyOmElement);
+ }
+
+ }
+
+ /**
+ * To create the Throttle from the policy element
+ *
+ * @param policyOmElement - valid throttle policy
+ */
+ protected void createThrottleMetaData(OMElement policyOmElement) {
+ try {
+ throttle = ThrottlePolicyProcessor
+ .processPoclicy(PolicyEngine.getPolicy(policyOmElement));
+ }
+ catch (ThrottleException e) {
+ handleException("Error during processing thorttle policy " + e.getMessage());
+ }
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public String getType() {
+ return ThrottleMediator.class.getName();
+ }
+
+ /**
+ * To get the policy key - The key for which lookup from the registry
+ *
+ * @return String
+ */
+ public String getPolicyKey() {
+ return policyKey;
+ }
+
+ /**
+ * To set the policy key - The key for which lookup from the registry
+ *
+ * @param policyKey
+ */
+ public void setPolicyKey(String policyKey) {
+ this.policyKey = policyKey;
+ }
+
+ /**
+ * getting throttle policy which has defined as InLineXML
+ *
+ * @return InLine Throttle Policy
+ */
+ public OMElement getInLinePolicy() {
+ return inLinePolicy;
+ }
+
+ /**
+ * setting throttle policy which has defined as InLineXML
+ *
+ * @param inLinePolicy
+ */
+ public void setInLinePolicy(OMElement inLinePolicy) {
+ this.inLinePolicy = inLinePolicy;
+ }
+}
Added: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java?view=auto&rev=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java (added)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorFactory.java Fri Mar 9 01:13:01 2007
@@ -0,0 +1,77 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+import org.apache.synapse.config.xml.Constants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import javax.xml.namespace.QName;
+
+
+/**
+ * The Factory for create throttle mediator- key or InLine XMl need to provide
+ */
+
+public class ThrottleMediatorFactory extends AbstractMediatorFactory {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediatorFactory.class);
+
+ /**
+ * The Tag Name for throttle
+ */
+ private static final QName TAG_NAME
+ = new QName(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
+
+ public Mediator createMediator(OMElement elem) {
+
+ ThrottleMediator throttleMediator = new ThrottleMediator();
+ OMElement policy = elem.getFirstChildWithName(
+ new QName(Constants.SYNAPSE_NAMESPACE, "policy"));
+ if (policy != null) {
+ OMAttribute key = policy.getAttribute(new QName(Constants.NULL_NAMESPACE, "key"));
+ if (key != null) {
+ String keyValue = key.getAttributeValue();
+ if (keyValue != null && !"".equals(keyValue)) {
+ throttleMediator.setPolicyKey(keyValue);
+ } else {
+ handleException("key attribute should have a value ");
+ }
+ } else {
+ OMElement inLine = policy.getFirstElement();
+ if (inLine != null) {
+ throttleMediator.setInLinePolicy(inLine);
+ }
+ }
+ } else {
+ handleException("Throttle Mediator must have a policy");
+ }
+ return throttleMediator;
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ public QName getTagQName() {
+ return TAG_NAME;
+ }
+}
Added: webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java?view=auto&rev=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java (added)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializer.java Fri Mar 9 01:13:01 2007
@@ -0,0 +1,74 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.synapse.mediators.throttle;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.AbstractMediatorSerializer;
+import org.apache.synapse.config.xml.Constants;
+
+/**
+ * The Serializer for Throttle Mediator saving throttle instance
+ */
+
+public class ThrottleMediatorSerializer extends AbstractMediatorSerializer {
+
+ private static final Log log = LogFactory.getLog(ThrottleMediatorSerializer.class);
+
+ private static final OMNamespace throttleNS
+ = fac.createOMNamespace(Constants.SYNAPSE_NAMESPACE + "/throttle", "throttle");
+
+ public OMElement serializeMediator(OMElement parent, Mediator m) {
+ if (!(m instanceof ThrottleMediator)) {
+ handleException("Invalid Mediator has passed to serializer");
+ }
+ ThrottleMediator throttleMediator = (ThrottleMediator) m;
+ OMElement throttle = fac.createOMElement("throttle", throttleNS);
+ OMElement policy = fac.createOMElement("policy", synNS);
+ String key = throttleMediator.getPolicyKey();
+ if (key != null) {
+ policy.addAttribute(fac.createOMAttribute(
+ "key", nullNS, key));
+ throttle.addChild(policy);
+ } else {
+ OMNode inlinePolicy = throttleMediator.getInLinePolicy();
+ if (inlinePolicy != null) {
+ policy.addChild(inlinePolicy);
+ throttle.addChild(policy);
+ }
+ }
+ if (parent != null) {
+ parent.addChild(throttle);
+ }
+ return throttle;
+
+ }
+
+ public String getMediatorClassName() {
+ return ThrottleMediator.class.getName();
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+}
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory?view=diff&rev=516340&r1=516339&r2=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory Fri Mar 9 01:13:01 2007
@@ -3,3 +3,4 @@
org.apache.synapse.mediators.spring.SpringMediatorFactory
org.apache.synapse.mediators.bsf.ScriptMediatorFactory
org.apache.synapse.mediators.attachment.AttachmentMediatorFactory
+org.apache.synapse.mediators.throttle.ThrottleMediatorFactory
\ No newline at end of file
Modified: webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer?view=diff&rev=516340&r1=516339&r2=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer (original)
+++ webservices/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorSerializer Fri Mar 9 01:13:01 2007
@@ -2,4 +2,5 @@
org.apache.synapse.mediators.transform.XSLTMediatorSerializer
org.apache.synapse.mediators.spring.SpringMediatorSerializer
org.apache.synapse.mediators.bsf.ScriptMediatorSerializer
-org.apache.synapse.mediators.bsf.InlineScriptMediatorSerializer
\ No newline at end of file
+org.apache.synapse.mediators.bsf.InlineScriptMediatorSerializer
+org.apache.synapse.mediators.throttle.ThrottleMediatorSerializer
\ No newline at end of file
Added: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializationTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializationTest.java?view=auto&rev=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializationTest.java (added)
+++ webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorSerializationTest.java Fri Mar 9 01:13:01 2007
@@ -0,0 +1,48 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.synapse.mediators.throttle;
+
+import org.apache.synapse.mediators.AbstractTestCase;
+
+
+/**
+ *
+ *
+ */
+
+public class ThrottleMediatorSerializationTest extends AbstractTestCase {
+
+ ThrottleMediatorFactory throttleMediatorFactory;
+ ThrottleMediatorSerializer throttleMediatorSerializer;
+
+ public ThrottleMediatorSerializationTest() {
+ throttleMediatorFactory = new ThrottleMediatorFactory();
+ throttleMediatorSerializer = new ThrottleMediatorSerializer();
+ }
+
+ public void testPropertyMediatorSerializationSenarioOne() throws Exception {
+ String inputXml = "<throttle:throttle xmlns:throttle=\"http://ws.apache.org/ns/synapse/throttle\" xmlns=\"http://ws.apache.org/ns/synapse\" >" +
+ "<policy key=\"thottleKey\"/></throttle:throttle>";
+ assertTrue(serialization(inputXml, throttleMediatorFactory, throttleMediatorSerializer));
+ assertTrue(serialization(inputXml, throttleMediatorSerializer));
+ }
+// public void testPropertyMediatorSerializationSenarioTwo() throws Exception {
+// String inputXml = "<throttle:throttle xmlns:throttle=\"http://ws.apache.org/ns/synapse/throttle\" xmlns=\"http://ws.apache.org/ns/synapse\" >" +
+// "<policy><inXml/></policy></throttle:throttle>";
+// assertTrue(serialization(inputXml, throttleMediatorFactory, throttleMediatorSerializer));
+// assertTrue(serialization(inputXml, throttleMediatorSerializer));
+// }
+}
Added: webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorTest.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorTest.java?view=auto&rev=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorTest.java (added)
+++ webservices/synapse/trunk/java/modules/extensions/src/test/java/org/apache/synapse/mediators/throttle/ThrottleMediatorTest.java Fri Mar 9 01:13:01 2007
@@ -0,0 +1,326 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.synapse.mediators.throttle;
+
+import junit.framework.TestCase;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.neethi.PolicyEngine;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.config.Entry;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.wso2.throttle.*;
+import org.wso2.throttle.factory.AccessControllerFactory;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+
+/**
+ * Throttle Mediator Test - This class test throttling when policy has specified as both of
+ * InLine and a registry key
+ *
+ */
+
+public class ThrottleMediatorTest extends TestCase {
+ private static final String REMOTE_ADDR = "Remote_Addr";
+
+ private static final String POLICY = " <wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\"\n" +
+ " xmlns:throttle=\"http://www.wso2.org/products/wso2commons/throttle\">\n" +
+ " <throttle:ThrottleAssertion>\n" +
+ " <wsp:All>\n" +
+ " <throttle:ID throttle:type=\"IP\">Other</throttle:ID>\n" +
+ " <wsp:ExactlyOne>\n" +
+ " <wsp:All>\n" +
+ " <throttle:MaximumCount>8</throttle:MaximumCount>\n" +
+ " <throttle:UnitTime>800000</throttle:UnitTime>\n" +
+ " <throttle:ProhibitTimePeriod wsp:Optional=\"true\">10</throttle:ProhibitTimePeriod>\n" +
+ " </wsp:All>\n" +
+ " <throttle:IsAllow>true</throttle:IsAllow>\n" +
+ " </wsp:ExactlyOne>\n" +
+ " </wsp:All>\n" +
+ " <wsp:All>\n" +
+ " <throttle:ID throttle:type=\"IP\">192.168.8.200-192.168.8.222</throttle:ID>\n" +
+ " <wsp:ExactlyOne>\n" +
+ " <wsp:All>\n" +
+ " <throttle:MaximumCount>3</throttle:MaximumCount>\n" +
+ " <throttle:UnitTime>800000</throttle:UnitTime>\n" +
+ " <throttle:ProhibitTimePeriod wsp:Optional=\"true\">10000</throttle:ProhibitTimePeriod>\n" +
+ " </wsp:All>\n" +
+ " <throttle:IsAllow>true</throttle:IsAllow>\n" +
+ " </wsp:ExactlyOne>\n" +
+ " </wsp:All>\n" +
+ " <wsp:All>\n" +
+ " <throttle:ID throttle:type=\"IP\">192.168.8.201</throttle:ID>\n" +
+ " <wsp:ExactlyOne>\n" +
+ " <wsp:All>\n" +
+ " <throttle:MaximumCount>200</throttle:MaximumCount>\n" +
+ " <throttle:UnitTime>600000</throttle:UnitTime>\n" +
+ " <throttle:ProhibitTimePeriod wsp:Optional=\"true\"></throttle:ProhibitTimePeriod>\n" +
+ " </wsp:All>\n" +
+ " <throttle:IsAllow>true</throttle:IsAllow>\n" +
+ " </wsp:ExactlyOne>\n" +
+ " </wsp:All>\n" +
+ " <wsp:All>\n" +
+ " <throttle:ID throttle:type=\"IP\">192.168.8.198</throttle:ID>\n" +
+ " <wsp:ExactlyOne>\n" +
+ " <wsp:All>\n" +
+ " <throttle:MaximumCount>50</throttle:MaximumCount>\n" +
+ " <throttle:UnitTime>500000</throttle:UnitTime>\n" +
+ " <throttle:ProhibitTimePeriod wsp:Optional=\"true\"></throttle:ProhibitTimePeriod>\n" +
+ " </wsp:All>\n" +
+ " <throttle:IsAllow>true</throttle:IsAllow>\n" +
+ " </wsp:ExactlyOne>\n" +
+ " </wsp:All>\n" +
+ " </throttle:ThrottleAssertion>\n" +
+ " </wsp:Policy>";
+
+ public ThrottleMediatorTest() {
+ super(ThrottleMediatorTest.class.getName());
+ }
+
+ public static MessageContext createLightweightSynapseMessageContext(
+ String payload) throws Exception {
+ org.apache.axis2.context.MessageContext mc =
+ new org.apache.axis2.context.MessageContext();
+ SynapseConfiguration config = new SynapseConfiguration();
+ SynapseEnvironment env = new Axis2SynapseEnvironment();
+ MessageContext synMc = new Axis2MessageContext(mc, config, env);
+ SOAPEnvelope envelope =
+ OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
+ OMDocument omDoc =
+ OMAbstractFactory.getSOAP11Factory().createOMDocument();
+ omDoc.addChild(envelope);
+
+ envelope.getBody().addChild(createOMElement(payload));
+
+ synMc.setEnvelope(envelope);
+ return synMc;
+ }
+
+ public static OMElement createOMElement(String xml) {
+ try {
+ XMLStreamReader reader = XMLInputFactory
+ .newInstance().createXMLStreamReader(new StringReader(xml));
+ StAXOMBuilder builder = new StAXOMBuilder(reader);
+ OMElement omElement = builder.getDocumentElement();
+ return omElement;
+ }
+ catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void testMediate() throws Exception {
+ ByteArrayInputStream in = new ByteArrayInputStream(POLICY.getBytes());
+ StAXOMBuilder builde = new StAXOMBuilder(in);
+ ThrottleTestMediator throttleMediator = new ThrottleTestMediator();
+ throttleMediator.setPolicyKey("throttlepolicy");
+ MessageContext synCtx = createLightweightSynapseMessageContext("<empty/>");
+ synCtx.setProperty(REMOTE_ADDR, "192.168.8.212");
+ SynapseConfiguration synCfg = new SynapseConfiguration();
+ Entry prop = new Entry();
+ prop.setKey("throttlepolicy");
+ prop.setType(Entry.INLINE_XML);
+ prop.setValue(builde.getDocumentElement());
+ synCfg.addEntry("throttlepolicy", prop);
+ synCtx.setConfiguration(synCfg);
+ for (int i = 0; i < 6; i++) {
+ try {
+ throttleMediator.mediate(synCtx);
+ Thread.sleep(1000);
+ }
+ catch (Exception e) {
+
+ if (i == 3) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ if (i == 4) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ if (i == 5) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ }
+ }
+
+ }
+ public void testMediateWithInLineXML() throws Exception {
+ ByteArrayInputStream in = new ByteArrayInputStream(POLICY.getBytes());
+ StAXOMBuilder build = new StAXOMBuilder(in);
+ ThrottleTestMediator throttleMediator = new ThrottleTestMediator();
+ throttleMediator.setInLinePolicy(build.getDocumentElement());
+ MessageContext synCtx = createLightweightSynapseMessageContext("<empty/>");
+ synCtx.setProperty(REMOTE_ADDR, "192.168.8.212");
+ SynapseConfiguration synCfg = new SynapseConfiguration();
+ synCtx.setConfiguration(synCfg);
+ for (int i = 0; i < 6; i++) {
+ try {
+ throttleMediator.mediate(synCtx);
+ Thread.sleep(1000);
+ }
+ catch (Exception e) {
+
+ if (i == 3) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ if (i == 4) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ if (i == 5) {
+ assertTrue(e.getMessage().lastIndexOf("IP_BASE") > 0);
+ }
+ }
+ }
+
+ }
+
+ public class ThrottleTestMediator extends AbstractMediator {
+
+ private String policyKey = null;
+
+ Throttle throttle = null;
+ private OMElement inLinePolicy = null;
+
+ public boolean mediate(MessageContext synCtx) {
+
+ init(synCtx);
+ try {
+ return canAcess(synCtx);
+ }
+ catch (
+ ThrottleException e) {
+ throw new SynapseException(e.getMessage());
+ }
+
+ }
+
+ protected boolean canAcess(MessageContext synContext)
+ throws SynapseException, ThrottleException {
+
+ if (throttle == null) {
+ throw new ThrottleException("Can not find a throttle");
+
+ }
+ //IP based throttling
+ Object remoteIP = synContext.getProperty(REMOTE_ADDR);
+ if (remoteIP == null) {
+ throw new ThrottleException("IP address of the caller can not find - Currently only support caller-IP base access control" +
+ "- Thottling will not happen ");
+
+ } else {
+ ThrottleContext throttleContext
+ = throttle.getThrottleContext(ThrottleConstants.IP_BASED_THROTTLE_KEY);
+ if (throttleContext == null) {
+ throw new ThrottleException("Can not find a configuartion for IP Based Throttle");
+
+ }
+ AccessController accessControler;
+ try {
+ accessControler = AccessControllerFactory.createAccessControler(ThrottleConstants.IP_BASE);
+ boolean canAccess = accessControler.canAccess(throttleContext, remoteIP);
+ if (!canAccess) {
+ throw new SynapseException("Access has currently been denied by the IP_BASE throttle for IP :\t" + remoteIP);
+ }
+ return canAccess;
+ }
+ catch (ThrottleException e) {
+ return true;
+ }
+ }
+ }
+
+ protected void init(MessageContext synContext) {
+
+ boolean reCreate = false; // It is not need to recreate ,if property is not dyanamic
+ OMElement policyOmElement = null;
+
+ if (policyKey != null) {
+ Entry entry = synContext.getConfiguration().getEntryDefinition(policyKey);
+ if (entry == null) {
+ return;
+ }
+ Object entryValue = entry.getValue();
+
+ if (!(entryValue instanceof OMElement)) {
+ return;
+ }
+ // if entry is dynamic, need to check wheather updated or not
+ if ((!entry.isCached() || entry.isExpired())) {
+ reCreate = true;
+ }
+ policyOmElement = (OMElement) entryValue;
+ } else if (inLinePolicy != null){
+ policyOmElement = inLinePolicy;
+ }
+ if (policyOmElement == null) {
+ return;
+ }
+ if (!reCreate) {
+ //The first time creation
+ if (throttle == null) {
+ createThrottleMetaData(policyOmElement);
+ }
+ } else {
+ createThrottleMetaData(policyOmElement);
+ }
+
+ }
+
+ protected void createThrottleMetaData(OMElement policyOmElement) {
+
+ try {
+ throttle = ThrottlePolicyProcessor
+ .processPoclicy(PolicyEngine.getPolicy(policyOmElement));
+ }
+ catch (ThrottleException e) {
+
+ throw new SynapseException(e.getMessage());
+ }
+ }
+
+ public String getType() {
+ return "ThrottleMediator";
+ }
+
+
+ public String getPolicyKey() {
+ return policyKey;
+ }
+
+ public void setPolicyKey(String policyKey) {
+ this.policyKey = policyKey;
+ }
+
+ public OMElement getInLinePolicy() {
+ return inLinePolicy;
+ }
+
+ public void setInLinePolicy(OMElement inLinePolicy) {
+ this.inLinePolicy = inLinePolicy;
+ }
+ }
+}
Modified: webservices/synapse/trunk/java/modules/nhttp/src/org/apache/axis2/transport/nhttp/HttpCoreNIOSender.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/nhttp/src/org/apache/axis2/transport/nhttp/HttpCoreNIOSender.java?view=diff&rev=516340&r1=516339&r2=516340
==============================================================================
--- webservices/synapse/trunk/java/modules/nhttp/src/org/apache/axis2/transport/nhttp/HttpCoreNIOSender.java (original)
+++ webservices/synapse/trunk/java/modules/nhttp/src/org/apache/axis2/transport/nhttp/HttpCoreNIOSender.java Fri Mar 9 01:13:01 2007
@@ -245,8 +245,7 @@
if (conn == null) {
SessionRequest req = ioReactor.connect(
- new InetSocketAddress(url.getHost(), port), null, axis2Req);
- req.setCallback(sessionRequestCallback);
+ new InetSocketAddress(url.getHost(), port), null, axis2Req,sessionRequestCallback);
log.debug("A new connection established");
} else {
((ClientHandler) handler).submitRequest(conn, axis2Req);
@@ -368,6 +367,10 @@
public void timeout(SessionRequest request) {
handleError(request);
+ }
+
+ public void cancelled(SessionRequest sessionRequest) {
+
}
private void handleError(SessionRequest request) {
Modified: webservices/synapse/trunk/java/pom.xml
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/pom.xml?view=diff&rev=516340&r1=516339&r2=516340
==============================================================================
--- webservices/synapse/trunk/java/pom.xml (original)
+++ webservices/synapse/trunk/java/pom.xml Fri Mar 9 01:13:01 2007
@@ -431,6 +431,13 @@
<artifactId>jakarta-httpcore-niossl</artifactId>
<version>${jakarta.httpcore.nio.version}</version>
</dependency>
+ <!-- WSO2 Throttle -->
+ <dependency>
+ <groupId>org.wso2.throttle</groupId>
+ <artifactId>wso2throttle</artifactId>
+ <version>${wso2commons.version}</version>
+ <type>jar</type>
+ </dependency>
</dependencies>
@@ -486,7 +493,7 @@
<repository>
<id>wso2</id>
<name>wso2</name>
- <url>http://dist.wso2.org/maven/</url>
+ <url>http://dist.wso2.org/maven2/</url>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
@@ -566,6 +573,18 @@
</snapshots>
</repository>
<repository>
+ <id>WSO2 Maven2 Repository</id>
+ <name>WSO2 Maven2 Repository</name>
+ <layout>legacy</layout>
+ <url>http://dist.wso2.org/maven2/</url>
+ <releases>
+ <updatePolicy>never</updatePolicy>
+ </releases>
+ <snapshots>
+ <updatePolicy>interval:1440</updatePolicy>
+ </snapshots>
+ </repository>
+ <repository>
<id>Jibx</id>
<name>Jibx</name>
<layout>legacy</layout>
@@ -643,6 +662,8 @@
<!-- misc -->
<junit.version>3.8.2</junit.version>
<log4j.version>1.2.13</log4j.version>
+ <wso2commons.version>1.2-SNAPSHOT</wso2commons.version>
+
</properties>
<developers>
---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org