From fb9f15d25ccd8829a54f3791d09f3c4b5f17b02d Mon Sep 17 00:00:00 2001 From: "chris.ditcher" Date: Thu, 18 Jul 2024 15:20:52 -0700 Subject: [PATCH] Added additional web configuration --- .../educ/api/trax/config/GradTraxConfig.java | 7 +++++ .../educ/api/trax/service/RESTService.java | 26 +++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/config/GradTraxConfig.java b/api/src/main/java/ca/bc/gov/educ/api/trax/config/GradTraxConfig.java index 17733c10..53df1c84 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/config/GradTraxConfig.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/config/GradTraxConfig.java @@ -23,6 +23,7 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; import reactor.netty.http.client.HttpClient; @Configuration @@ -52,7 +53,10 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) { public WebClient getTraxClientWebClient(OAuth2AuthorizedClientManager authorizedClientManager) { ServletOAuth2AuthorizedClientExchangeFilterFunction filter = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager); filter.setDefaultClientRegistrationId("traxclient"); + DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory(); + defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE); return WebClient.builder() + .uriBuilderFactory(defaultUriBuilderFactory) .exchangeStrategies(ExchangeStrategies .builder() .codecs(codecs -> codecs @@ -68,7 +72,10 @@ public WebClient getTraxClientWebClient(OAuth2AuthorizedClientManager authorized public WebClient getInstituteWebClient(OAuth2AuthorizedClientManager authorizedClientManager) { ServletOAuth2AuthorizedClientExchangeFilterFunction filter = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager); filter.setDefaultClientRegistrationId("institute-web-client"); + DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory(); + defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE); return WebClient.builder() + .uriBuilderFactory(defaultUriBuilderFactory) .exchangeStrategies(ExchangeStrategies .builder() .codecs(codecs -> codecs diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/service/RESTService.java b/api/src/main/java/ca/bc/gov/educ/api/trax/service/RESTService.java index 36101de3..b060236f 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/service/RESTService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/service/RESTService.java @@ -5,12 +5,12 @@ import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; import ca.bc.gov.educ.api.trax.util.ThreadLocalStateUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; import reactor.util.retry.Retry; @@ -63,7 +63,11 @@ public T get(String url, Class clazz, String accessToken) { .block(); } catch (Exception e) { // catches IOExceptions and the like - throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e); + throw new ServiceException(getErrorMessage( + url, + e.getLocalizedMessage()), + (e instanceof WebClientResponseException) ? ((WebClientResponseException) e).getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(), + e); } return obj; } @@ -92,7 +96,11 @@ public T get(String url, Class clazz, WebClient webClient) { .block(); } catch (Exception e) { // catches IOExceptions and the like - throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e); + throw new ServiceException(getErrorMessage( + url, + e.getLocalizedMessage()), + (e instanceof WebClientResponseException) ? ((WebClientResponseException) e).getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(), + e); } return obj; } @@ -124,7 +132,11 @@ public T post(String url, Object body, Class clazz, String accessToken) { })) .block(); } catch (Exception e) { - throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e); + throw new ServiceException(getErrorMessage( + url, + e.getLocalizedMessage()), + (e instanceof WebClientResponseException) ? ((WebClientResponseException) e).getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(), + e); } return obj; } @@ -149,7 +161,11 @@ public T post(String url, Object body, Class clazz, WebClient webClient) })) .block(); } catch (Exception e) { - throw new ServiceException(getErrorMessage(url, e.getLocalizedMessage()), HttpStatus.SERVICE_UNAVAILABLE.value(), e); + throw new ServiceException(getErrorMessage( + url, + e.getLocalizedMessage()), + (e instanceof WebClientResponseException) ? ((WebClientResponseException) e).getStatusCode().value() : HttpStatus.SERVICE_UNAVAILABLE.value(), + e); } return obj; }