<commit_before>/*
*  java-cli
*
*  Copyright 2016 <NAME> <<EMAIL>>, <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
*
*  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 implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.continuousassurance.swamp.cli;

import edu.uiuc.ncsa.security.util.ssl.SSLConfiguration;
import org.continuousassurance.swamp.api.*;
import org.continuousassurance.swamp.session.Session;
import org.continuousassurance.swamp.session.handlers.HandlerFactory;
import org.continuousassurance.swamp.session.handlers.PackageHandler;
import org.continuousassurance.swamp.session.util.ConversionMapImpl;
import org.continuousassurance.swamp.util.HandlerFactoryUtil;
import net.sf.json.JSONException;
import org.apache.http.client.CookieStore;
import org.continuousassurance.swamp.cli.exceptions.*;

import java.io.*;
import java.util.*;


public class SwampApiWrapper {

    private static final String USER_PATH = System.getProperty("user.home");
    private static final String FILE_SEPARATOR_KEY = System.getProperty("file.separator");
    private static final String SWAMP_DIR_NAME = ".SWAMP_SESSION";
    public static final String SWAMP_DIR_PATH = USER_PATH + FILE_SEPARATOR_KEY + SWAMP_DIR_NAME + FILE_SEPARATOR_KEY;

    private String rwsAddress;
    private String csaAddress;
    private String originHeader;
    private String refereHeader;
    private String hostHeader;
    private Map<String, Project> projectMap;
    private Map<String, Integer> packageTypeMap;
    private Map<String, PackageThing> packageMap;
    private Map<String, PackageVersion> packageVersionMap;
    private Map<String, Tool> toolMap;
    private Map<String, Platform> platformMap;
    private String cachedPkgProjectID;
    private String cachedPkgVersionProjectID;
    private String cachedToolProjectID;

    HandlerFactory handlerFactory;

    public enum HostType {
        PRODUCTION,
        INTEGRATION,
        DEVELOPMENT,
        CUSTOM
    }
    
    public static final Map<HostType, String> SWAMP_HOST_NAMES_MAP = 
            Collections.unmodifiableMap(new HashMap<HostType, String>() {{ 
                put(HostType.PRODUCTION, HandlerFactoryUtil.PD_CSA_ADDRESS);
                put(HostType.INTEGRATION, HandlerFactoryUtil.IT_CSA_ADDRESS);
                put(HostType.DEVELOPMENT, HandlerFactoryUtil.DT_CSA_ADDRESS);
            }});    

    public Properties getProp(String filepath){
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(filepath));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return prop;
    }

    protected String getRwsAddress() {
        return rwsAddress;
    }

    protected void setRwsAddress(String rwsAddress) {
        this.rwsAddress = rwsAddress;
    }

    protected String getCsaAddress() {
        return csaAddress;
    }

    protected void setCsaAddress(String csaAddress) {
        this.csaAddress = csaAddress;
    }

    protected String getOriginHeader() {
        return originHeader;
    }

    protected void setOriginHeader(String originHeader) {
        this.originHeader = originHeader;
    }

    protected String getRefereHeader() {
        return refereHeader;
    }

    protected void setRefereHeader(String refereHeader) {
        this.refereHeader = refereHeader;
    }

    protected String getHostHeader() {
        return hostHeader;
    }

    protected void setHostHeader(String hostHeader) {
        this.hostHeader = hostHeader;
    }

    public SwampApiWrapper(HostType host_type, String host_name) {
        setHost(host_type, host_name);
        cachedPkgProjectID = "";
        cachedPkgVersionProjectID = "";
        cachedToolProjectID = "";
    }

    public SwampApiWrapper() throws Exception {
        this(HostType.DEVELOPMENT, null);
    }

    public final void setHost(HostType host_type, String host_name) {
        switch(host_type){
        case PRODUCTION:
            //setRwsAddress(HandlerFactoryUtil.PD_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.PD_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.PD_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.PD_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.PD_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.PD_HOST_HEADER);
            break;
        case INTEGRATION:
            //setRwsAddress(HandlerFactoryUtil.IT_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.IT_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.IT_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.IT_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.IT_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.IT_HOST_HEADER);
            break;
        case CUSTOM:
            setHostName(host_name);
            break;
        case DEVELOPMENT:
        default:
            //setRwsAddress(HandlerFactoryUtil.DT_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.DT_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.DT_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.DT_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.DT_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.DT_HOST_HEADER);
            break;
            //throw new Exception("Server Type Not Found");
        }
    }
    
    public void setHostName(String host_name) {
        if (host_name == null) {
            throw new InvalidIdentifierException("host_name cannot be null");
        }
        setRwsAddress(host_name);
        setCsaAddress(host_name);<commit_msg>Added functionality to get web server address from the front-end server<commit_after>/*
*  java-cli
*
*  Copyright 2016 <NAME> <<EMAIL>>, <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
*
*  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 implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.continuousassurance.swamp.cli;

import edu.uiuc.ncsa.security.util.ssl.SSLConfiguration;
import org.continuousassurance.swamp.api.*;
import org.continuousassurance.swamp.session.Session;
import org.continuousassurance.swamp.session.handlers.HandlerFactory;
import org.continuousassurance.swamp.session.handlers.PackageHandler;
import org.continuousassurance.swamp.session.util.ConversionMapImpl;
import org.continuousassurance.swamp.session.util.SWAMPConfigurationLoader;
import org.continuousassurance.swamp.util.HandlerFactoryUtil;
import net.sf.json.JSONException;
import org.apache.http.client.CookieStore;
import org.continuousassurance.swamp.cli.exceptions.*;

import java.io.*;
import java.util.*;


public class SwampApiWrapper {

    private static final String USER_PATH = System.getProperty("user.home");
    private static final String FILE_SEPARATOR_KEY = System.getProperty("file.separator");
    private static final String SWAMP_DIR_NAME = ".SWAMP_SESSION";
    public static final String SWAMP_DIR_PATH = USER_PATH + FILE_SEPARATOR_KEY + SWAMP_DIR_NAME + FILE_SEPARATOR_KEY;

    private String rwsAddress;
    private String csaAddress;
    private String originHeader;
    private String refereHeader;
    private String hostHeader;
    private Map<String, Project> projectMap;
    private Map<String, Integer> packageTypeMap;
    private Map<String, PackageThing> packageMap;
    private Map<String, PackageVersion> packageVersionMap;
    private Map<String, Tool> toolMap;
    private Map<String, Platform> platformMap;
    private String cachedPkgProjectID;
    private String cachedPkgVersionProjectID;
    private String cachedToolProjectID;

    HandlerFactory handlerFactory;

    public enum HostType {
        PRODUCTION,
        INTEGRATION,
        DEVELOPMENT,
        CUSTOM
    }
    
    public static final Map<HostType, String> SWAMP_HOST_NAMES_MAP = 
            Collections.unmodifiableMap(new HashMap<HostType, String>() {{ 
                put(HostType.PRODUCTION, HandlerFactoryUtil.PD_CSA_ADDRESS);
                put(HostType.INTEGRATION, HandlerFactoryUtil.IT_CSA_ADDRESS);
                put(HostType.DEVELOPMENT, HandlerFactoryUtil.DT_CSA_ADDRESS);
            }});    

    public Properties getProp(String filepath){
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(filepath));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return prop;
    }

    protected String getRwsAddress() {
        return rwsAddress;
    }

    protected void setRwsAddress(String rwsAddress) {
        this.rwsAddress = rwsAddress;
    }

    protected String getCsaAddress() {
        return csaAddress;
    }

    protected void setCsaAddress(String csaAddress) {
        this.csaAddress = csaAddress;
    }

    protected String getOriginHeader() {
        return originHeader;
    }

    protected void setOriginHeader(String originHeader) {
        this.originHeader = originHeader;
    }

    protected String getRefereHeader() {
        return refereHeader;
    }

    protected void setRefereHeader(String refereHeader) {
        this.refereHeader = refereHeader;
    }

    protected String getHostHeader() {
        return hostHeader;
    }

    protected void setHostHeader(String hostHeader) {
        this.hostHeader = hostHeader;
    }

    public SwampApiWrapper(HostType host_type, String host_name) {
        setHost(host_type, host_name);
        cachedPkgProjectID = "";
        cachedPkgVersionProjectID = "";
        cachedToolProjectID = "";
    }

    public SwampApiWrapper() throws Exception {
        this(HostType.PRODUCTION, HandlerFactoryUtil.PD_ORIGIN_HEADER);
    }

    public final void setHost(HostType host_type, String host_name) {
    	
    	String web_server = SWAMPConfigurationLoader.getWebServiceURL(host_name);
    	System.out.println("SWAMP SERVER URL: " + host_name);
    	System.out.println("WEB SERVER URL: " + web_server);
    	System.out.println("WEB SERVER TYPE: " + host_type);
    	
        switch(host_type){
        case PRODUCTION:
            //setRwsAddress(HandlerFactoryUtil.PD_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.PD_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.PD_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.PD_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.PD_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.PD_HOST_HEADER);
            break;
        case INTEGRATION:
            //setRwsAddress(HandlerFactoryUtil.IT_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.IT_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.IT_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.IT_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.IT_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.IT_HOST_HEADER);
            break;
        case CUSTOM:
            setHostName(web_server);
            break;
        case DEVELOPMENT:
        default:
            //setRwsAddress(HandlerFactoryUtil.DT_RWS_ADDRESS);
            setRwsAddress(HandlerFactoryUtil.DT_CSA_ADDRESS);
            setCsaAddress(HandlerFactoryUtil.DT_CSA_ADDRESS);
            setOriginHeader(HandlerFactoryUtil.DT_ORIGIN_HEADER);
            setRefereHeader(HandlerFactoryUtil.DT_REFERER_HEADER);
            setHostHeader(HandlerFactoryUtil.DT_HOST_HEADER);
            break;
            //throw new Exception("Server Type Not Found");
        }
    }
    
    public void setHostName(String host_name) {
        if (host_name == null) {
            throw new InvalidIdentifierException("host_name cannot be null");
        }
        setRwsAddress(host_name);
        setCsaAddress(host_name);
{'fraction_non_alphanumeric': 0.07879123487065896, 'fraction_numerical': 0.0012657226485246421, 'mean_word_length': 3.100551410963347, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6272974', 'n_tokens_mistral': 3597, 'n_tokens_neox': 3499, 'n_words': 760}