You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by hi...@apache.org on 2011/12/14 13:24:38 UTC

svn commit: r1214211 [2/3] - in /synapse/trunk/java: ./ modules/core/ modules/core/src/main/java/org/apache/synapse/config/ modules/core/src/main/java/org/apache/synapse/config/xml/ modules/core/src/main/java/org/apache/synapse/config/xml/rest/ modules...

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/Resource.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/Resource.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/Resource.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/Resource.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,427 @@
+/*
+ *  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;
+
+import org.apache.axiom.util.UIDGenerator;
+import org.apache.axis2.Constants;
+import org.apache.http.HttpHeaders;
+import org.apache.http.protocol.HTTP;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.core.axis2.Axis2Sender;
+import org.apache.synapse.mediators.MediatorFaultHandler;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.synapse.rest.dispatch.DispatcherHelper;
+import org.apache.synapse.transport.nhttp.NhttpConstants;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class Resource extends AbstractRESTProcessor implements ManagedLifecycle {
+
+    /**
+     * List of HTTP methods applicable on this method. Empty list means all methods
+     * are applicable.
+     */
+    private Set<String> methods = new HashSet<String>(4);
+
+    private String contentType;
+
+    private String userAgent;
+
+    private int protocol = RESTConstants.PROTOCOL_HTTP_AND_HTTPS;
+
+    /**
+     * In-lined sequence to be executed upon receiving messages
+     */
+    private SequenceMediator inSequence;
+
+    private SequenceMediator outSequence;
+
+    private SequenceMediator faultSequence;
+
+    /**
+     * Identifier of the sequence to be executed upon receiving a message
+     */
+    private String inSequenceKey;
+
+    private String outSequenceKey;
+
+    private String faultSequenceKey;
+
+    /**
+     * DispatcherHelper instance which is  used to determine whether a particular resource
+     * should be dispatched to this resource or not
+     */
+    private DispatcherHelper dispatcherHelper;
+
+    public Resource() {
+        super(UIDGenerator.generateUID());
+    }
+
+    protected String getName() {
+        return name;
+    }
+
+    public SequenceMediator getInSequence() {
+        return inSequence;
+    }
+
+    public void setInSequence(SequenceMediator inSequence) {
+        this.inSequence = inSequence;
+    }
+
+    public SequenceMediator getOutSequence() {
+        return outSequence;
+    }
+
+    public void setOutSequence(SequenceMediator outSequence) {
+        this.outSequence = outSequence;
+    }
+
+    public String getInSequenceKey() {
+        return inSequenceKey;
+    }
+
+    public void setInSequenceKey(String inSequenceKey) {
+        this.inSequenceKey = inSequenceKey;
+    }
+
+    public String getOutSequenceKey() {
+        return outSequenceKey;
+    }
+
+    public void setOutSequenceKey(String outSequenceKey) {
+        this.outSequenceKey = outSequenceKey;
+    }
+
+    public SequenceMediator getFaultSequence() {
+        return faultSequence;
+    }
+
+    public void setFaultSequence(SequenceMediator faultSequence) {
+        this.faultSequence = faultSequence;
+    }
+
+    public String getFaultSequenceKey() {
+        return faultSequenceKey;
+    }
+
+    public void setFaultSequenceKey(String faultSequenceKey) {
+        this.faultSequenceKey = faultSequenceKey;
+    }
+
+    public boolean addMethod(String method) {
+        for (RESTConstants.METHODS allowedMethod : RESTConstants.METHODS.values()) {
+            if (allowedMethod.name().equals(method)) {
+                methods.add(method);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public String[] getMethods() {
+        return methods.toArray(new String[methods.size()]);
+    }
+
+    public DispatcherHelper getDispatcherHelper() {
+        return dispatcherHelper;
+    }
+
+    public void setDispatcherHelper(DispatcherHelper dispatcherHelper) {
+        this.dispatcherHelper = dispatcherHelper;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public void setContentType(String contentType) {
+        if (contentType.indexOf('/') == -1 || contentType.split("/").length != 2) {
+            throw new SynapseException("Invalid content type: " + contentType);
+        }
+        this.contentType = contentType;
+    }
+
+    public String getUserAgent() {
+        return userAgent;
+    }
+
+    public void setUserAgent(String userAgent) {
+        this.userAgent = userAgent;
+    }
+
+    public int getProtocol() {
+        return protocol;
+    }
+
+    public void setProtocol(int protocol) {
+        this.protocol = protocol;
+    }
+
+    @Override
+    boolean canProcess(MessageContext synCtx) {
+        if (synCtx.isResponse()) {
+            return true;
+        }
+
+        org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).
+                getAxis2MessageContext();
+        if (protocol == RESTConstants.PROTOCOL_HTTP_ONLY &&
+                !Constants.TRANSPORT_HTTP.equals(msgCtx.getIncomingTransportName())) {
+            if (log.isDebugEnabled()) {
+                log.debug("Protocol information does not match - Expected HTTP");
+            }
+            return false;
+        }
+
+        if (protocol == RESTConstants.PROTOCOL_HTTPS_ONLY &&
+                !Constants.TRANSPORT_HTTPS.equals(msgCtx.getIncomingTransportName())) {
+            if (log.isDebugEnabled()) {
+                log.debug("Protocol information does not match - Expected HTTPS");
+            }
+            return false;
+        }
+
+        String method = (String) msgCtx.getProperty(Constants.Configuration.HTTP_METHOD);
+        synCtx.setProperty(RESTConstants.REST_METHOD, method);
+
+        if (RESTConstants.METHOD_OPTIONS.equals(method)) {
+            return true; // OPTIONS requests are always welcome
+        } else if (!methods.isEmpty()) {
+            if (!methods.contains(method)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("HTTP method does not match");
+                }
+                return false;
+            }
+        }
+
+        Map transportHeaders = (Map) msgCtx.getProperty(
+                org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
+        if ((contentType != null || userAgent != null) && transportHeaders == null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Transport headers not available on the message");
+            }
+            return false;
+        }
+
+        boolean hasPayload = !Boolean.TRUE.equals(msgCtx.getProperty(NhttpConstants.NO_ENTITY_BODY));
+        if (contentType != null && hasPayload) {
+            String type = (String) transportHeaders.get(HTTP.CONTENT_TYPE);
+            if (!contentType.equals(type)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Content type does not match - Expected: " + contentType + ", " +
+                            "Found: " + type);
+                }
+                return false;
+            }
+        }
+
+        if (userAgent != null) {
+            String agent = (String) transportHeaders.get(HTTP.USER_AGENT);
+            if (agent == null || !agent.matches(this.userAgent)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("User agent does not match - Expected: " + userAgent + ", " +
+                            "Found: " + agent);
+                }
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    void process(MessageContext synCtx) {
+        if (log.isDebugEnabled()) {
+            log.debug("Processing message with ID: " + synCtx.getMessageID() + " through the " +
+                    "resource: " + name);
+        }
+
+        if (!synCtx.isResponse()) {
+            String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD);
+            if (RESTConstants.METHOD_OPTIONS.equals(method) && sendOptions(synCtx)) {
+                return;
+            }
+
+            synCtx.setProperty(RESTConstants.SYNAPSE_RESOURCE, name);
+            String path = RESTUtils.getFullRequestPath(synCtx);
+
+            int queryIndex = path.indexOf('?');
+            if (queryIndex != -1) {
+                String query = path.substring(queryIndex + 1);
+                String[] entries = query.split("&");
+                for (String entry : entries) {
+                    int index = entry.indexOf('=');
+                    if (index != -1) {
+                        try {
+                            String name = entry.substring(0, index);
+                            String value = URLDecoder.decode(entry.substring(index + 1),
+                                    RESTConstants.DEFAULT_ENCODING);
+                            synCtx.setProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + name, value);
+                        } catch (UnsupportedEncodingException ignored) {
+
+                        }
+                    }
+                }
+            }
+        }
+
+        SequenceMediator sequence = synCtx.isResponse() ? outSequence : inSequence;
+        if (sequence != null) {
+            registerFaultHandler(synCtx);
+            sequence.mediate(synCtx);
+            return;
+        }
+
+        String sequenceKey = synCtx.isResponse() ? outSequenceKey : inSequenceKey;
+        if (sequenceKey != null) {
+            registerFaultHandler(synCtx);
+            Mediator referredSequence = synCtx.getSequence(sequenceKey);
+            if (referredSequence != null) {
+                referredSequence.mediate(synCtx);
+            } else {
+                throw new SynapseException("Specified sequence: " + sequenceKey + " cannot " +
+                        "be found");
+            }
+            return;
+        }
+
+        // Neither a sequence nor a sequence key has been specified. If this message is a
+        // response, simply send it back to the client.
+        if (synCtx.isResponse()) {
+            if (log.isDebugEnabled()) {
+                log.debug("No out-sequence configured. Sending the response back.");
+            }
+            registerFaultHandler(synCtx);
+            Axis2Sender.sendBack(synCtx);
+        } else if (log.isDebugEnabled()) {
+            log.debug("No in-sequence configured. Dropping the request.");
+        }
+    }
+
+    private void registerFaultHandler(MessageContext synCtx) {
+        if (faultSequence != null) {
+            synCtx.pushFaultHandler(new MediatorFaultHandler(faultSequence));
+        } else if (faultSequenceKey != null) {
+            Mediator faultSequence = synCtx.getSequence(faultSequenceKey);
+            if (faultSequence != null) {
+                synCtx.pushFaultHandler(new MediatorFaultHandler(faultSequence));
+            } else {
+                synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
+            }
+        } else {
+            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
+        }
+    }
+
+    private boolean sendOptions(MessageContext synCtx) {
+        org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).
+                getAxis2MessageContext();
+        Map<String,String> transportHeaders = (Map<String,String>) msgCtx.getProperty(
+                org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
+
+        if (methods.contains(RESTConstants.METHOD_OPTIONS)) {
+            // Resource should mediate the OPTIONS request
+            String maxForwardsHeader = transportHeaders.get(HttpHeaders.MAX_FORWARDS);
+            if (maxForwardsHeader != null) {
+                int maxForwards = Integer.parseInt(maxForwardsHeader);
+                if (maxForwards == 0) {
+                    // Resource should respond to the OPTIONS request
+                    synCtx.setResponse(true);
+                    synCtx.setTo(null);
+                    transportHeaders.put(HttpHeaders.ALLOW, getSupportedMethods());
+                    Axis2Sender.sendBack(synCtx);
+                    return true;
+                } else {
+                    transportHeaders.put(HttpHeaders.MAX_FORWARDS, String.valueOf(maxForwards - 1));
+                }
+            }
+            return false;
+
+        } else {
+            // Resource should respond to the OPTIONS request
+            synCtx.setResponse(true);
+            synCtx.setTo(null);
+            transportHeaders.put(HttpHeaders.ALLOW, getSupportedMethods());
+            Axis2Sender.sendBack(synCtx);
+            return true;
+        }
+    }
+
+    private String getSupportedMethods() {
+        String value = "";
+        if (methods.isEmpty()) {
+            value = RESTConstants.REST_ALL_SUPPORTED_METHODS;
+        } else {
+            for (String method : methods) {
+                if (RESTConstants.METHOD_OPTIONS.equals(method)) {
+                    continue;
+                }
+
+                if (value.length() > 0) {
+                    value += ", ";
+                }
+                value += method;
+            }
+        }
+        return value;
+    }
+
+    public void init(SynapseEnvironment se) {
+        if (log.isDebugEnabled()) {
+            log.debug("Initializing resource with ID: " + name);
+        }
+
+        if (inSequence != null) {
+            inSequence.init(se);
+        }
+        if (outSequence != null) {
+            outSequence.init(se);
+        }
+        if (faultSequence != null) {
+            faultSequence.init(se);
+        }
+    }
+
+    public void destroy() {
+        if (log.isDebugEnabled()) {
+            log.debug("Destroying resource with ID: " + name);
+        }
+
+        if (inSequence != null && inSequence.isInitialized()) {
+            inSequence.destroy();
+        }
+        if (outSequence != null && outSequence.isInitialized()) {
+            outSequence.destroy();
+        }
+        if (faultSequence != null && faultSequence.isInitialized()) {
+            faultSequence.destroy();
+        }
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DefaultDispatcher.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DefaultDispatcher.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DefaultDispatcher.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DefaultDispatcher.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,38 @@
+/*
+ *  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.dispatch;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.rest.dispatch.RESTDispatcher;
+import org.apache.synapse.rest.Resource;
+
+import java.util.Collection;
+
+public class DefaultDispatcher implements RESTDispatcher {
+
+    public Resource findResource(MessageContext synCtx, Collection<Resource> resources) {
+        for (Resource resource : resources) {
+            if (resource.getDispatcherHelper() == null) {
+                return resource;
+            }
+        }
+        return null;
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DispatcherHelper.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DispatcherHelper.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DispatcherHelper.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/DispatcherHelper.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,26 @@
+/*
+ *  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.dispatch;
+
+public interface DispatcherHelper {
+
+    public String getString();
+
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/RESTDispatcher.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/RESTDispatcher.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/RESTDispatcher.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/RESTDispatcher.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,42 @@
+/*
+ *  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.dispatch;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.rest.Resource;
+
+import java.util.Collection;
+
+/**
+ * Interface for finding a Resource through which a given request can be mediated.
+ * Implementations of this interface should attempt to find a Resource out of the
+ * provided collection using which the given message can br further processed.
+ */
+public interface RESTDispatcher {
+
+    /**
+     * Find a Resource instance suitable for processing the given message
+     *
+     * @param synCtx MessageContext to be processed through a Resource
+     * @param resources Collection of available Resource instances
+     * @return A matching Resource instance or null
+     */
+    public Resource findResource(MessageContext synCtx, Collection<Resource> resources);
+
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateBasedDispatcher.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateBasedDispatcher.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateBasedDispatcher.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateBasedDispatcher.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,50 @@
+/*
+ *  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.dispatch;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.rest.*;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+public class URITemplateBasedDispatcher implements RESTDispatcher {
+
+    public Resource findResource(MessageContext synCtx, Collection<Resource> resources) {
+        String url = RESTUtils.getSubRequestPath(synCtx);
+        for (Resource r : resources) {
+            DispatcherHelper helper = r.getDispatcherHelper();
+            if (helper instanceof URITemplateHelper) {
+                URITemplateHelper templateHelper = (URITemplateHelper) helper;
+                Map<String,String> variables = new HashMap<String,String>();
+                if (templateHelper.getUriTemplate().matches(url, variables)) {
+                    for (Map.Entry<String,String> entry : variables.entrySet()) {
+                        synCtx.setProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + entry.getKey(),
+                                entry.getValue());
+                    }
+                    return r;
+                }
+            }
+
+        }
+        return null;
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateHelper.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateHelper.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateHelper.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URITemplateHelper.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,48 @@
+/*
+ *  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.dispatch;
+
+import org.apache.synapse.SynapseException;
+import org.wso2.uri.template.URITemplate;
+import org.wso2.uri.template.URITemplateException;
+
+public class URITemplateHelper implements DispatcherHelper {
+
+    private String templateString;
+
+    private URITemplate uriTemplate;
+
+    public URITemplateHelper(String templateString) {
+        this.templateString = templateString;
+        try {
+            this.uriTemplate = new URITemplate(templateString);
+        } catch (URITemplateException e) {
+            throw new SynapseException("Error while parsing the URI template", e);
+        }
+    }
+
+    public URITemplate getUriTemplate() {
+        return uriTemplate;
+    }
+
+    public String getString() {
+        return templateString;
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingBasedDispatcher.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingBasedDispatcher.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingBasedDispatcher.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingBasedDispatcher.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,87 @@
+/*
+ *  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.dispatch;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.rest.*;
+
+import java.util.*;
+
+public class URLMappingBasedDispatcher implements RESTDispatcher {
+
+    private static final Log log = LogFactory.getLog(URLMappingBasedDispatcher.class);
+
+    public Resource findResource(MessageContext synCtx, Collection<Resource> resources) {
+        List<URLMappingHelper> mappings = new ArrayList<URLMappingHelper>();
+        List<Resource> filteredResources = new ArrayList<Resource>();
+
+        for (Resource r : resources) {
+            DispatcherHelper helper = r.getDispatcherHelper();
+            if (helper instanceof URLMappingHelper) {
+                mappings.add((URLMappingHelper) helper);
+                filteredResources.add(r);
+            }
+        }
+
+        int count = filteredResources.size();
+        if (count == 0) {
+            return null;
+        }
+
+        String url = RESTUtils.getSubRequestPath(synCtx);
+        for (int i = 0; i < count; i++) {
+            if (mappings.get(i).isExactMatch(url)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Found exact URL match for: " + url);
+                }
+                return filteredResources.get(i);
+            }
+        }
+
+        int maxLength = 0;
+        Resource matchedResource = null;
+        for (int i = 0; i < count; i++) {
+            int length = mappings.get(i).getPrefixMatchingLength(url);
+            if (length > maxLength) {
+                maxLength = length;
+                matchedResource = filteredResources.get(i);
+            }
+        }
+        if (matchedResource != null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Found path match for: " + url + " with matching length: " + maxLength);
+            }
+            return matchedResource;
+        }
+
+        for (int i = 0; i < count; i++) {
+            if (mappings.get(i).isExtensionMatch(url)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Found extension match for: " + url);
+                }
+                return filteredResources.get(i);
+            }
+        }
+
+        return null;
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingHelper.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingHelper.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingHelper.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/rest/dispatch/URLMappingHelper.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,97 @@
+/*
+ *  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.dispatch;
+
+import org.apache.synapse.rest.RESTUtils;
+
+public class URLMappingHelper implements DispatcherHelper {
+
+    private String[] pathSegments;
+    private String extension;
+    private String exactMatch;
+
+    public URLMappingHelper(String urlMapping) {
+        if (urlMapping.startsWith("/") && urlMapping.endsWith("/*")) {
+            if (urlMapping.length() > 2) {
+                pathSegments = urlMapping.substring(1, urlMapping.length() - 2).split("/");
+            } else {
+                pathSegments = new String[] {};
+            }
+        } else if (urlMapping.startsWith("*.")) {
+            extension = urlMapping.substring(1);
+        } else {
+            exactMatch = urlMapping;
+        }
+    }
+
+    public boolean isExactMatch(String url) {
+        if (!"/".equals(url)) {
+            url = RESTUtils.trimTrailingSlashes(url);
+        }
+        return exactMatch != null && exactMatch.equals(url);
+    }
+
+    public boolean isExtensionMatch(String url) {
+        int index = url.indexOf('?');
+        if (index != -1) {
+            url = url.substring(0, index);
+        }
+        return extension != null && url.endsWith(extension);
+    }
+
+    public int getPrefixMatchingLength(String url) {
+        if (pathSegments != null) {
+            if (pathSegments.length == 0) {
+                return 1;
+            }
+
+            url = RESTUtils.trimSlashes(url);
+            String[] segments = url.split("/");
+            int matchingLength = 0;
+            for (int i = 0; i < pathSegments.length; i++) {
+                if (segments.length > i) {
+                    if (segments[i].equals(pathSegments[i])) {
+                        matchingLength++;
+                    } else {
+                        return 0;
+                    }
+                } else {
+                    return 0;
+                }
+            }
+            return matchingLength;
+        }
+        return 0;
+    }
+
+    public String getString() {
+        if (pathSegments != null) {
+            String str = "";
+            for (String segment : pathSegments) {
+                str += "/" + segment;
+            }
+            return str + "/*";
+        } else if (extension != null) {
+            return "*." + extension;
+        } else {
+            return exactMatch;
+        }
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/config/xml/rest/APISerializationTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/config/xml/rest/APISerializationTest.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/config/xml/rest/APISerializationTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/config/xml/rest/APISerializationTest.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,81 @@
+/*
+ *  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.config.xml.rest;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.synapse.config.xml.AbstractTestCase;
+import org.apache.synapse.rest.API;
+
+public class APISerializationTest extends AbstractTestCase {
+
+    public void testAPISerialization1() throws Exception {
+        String xml = "<api name=\"test\" context=\"/dictionary\" xmlns=\"http://ws.apache.org/ns/synapse\">" +
+                "<resource url-mapping=\"/admin/view\" inSequence=\"in\" outSequence=\"out\"/></api>";
+        OMElement om = AXIOMUtil.stringToOM(xml);
+        API api = APIFactory.createAPI(om);
+        OMElement out = APISerializer.serializeAPI(api);
+        assertXMLEqual(xml, out.toString());
+    }
+
+    public void testAPISerialization2() throws Exception {
+        String xml = "<api name=\"test\" context=\"/dictionary\" hostname=\"apache.org\" port=\"8243\"" +
+                " xmlns=\"http://ws.apache.org/ns/synapse\"><resource url-mapping=\"/admin/view\" " +
+                "inSequence=\"in\" outSequence=\"out\"/></api>";
+        OMElement om = AXIOMUtil.stringToOM(xml);
+        API api = APIFactory.createAPI(om);
+        OMElement out = APISerializer.serializeAPI(api);
+        assertXMLEqual(xml, out.toString());
+    }
+
+    public void testAPISerialization3() throws Exception {
+        String xml = "<api name=\"test\" context=\"/dictionary\" hostname=\"apache.org\" port=\"8243\"" +
+                " xmlns=\"http://ws.apache.org/ns/synapse\"><resource url-mapping=\"/admin/view\" " +
+                "inSequence=\"in\"><outSequence><log/><send/></outSequence></resource></api>";
+        OMElement om = AXIOMUtil.stringToOM(xml);
+        API api = APIFactory.createAPI(om);
+        OMElement out = APISerializer.serializeAPI(api);
+        assertXMLEqual(xml, out.toString());
+    }
+
+    public void testAPISerialization4() throws Exception {
+        String xml = "<api name=\"test\" context=\"/dictionary\" hostname=\"apache.org\" port=\"8243\"" +
+                " xmlns=\"http://ws.apache.org/ns/synapse\"><resource url-mapping=\"/admin/view\" " +
+                "outSequence=\"out\"><inSequence><log/><send/></inSequence></resource></api>";
+        OMElement om = AXIOMUtil.stringToOM(xml);
+        API api = APIFactory.createAPI(om);
+        OMElement out = APISerializer.serializeAPI(api);
+        assertXMLEqual(xml, out.toString());
+    }
+
+    public void testAPISerialization5() throws Exception {
+        String xml = "<api name=\"test\" context=\"/dictionary\" hostname=\"apache.org\" port=\"8243\"" +
+                " xmlns=\"http://ws.apache.org/ns/synapse\"><resource url-mapping=\"/admin/view/*\" " +
+                "><inSequence><log/><send/></inSequence><outSequence><log/><send/></outSequence></resource>" +
+                "<resource url-mapping=\"/admin/*\"><inSequence><log/><send/></inSequence><outSequence>" +
+                "<log/><send/></outSequence></resource><resource uri-template=\"/{char}/{word}\">" +
+                "<inSequence><send/></inSequence><faultSequence><log level=\"full\"/></faultSequence>" +
+                "</resource></api>";
+        OMElement om = AXIOMUtil.stringToOM(xml);
+        API api = APIFactory.createAPI(om);
+        OMElement out = APISerializer.serializeAPI(api);
+        assertXMLEqual(xml, out.toString());
+    }
+}

Added: 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=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/APIDispatcherTest.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,178 @@
+/*
+ *  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;
+
+import org.apache.http.protocol.HTTP;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+
+public class APIDispatcherTest extends RESTMediationTestCase {
+
+    private static final String TEST_API = "TestAPI";
+
+    public void testGeneralAPIDispatch() throws Exception {
+        API api = new API(TEST_API, "/");
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(TEST_API, api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        synCtx = getMessageContext(synapseConfig, false, "/", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        synCtx = getMessageContext(synapseConfig, false, "/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+    }
+
+    public void testBasicAPIDispatch() throws Exception {
+        API api = new API(TEST_API, "/test");
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(TEST_API, 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));
+        synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+        synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // 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));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test1/bar?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+    }
+
+    public void testResponseDispatch() throws Exception {
+        API api = new API(TEST_API, "/test");
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(TEST_API, api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        // Messages with '/test' context should ne dispatched
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        synCtx.setResponse(true);
+        assertFalse(handler.process(synCtx));
+
+        synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, TEST_API);
+        assertTrue(handler.process(synCtx));
+    }
+
+    public void testHostBasedAPIDispatch() throws Exception {
+        API api = new API(TEST_API, "/test");
+        api.setHost("synapse.apache.org");
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(TEST_API, api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        // Messages that don't have the proper host set should not be dispatched
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // Messages with the correct host should be dispatched
+        synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
+        addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // API should be able to infer the default HTTP port
+        api.setPort(80);
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // Messages with an incorrect port number should not be dispatched
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
+        addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org:8280", synCtx);
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // Messages with the correct port number should be dispatched
+        api.setPort(8280);
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        api.setPort(443);
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
+        addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        // API should accurately infer the default HTTPS port
+        synCtx = getMessageContext(synapseConfig, true, "/test/foo/bar?a=5", "GET");
+        addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
+        handler.process(synCtx);
+        assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+    }
+
+    public void testMultipleAPIDispatch() throws Exception {
+        String apiName1 = "TestAPI1";
+        String apiName2 = "TestAPI2";
+        String apiName3 = "TestAPI3";
+
+        API api1 = new API(apiName1, "/test");
+        API api2 = new API(apiName2, "/dictionary");
+        api2.setHost("synapse.apache.org");
+        API api3 = new API(apiName3, "/foo/bar");
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(apiName1, api1);
+        synapseConfig.addAPI(apiName2, api2);
+        synapseConfig.addAPI(apiName3, api3);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        handler.process(synCtx);
+        assertEquals(apiName1, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
+        addHttpHeader(HTTP.TARGET_HOST, "synapse.apache.org", synCtx);
+        handler.process(synCtx);
+        assertEquals(apiName2, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        synCtx = getMessageContext(synapseConfig, false, "/foo/bar/index.jsp?user=test", "GET");
+        handler.process(synCtx);
+        assertEquals(apiName3, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+
+        synCtx = getMessageContext(synapseConfig, false, "/foo/index.jsp?user=test", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
+    }
+
+}
\ No newline at end of file

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/RESTMediationTestCase.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/RESTMediationTestCase.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/RESTMediationTestCase.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/RESTMediationTestCase.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,72 @@
+/*
+ *  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;
+
+import junit.framework.TestCase;
+import org.apache.axis2.Constants;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.TestUtils;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.synapse.mediators.builtin.PropertyMediator;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class RESTMediationTestCase extends TestCase {
+
+    protected MessageContext getMessageContext(SynapseConfiguration synapseConfig, boolean https,
+                                               String url, String method) throws Exception {
+        MessageContext synCtx = TestUtils.createSynapseMessageContext("<foo/>", synapseConfig);
+        org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).
+                getAxis2MessageContext();
+        if (https) {
+            msgCtx.setIncomingTransportName("https");
+        } else {
+            msgCtx.setIncomingTransportName("http");
+        }
+        msgCtx.setProperty(Constants.Configuration.HTTP_METHOD, method);
+        msgCtx.setProperty(Constants.Configuration.TRANSPORT_IN_URL, url);
+        return synCtx;
+    }
+
+    protected void addHttpHeader(String name, String value, MessageContext synCtx) {
+        org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).
+                getAxis2MessageContext();
+        Object obj = msgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
+        if (obj != null) {
+            ((Map) obj).put(name, value);
+        } else {
+            Map map = new HashMap();
+            map.put(name, value);
+            msgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
+        }
+    }
+
+    protected SequenceMediator getTestSequence(String name, String value) {
+        SequenceMediator seq = new SequenceMediator();
+        PropertyMediator prop = new PropertyMediator();
+        prop.setName(name);
+        prop.setValue(value);
+        seq.addChild(prop);
+        return seq;
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/ResourceTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/ResourceTest.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/ResourceTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/ResourceTest.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,92 @@
+/*
+ *  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;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
+import org.apache.synapse.core.axis2.MessageContextCreatorForAxis2;
+import org.apache.synapse.core.axis2.SynapseMessageReceiver;
+import org.apache.synapse.mediators.Value;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.synapse.mediators.builtin.PropertyMediator;
+import org.apache.synapse.mediators.transform.XSLTMediator;
+import org.apache.synapse.rest.dispatch.URITemplateHelper;
+
+public class ResourceTest extends RESTMediationTestCase {
+
+    public void testQueryParams() throws Exception {
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false,
+                "/test/admin?a=5&b=10&user=bar", "GET");
+        handler.process(synCtx);
+        assertEquals("5", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
+        assertEquals("10", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "b"));
+        assertEquals("bar", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "user"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals("5", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/admin?a=Hello%20World&b=10&c=/foo/bar", "GET");
+        handler.process(synCtx);
+        assertEquals("Hello World", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "a"));
+        assertEquals("10", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "b"));
+        assertEquals("/foo/bar", synCtx.getProperty(RESTConstants.REST_QUERY_PARAM_PREFIX + "c"));
+    }
+
+    public void testFaultSequence() throws Exception {
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
+        SequenceMediator inSequence = getTestSequence("seq.in", "seq.in.value");
+        ((PropertyMediator) inSequence.getChild(0)).setScope("axis2");
+        XSLTMediator xsltMediator = new XSLTMediator();
+        xsltMediator.setXsltKey(new Value("/bogus/key"));
+        inSequence.addChild(xsltMediator);
+        resource.setInSequence(inSequence);
+        SequenceMediator faultSequence = getTestSequence("seq.fault", "seq.fault.value");
+        ((PropertyMediator) faultSequence.getChild(0)).setScope("axis2");
+        resource.setFaultSequence(faultSequence);
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+        synapseConfig.addSequence("main", getTestSequence("main.in", "main.value"));
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
+        MessageContextCreatorForAxis2.setSynConfig(synapseConfig);
+        MessageContextCreatorForAxis2.setSynEnv(synCtx.getEnvironment());
+
+        org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
+        mc.setConfigurationContext(((Axis2SynapseEnvironment) synCtx.getEnvironment()).getAxis2ConfigurationContext());
+        new SynapseMessageReceiver().receive(mc);
+        assertEquals("seq.in.value", mc.getProperty("seq.in"));
+        assertEquals("seq.fault.value", mc.getProperty("seq.fault"));
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URITemplateBasedDispatcherTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URITemplateBasedDispatcherTest.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URITemplateBasedDispatcherTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URITemplateBasedDispatcherTest.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,165 @@
+/*
+ *  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;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.rest.dispatch.URITemplateHelper;
+
+public class URITemplateBasedDispatcherTest extends RESTMediationTestCase {
+
+    private static final String PROP_NAME = "prop.name";
+    private static final String PROP_VALUE = "prop.value";
+
+    public void testBasicTemplateDispatch1() throws Exception {
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+        assertEquals("foo", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "user"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/~foo/bar", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testBasicTemplateDispatch2() throws Exception {
+
+        API api = new API("TestAPI", "/");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+        assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
+        assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/d/dog/", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+        assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
+        assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testDefaultDispatch() throws Exception {
+
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URITemplateHelper("/"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testMultipleResourceDispatch() throws Exception {
+
+        API api = new API("TestAPI", "/");
+        Resource resource1 = new Resource();
+        resource1.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
+        resource1.setInSequence(getTestSequence(PROP_NAME, "r1"));
+        api.addResource(resource1);
+
+        Resource resource2 = new Resource();
+        resource2.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}"));
+        resource2.setInSequence(getTestSequence(PROP_NAME, "r2"));
+        api.addResource(resource2);
+
+        Resource resource3 = new Resource();
+        resource3.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}{#ref}"));
+        resource3.setInSequence(getTestSequence(PROP_NAME, "r3"));
+        api.addResource(resource3);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
+        handler.process(synCtx);
+        assertEquals("r1", synCtx.getProperty(PROP_NAME));
+        assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
+        assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/d", "GET");
+        handler.process(synCtx);
+        assertEquals("r2", synCtx.getProperty(PROP_NAME));
+        assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/e#test", "GET");
+        handler.process(synCtx);
+        assertEquals("r3", synCtx.getProperty(PROP_NAME));
+        assertEquals("e", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
+        assertEquals("test", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "ref"));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat/test", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat#ref", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URLMappingBasedDispatcherTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URLMappingBasedDispatcherTest.java?rev=1214211&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URLMappingBasedDispatcherTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/rest/URLMappingBasedDispatcherTest.java Wed Dec 14 12:24:36 2011
@@ -0,0 +1,225 @@
+/*
+ *  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;
+
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.rest.dispatch.URLMappingHelper;
+
+public class URLMappingBasedDispatcherTest extends RESTMediationTestCase {
+
+    private static final String PROP_NAME = "prop.name";
+    private static final String PROP_VALUE = "prop.value";
+
+    public void testDefaultResourceDispatch() throws Exception {
+
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URLMappingHelper("/"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testPathBasedDispatch() throws Exception {
+
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testExtensionBasedDispatch() throws Exception {
+
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URLMappingHelper("*.jsp"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/welcome.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp?a=5&b=10", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testExactMatchBasedDispatch() throws Exception {
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URLMappingHelper("/foo/bar"));
+        resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.html", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testMultipleResourceDispatch() throws Exception {
+        API api = new API("TestAPI", "/test");
+
+        Resource resource1 = new Resource();
+        resource1.setDispatcherHelper(new URLMappingHelper("/foo/*"));
+        resource1.setInSequence(getTestSequence(PROP_NAME, "resource1"));
+
+        Resource resource2 = new Resource();
+        resource2.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
+        resource2.setInSequence(getTestSequence(PROP_NAME, "resource2"));
+
+        Resource resource3 = new Resource();
+        resource3.setDispatcherHelper(new URLMappingHelper("*.jsp"));
+        resource3.setInSequence(getTestSequence(PROP_NAME, "resource3"));
+
+        api.addResource(resource1);
+        api.addResource(resource2);
+        api.addResource(resource3);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
+        handler.process(synCtx);
+        assertEquals("resource1", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals("resource1", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars", "GET");
+        handler.process(synCtx);
+        assertEquals("resource1", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals("resource1", synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
+        handler.process(synCtx);
+        assertEquals("resource2", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.html?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals("resource2", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/hello", "GET");
+        handler.process(synCtx);
+        assertEquals("resource2", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals("resource2", synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals("resource3", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/hello/index.jsp?a=5", "GET");
+        handler.process(synCtx);
+        assertEquals("resource3", synCtx.getProperty(PROP_NAME));
+        synCtx = getMessageContext(synapseConfig, false, "/test/foolish/bars/index.jsp", "GET");
+        handler.process(synCtx);
+        assertEquals("resource3", synCtx.getProperty(PROP_NAME));
+
+        synCtx = getMessageContext(synapseConfig, false, "/test/foolish/index.html", "GET");
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+    }
+
+    public void testResponseDispatch() throws Exception {
+        API api = new API("TestAPI", "/test");
+        Resource resource = new Resource();
+        resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
+        resource.setOutSequence(getTestSequence(PROP_NAME, PROP_VALUE));
+        api.addResource(resource);
+
+        SynapseConfiguration synapseConfig = new SynapseConfiguration();
+        synapseConfig.addAPI(api.getName(), api);
+
+        RESTRequestHandler handler = new RESTRequestHandler();
+
+        MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
+        synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, api.getName());
+        synCtx.setResponse(true);
+        handler.process(synCtx);
+        assertNull(synCtx.getProperty(PROP_NAME));
+
+        synCtx.setProperty(RESTConstants.SYNAPSE_RESOURCE, resource.getName());
+        handler.process(synCtx);
+        assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
+    }
+}

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ClientConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ClientConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ClientConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ClientConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;
 

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ServerConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ServerConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ServerConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/Axis2ServerConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;
 

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/DerbyConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/DerbyConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/DerbyConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/DerbyConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;
 

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/JMSBrokerConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/JMSBrokerConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/JMSBrokerConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/JMSBrokerConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;
 

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SampleConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SampleConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SampleConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SampleConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;
 

Modified: synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SynapseServerConfiguration.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SynapseServerConfiguration.java?rev=1214211&r1=1214210&r2=1214211&view=diff
==============================================================================
--- synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SynapseServerConfiguration.java (original)
+++ synapse/trunk/java/modules/integration/src/test/java/org/apache/synapse/samples/framework/config/SynapseServerConfiguration.java Wed Dec 14 12:24:36 2011
@@ -1,20 +1,21 @@
 /*
-*  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.
-*/
+ *  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.samples.framework.config;