<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Coverity, Inc

 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Coverity, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity;

import com.coverity.ws.v6.ConfigurationService;
import com.coverity.ws.v6.ConfigurationServiceService;
import com.coverity.ws.v6.CovRemoteServiceException_Exception;
import com.coverity.ws.v6.DefectService;
import com.coverity.ws.v6.DefectServiceService;
import com.coverity.ws.v6.MergedDefectDataObj;
import com.coverity.ws.v6.MergedDefectFilterSpecDataObj;
import com.coverity.ws.v6.MergedDefectsPageDataObj;
import com.coverity.ws.v6.PageSpecDataObj;
import com.coverity.ws.v6.ProjectDataObj;
import com.coverity.ws.v6.ProjectFilterSpecDataObj;
import com.coverity.ws.v6.StreamDataObj;
import com.coverity.ws.v6.StreamFilterSpecDataObj;
import com.coverity.ws.v6.StreamIdDataObj;
import hudson.model.Hudson;
import hudson.util.FormValidation;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Represents one Coverity Integrity Manager server. Abstracts functions like getting streams and defects.
 */
public class CIMInstance {

    public static final String COVERITY_V5_NAMESPACE = "http://ws.coverity.com/v6";
    public static final String COVERITY_V9_NAMESPACE = "http://ws.coverity.com/v9";

    public static final String CONFIGURATION_SERVICE_V5_WSDL = "/ws/v6/configurationservice?wsdl";
    public static final String DEFECT_SERVICE_V5_WSDL = "/ws/v6/defectservice?wsdl";

    public static final String CONFIGURATION_SERVICE_V9_WSDL = "/ws/v9/configurationservice?wsdl";
    public static final String DEFECT_SERVICE_V9_WSDL = "/ws/v9/defectservice?wsdl";

    /**
     * Pattern to ignore streams - this is used to filter out internal DA streams, which are irrelevant to this plugin
     */
    public static final String STREAM_NAME_IGNORE_PATTERN = "__internal_.*";

    /**
     * The id for this instance, used as a key in CoverityPublisher
     */
    private final String name;

    /**
     * The host name for the CIM server
     */
    private final String host;

    /**
     * The port for the CIM server (this is the HTTP port and not the data port)
     */
    private final int port;

    /**
     * The commit port for the CIM server. This is only for cov-commit-defects.
     */
    private final int dataPort;

    /**
     * Username for connecting to the CIM server
     */
    private final String user;

    /**
     * Password for connecting to the CIM server
     */
    private final String password;

    /**
     * Use SSL
     */
    private final boolean useSSL;

    /**
     * cached webservice port for Defect service
     */
    private transient com.coverity.ws.v6.DefectServiceService defectServiceService;

    /**
     * cached webservice port for Configuration service
     */
    private transient com.coverity.ws.v6.ConfigurationServiceService configurationServiceService;

    /**
     * cached webservice port for Defect service for Indio
     */
    private transient com.coverity.ws.v9.DefectServiceService defectServiceServiceIndio;

    /**
     * cached webservice port for Configuration service for Indio
     */
    private transient com.coverity.ws.v9.ConfigurationServiceService configurationServiceServiceIndio;

