<commit_before>/*
 * Copyright (c) 2019, 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.analytics.apim.idp.client;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.analytics.idp.client.core.api.AnalyticsHttpClientBuilderService;
import org.wso2.carbon.analytics.idp.client.core.api.IdPClient;
import org.wso2.carbon.analytics.idp.client.core.exception.IdPClientException;
import org.wso2.carbon.analytics.idp.client.core.spi.IdPClientFactory;
import org.wso2.carbon.analytics.idp.client.core.utils.IdPClientConstants;
import org.wso2.carbon.analytics.idp.client.core.utils.config.IdPClientConfiguration;
import org.wso2.carbon.analytics.idp.client.external.impl.DCRMServiceStub;
import org.wso2.carbon.analytics.idp.client.external.impl.OAuth2ServiceStubs;
import org.wso2.carbon.analytics.idp.client.external.models.OAuthApplicationInfo;
import org.wso2.carbon.datasource.core.api.DataSourceService;
import org.wso2.carbon.secvault.SecretRepository;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Factory for APIM IdPClient.
 */
@Component(
        name = "org.wso2.analytics.apim.idp.client.ApimIdPClientFactory",
        immediate = true
)
public class ApimIdPClientFactory implements IdPClientFactory {
    private static final Logger LOG = LoggerFactory.getLogger(ApimIdPClientFactory.class);
    private DataSourceService dataSourceService;
    private SecretRepository secretRepository;
    private AnalyticsHttpClientBuilderService analyticsHttpClientBuilderService;

    @Activate
    protected void activate(BundleContext bundleContext) {
        LOG.debug("APIM IDP client factory activated.");
    }

    @Deactivate
    protected void deactivate(BundleContext bundleContext) {
        LOG.debug("APIM IDP client factory deactivated.");
    }

