Skip to content

Commit

Permalink
Added additional web configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
chris.ditcher authored and chris.ditcher committed Jul 18, 2024
1 parent a9f6478 commit fb9f15d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 21 additions & 5 deletions api/src/main/java/ca/bc/gov/educ/api/trax/service/RESTService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,7 +63,11 @@ public <T> T get(String url, Class<T> 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;
}
Expand Down Expand Up @@ -92,7 +96,11 @@ public <T> T get(String url, Class<T> 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;
}
Expand Down Expand Up @@ -124,7 +132,11 @@ public <T> T post(String url, Object body, Class<T> 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;
}
Expand All @@ -149,7 +161,11 @@ public <T> T post(String url, Object body, Class<T> 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;
}
Expand Down

0 comments on commit fb9f15d

Please sign in to comment.