package com.broadleafdemo.catalogbrowse;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;
import com.broadleafcommerce.catalogbrowse.service.autoconfigure.SSLVerificationProperties;
import com.broadleafcommerce.catalogbrowse.service.provider.CatalogProvider;
import com.broadleafcommerce.catalogbrowse.service.provider.PricingProvider;
import com.broadleafcommerce.common.extension.TypeFactory;
import com.broadleafdemo.catalogbrowse.service.provider.ProductEnrichmentProvider;
import com.broadleafdemo.catalogbrowse.service.provider.external.DemoExternalCatalogProvider;
import com.broadleafdemo.catalogbrowse.service.provider.external.ExternalProductEnrichmentProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.function.Supplier;
import javax.net.ssl.SSLException;
@Configuration
@EnableConfigurationProperties(SSLVerificationProperties.class)
public class DemoCatalogBrowseServiceAutoConfiguration {
@Bean
@Primary
public CatalogProvider demoBrowseCatalogProvider(
@Qualifier("catalogBrowseWebClient") WebClient catalogBrowseWebClient,
ObjectMapper mapper,
PricingProvider browsePricingProvider,
ProductEnrichmentProvider productEnrichmentProvider,
TypeFactory typeFactory) {
return new DemoExternalCatalogProvider(catalogBrowseWebClient,
mapper,
browsePricingProvider,
productEnrichmentProvider,
typeFactory);
}
@Bean
public ProductEnrichmentProvider productEnrichmentProvider(
@Qualifier("enrichmentServiceWebClient") WebClient enrichmentServiceWebClient,
ObjectMapper mapper,
TypeFactory typeFactory) {
return new ExternalProductEnrichmentProvider(enrichmentServiceWebClient,
mapper,
typeFactory);
}
@Bean
public WebClient enrichmentServiceWebClient(
@Qualifier("oAuth2FilterFunctionSupplier") Supplier<ServletOAuth2AuthorizedClientExchangeFilterFunction> oauth2FilterSupplier,
ObjectMapper objectMapper,
SSLVerificationProperties sslVerificationProperties) throws SSLException {
// TODO: build your own WebClient adequate for your External API
ExchangeStrategies strategies = ExchangeStrategies
.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(
new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(
new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024);
}).build();
WebClient.Builder webClientBuilder = WebClient.builder();
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
return webClientBuilder
.uriBuilderFactory(uriBuilderFactory)
.exchangeStrategies(strategies)
.apply(oauth2FilterSupplier.get().oauth2Configuration())
.build();
}
}