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

package org.wso2.carbon.dssapi.util;

import org.apache.axis2.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.model.*;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.dataservices.ui.beans.*;
import org.wso2.carbon.dssapi.core.DSSAPIException;
import org.wso2.carbon.dssapi.model.Application;
import org.wso2.carbon.dssapi.model.LifeCycleEventDao;
import org.wso2.carbon.dssapi.observer.DataHolder;
import org.wso2.carbon.service.mgt.ServiceAdmin;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * APIUtil method has all the util methods to help dss api
 */
@SuppressWarnings("unchecked")
public class APIUtil {
    private static final String HTTP_PORT = "mgt.transport.http.port";
    private static final String HOST_NAME = "carbon.local.ip";
    public static final String APPLICATION_XML = "_application.xml";
    private JSONObject swagger12Json = new JSONObject();
    private Map<String, JSONArray> resourceMap = new LinkedHashMap<String, JSONArray>();
    private static final Log log = LogFactory.getLog(APIUtil.class);

    /**
     * To get the API provider
     *
     * @param username username of the logged user
     * @return APIProvider object according to user
     */
    private APIProvider getAPIProvider(String username) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Create APIProvider from username:" + username);
            }
            return APIManagerFactory.getInstance().getAPIProvider(username);
        } catch (APIManagementException e) {
            log.error("Failed to Create APIProvider for" + username, e);
        }
        return null;
    }

    /**
     * To add an API
     *
     * @param serviceId service name of the service
     * @param username  username of the logged user
     * @param data      data service object
     * @param version   version of the api
     */
    public void addApi(String serviceId, String username, Data data, String version) {
        APIProvider apiProvider;
        apiProvider = getAPIProvider(username);
        API api = createApiObject(serviceId, username, data, version, apiProvider);

        if (api != null) {

            try {
                apiProvider.addAPI(api);
                updateSwagger12Definition(api, apiProvider);
                api.setStatus(APIStatus.CREATED);
                apiProvider.changeAPIStatus(api, APIStatus.PUBLISHED, username, false);
            } catch (APIManagementException e) {
                log.error("couldn't Create API for " + serviceId + "Service", e);
            }
            String DSSRepositoryPath;
            int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();

            if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
                DSSRepositoryPath = CarbonUtils.getCarbonRepository() + "/dataservices";
            } else {
                DSSRepositoryPath = CarbonUtils.getCarbonTenantsDirPath() + "/" + tenantId + "/dataservices";
            }
            try {
                String deployedTime = new ServiceAdmin(DataHolder.getConfigurationContext().getAxisConfiguration())
                        .getServiceData(serviceId).getServiceDeployedTime();
                String applicationXmlPath = DSSRepositoryPath + "/" + serviceId + APPLICATION_XML;
                File file = new File(applicationXmlPath);
                if (!file.exists()) {
                    Application application = new Application(true, deployedTime, username, version,
                            MultitenantUtils.getTenantDomain(username));
                    JAXBContext jaxbContext = JAXBContext.newInstance(Application.class);
                    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    jaxbMarshaller.marshal(application, file);
                    if (log.isDebugEnabled()) {
                        log.debug("API created successfully for " + serviceId);
                    }
                }
            } catch (FileNotFoundException e) {
                log.error("couldn't found path :" + DSSRepositoryPath + " application xml file for " + serviceId +
                        "Service", e);
            } catch (XMLStreamException e) {
                log.error("couldn't write application xml file for " + serviceId + "Service", e);
            } catch (Exception e) {
                log.error("Couldn't get ServiceMetaData Object for the service " + serviceId, e);
            }


        }
    }

    /**
     * To create the model of the API
     *
     * @param apiProvider API Provider
     * @param apiContext  API Context
     * @param apiEndpoint Endpoint url
     * @param authType    Authentication type
     * @param identifier  API identifier
     * @param data        data service object
     * @return API model
     */
    private API createAPIModel(APIProvider apiProvider, String apiContext, String apiEndpoint, String authType,
                               APIIdentifier identifier, Data data) {
        API api = null;
        try {
            api = new API(identifier);
            api.setContext(apiContext);
            api.setUriTemplates(getURITemplates(apiEndpoint, authType, data, apiProvider.getTiers()));
            api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
            api.addAvailableTiers(apiProvider.getTiers());
            api.setEndpointSecured(false);
            api.setStatus(APIStatus.CREATED);
            api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
            api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
            api.setResponseCache(APIConstants.DISABLED);
            api.setImplementation("endpoint");
            String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + apiEndpoint +
                    "\",\"config\":null},\"endpoint_type\":\"http\"}";
            api.setEndpointConfig(endpointConfig);
            if (log.isDebugEnabled()) {
                log.debug(
                        "API Object Created for API:" + identifier.getApiName() + "version:" + identifier.getVersion());
            }
        } catch (APIManagementException e) {
            log.error("couldn't get tiers for provider:" + identifier.getProviderName(), e);
        }
        return api;
    }

    /**
     * To get URI templates
     *
     * @param endpoint Endpoint URL
     * @param authType Authentication type
     * @param data     data service object
     * @param tiers
     * @return URI templates
     */
    private Set<URITemplate> getURITemplates(String endpoint, String authType, Data data, Set<Tier> tiers) {
        //todo improve to add sub context paths for uri templates as well
        Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
        ArrayList<Operation> operations = data.getOperations();
        ArrayList<Resource> resourceList = data.getResources();
        if (authType.equals(APIConstants.AUTH_NO_AUTHENTICATION)) {
            for (Resource resource : resourceList) {
                URITemplate template = new URITemplate();
                template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
                template.setHTTPVerb(resource.getMethod());
                template.setResourceURI(endpoint);
                template.setUriTemplate("/" + resource.getPath());
                for (Tier tier : tiers.toArray(new Tier[tiers.size()])) {
                    template.setThrottlingTier(tier.getName());
                }
                uriTemplates.add(template);
                addApiArray(template, resource.getCallQuery().getWithParams());
            }
            for (Operation operation : operations) {
                URITemplate template = new URITemplate();
                template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
                template.setHTTPVerb(getOperationBasedHttpVerbs(operation.getCallQuery().getHref(), data));
                template.setResourceURI(endpoint);
                template.setUriTemplate("/" + operation.getName());
                for (Tier tier : tiers.toArray(new Tier[tiers.size()])) {
                    template.setThrottlingTier(tier.getName());<commit_msg>reviewed changes completed<commit_after>/*
 *  Copyright (c) 2005-2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

package org.wso2.carbon.dssapi.util;

import org.apache.axis2.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.model.*;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.dataservices.ui.beans.*;
import org.wso2.carbon.dssapi.core.DSSAPIException;
import org.wso2.carbon.dssapi.model.Application;
import org.wso2.carbon.dssapi.model.LifeCycleEventDao;
import org.wso2.carbon.dssapi.observer.DataHolder;
import org.wso2.carbon.service.mgt.ServiceAdmin;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * APIUtil method has all the util methods to help dss api
 */
@SuppressWarnings("unchecked")
public class APIUtil {
    private static final String HTTP_PORT = "mgt.transport.http.port";
    private static final String HOST_NAME = "carbon.local.ip";
    public static final String APPLICATION_XML = "_application.xml";
    private JSONObject swagger12Json = new JSONObject();
    private Map<String, JSONArray> resourceMap = new LinkedHashMap<String, JSONArray>();
    private static final Log log = LogFactory.getLog(APIUtil.class);

    /**
     * To get the API provider
     *
     * @param username username of the logged user
     * @return APIProvider object according to user
     */
    private APIProvider getAPIProvider(String username) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Create APIProvider from username:" + username);
            }
            return APIManagerFactory.getInstance().getAPIProvider(username);
        } catch (APIManagementException e) {
            log.error("Failed to Create APIProvider for" + username, e);
        }
        return null;
    }

    /**
     * To add an API
     *
     * @param serviceId service name of the service
     * @param username  username of the logged user
     * @param data      data service object
     * @param version   version of the api
     */
    public void addApi(String serviceId, String username, Data data, String version) {
        APIProvider apiProvider;
        apiProvider = getAPIProvider(username);
        API api = createApiObject(serviceId, username, data, version, apiProvider);

        if (api != null) {

            try {
                apiProvider.addAPI(api);
                updateSwagger12Definition(api, apiProvider);
                api.setStatus(APIStatus.CREATED);
                apiProvider.changeAPIStatus(api, APIStatus.PUBLISHED, username, false);
            } catch (APIManagementException e) {
                log.error("couldn't Create API for " + serviceId + "Service", e);
            }
            String DSSRepositoryPath;
            int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();

            if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
                DSSRepositoryPath = CarbonUtils.getCarbonRepository() + "/dataservices";
            } else {
                DSSRepositoryPath = CarbonUtils.getCarbonTenantsDirPath() + "/" + tenantId + "/dataservices";
            }
            try {
                String deployedTime = new ServiceAdmin(DataHolder.getConfigurationContext().getAxisConfiguration())
                        .getServiceData(serviceId).getServiceDeployedTime();
                String applicationXmlPath = DSSRepositoryPath + "/" + serviceId + APPLICATION_XML;
                File file = new File(applicationXmlPath);
                if (!file.exists()) {
                    Application application = new Application(true, deployedTime, username, version,
                            MultitenantUtils.getTenantDomain(username));
                    JAXBContext jaxbContext = JAXBContext.newInstance(Application.class);
                    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    jaxbMarshaller.marshal(application, file);
                    if (log.isDebugEnabled()) {
                        log.debug("API created successfully for " + serviceId);
                    }
                }
            } catch (FileNotFoundException e) {
                log.error("couldn't found path :" + DSSRepositoryPath + " application xml file for " + serviceId +
                        "Service", e);
            } catch (XMLStreamException e) {
                log.error("couldn't write application xml file for " + serviceId + "Service", e);
            } catch (Exception e) {
                log.error("Couldn't get ServiceMetaData Object for the service " + serviceId, e);
            }


        }
    }

    /**
     * To create the model of the API
     *
     * @param apiProvider API Provider
     * @param apiContext  API Context
     * @param apiEndpoint Endpoint url
     * @param authType    Authentication type
     * @param identifier  API identifier
     * @param data        data service object
     * @return API model
     */
    private API createAPIModel(APIProvider apiProvider, String apiContext, String apiEndpoint, String authType,
                               APIIdentifier identifier, Data data) {
        API api = null;
        try {
            api = new API(identifier);
            api.setContext(apiContext);
            api.setUriTemplates(getURITemplates(apiEndpoint, authType, data, apiProvider.getTiers()));
            api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
            api.addAvailableTiers(apiProvider.getTiers());
            api.setEndpointSecured(false);
            api.setStatus(APIStatus.CREATED);
            api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
            api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
            api.setResponseCache(APIConstants.DISABLED);
            api.setImplementation("endpoint");
            String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + apiEndpoint +
                    "\",\"config\":null},\"endpoint_type\":\"http\"}";
            api.setEndpointConfig(endpointConfig);
            if (log.isDebugEnabled()) {
                log.debug(
                        "API Object Created for API:" + identifier.getApiName() + "version:" + identifier.getVersion());
            }
        } catch (APIManagementException e) {
            log.error("couldn't get tiers for provider:" + identifier.getProviderName(), e);
        }
        return api;
    }

    /**
     * To get URI templates
     *
     * @param endpoint Endpoint URL
     * @param authType Authentication type
     * @param data     data service object
     * @param tiers
     * @return URI templates
     */
    private Set<URITemplate> getURITemplates(String endpoint, String authType, Data data, Set<Tier> tiers) {
        Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
        ArrayList<Operation> operations = data.getOperations();
        ArrayList<Resource> resourceList = data.getResources();
        if (authType.equals(APIConstants.AUTH_NO_AUTHENTICATION)) {
            for (Resource resource : resourceList) {
                URITemplate template = new URITemplate();
                template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
                template.setHTTPVerb(resource.getMethod());
                template.setResourceURI(endpoint);
                template.setUriTemplate("/" + resource.getPath());
                for (Tier tier : tiers.toArray(new Tier[tiers.size()])) {
                    template.setThrottlingTier(tier.getName());
                }
                uriTemplates.add(template);
                addApiArray(template, resource.getCallQuery().getWithParams());
            }
            for (Operation operation : operations) {
                URITemplate template = new URITemplate();
                template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
                template.setHTTPVerb(getOperationBasedHttpVerbs(operation.getCallQuery().getHref(), data));
                template.setResourceURI(endpoint);
                template.setUriTemplate("/" + operation.getName());
                for (Tier tier : tiers.toArray(new Tier[tiers.size()])) {
                    template.setThrottlingTier(tier.getName());
{'fraction_non_alphanumeric': 0.09778588437959163, 'fraction_numerical': 0.00364797081623347, 'mean_word_length': 2.5240135690055348, 'pattern_counts': {'":': 8, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1092849', 'n_tokens_mistral': 5384, 'n_tokens_neox': 5252, 'n_words': 1229}