/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.core.configs;

import com.epam.ta.reportportal.auth.UatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;

/**
 * Configuration of clients for other services
 *
 * @author <a href="mailto:andrei_varabyeu@epam.com">Andrei Varabyeu</a>
 */
@Configuration
public class ServicesConfiguration {

	@LoadBalanced
	@Bean
	public OAuth2RestTemplate loadBalancedOauth2RestTemplate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext context) {
		return new OAuth2RestTemplate(resource, context);
	}

	@Bean
	public UatClient uatClient(@Qualifier("loadBalancedOauth2RestTemplate") OAuth2RestTemplate restTemplate) {
		return new UatClient(restTemplate);
	}
}

@@ -929,25 +929,174 @@
 otation.
-Qualifier
+Autowired;%0Aimport org.springframework.beans.factory.annotation.Qualifier;%0Aimport org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties
 ;%0Aimport
@@ -1500,16 +1500,119 @@
 Details;
+%0Aimport org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
 %0A%0A/**%0A *
@@ -1789,16 +1789,84 @@
 tion %7B%0A%0A
+%09@Autowired%0A%09private OAuth2ClientProperties oauthClientProperties;%0A%0A
 %09@LoadBa
@@ -1906,34 +1906,26 @@
 emplate 
-loadBalancedOauth2
+rpInternal
 RestTemp
@@ -2007,46 +2007,508 @@
 %7B%0A%09%09
-return new OAuth2RestTemplate(resource
+ClientCredentialsResourceDetails clientCredentialsResourceDetails = new ClientCredentialsResourceDetails();%0A%09%09clientCredentialsResourceDetails.setClientId(oauthClientProperties.getClientId());%0A%09%09clientCredentialsResourceDetails.setClientSecret(oauthClientProperties.getClientSecret());%0A%09%09clientCredentialsResourceDetails.setAccessTokenUri(resource.getAccessTokenUri());%0A%09%09clientCredentialsResourceDetails.setScope(resource.getScope());%0A%09%09return new OAuth2RestTemplate(clientCredentialsResourceDetails
 , co
@@ -2570,26 +2570,18 @@
 er(%22
-loadBalancedOauth2
+rpInternal
 Rest

update services configuration