Author: uswick
Date: Thu Mar 22 11:07:30 2012
New Revision: 1303731

URL: http://svn.apache.org/viewvc?rev=1303731&view=rev
Log:
committing API resource versioning support..Currently only URL based versoning 
is included..

Added:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategyFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategySerializer.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/AbstractVersionStrategy.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/DefaultStrategy.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/URLBasedVersionStrategy.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/VersionStrategy.java
Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APIFactory.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APISerializer.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/API.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/RESTConstants.java
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APIFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APIFactory.java?rev=1303731&r1=1303730&r2=1303731&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APIFactory.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APIFactory.java
 Thu Mar 22 11:07:30 2012
@@ -27,6 +27,7 @@ import org.apache.synapse.SynapseExcepti
 import org.apache.synapse.config.xml.XMLConfigConstants;
 import org.apache.synapse.rest.API;
 import org.apache.synapse.rest.Handler;
+import org.apache.synapse.rest.version.VersionStrategy;
 
 import javax.xml.namespace.QName;
 import java.util.Iterator;
@@ -53,6 +54,10 @@ public class APIFactory {
             api.setHost(hostAtt.getAttributeValue());
         }
 
+        VersionStrategy vStrategy = 
VersionStrategyFactory.createVersioningStrategy(api, apiElt);
+
+        api.setVersionStrategy(vStrategy);
+
         OMAttribute portAtt = apiElt.getAttribute(new QName("port"));
         if (portAtt != null && !"".equals(portAtt.getAttributeValue())) {
             api.setPort(Integer.parseInt(portAtt.getAttributeValue()));

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APISerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APISerializer.java?rev=1303731&r1=1303730&r2=1303731&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APISerializer.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/APISerializer.java
 Thu Mar 22 11:07:30 2012
@@ -35,6 +35,8 @@ public class APISerializer {
         OMElement apiElt = fac.createOMElement("api", 
SynapseConstants.SYNAPSE_OMNAMESPACE);
         apiElt.addAttribute("name", api.getName(), null);
         apiElt.addAttribute("context", api.getContext(), null);
+
+        
VersionStrategySerializer.serializeVersioningStrategy(api.getVersionStrategy(), 
apiElt) ;
         if (api.getHost() != null) {
             apiElt.addAttribute("hostname", api.getHost(), null);
         }

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategyFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategyFactory.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategyFactory.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategyFactory.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,83 @@
+/*
+*  Copyright (c) 2005-2010, 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.config.xml.rest;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.rest.API;
+import org.apache.synapse.rest.version.DefaultStrategy;
+import org.apache.synapse.rest.version.URLBasedVersionStrategy;
+import org.apache.synapse.rest.version.VersionStrategy;
+
+import javax.xml.namespace.QName;
+
+public class VersionStrategyFactory {
+    private static final Log log = 
LogFactory.getLog(VersionStrategyFactory.class);
+    public static final String TYPE_URL = "url";
+    public static final String TYPE_NULL = "";
+
+    public static VersionStrategy createVersioningStrategy(API api, OMElement 
apiElt) {
+        OMAttribute versionAtt = apiElt.getAttribute(new QName("version"));
+        if (versionAtt == null || "".equals(versionAtt.getAttributeValue())) {
+//            handleException("Attribute 'version' is required for an API 
definition");
+        }
+
+        OMAttribute versionTypeAtt = apiElt.getAttribute(new 
QName("version-type"));
+        if (versionAtt != null && (versionTypeAtt == null || 
"".equals(versionTypeAtt.getAttributeValue()))) {
+            handleException("Attribute 'version-type' is required for an API 
definition");
+        }
+
+        OMAttribute versionParamAtt = apiElt.getAttribute(new 
QName("version-param"));
+
+        String version = "";
+        if (versionAtt != null) {
+            version = versionAtt.getAttributeValue();
+        }
+        String versionType = "";
+        if (versionTypeAtt != null) {
+            versionType = versionTypeAtt.getAttributeValue();
+        }
+        String versionParam = "";
+        if (versionParamAtt != null) {
+            versionParam = versionParamAtt.getAttributeValue();
+        }
+
+        return selectVersionStrategy(api, version, versionType, versionParam);
+    }
+
+    private static VersionStrategy selectVersionStrategy(API api, String 
version, String versionType,
+                                                         String versionParam) {
+        if (versionType != null && TYPE_URL.equals(versionType.trim())) {
+            return new URLBasedVersionStrategy(api, version, versionParam);
+        }
+        if (versionType == null || TYPE_NULL.equals(versionType.trim())) {
+            //no versioning supported here
+            //intended for backward compatability with API's
+            return new DefaultStrategy(api);
+        }
+        return null;
+    }
+
+    private static void handleException(String msg) {
+        log.error(msg);
+        throw new SynapseException(msg);
+    }
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategySerializer.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategySerializer.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategySerializer.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/rest/VersionStrategySerializer.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,52 @@
+/*
+*  Copyright (c) 2005-2010, 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.config.xml.rest;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.rest.API;
+import org.apache.synapse.rest.version.DefaultStrategy;
+import org.apache.synapse.rest.version.URLBasedVersionStrategy;
+import org.apache.synapse.rest.version.VersionStrategy;
+
+import javax.xml.namespace.QName;
+
+public class VersionStrategySerializer {
+    private static final Log log = 
LogFactory.getLog(VersionStrategySerializer.class);
+
+    public static OMElement serializeVersioningStrategy(VersionStrategy 
strategy, OMElement apiElement) {
+        assert strategy != null;
+
+        if (strategy.getVersion() != null && 
!"".equals(strategy.getVersion())) {
+            apiElement.addAttribute("version", strategy.getVersion(), null);
+        }
+        if (strategy.getVersionType() != null && 
!"".equals(strategy.getVersionType())) {
+            apiElement.addAttribute("version-type", strategy.getVersionType(), 
null);
+        }
+        if (strategy.getVersionParam() != null && 
!"".equals(strategy.getVersionParam())) {
+            apiElement.addAttribute("version-param", 
strategy.getVersionParam(), null);
+        }
+        return apiElement;
+    }
+
+}

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/API.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/API.java?rev=1303731&r1=1303730&r2=1303731&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/API.java 
(original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/API.java 
Thu Mar 22 11:07:30 2012
@@ -27,6 +27,9 @@ import org.apache.synapse.core.SynapseEn
 import org.apache.synapse.core.axis2.Axis2MessageContext;
 import org.apache.synapse.rest.dispatch.DispatcherHelper;
 import org.apache.synapse.rest.dispatch.RESTDispatcher;
+import org.apache.synapse.rest.version.DefaultStrategy;
+import org.apache.synapse.rest.version.VersionStrategy;
+import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
 import org.apache.synapse.transport.nhttp.NhttpConstants;
 
 import java.util.*;
@@ -39,6 +42,8 @@ public class API extends AbstractRESTPro
     private Map<String,Resource> resources = new 
LinkedHashMap<String,Resource>();
     private List<Handler> handlers = new ArrayList<Handler>();
 
+    private VersionStrategy versionStrategy = new DefaultStrategy(this);
+
     private String fileName;
 
     public API(String name, String context) {
@@ -49,7 +54,15 @@ public class API extends AbstractRESTPro
         this.context = RESTUtils.trimTrailingSlashes(context);
     }
 
+    /**
+     * get Fully Qualified name of this API
+     * @return returns the key combiantion for API NAME + VERSION
+     */
     public String getName() {
+        //check if a versioning strategy exists
+        if (versionStrategy.getVersion() != null && 
!"".equals(versionStrategy.getVersion()) ) {
+            return name + ":v" +versionStrategy.getVersion();
+        }
         return name;
     }
 
@@ -131,7 +144,8 @@ public class API extends AbstractRESTPro
     boolean canProcess(MessageContext synCtx) {
         if (synCtx.isResponse()) {
             String apiName = (String) 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
-            if (!name.equals(apiName)) {
+            String version = (String) 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
+            if (!name.equals(apiName) && 
!versionStrategy.getVersion().equals(version)) {
                 return false;
             }
         } else {
@@ -144,6 +158,10 @@ public class API extends AbstractRESTPro
                 return false;
             }
 
+            if(!versionStrategy.isMatchingVersion(synCtx)){
+                return false;
+            }
+
             if (host != null || port != -1) {
                 org.apache.axis2.context.MessageContext msgCtx =
                         ((Axis2MessageContext) 
synCtx).getAxis2MessageContext();
@@ -183,6 +201,10 @@ public class API extends AbstractRESTPro
                     "API: " + name);
         }
 
+        synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, getName());
+        synCtx.setProperty(RESTConstants.SYNAPSE_REST_API_VERSION, 
versionStrategy.getVersion());
+        synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);
+
         for (Handler handler : handlers) {
             if (log.isDebugEnabled()) {
                 log.debug("Processing message with ID: " + 
synCtx.getMessageID() + " through " +
@@ -215,10 +237,15 @@ public class API extends AbstractRESTPro
         }
 
 
-        synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, name);
-        synCtx.setProperty(RESTConstants.REST_API_CONTEXT, context);
         String path = RESTUtils.getFullRequestPath(synCtx);
-        String subPath = path.substring(context.length());
+        String subPath = null;
+        if 
(versionStrategy.getVersionType().equals(VersionStrategyFactory.TYPE_URL)) {
+            //for URL based
+            //request --> http://{host:port}/context/version/path/to/resource
+            subPath = path.substring(context.length() + 
versionStrategy.getVersion().length() + 1);
+        } else {
+            subPath = path.substring(context.length());
+        }
         if ("".equals(subPath)) {
             subPath = "/";
         }
@@ -308,4 +335,12 @@ public class API extends AbstractRESTPro
             resource.destroy();
         }
     }
