Author: hiranya
Date: Mon Aug 23 06:04:07 2010
New Revision: 987999

URL: http://svn.apache.org/viewvc?rev=987999&view=rev
Log:
Implemented the mediator factory


Added:
    
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteAction.java
    
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/
    
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/URLRewriteMediatorFactory.java
    synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/
    synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/mediator.xml
Modified:
    
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteRule.java
    
synapse/trunk/scratch/hiranya/urlrewrite/src/test/java/org/apache/synapse/mediators/URLRewriteTest.java

Added: 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteAction.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteAction.java?rev=987999&view=auto
==============================================================================
--- 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteAction.java
 (added)
+++ 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteAction.java
 Mon Aug 23 06:04:07 2010
@@ -0,0 +1,106 @@
+/*
+ *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights 
Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you 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;
+
+import org.apache.synapse.util.xpath.SynapseXPath;
+import org.apache.synapse.MessageContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+public class RewriteAction {
+
+    private static final Log log = LogFactory.getLog(RewriteAction.class);
+
+    private String value;
+    private SynapseXPath xpath;
+    private int fragmentIndex = URLRewriteMediator.FULL_URI;
+
+    public void execute(Object[] fragments, MessageContext messageContext) {
+        String result;
+        if (xpath != null) {
+            result = xpath.stringValueOf(messageContext);
+        } else {
+            result = value;
+        }
+
+        if (fragmentIndex == URLRewriteMediator.FULL_URI) {
+            try {
+                URI uri;
+                if (result != null) {
+                    uri = new URI(result);
+                    if (log.isDebugEnabled()) {
+                        log.debug("Setting the URI to: " + result);
+                    }
+                } else {
+                    uri = new URI("");
+                }
+
+                fragments[0] = uri.getScheme();
+                fragments[1] = uri.getUserInfo();
+                fragments[2] = uri.getHost();
+                fragments[3] = uri.getPort();
+                // The uri.getPath() return the empty string for empty URIs
+                // We are better off setting it to null
+                fragments[4] = "".equals(uri.getPath()) ? null : uri.getPath();
+                fragments[5] = uri.getQuery();
+                fragments[6] = uri.getFragment();
+
+            } catch (URISyntaxException e) {
+                return;
+            }
+        } else if (fragmentIndex == URLRewriteMediator.PORT) {
+            // When setting the port we must first convert the value into an 
integer
+            if (result != null) {
+                fragments[fragmentIndex] = Integer.parseInt(result);
+            } else {
+                fragments[fragmentIndex] = -1;
+            }
+        } else {
+            fragments[fragmentIndex] = result;
+        }
+    }
+
+    public int getFragmentIndex() {
+        return fragmentIndex;
+    }
+
+    public void setFragmentIndex(int fragmentIndex) {
+        this.fragmentIndex = fragmentIndex;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public SynapseXPath getXpath() {
+        return xpath;
+    }
+
+    public void setXpath(SynapseXPath xpath) {
+        this.xpath = xpath;
+    }
+}

Modified: 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteRule.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteRule.java?rev=987999&r1=987998&r2=987999&view=diff
==============================================================================
--- 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteRule.java
 (original)
+++ 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/RewriteRule.java
 Mon Aug 23 06:04:07 2010
@@ -22,25 +22,20 @@ package org.apache.synapse.mediators;
 import org.apache.synapse.commons.evaluators.Evaluator;
 import org.apache.synapse.commons.evaluators.EvaluatorContext;
 import org.apache.synapse.commons.evaluators.EvaluatorException;
-import org.apache.synapse.util.xpath.SynapseXPath;
 import org.apache.synapse.MessageContext;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import java.util.Map;
-import java.net.URI;
-import java.net.URISyntaxException;
+import java.util.List;
+import java.util.ArrayList;
 
 public class RewriteRule {
 
     private static final Log log = LogFactory.getLog(RewriteRule.class);
 
     private Evaluator condition;
-
-    private String value;
-    private SynapseXPath xpath;
-
-    private int fragmentIndex = URLRewriteMediator.FULL_URI;
+    private List<RewriteAction> actions = new ArrayList<RewriteAction>();
 
     public void rewrite(Object[] fragments, MessageContext messageContext, 
String uriString,
                         Map<String,String> headers) {
@@ -68,47 +63,8 @@ public class RewriteRule {
             }
         }
 
-        String result;
-        if (xpath != null) {
-            result = xpath.stringValueOf(messageContext);
-        } else {
-            result = value;
-        }
-
-        if (fragmentIndex == URLRewriteMediator.FULL_URI) {
-            try {
-                URI uri;
-                if (result != null) {
-                    uri = new URI(result);
-                    if (log.isDebugEnabled()) {
-                        log.debug("Setting the URI to: " + result);
-                    }
-                } else {
-                    uri = new URI("");
-                }
-
-                fragments[0] = uri.getScheme();
-                fragments[1] = uri.getUserInfo();
-                fragments[2] = uri.getHost();
-                fragments[3] = uri.getPort();
-                // The uri.getPath() return the empty string for empty URIs
-                // We are better off setting it to null
-                fragments[4] = "".equals(uri.getPath()) ? null : uri.getPath();
-                fragments[5] = uri.getQuery();
-                fragments[6] = uri.getFragment();
-
-            } catch (URISyntaxException e) {
-                return;
-            }
-        } else if (fragmentIndex == URLRewriteMediator.PORT) {
-            // When setting the port we must first convert the value into an 
integer
-            if (result != null) {
-                fragments[fragmentIndex] = Integer.parseInt(result);
-            } else {
-                fragments[fragmentIndex] = -1;
-            }
-        } else {
-            fragments[fragmentIndex] = result;
+        for (RewriteAction action : actions) {
+            action.execute(fragments, messageContext);
         }
     }
 
@@ -120,27 +76,7 @@ public class RewriteRule {
         this.condition = condition;
     }
 
-    public int getFragmentIndex() {
-        return fragmentIndex;
-    }
-
-    public void setFragmentIndex(int fragmentIndex) {
-        this.fragmentIndex = fragmentIndex;
-    }
-
-    public String getValue() {
-        return value;
-    }
-
-    public void setValue(String value) {
-        this.value = value;
-    }
-
-    public SynapseXPath getXpath() {
-        return xpath;
-    }
-
-    public void setXpath(SynapseXPath xpath) {
-        this.xpath = xpath;
+    public void addRewriteAction(RewriteAction action) {
+        actions.add(action);
     }
 }

Added: 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/URLRewriteMediatorFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/URLRewriteMediatorFactory.java?rev=987999&view=auto
==============================================================================
--- 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/URLRewriteMediatorFactory.java
 (added)
+++ 
synapse/trunk/scratch/hiranya/urlrewrite/src/main/java/org/apache/synapse/mediators/xml/URLRewriteMediatorFactory.java
 Mon Aug 23 06:04:07 2010
@@ -0,0 +1,133 @@
+/*
+ *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights 
Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you 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.xml;
+
+import org.apache.synapse.config.xml.AbstractMediatorFactory;
+import org.apache.synapse.config.xml.XMLConfigConstants;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.util.xpath.SynapseXPath;
+import org.apache.synapse.commons.evaluators.config.EvaluatorFactoryFinder;
+import org.apache.synapse.commons.evaluators.Evaluator;
+import org.apache.synapse.commons.evaluators.EvaluatorException;
+import org.apache.synapse.mediators.URLRewriteMediator;
+import org.apache.synapse.mediators.RewriteRule;
+import org.apache.synapse.mediators.RewriteAction;
+import org.apache.axiom.om.OMElement;
+import org.jaxen.JaxenException;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+
+public class URLRewriteMediatorFactory extends AbstractMediatorFactory {
+
+    private static final QName REWRITE_Q    = new 
QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "rewrite");
+
+    public Mediator createMediator(OMElement element) {
+        Iterator rules = element.getChildrenWithName(new QName("rule"));
+        URLRewriteMediator mediator = new URLRewriteMediator();
+        while (rules.hasNext()) {
+            RewriteRule rule = parseRule((OMElement) rules.next());
+            mediator.addRule(rule);
+        }
+
+        return mediator;
+    }
+
+    private RewriteRule parseRule(OMElement ruleElement) {
+        OMElement conditionElt = ruleElement.getFirstChildWithName(new QName(
+                SynapseConstants.SYNAPSE_NAMESPACE, "condition"));
+        Iterator actions = ruleElement.getChildrenWithName(new QName(
+                SynapseConstants.SYNAPSE_NAMESPACE, "action"));
+
+        if (actions == null) {
+            handleException("At least one rewrite action is required");
+        }
+
+        RewriteRule rule = new RewriteRule();
+        while (actions.hasNext()) {
+            rule.addRewriteAction(parseAction((OMElement) actions.next()));
+        }
+
+        if (conditionElt != null) {
+            OMElement child = conditionElt.getFirstElement();
+            if (child != null) {
+                try {
+                    Evaluator eval = 
EvaluatorFactoryFinder.getInstance().getEvaluator(child);
+                    rule.setCondition(eval);
+                } catch (EvaluatorException e) {
+                    handleException("Error while parsing the evaluator 
configuration", e);
+                }
+            }
+        }
+
+        return rule;
+    }
+
+    private RewriteAction parseAction(OMElement actionElt) {
+        String value = actionElt.getAttributeValue(new QName("value"));
+        String xpath = actionElt.getAttributeValue(new QName("xpath"));
+
+        if (value == null && xpath == null) {
+            handleException("value or xpath attribute is required on the 
action element");
+        }
+
+        RewriteAction action = new RewriteAction();
+        if (xpath != null) {
+            try {
+                action.setXpath(new SynapseXPath(xpath));
+            } catch (JaxenException e) {
+                handleException("Error while parsign the XPath expression: " + 
xpath, e);
+            }
+        } else if (value != null) {
+            action.setValue(value);
+        }
+
+        String fragment = actionElt.getAttributeValue(new QName("fragment"));
+        if (fragment != null) {
+            if ("protocol".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.PROTOCOL);
+            } else if ("user".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.USER_INFO);
+            } else if ("host".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.HOST);
+            } else if ("port".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.PORT);
+            } else if ("path".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.PATH);
+            } else if ("query".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.QUERY);
+            } else if ("ref".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.REF);
+            } else if ("full".equals(fragment)) {
+                action.setFragmentIndex(URLRewriteMediator.FULL_URI);
+            } else {
+                handleException("Unknown URL fragment name: " + fragment);
+            }
+        } else {
+            action.setFragmentIndex(URLRewriteMediator.FULL_URI);
+        }
+        return action;
+    }
+
+    public QName getTagQName() {
+        return REWRITE_Q;
+    }
+}

Modified: 
synapse/trunk/scratch/hiranya/urlrewrite/src/test/java/org/apache/synapse/mediators/URLRewriteTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/urlrewrite/src/test/java/org/apache/synapse/mediators/URLRewriteTest.java?rev=987999&r1=987998&r2=987999&view=diff
==============================================================================
--- 
synapse/trunk/scratch/hiranya/urlrewrite/src/test/java/org/apache/synapse/mediators/URLRewriteTest.java
 (original)
+++ 
synapse/trunk/scratch/hiranya/urlrewrite/src/test/java/org/apache/synapse/mediators/URLRewriteTest.java
 Mon Aug 23 06:04:07 2010
@@ -21,6 +21,8 @@ package org.apache.synapse.mediators;
 
 import junit.framework.TestCase;
 import org.apache.synapse.MessageContext;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.mediators.xml.URLRewriteMediatorFactory;
 import org.apache.synapse.util.xpath.SynapseXPath;
 import org.apache.synapse.commons.evaluators.EqualEvaluator;
 import org.apache.synapse.commons.evaluators.source.URLTextRetriever;
@@ -28,6 +30,8 @@ import org.apache.synapse.config.Synapse
 import org.apache.synapse.core.axis2.Axis2MessageContext;
 import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
 import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axis2.addressing.EndpointReference;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -53,6 +57,32 @@ public class URLRewriteTest extends Test
         }
     }
 
+    public void testMediateWithFactory() throws Exception {
+        String xml = "<rewrite 
xmlns=\"http://synapse.apache.org/ns/2010/04/configuration\";>" +
+                "    <rule>" +
+                "        <condition>" +
+                "            <match type=\"url\" fragment=\"host\" 
regex=\"wso2.org\"/>" +
+                "        </condition>" +
+                "        <action fragment=\"host\" value=\"wso2.com\"/>" +
+                "        <action fragment=\"port\" value=\"9443\"/>" +
+                "        <action fragment=\"protocol\" value=\"https\"/>" +
+                "    </rule>" +
+                "</rewrite>";
+        URLRewriteMediatorFactory fac = new URLRewriteMediatorFactory();
+        Mediator mediator = fac.createMediator(AXIOMUtil.stringToOM(xml));
+
+        org.apache.axis2.context.MessageContext mc =
+                new org.apache.axis2.context.MessageContext();
+        SynapseConfiguration config = new SynapseConfiguration();
+        SynapseEnvironment env = new Axis2SynapseEnvironment(config);
+        MessageContext synMc = new Axis2MessageContext(mc, config, env);
+        synMc.setProperty("prop1", "ref1");
+        synMc.setTo(new 
EndpointReference("http://wso2.org:9763/services/MyService";));
+
+        mediator.mediate(synMc);
+        System.out.println(synMc.getTo().getAddress());
+    }
+
     public void testMediate() throws Exception {
         org.apache.axis2.context.MessageContext mc =
                 new org.apache.axis2.context.MessageContext();
@@ -64,12 +94,16 @@ public class URLRewriteTest extends Test
         URLRewriteMediator mediator = new URLRewriteMediator();
 
         RewriteRule r1 = new RewriteRule();
-        r1.setValue("http://localhost:8080/";);
+        RewriteAction a1 = new RewriteAction();
+        a1.setValue("http://localhost:8080/";);
+        r1.addRewriteAction(a1);
         mediator.addRule(r1);
 
         RewriteRule r2 = new RewriteRule();
-        r2.setValue("/services/TestService");
-        r2.setFragmentIndex(URLRewriteMediator.PATH);
+        RewriteAction a2 = new RewriteAction();
+        a2.setValue("/services/TestService");
+        a2.setFragmentIndex(URLRewriteMediator.PATH);
+        r2.addRewriteAction(a2);
         mediator.addRule(r2);
 
         EqualEvaluator eval = new EqualEvaluator();
@@ -78,9 +112,11 @@ public class URLRewriteTest extends Test
         eval.setTextRetriever(txtRtvr);
         eval.setValue("8080");
         RewriteRule r3 = new RewriteRule();
+        RewriteAction a3 = new RewriteAction();
         r3.setCondition(eval);
-        r3.setXpath(new SynapseXPath("get-property('prop1')"));
-        r3.setFragmentIndex(URLRewriteMediator.REF);
+        a3.setXpath(new SynapseXPath("get-property('prop1')"));
+        a3.setFragmentIndex(URLRewriteMediator.REF);
+        r3.addRewriteAction(a3);
         mediator.addRule(r3);
 
         mediator.mediate(synMc);

Added: synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/mediator.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/mediator.xml?rev=987999&view=auto
==============================================================================
--- synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/mediator.xml 
(added)
+++ synapse/trunk/scratch/hiranya/urlrewrite/src/test/resources/mediator.xml 
Mon Aug 23 06:04:07 2010
@@ -0,0 +1,8 @@
+<rewrite xmlns="http://synapse.apache.org/ns/2010/04/configuration";>
+    <rule>
+        <condition>
+            <match type="url" fragment="host" regex="foo"/>
+        </condition>
+        <action fragment="host" value="bar"/>            
+    </rule>
+</rewrite>
\ No newline at end of file


Reply via email to