<commit_before>/**
 * Hub Common
 *
 * Copyright (C) 2017 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF 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 com.blackducksoftware.integration.hub.validator;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.blackducksoftware.integration.hub.builder.HubCredentialsBuilder;
import com.blackducksoftware.integration.hub.global.HubCredentials;
import com.blackducksoftware.integration.hub.global.HubProxyInfo;
import com.blackducksoftware.integration.hub.global.HubProxyInfoFieldEnum;
import com.blackducksoftware.integration.hub.global.HubServerConfigFieldEnum;
import com.blackducksoftware.integration.validator.AbstractValidator;
import com.blackducksoftware.integration.validator.ValidationResult;
import com.blackducksoftware.integration.validator.ValidationResultEnum;
import com.blackducksoftware.integration.validator.ValidationResults;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HubServerConfigValidator extends AbstractValidator {
    public static final String ERROR_MSG_URL_NOT_FOUND = "No Hub Url was found.";

    public static final String ERROR_MSG_URL_NOT_VALID_PREFIX = "This is not a valid URL : ";

    public static final String ERROR_MSG_UNREACHABLE_PREFIX = "Can not reach this server : ";

    public static final String ERROR_MSG_URL_NOT_VALID = "The Hub Url is not a valid URL.";

    public static int DEFAULT_TIMEOUT_SECONDS = 120;

    private String hubUrl;

    private String timeoutSeconds;

    private String username;

    private String password;

    private int passwordLength;

    private String proxyHost;

    private String proxyPort;

    private String proxyUsername;

    private String proxyPassword;

    private int proxyPasswordLength;

    private String ignoredProxyHosts;

    private HubProxyInfo proxyInfo;

    @Override
    public ValidationResults assertValid() {
        final ValidationResults proxyResult = assertProxyValid();
        final ValidationResults credentialResult = assertCredentialsValid();
        final ValidationResults result = new ValidationResults();
        result.addAllResultsStrings(proxyResult.getResultMap(), proxyResult.getValidationStatus());
        result.addAllResultsStrings(credentialResult.getResultMap(), credentialResult.getValidationStatus());
        validateHubUrl(result);
        validateTimeout(result, null);
        return result;
    }

    public ValidationResults assertProxyValid() {
        final HubProxyValidator validator = new HubProxyValidator();
        validator.setHost(proxyHost);
        validator.setPort(proxyPort);
        validator.setIgnoredProxyHosts(ignoredProxyHosts);
        validator.setUsername(proxyUsername);
        validator.setPassword(<PASSWORD>);
        if (proxyPasswordLength > 0) {
            validator.setPasswordLength(proxyPasswordLength);
        }

        final ValidationResults results = validator.assertValid();
        if (results.isSuccess()) {
            final int port = NumberUtils.toInt(proxyPort);
            if (validator.hasAuthenticatedProxySettings()) {

                final HubCredentialsBuilder credBuilder = new HubCredentialsBuilder();
                credBuilder.setUsername(proxyUsername);
                credBuilder.setPassword(<PASSWORD>);
                credBuilder.setPasswordLength(proxyPasswordLength);
                final HubCredentials credResult = credBuilder.build();

                proxyInfo = new HubProxyInfo(proxyHost, port, credResult, ignoredProxyHosts);

            } else {
                // password is blank or already encrypted so we just pass in the
                // values given to us
                proxyInfo = new HubProxyInfo(proxyHost, port, null, ignoredProxyHosts);
            }
        }
        return results;
    }

    public ValidationResults assertCredentialsValid() {
        final HubCredentialsValidator credentialsBuilder = new HubCredentialsValidator();
        credentialsBuilder.setUsername(username);
        credentialsBuilder.setPassword(password);
        return credentialsBuilder.assertValid();
    }

    public void validateHubUrl(final ValidationResults result) {
        if (hubUrl == null) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_FOUND));
            return;
        }

        URL hubURL = null;
        try {
            hubURL = new URL(hubUrl);
            hubURL.toURI();
        } catch (final MalformedURLException e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_VALID));
        } catch (final URISyntaxException e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_VALID));
        }

        if (hubURL == null) {
            return;
        }

        final HttpUrl url = HttpUrl.parse(hubUrl);

        final OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.connectTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        builder.writeTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        builder.readTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        if (proxyInfo != null) {
            builder.proxy(proxyInfo.getProxy(hubURL));
            String password = null;
            try {
                password = proxyInfo.getDecryptedPassword();
            } catch (final Exception e) {
                result.addResult(HubProxyInfoFieldEnum.PROXYPASSWORD,
                        new ValidationResult(ValidationResultEnum.ERROR, e.getMessage(), e));
                return;
            }
            if (StringUtils.isNotBlank(proxyInfo.getUsername()) && StringUtils.isNotBlank(password)) {
                builder.proxyAuthenticator(
                        new com.blackducksoftware.integration.hub.proxy.OkAuthenticator(proxyInfo.getUsername(),
                                password));
            }
        }
        try {
            final OkHttpClient client = builder.build();

            final Request request = new Request.Builder()
                    .url(url).get().build();

            Response response = null;
            try {
                response = client.newCall(request).execute();

                if (!response.isSuccessful()) {
                    if (response.code() == 407) {
                        result.addResult(HubProxyInfoFieldEnum.PROXYUSERNAME,
                                new ValidationResult(ValidationResultEnum.ERROR, response.message()));
                        return;
                    }
                    result.addResult(HubServerConfigFieldEnum.HUBURL,
                            new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_UNREACHABLE_PREFIX + hubUrl));

                }
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (final Exception e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_UNREACHABLE_PREFIX + hubUrl, e));
        }
    }

    private int stringToNonNegativeInteger(final String intString) {
        try {
            final int intValue = stringToInteger(intString);
            if (intValue < 0) {
                return 0;
            }
            return intValue;
        } catch (final Exception e) {
            return 0;
        }
    }<commit_msg>Fixing Validator in case User did not call assertProxyValid first<commit_after>/**
 * Hub Common
 *
 * Copyright (C) 2017 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF 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 com.blackducksoftware.integration.hub.validator;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.blackducksoftware.integration.hub.builder.HubCredentialsBuilder;
import com.blackducksoftware.integration.hub.global.HubCredentials;
import com.blackducksoftware.integration.hub.global.HubProxyInfo;
import com.blackducksoftware.integration.hub.global.HubProxyInfoFieldEnum;
import com.blackducksoftware.integration.hub.global.HubServerConfigFieldEnum;
import com.blackducksoftware.integration.validator.AbstractValidator;
import com.blackducksoftware.integration.validator.ValidationResult;
import com.blackducksoftware.integration.validator.ValidationResultEnum;
import com.blackducksoftware.integration.validator.ValidationResults;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HubServerConfigValidator extends AbstractValidator {
    public static final String ERROR_MSG_URL_NOT_FOUND = "No Hub Url was found.";

    public static final String ERROR_MSG_URL_NOT_VALID_PREFIX = "This is not a valid URL : ";

    public static final String ERROR_MSG_UNREACHABLE_PREFIX = "Can not reach this server : ";

    public static final String ERROR_MSG_URL_NOT_VALID = "The Hub Url is not a valid URL.";

    public static int DEFAULT_TIMEOUT_SECONDS = 120;

    private String hubUrl;

    private String timeoutSeconds;

    private String username;

    private String password;

    private int passwordLength;

    private String proxyHost;

    private String proxyPort;

    private String proxyUsername;

    private String proxyPassword;

    private int proxyPasswordLength;

    private String ignoredProxyHosts;

    private HubProxyInfo proxyInfo;

    @Override
    public ValidationResults assertValid() {
        final ValidationResults proxyResult = assertProxyValid();
        final ValidationResults credentialResult = assertCredentialsValid();
        final ValidationResults result = new ValidationResults();
        result.addAllResultsStrings(proxyResult.getResultMap(), proxyResult.getValidationStatus());
        result.addAllResultsStrings(credentialResult.getResultMap(), credentialResult.getValidationStatus());
        validateHubUrl(result);
        validateTimeout(result, null);
        return result;
    }

    public ValidationResults assertProxyValid() {
        final HubProxyValidator validator = new HubProxyValidator();
        validator.setHost(proxyHost);
        validator.setPort(proxyPort);
        validator.setIgnoredProxyHosts(ignoredProxyHosts);
        validator.setUsername(proxyUsername);
        validator.setPassword(<PASSWORD>);
        if (proxyPasswordLength > 0) {
            validator.setPasswordLength(proxyPasswordLength);
        }

        final ValidationResults results = validator.assertValid();
        if (results.isSuccess()) {
            final int port = NumberUtils.toInt(proxyPort);
            if (validator.hasAuthenticatedProxySettings()) {

                final HubCredentialsBuilder credBuilder = new HubCredentialsBuilder();
                credBuilder.setUsername(proxyUsername);
                credBuilder.setPassword(<PASSWORD>);
                credBuilder.setPasswordLength(proxyPasswordLength);
                final HubCredentials credResult = credBuilder.build();

                proxyInfo = new HubProxyInfo(proxyHost, port, credResult, ignoredProxyHosts);

            } else {
                // password is blank or already encrypted so we just pass in the
                // values given to us
                proxyInfo = new HubProxyInfo(proxyHost, port, null, ignoredProxyHosts);
            }
        }
        return results;
    }

    public ValidationResults assertCredentialsValid() {
        final HubCredentialsValidator credentialsBuilder = new HubCredentialsValidator();
        credentialsBuilder.setUsername(username);
        credentialsBuilder.setPassword(password);
        return credentialsBuilder.assertValid();
    }

    public void validateHubUrl(final ValidationResults result) {
        if (hubUrl == null) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_FOUND));
            return;
        }
        if (proxyHost != null && proxyPort != null && proxyInfo == null) {
            assertProxyValid();
        }
        URL hubURL = null;
        try {
            hubURL = new URL(hubUrl);
            hubURL.toURI();
        } catch (final MalformedURLException e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_VALID));
        } catch (final URISyntaxException e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_URL_NOT_VALID));
        }
        if (hubURL == null) {
            return;
        }
        final HttpUrl url = HttpUrl.parse(hubUrl);
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        builder.writeTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        builder.readTimeout(stringToNonNegativeInteger(timeoutSeconds), TimeUnit.SECONDS);
        if (proxyInfo != null) {
            builder.proxy(proxyInfo.getProxy(hubURL));
            String password = <PASSWORD>;
            try {
                password = proxyInfo.getDecryptedPassword();
            } catch (final Exception e) {
                result.addResult(HubProxyInfoFieldEnum.PROXYPASSWORD,
                        new ValidationResult(ValidationResultEnum.ERROR, e.getMessage(), e));
                return;
            }
            if (StringUtils.isNotBlank(proxyInfo.getUsername()) && StringUtils.isNotBlank(password)) {
                builder.proxyAuthenticator(
                        new com.blackducksoftware.integration.hub.proxy.OkAuthenticator(proxyInfo.getUsername(),
                                password));
            }
        }
        try {
            final OkHttpClient client = builder.build();

            final Request request = new Request.Builder()
                    .url(url).get().build();
            Response response = null;
            try {
                response = client.newCall(request).execute();
                if (!response.isSuccessful()) {
                    if (response.code() == 407) {
                        result.addResult(HubProxyInfoFieldEnum.PROXYUSERNAME,
                                new ValidationResult(ValidationResultEnum.ERROR, response.message()));
                        return;
                    }
                    result.addResult(HubServerConfigFieldEnum.HUBURL,
                            new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_UNREACHABLE_PREFIX + hubUrl));
                }
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (final Exception e) {
            result.addResult(HubServerConfigFieldEnum.HUBURL,
                    new ValidationResult(ValidationResultEnum.ERROR, ERROR_MSG_UNREACHABLE_PREFIX + hubUrl, e));
        }
    }

    private int stringToNonNegativeInteger(final String intString) {
        try {
            final int intValue = stringToInteger(intString);
            if (intValue < 0) {
                return 0;
            }
            return intValue;
        } catch (final Exception e) {
            return 0;
        }
    }
{'fraction_non_alphanumeric': 0.07638059489279418, 'fraction_numerical': 0.002729909571745436, 'mean_word_length': 2.635311143270622, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2148818', 'n_tokens_mistral': 4437, 'n_tokens_neox': 4223, 'n_words': 1049}