    /**
     * Register datasource service.
     *
     * @param dataSourceService
     */
    @Reference(
            name = "org.wso2.carbon.datasource.DataSourceService",
            service = DataSourceService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterDataSourceService"
    )
    protected void registerDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = dataSourceService;
    }

    /**
     * Unregister datasource service.
     *
     * @param dataSourceService datasource service
     */
    protected void unregisterDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = null;
    }

    /**
     * Register secret repository.
     *
     * @param secretRepository
     */
    @Reference(
            name = "org.wso2.carbon.secvault.repository.DefaultSecretRepository",
            service = SecretRepository.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterSecretRepository"
    )
    protected void registerSecretRepository(SecretRepository secretRepository) {
        this.secretRepository = secretRepository;
    }

    /**
     * Unregister secret repository.
     *
     * @param secretRepository
     */
    protected void unregisterSecretRepository(SecretRepository secretRepository) {
        this.secretRepository = null;
    }

    @Reference(
            service = AnalyticsHttpClientBuilderService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterAnalyticsHttpClient"
    )
    protected void registerAnalyticsHttpClient(AnalyticsHttpClientBuilderService service) {
        this.analyticsHttpClientBuilderService = service;
        LOG.debug("@Reference(bind) AnalyticsHttpClientBuilderService at '{}'",
                AnalyticsHttpClientBuilderService.class.getName());
    }

    protected void unregisterAnalyticsHttpClient(AnalyticsHttpClientBuilderService service) {
        LOG.debug("@Reference(unbind) AnalyticsHttpClientBuilderService at '{}'",
                AnalyticsHttpClientBuilderService.class.getName());
        this.analyticsHttpClientBuilderService = null;
    }

    @Override
    public String getType() {
        return ApimIdPClientConstants.EXTERNAL_IDP_CLIENT_TYPE;
    }

    @Override
    public IdPClient getIdPClient(IdPClientConfiguration idPClientConfiguration)
            throws IdPClientException {
        Map<String, String> properties = idPClientConfiguration.getProperties();
        String adminServiceUsername = properties.getOrDefault(ApimIdPClientConstants.ADMIN_USERNAME,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_USERNAME);
        String adminServicePassword = properties.getOrDefault(ApimIdPClientConstants.ADMIN_PASSWORD,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_PASSWORD);
        String adminServiceBaseUrl = properties.getOrDefault(ApimIdPClientConstants.ADMIN_SERVICE_BASE_URL,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_BASE_URL);
        String adminScopeName = properties.getOrDefault(ApimIdPClientConstants.ADMIN_SCOPE,
                ApimIdPClientConstants.DEFAULT_ADMIN_SCOPE);
        String allScopes = properties.getOrDefault(ApimIdPClientConstants.ALL_SCOPES,
                ApimIdPClientConstants.DEFAULT_ALL_SCOPES);

        String dcrEndpoint = properties.getOrDefault(ApimIdPClientConstants.KM_DCR_URL,
                ApimIdPClientConstants.DEFAULT_KM_DCR_URL);
        String kmUsername = properties.getOrDefault(ApimIdPClientConstants.KM_USERNAME,
                ApimIdPClientConstants.DEFAULT_KM_USERNAME);
        String kmPassword = properties.getOrDefault(ApimIdPClientConstants.KM_PASSWORD,
                ApimIdPClientConstants.DEFAULT_KM_PASSWORD);
        String kmTokenUrl = properties.getOrDefault(ApimIdPClientConstants.KM_TOKEN_URL,
                ApimIdPClientConstants.DEFAULT_KM_TOKEN_URL);
        String dcrAppOwner = properties.getOrDefault(ApimIdPClientConstants.DCR_APP_OWNER, kmUsername);
        String introspectUrl = properties.getOrDefault(ApimIdPClientConstants.INTROSPECTION_URL,
                kmTokenUrl + ApimIdPClientConstants.INTROSPECT_POSTFIX);

        String baseUrl = properties.getOrDefault(ApimIdPClientConstants.BASE_URL,
                ApimIdPClientConstants.DEFAULT_BASE_URL);
        String grantType = properties.getOrDefault(ApimIdPClientConstants.GRANT_TYPE,
                IdPClientConstants.PASSWORD_GRANT_TYPE);


        String portalAppContext = properties.getOrDefault(ApimIdPClientConstants.PORTAL_APP_CONTEXT,
                ApimIdPClientConstants.DEFAULT_PORTAL_APP_CONTEXT);
        String businessAppContext = properties.getOrDefault(ApimIdPClientConstants.BR_DB_APP_CONTEXT,
                ApimIdPClientConstants.DEFAULT_BR_DB_APP_CONTEXT);

        OAuthApplicationInfo spOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.SP_APP_NAME,
                properties.get(ApimIdPClientConstants.SP_CLIENT_ID),
                properties.get(ApimIdPClientConstants.SP_CLIENT_SECRET));
        OAuthApplicationInfo portalOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.PORTAL_APP_NAME,
                properties.get(ApimIdPClientConstants.PORTAL_CLIENT_ID),
                properties.get(ApimIdPClientConstants.PORTAL_CLIENT_SECRET));
        OAuthApplicationInfo businessOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.BR_DB_APP_NAME,
                properties.get(ApimIdPClientConstants.BR_DB_CLIENT_ID),
                properties.get(ApimIdPClientConstants.BR_DB_CLIENT_SECRET));

        Map<String, OAuthApplicationInfo> oAuthAppInfoMap = new HashMap<>();
        oAuthAppInfoMap.put(ApimIdPClientConstants.DEFAULT_SP_APP_CONTEXT, spOAuthApp);
        oAuthAppInfoMap.put(portalAppContext, portalOAuthApp);
        oAuthAppInfoMap.put(businessAppContext, businessOAuthApp);

        if (adminServiceBaseUrl == null || adminServiceBaseUrl.isEmpty()) {
            String error = "Admin service base url cannot be empty. Please provide an admin service base url in the " +
                    "deployment.yaml file.";
            LOG.error(error);
            throw new IdPClientException(error);
        }
        URI uri;
        try {<commit_msg>Use StringUtils isNullOrEmpty() for null and empty check<commit_after>/*
 * Copyright (c) 2019, 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.analytics.apim.idp.client;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.analytics.idp.client.core.api.AnalyticsHttpClientBuilderService;
import org.wso2.carbon.analytics.idp.client.core.api.IdPClient;
import org.wso2.carbon.analytics.idp.client.core.exception.IdPClientException;
import org.wso2.carbon.analytics.idp.client.core.spi.IdPClientFactory;
import org.wso2.carbon.analytics.idp.client.core.utils.IdPClientConstants;
import org.wso2.carbon.analytics.idp.client.core.utils.config.IdPClientConfiguration;
import org.wso2.carbon.analytics.idp.client.external.impl.DCRMServiceStub;
import org.wso2.carbon.analytics.idp.client.external.impl.OAuth2ServiceStubs;
import org.wso2.carbon.analytics.idp.client.external.models.OAuthApplicationInfo;
import org.wso2.carbon.datasource.core.api.DataSourceService;
import org.wso2.carbon.secvault.SecretRepository;
import org.wso2.carbon.utils.StringUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Factory for APIM IdPClient.
 */
@Component(
        name = "org.wso2.analytics.apim.idp.client.ApimIdPClientFactory",
        immediate = true
)
public class ApimIdPClientFactory implements IdPClientFactory {
    private static final Logger LOG = LoggerFactory.getLogger(ApimIdPClientFactory.class);
    private DataSourceService dataSourceService;
    private SecretRepository secretRepository;
    private AnalyticsHttpClientBuilderService analyticsHttpClientBuilderService;

    @Activate
    protected void activate(BundleContext bundleContext) {
        LOG.debug("APIM IDP client factory activated.");
    }

    @Deactivate
    protected void deactivate(BundleContext bundleContext) {
        LOG.debug("APIM IDP client factory deactivated.");
    }

    /**
     * Register datasource service.
     *
     * @param dataSourceService
     */
    @Reference(
            name = "org.wso2.carbon.datasource.DataSourceService",
            service = DataSourceService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterDataSourceService"
    )
    protected void registerDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = dataSourceService;
    }

    /**
     * Unregister datasource service.
     *
     * @param dataSourceService datasource service
     */
    protected void unregisterDataSourceService(DataSourceService dataSourceService) {
        this.dataSourceService = null;
    }

    /**
     * Register secret repository.
     *
     * @param secretRepository
     */
    @Reference(
            name = "org.wso2.carbon.secvault.repository.DefaultSecretRepository",
            service = SecretRepository.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterSecretRepository"
    )
    protected void registerSecretRepository(SecretRepository secretRepository) {
        this.secretRepository = secretRepository;
    }

    /**
     * Unregister secret repository.
     *
     * @param secretRepository
     */
    protected void unregisterSecretRepository(SecretRepository secretRepository) {
        this.secretRepository = null;
    }

    @Reference(
            service = AnalyticsHttpClientBuilderService.class,
            cardinality = ReferenceCardinality.MANDATORY,
            policy = ReferencePolicy.DYNAMIC,
            unbind = "unregisterAnalyticsHttpClient"
    )
    protected void registerAnalyticsHttpClient(AnalyticsHttpClientBuilderService service) {
        this.analyticsHttpClientBuilderService = service;
        LOG.debug("@Reference(bind) AnalyticsHttpClientBuilderService at '{}'",
                AnalyticsHttpClientBuilderService.class.getName());
    }

    protected void unregisterAnalyticsHttpClient(AnalyticsHttpClientBuilderService service) {
        LOG.debug("@Reference(unbind) AnalyticsHttpClientBuilderService at '{}'",
                AnalyticsHttpClientBuilderService.class.getName());
        this.analyticsHttpClientBuilderService = null;
    }

    @Override
    public String getType() {
        return ApimIdPClientConstants.EXTERNAL_IDP_CLIENT_TYPE;
    }

    @Override
    public IdPClient getIdPClient(IdPClientConfiguration idPClientConfiguration)
            throws IdPClientException {
        Map<String, String> properties = idPClientConfiguration.getProperties();
        String adminServiceUsername = properties.getOrDefault(ApimIdPClientConstants.ADMIN_USERNAME,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_USERNAME);
        String adminServicePassword = properties.getOrDefault(ApimIdPClientConstants.ADMIN_PASSWORD,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_PASSWORD);
        String adminServiceBaseUrl = properties.getOrDefault(ApimIdPClientConstants.ADMIN_SERVICE_BASE_URL,
                ApimIdPClientConstants.DEFAULT_ADMIN_SERVICE_BASE_URL);
        String adminScopeName = properties.getOrDefault(ApimIdPClientConstants.ADMIN_SCOPE,
                ApimIdPClientConstants.DEFAULT_ADMIN_SCOPE);
        String allScopes = properties.getOrDefault(ApimIdPClientConstants.ALL_SCOPES,
                ApimIdPClientConstants.DEFAULT_ALL_SCOPES);

        String dcrEndpoint = properties.getOrDefault(ApimIdPClientConstants.KM_DCR_URL,
                ApimIdPClientConstants.DEFAULT_KM_DCR_URL);
        String kmUsername = properties.getOrDefault(ApimIdPClientConstants.KM_USERNAME,
                ApimIdPClientConstants.DEFAULT_KM_USERNAME);
        String kmPassword = properties.getOrDefault(ApimIdPClientConstants.KM_PASSWORD,
                ApimIdPClientConstants.DEFAULT_KM_PASSWORD);
        String kmTokenUrl = properties.getOrDefault(ApimIdPClientConstants.KM_TOKEN_URL,
                ApimIdPClientConstants.DEFAULT_KM_TOKEN_URL);
        String dcrAppOwner = properties.getOrDefault(ApimIdPClientConstants.DCR_APP_OWNER, kmUsername);
        String introspectUrl = properties.getOrDefault(ApimIdPClientConstants.INTROSPECTION_URL,
                kmTokenUrl + ApimIdPClientConstants.INTROSPECT_POSTFIX);

        String baseUrl = properties.getOrDefault(ApimIdPClientConstants.BASE_URL,
                ApimIdPClientConstants.DEFAULT_BASE_URL);
        String grantType = properties.getOrDefault(ApimIdPClientConstants.GRANT_TYPE,
                IdPClientConstants.PASSWORD_GRANT_TYPE);


        String portalAppContext = properties.getOrDefault(ApimIdPClientConstants.PORTAL_APP_CONTEXT,
                ApimIdPClientConstants.DEFAULT_PORTAL_APP_CONTEXT);
        String businessAppContext = properties.getOrDefault(ApimIdPClientConstants.BR_DB_APP_CONTEXT,
                ApimIdPClientConstants.DEFAULT_BR_DB_APP_CONTEXT);

        OAuthApplicationInfo spOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.SP_APP_NAME,
                properties.get(ApimIdPClientConstants.SP_CLIENT_ID),
                properties.get(ApimIdPClientConstants.SP_CLIENT_SECRET));
        OAuthApplicationInfo portalOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.PORTAL_APP_NAME,
                properties.get(ApimIdPClientConstants.PORTAL_CLIENT_ID),
                properties.get(ApimIdPClientConstants.PORTAL_CLIENT_SECRET));
        OAuthApplicationInfo businessOAuthApp = new OAuthApplicationInfo(
                ApimIdPClientConstants.BR_DB_APP_NAME,
                properties.get(ApimIdPClientConstants.BR_DB_CLIENT_ID),
                properties.get(ApimIdPClientConstants.BR_DB_CLIENT_SECRET));

        Map<String, OAuthApplicationInfo> oAuthAppInfoMap = new HashMap<>();
        oAuthAppInfoMap.put(ApimIdPClientConstants.DEFAULT_SP_APP_CONTEXT, spOAuthApp);
        oAuthAppInfoMap.put(portalAppContext, portalOAuthApp);
        oAuthAppInfoMap.put(businessAppContext, businessOAuthApp);

        if (StringUtils.isNullOrEmpty(adminServiceBaseUrl)) {
            String error = "Admin service base url cannot be empty. Please provide an admin service base url in the " +
                    "deployment.yaml file.";
            LOG.error(error);
            throw new IdPClientException(error);
        }
        URI uri;
        try {
{'fraction_non_alphanumeric': 0.07349429556911648, 'fraction_numerical': 0.003130803926771027, 'mean_word_length': 3.5764934434191353, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4251652', 'n_tokens_mistral': 5288, 'n_tokens_neox': 5162, 'n_words': 871}