+
+    public VersionStrategy getVersionStrategy() {
+        return versionStrategy;
+    }
+
+    public void setVersionStrategy(VersionStrategy versionStrategy) {
+        this.versionStrategy = versionStrategy;
+    }
 }

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/RESTConstants.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/RESTConstants.java?rev=1303731&r1=1303730&r2=1303731&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/RESTConstants.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/RESTConstants.java
 Thu Mar 22 11:07:30 2012
@@ -39,6 +39,7 @@ public class RESTConstants {
 
     public static final String SYNAPSE_RESOURCE = "SYNAPSE_RESOURCE";
     public static final String SYNAPSE_REST_API = "SYNAPSE_REST_API";
+    public static final String SYNAPSE_REST_API_VERSION = 
"SYNAPSE_REST_API_VERSION";
 
     public static final String REST_API_CONTEXT = "REST_API_CONTEXT";
     public static final String REST_URL_PREFIX = "REST_URL_PREFIX";

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/AbstractVersionStrategy.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/AbstractVersionStrategy.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/AbstractVersionStrategy.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/AbstractVersionStrategy.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,47 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.rest.version;
+
+import org.apache.synapse.rest.API;
+
+public abstract class AbstractVersionStrategy implements VersionStrategy {
+    protected API api;
+    protected String version;
+    protected String versionType ;
+
+    public AbstractVersionStrategy(API api, String version, String 
versionType) {
+        this.api = api;
+        this.version = version;
+        this.versionType = versionType;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public API getAPI(){
+        return api;
+    }
+
+    public String getVersionType(){
+        return versionType;
+    }
+
+
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/DefaultStrategy.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/DefaultStrategy.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/DefaultStrategy.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/DefaultStrategy.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,39 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.rest.version;
+
+import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
+import org.apache.synapse.rest.API;
+
+public class DefaultStrategy extends  AbstractVersionStrategy{
+
+    public DefaultStrategy(API api) {
+        super(api, "", VersionStrategyFactory.TYPE_NULL);
+    }
+
+    public boolean isMatchingVersion(Object versionInfoObj) {
+        return true;
+    }
+
+    public String getVersionParam() {
+        return "";
+    }
+
+
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/URLBasedVersionStrategy.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/URLBasedVersionStrategy.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/URLBasedVersionStrategy.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/URLBasedVersionStrategy.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,95 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.rest.version;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
+import org.apache.synapse.rest.API;
+import org.apache.synapse.rest.RESTUtils;
+
+public class URLBasedVersionStrategy extends AbstractVersionStrategy {
+    String versionParam;
+
+    public URLBasedVersionStrategy(API api, String version, String 
versionParam) {
+        super(api, version, VersionStrategyFactory.TYPE_URL);
+        this.versionParam = versionParam;
+    }
+
+    public boolean isMatchingVersion(Object versionInfoObj) {
+        MessageContext msgContext = (MessageContext) versionInfoObj;
+        String path = RESTUtils.getFullRequestPath(msgContext);
+
+        String context = getAPI().getContext();
+        String pathStringAfterContext = path.substring(context.length());
+
+        //check if context can be invalid for path url and avoid matching a 
false version string
+        if (pathStringAfterContext != null && 
!pathStringAfterContext.startsWith("/")) {
+            return false;
+        }
+
+        //path after context will be starting from a "/" and compute index for 
next immediate "/"
+        int indexOfNextSlash = 
pathStringAfterContext.substring(1).indexOf("/");
+
+        String incomingVersionStr = null;
+        if (indexOfNextSlash != -1) {
+            incomingVersionStr = pathStringAfterContext.substring(1, 
indexOfNextSlash + 1);
+        } else {
+            String versionStr = pathStringAfterContext.substring(1);
+            //assume no slash at the end ie:-GET /context/1.0.0
+            incomingVersionStr = versionStr;
+            //check special case
+            if (versionStr.contains("?")) {
+                incomingVersionStr = 
versionStr.substring(0,versionStr.indexOf("?"));
+            }
+        }
+
+        return version.equals(incomingVersionStr);
+    }
+
+    public String getVersionParam() {
+        return versionParam;
+    }
+
+/*
+    public static void main(String[] args) {
+        String version = "1.0.0";
+//        String version = "1.0.1";
+
+//        String path = "/test/1.0.1/foo/abc/";
+//        String path = "/test/1.0/foo/abc/";
+//        String path = "/test/1.0/foo/abc/abc='asasa'";
+        String path = "/test11.0.0/1.0/foo/abc/abc='asasa'";
+
+//        String context = "/test";
+//        String context = "/test123";
+        String context = "/test";
+        String pathStringAfterContext = path.substring(context.length());
+
+        System.out.println("pathStringAfterContext : " + 
pathStringAfterContext);
+
+        int indexOfNextSlash = 
pathStringAfterContext.substring(1).indexOf("/");
+        System.out.println(indexOfNextSlash);
+        String incomingVersionStr = pathStringAfterContext.substring(1, 
indexOfNextSlash + 1);
+        System.out.println("incomingVersionStr : " + incomingVersionStr);
+
+        System.out.println(version.equals(incomingVersionStr));
+    }
+*/
+
+}

Added: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/VersionStrategy.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/VersionStrategy.java?rev=1303731&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/VersionStrategy.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/version/VersionStrategy.java
 Thu Mar 22 11:07:30 2012
@@ -0,0 +1,53 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.rest.version;
+
+/**
+ * This class is responsible for version delegation for API resources
+ */
+public interface VersionStrategy {
+
+    /**
+     * check if version matches from the version ifo object.
+     * @param versionInfoObj object that can be used to extract data about the 
version
+     * @return
+     */
+    public boolean isMatchingVersion(Object versionInfoObj);
+
+    /**
+     * get the assigned version for the delegated object
+     * @return string representation for the version
+     */
+    public String getVersion();
+
+    /**
+     * returns version type information. ie:- version can be embedded in 
request URL , HTTP header
+     * , etc
+     * @return version type String
+     */
+    public String getVersionType();
+
+    /**
+     * returns type information ie:- location regex/string that can be used t 
extract version..
+     * @return version param
+     */
+    public String getVersionParam();
+
+
+}

Modified: 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java?rev=1303731&r1=1303730&r2=1303731&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java
 Thu Mar 22 11:07:30 2012
@@ -22,10 +22,13 @@ package org.apache.synapse.rest;
 import org.apache.http.protocol.HTTP;
 import org.apache.synapse.MessageContext;
 import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.rest.version.DefaultStrategy;
+import org.apache.synapse.rest.version.URLBasedVersionStrategy;
 
 public class APIDispatcherTest extends RESTMediationTestCase {
 
     private static final String TEST_API = "TestAPI";
+    private static final String TEST_API_VERSION = "1.0.0";
 
     public void testGeneralAPIDispatch() throws Exception {
         API api = new API(TEST_API, "/");
@@ -175,4 +178,105 @@ public class APIDispatcherTest extends R
         assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
     }
 
+    public void testAPIDefaultVersionBasedDispatch() throws Exception {
+        API api = new API(TEST_API, "/test");
+        api.setVersionStrategy(new DefaultStrategy(api));
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        // Messages with '/test' context should ne dispatched
+        MessageContext synCtx = getMessageContext(synapseConfig, false, 
"/test", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals("",synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+        synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals("",synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", 
"GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals("",synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+        synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals("",synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        // Messages WITHOUT the '/test' context should NOT be dispatched
+        synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", 
"GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test1/bar?a=5", 
"GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+    }
+
+    public void testAPIURLVersionBasedDispatch() throws Exception {
+        API api = new API(TEST_API, "/test");
+        api.setVersionStrategy(new 
URLBasedVersionStrategy(api,TEST_API_VERSION,null));
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        // Messages with '/test' context should NOT be dispatched
+        MessageContext synCtx = getMessageContext(synapseConfig, false, 
"/test/", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0", "GET");
+        handler.process(synCtx);
+//        System.out.println(api.getName() + " : " + 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertEquals(api.getName(), 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals(TEST_API_VERSION,synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/", 
"GET");
+        handler.process(synCtx);
+//        System.out.println(api.getName() + " : " + 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertEquals(api.getName(), 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals(TEST_API_VERSION,synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, 
"/test/1.0.0/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals(api.getName(), 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals(TEST_API_VERSION,synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+//        
System.out.println(synCtx.getProperty(RESTConstants.REST_SUB_REQUEST_PATH));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", 
"GET");
+        handler.process(synCtx);
+//        
System.out.println(synCtx.getProperty(RESTConstants.REST_SUB_REQUEST_PATH));
+        assertEquals(api.getName(), 
synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        
assertEquals(TEST_API_VERSION,synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        // Messages WITHOUT the '/test' context should NOT be dispatched
+        synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", 
"GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        // Messages WITHOUT the '/test' context and proper version should NOT 
be dispatched
+        synCtx = getMessageContext(synapseConfig, false, 
"/test/1.0.1/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, 
"/test/2.0/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+
+        synCtx = getMessageContext(synapseConfig, false, 
"/test/2.0.0.0/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
+    }
+
+
+
 }
\ No newline at end of file


Reply via email to