/*                                                                             
 * Copyright 2004,2005 The Apache Software Foundation.                         
 *                                                                             
 * Licensed under the Apache License, Version 2.0 (the "License");             
 * you may not use this file except in compliance with the License.            
 * You may obtain a copy of the License at                                     
 *                                                                             
 *      http://www.apache.org/licenses/LICENSE-2.0                             
 *                                                                             
 * Unless required by applicable law or agreed to in writing, software         
 * distributed under the License is distributed on an "AS IS" BASIS,           
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
 * See the License for the specific language governing permissions and         
 * limitations under the License.                                              
 */
package org.wso2.carbon.tenant.dispatcher;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.HandlerDescription;
import org.apache.axis2.engine.AbstractDispatcher;
import org.apache.axis2.engine.AxisConfiguration;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import javax.xml.namespace.QName;

/**
 * If none of the dispatcher were able to find an Axis2 service or operation, this dispatcher will
 * be reached, and it will dispatch to the MultitenantService, which is associated with the
 * {@link org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver}
 */
public class MultitenantDispatcher extends AbstractDispatcher {

    public static final String NAME = "MultitenantDispatcher";

    public void initDispatcher() {
        QName qn = new QName("http://wso2.org/projects/carbon", NAME);
        HandlerDescription hd = new HandlerDescription(qn.getLocalPart());
        super.init(hd);
    }

    public AxisService findService(MessageContext mc) throws AxisFault {
        AxisService service = mc.getAxisService();
        if (service == null) {
            String to = mc.getTo().getAddress();

            int tenantDelimiterIndex = to.indexOf("/t/");
            if (tenantDelimiterIndex != -1) {
                AxisConfiguration ac = mc.getConfigurationContext().getAxisConfiguration();
                return ac.getService(MultitenantConstants.MULTITENANT_DISPATCHER_SERVICE);
            }
        }
        return service;
    }

    public AxisOperation findOperation(AxisService svc, MessageContext mc) throws AxisFault {
        AxisOperation operation = mc.getAxisOperation();
        if (operation == null) {
            return svc.getOperation(MultitenantConstants.MULTITENANT_DISPATCHER_OPERATION);
        }
        return operation;
    }
}

@@ -1493,32 +1493,162 @@
 sConfiguration;%0A
+import org.apache.axis2.util.LoggingControl;%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import org.wso2.
@@ -1695,16 +1695,16 @@
 stants;%0A
-
 %0Aimport 
@@ -2067,16 +2067,99 @@
 cher %7B%0A%0A
+    private static final Log log = LogFactory.getLog(MultitenantDispatcher.class);%0A
     publ
@@ -2631,16 +2631,432 @@
 ess();%0A%0A
+            if (!isValidPath(to, mc.getConfigurationContext().getServiceContextPath())) %7B%0A                if (LoggingControl.debugLoggingAllowed && log.isDebugEnabled()) %7B%0A                    log.debug(mc.getLogIDString() +%0A                              %22 Attempted to check for Service using target endpoint URI, but the service fragment was missing%22);%0A                %7D%0A                return null;%0A            %7D%0A%0A
         
@@ -3701,16 +3701,814 @@
 ration;%0A
-
     %7D%0A
+%0A    /**%0A     * Check given path contains servicePath value%0A     *%0A     * @param path        - incoming EPR%0A     * @param servicePath - Ex: 'services'%0A     * @return - validity status of the path%0A     */%0A    private boolean isValidPath(String path, String servicePath) %7B%0A%0A        if (path == null) %7B%0A            return false;%0A        %7D%0A%0A        //with this chances that substring matching a different place in the URL is reduced%0A        if (!servicePath.endsWith(%22/%22)) %7B%0A            servicePath = servicePath + %22/%22;%0A        %7D%0A%0A        int index = path.lastIndexOf(servicePath);%0A%0A        if (-1 != index) %7B%0A            int serviceStart = index + servicePath.length();%0A%0A            if (path.length() %3E serviceStart) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
 %7D%0A

Add service path validation to mulitenant dispatcher