/*
 *
 *  Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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.apimgt.rest.api.util.interceptors;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.definitions.OASParserUtil;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;

import java.io.IOException;


public class URLValidationInterceptor extends AbstractPhaseInterceptor<Message> {

    private static final Log log = LogFactory.getLog(URLValidationInterceptor.class);
    private static String definition, version = null;
    private static String majorVersion = "v1";
    private String pathSeparator = "/";
    private final String BASE_PATH = "org.apache.cxf.message.Message.BASE_PATH";
    private final String PATH_INFO = "org.apache.cxf.message.Message.PATH_INFO";
    private final String REQUEST_URI = "org.apache.cxf.request.uri";
    private final String REQUEST_URL = "org.apache.cxf.request.url";

    public URLValidationInterceptor() throws IOException, APIManagementException {
        super(Phase.RECEIVE);
        definition = IOUtils
                .toString(RestApiUtil.class.getResourceAsStream("/publisher-api.yaml"), "UTF-8");

        version = OASParserUtil.getOASParser(definition).getOASVersion(definition);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        if (message.get(PATH_INFO).toString()
                .contains(message.get(BASE_PATH).toString().concat(version + pathSeparator))) {
            message.put(PATH_INFO, message.get(PATH_INFO).toString()
                    .replace(version + pathSeparator, ""));
            message.put(REQUEST_URI, message.get(REQUEST_URI).toString().
                    replace(version + pathSeparator, ""));
            message.put(REQUEST_URL, message.get(REQUEST_URL).toString().
                    replace(version + pathSeparator, ""));
        }
        if (message.get(PATH_INFO).toString()
                .contains(message.get(BASE_PATH).toString().concat(majorVersion + pathSeparator))) {
            message.put(PATH_INFO, message.get(PATH_INFO).toString()
                    .replace(majorVersion + pathSeparator, ""));
            message.put(REQUEST_URI, message.get(REQUEST_URI).toString()
                    .replace(majorVersion + pathSeparator, ""));
            message.put(REQUEST_URL, message.get(REQUEST_URL).toString()
                    .replace(majorVersion + pathSeparator, ""));
        }

    }

}


@@ -1413,35 +1413,72 @@
 ing 
-definition, version = null;
+majorVersion = %22v1%22;%0A    //TODO: Get latest version from swagger
 %0A   
@@ -1492,37 +1492,38 @@
 e static String 
-major
+latest
 Version = %22v1%22;%0A
@@ -1515,24 +1515,26 @@
 ersion = %22v1
+.1
 %22;%0A    priva
@@ -1982,220 +1982,8 @@
 E);%0A
-        definition = IOUtils%0A                .toString(RestApiUtil.class.getResourceAsStream(%22/publisher-api.yaml%22), %22UTF-8%22);%0A%0A        version = OASParserUtil.getOASParser(definition).getOASVersion(definition);%0A
     
@@ -2174,17 +2174,23 @@
 .concat(
-v
+latestV
 ersion +
@@ -2299,33 +2299,39 @@
        .replace(
-v
+latestV
 ersion + pathSep
@@ -2438,33 +2438,39 @@
         replace(
-v
+latestV
 ersion + pathSep
@@ -2585,17 +2585,23 @@
 replace(
-v
+latestV
 ersion +

Change interceptor to get latest version