    @DataBoundConstructor
    public CIMInstance(String name, String host, int port, String user, String password, boolean useSSL, int dataPort) {
        this.name = name;
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.useSSL = useSSL;
        this.dataPort = dataPort;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getName() {
        return name;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public boolean isUseSSL() {
        return useSSL;
    }

    public int getDataPort() {
        return dataPort;
    }

    /**
     * The root URL for the CIM instance
     *
     * @return a url
     * @throws MalformedURLException should not happen if host is valid
     */
    public URL getURL() throws MalformedURLException {
        return new URL(isUseSSL() ? "https" : "http", host, port, "/");
    }

    /**
     * Returns a Defect service client using v6 web services.
     */
    public com.coverity.ws.v6.DefectService getDefectService() throws IOException {
        synchronized(this) {
            if(defectServiceService == null) {
                defectServiceService = new com.coverity.ws.v6.DefectServiceService(
                        new URL(getURL(), DEFECT_SERVICE_V5_WSDL),
                        new QName(COVERITY_V5_NAMESPACE, "DefectServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            com.coverity.ws.v6.DefectService defectService = defectServiceService.getDefectServicePort();
            attachAuthenticationHandler((BindingProvider) defectService);

            return defectService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Returns a Defect service client using v9 web services.
     */
    public com.coverity.ws.v9.DefectService getDefectServiceIndio() throws IOException {
        synchronized(this) {
            if(defectServiceService == null) {
                defectServiceService = new com.coverity.ws.v6.DefectServiceService(
                        new URL(getURL(), DEFECT_SERVICE_V5_WSDL),
                        new QName(COVERITY_V5_NAMESPACE, "DefectServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            com.coverity.ws.v9.DefectService defectService = defectServiceServiceIndio.getDefectServicePort();
            attachAuthenticationHandler((BindingProvider) defectService);

            return defectService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Attach an authentication handler to the web service, that uses the configured user and password
     */
    private void attachAuthenticationHandler(BindingProvider service) {
        service.getBinding().setHandlerChain(Arrays.<Handler>asList(new ClientAuthenticationHandlerWSS(user, password)));
    }

    /**
     * Returns a Configuration service client using v6 web services.
     */
    public ConfigurationService getConfigurationService() throws IOException {
        synchronized(this) {
            if(configurationServiceService == null) {
                // Create a Web Services port to the server
                configurationServiceService = new ConfigurationServiceService(
                        new URL(getURL(), CONFIGURATION_SERVICE_V5_WSDL),
                        new QName(COVERITY_V5_NAMESPACE, "ConfigurationServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            ConfigurationService configurationService = configurationServiceService.getConfigurationServicePort();
            attachAuthenticationHandler((BindingProvider) configurationService);

            return configurationService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Returns a Configuration service client using v9 web services.
     */
    public com.coverity.ws.v9.ConfigurationService getConfigurationServiceIndio() throws IOException {
        synchronized(this) {
            if(configurationServiceService == null) {
                // Create a Web Services port to the server
                configurationServiceService = new ConfigurationServiceService(
                        new URL(getURL(), CONFIGURATION_SERVICE_V9_WSDL),
                        new QName(COVERITY_V9_NAMESPACE, "ConfigurationServiceService"));
            }
        }
<commit_msg>Fix:76606<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Coverity, Inc

 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Coverity, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity;

import com.coverity.ws.v6.ConfigurationService;
import com.coverity.ws.v6.ConfigurationServiceService;
import com.coverity.ws.v6.CovRemoteServiceException_Exception;
import com.coverity.ws.v6.DefectService;
import com.coverity.ws.v6.DefectServiceService;
import com.coverity.ws.v6.MergedDefectDataObj;
import com.coverity.ws.v6.MergedDefectFilterSpecDataObj;
import com.coverity.ws.v6.MergedDefectsPageDataObj;
import com.coverity.ws.v6.PageSpecDataObj;
import com.coverity.ws.v6.ProjectDataObj;
import com.coverity.ws.v6.ProjectFilterSpecDataObj;
import com.coverity.ws.v6.StreamDataObj;
import com.coverity.ws.v6.StreamFilterSpecDataObj;
import com.coverity.ws.v6.StreamIdDataObj;
import hudson.model.Hudson;
import hudson.util.FormValidation;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Represents one Coverity Integrity Manager server. Abstracts functions like getting streams and defects.
 */
public class CIMInstance {

    public static final String COVERITY_V5_NAMESPACE = "http://ws.coverity.com/v6";
    public static final String COVERITY_V9_NAMESPACE = "http://ws.coverity.com/v9";

    public static final String CONFIGURATION_SERVICE_V5_WSDL = "/ws/v6/configurationservice?wsdl";
    public static final String DEFECT_SERVICE_V5_WSDL = "/ws/v6/defectservice?wsdl";

    public static final String CONFIGURATION_SERVICE_V9_WSDL = "/ws/v9/configurationservice?wsdl";
    public static final String DEFECT_SERVICE_V9_WSDL = "/ws/v9/defectservice?wsdl";

    /**
     * Pattern to ignore streams - this is used to filter out internal DA streams, which are irrelevant to this plugin
     */
    public static final String STREAM_NAME_IGNORE_PATTERN = "__internal_.*";

    /**
     * The id for this instance, used as a key in CoverityPublisher
     */
    private final String name;

    /**
     * The host name for the CIM server
     */
    private final String host;

    /**
     * The port for the CIM server (this is the HTTP port and not the data port)
     */
    private final int port;

    /**
     * The commit port for the CIM server. This is only for cov-commit-defects.
     */
    private final int dataPort;

    /**
     * Username for connecting to the CIM server
     */
    private final String user;

    /**
     * Password for connecting to the CIM server
     */
    private final String password;

    /**
     * Use SSL
     */
    private final boolean useSSL;

    /**
     * cached webservice port for Defect service
     */
    private transient com.coverity.ws.v6.DefectServiceService defectServiceService;

    /**
     * cached webservice port for Configuration service
     */
    private transient com.coverity.ws.v6.ConfigurationServiceService configurationServiceService;

    /**
     * cached webservice port for Defect service for Indio
     */
    private transient com.coverity.ws.v9.DefectServiceService defectServiceServiceIndio;

    /**
     * cached webservice port for Configuration service for Indio
     */
    private transient com.coverity.ws.v9.ConfigurationServiceService configurationServiceServiceIndio;

    @DataBoundConstructor
    public CIMInstance(String name, String host, int port, String user, String password, boolean useSSL, int dataPort) {
        this.name = name;
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.useSSL = useSSL;
        this.dataPort = dataPort;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getName() {
        return name;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public boolean isUseSSL() {
        return useSSL;
    }

    public int getDataPort() {
        return dataPort;
    }

    /**
     * The root URL for the CIM instance
     *
     * @return a url
     * @throws MalformedURLException should not happen if host is valid
     */
    public URL getURL() throws MalformedURLException {
        return new URL(isUseSSL() ? "https" : "http", host, port, "/");
    }

    /**
     * Returns a Defect service client using v6 web services.
     */
    public com.coverity.ws.v6.DefectService getDefectService() throws IOException {
        synchronized(this) {
            if(defectServiceService == null) {
                defectServiceService = new com.coverity.ws.v6.DefectServiceService(
                        new URL(getURL(), DEFECT_SERVICE_V5_WSDL),
                        new QName(COVERITY_V5_NAMESPACE, "DefectServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            com.coverity.ws.v6.DefectService defectService = defectServiceService.getDefectServicePort();
            attachAuthenticationHandler((BindingProvider) defectService);

            return defectService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Returns a Defect service client using v9 web services.
     */
    public com.coverity.ws.v9.DefectService getDefectServiceIndio() throws IOException {
        synchronized(this) {
            if(defectServiceServiceIndio == null) {
                defectServiceServiceIndio = new com.coverity.ws.v9.DefectServiceService(
                        new URL(getURL(), DEFECT_SERVICE_V9_WSDL),
                        new QName(COVERITY_V9_NAMESPACE, "DefectServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            com.coverity.ws.v9.DefectService defectService = defectServiceServiceIndio.getDefectServicePort();
            attachAuthenticationHandler((BindingProvider) defectService);

            return defectService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Attach an authentication handler to the web service, that uses the configured user and password
     */
    private void attachAuthenticationHandler(BindingProvider service) {
        service.getBinding().setHandlerChain(Arrays.<Handler>asList(new ClientAuthenticationHandlerWSS(user, password)));
    }

    /**
     * Returns a Configuration service client using v6 web services.
     */
    public ConfigurationService getConfigurationService() throws IOException {
        synchronized(this) {
            if(configurationServiceService == null) {
                // Create a Web Services port to the server
                configurationServiceService = new ConfigurationServiceService(
                        new URL(getURL(), CONFIGURATION_SERVICE_V5_WSDL),
                        new QName(COVERITY_V5_NAMESPACE, "ConfigurationServiceService"));
            }
        }

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            ConfigurationService configurationService = configurationServiceService.getConfigurationServicePort();
            attachAuthenticationHandler((BindingProvider) configurationService);

            return configurationService;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }

    /**
     * Returns a Configuration service client using v9 web services.
     */
    public com.coverity.ws.v9.ConfigurationService getConfigurationServiceIndio() throws IOException {
        synchronized(this) {
            if(configurationServiceServiceIndio == null) {
                // Create a Web Services port to the server
                configurationServiceServiceIndio = new com.coverity.ws.v9.ConfigurationServiceService(
                        new URL(getURL(), CONFIGURATION_SERVICE_V9_WSDL),
                        new QName(COVERITY_V9_NAMESPACE, "ConfigurationServiceService"));
            }
        }

{'fraction_non_alphanumeric': 0.09911710811261036, 'fraction_numerical': 0.006663334999167083, 'mean_word_length': 2.8441835645677696, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '4936254', 'n_tokens_mistral': 4857, 'n_tokens_neox': 4737, 'n_words': 1198}