/*
 * Copyright 2015 the original author or authors.
 *
 * 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 or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.security.oauth2.client;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;

/**
 * @author Dave Syer
 *
 */
@Configuration
@ConditionalOnClass({ LoadBalancerInterceptor.class, OAuth2RestTemplate.class })
@ConditionalOnBean(LoadBalancerInterceptor.class)
@AutoConfigureAfter(OAuth2AutoConfiguration.class)
public class OAuth2LoadBalancerClientAutoConfiguration {

	@Configuration
	@ConditionalOnProperty(value = "security.oauth2.resource.loadBalanced", matchIfMissing = false)
	protected static class UserInfoLoadBalancerConfig {
		@Bean
		public UserInfoRestTemplateCustomizer loadBalancedUserInfoRestTemplateCustomizer(
				final LoadBalancerInterceptor loadBalancerInterceptor) {
			return new UserInfoRestTemplateCustomizer() {
				@Override
				public void customize(OAuth2RestTemplate restTemplate) {
					List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(
							restTemplate.getInterceptors());
					interceptors.add(loadBalancerInterceptor);
					restTemplate.setInterceptors(interceptors);
				}
			};
		}
	}

}

@@ -1277,32 +1277,115 @@
 cerInterceptor;%0A
+import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor;%0A
 import org.sprin
@@ -1416,24 +1416,24 @@
 ation.Bean;%0A
-
 import org.s
@@ -1692,41 +1692,8 @@
 ass(
-%7B LoadBalancerInterceptor.class, 
 OAut
@@ -1716,10 +1716,8 @@
 lass
- %7D
 )%0A@C
@@ -1732,42 +1732,88 @@
 alOn
-Bean(LoadBalancerInterceptor.class
+Property(value = %22security.oauth2.resource.loadBalanced%22, matchIfMissing = false
 )%0A@A
@@ -1954,88 +1954,42 @@
 alOn
-Property(value = %22security.oauth2.resource.loadBalanced%22, matchIfMissing = false
+Bean(LoadBalancerInterceptor.class
 )%0A%09p
@@ -2545,10 +2545,1034 @@
 %09%09%7D%0A
-
 %09%7D%0A%0A
+        @Configuration%0A        @ConditionalOnBean(RetryLoadBalancerInterceptor.class)%0A        protected static class UserInfoRetryLoadBalancerConfig %7B%0A                @Bean%0A                public UserInfoRestTemplateCustomizer retryLoadBalancedUserInfoRestTemplateCustomizer(%0A                                final RetryLoadBalancerInterceptor loadBalancerInterceptor) %7B%0A                        return new UserInfoRestTemplateCustomizer() %7B%0A                                @Override%0A                                public void customize(OAuth2RestTemplate restTemplate) %7B%0A                                        List%3CClientHttpRequestInterceptor%3E interceptors = new ArrayList%3C%3E(%0A                                                        restTemplate.getInterceptors());%0A                                        interceptors.add(loadBalancerInterceptor);%0A                                        restTemplate.setInterceptors(interceptors);%0A                                %7D%0A                        %7D;%0A                %7D%0A        %7D%0A%0A
 %7D%0A

configure load-balancing with spring-retry (fixes #110)