Skip to content

Commit

Permalink
Refactor BrukerServiceConsumer and BrukerServiceGetTilgangCommand to …
Browse files Browse the repository at this point in the history
…return Mono<List<String>> for improved reactive handling #deploy-test-dolly-backend #deploy-dolly-backend
  • Loading branch information
krharum committed Feb 28, 2025
1 parent ae9aa6b commit 0e87874
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Service
@Slf4j
Expand All @@ -29,10 +32,10 @@ public BrukerServiceConsumer(
.build();
}

public Flux<String> getKollegaerIOrganisasjon(String brukerId) {
public Mono<List<String>> getKollegaerIOrganisasjon(String brukerId) {

return tokenService.exchange(serverProperties)
.flatMapMany(token ->
.flatMap(token ->
new BrukerServiceGetTilgangCommand(webClient, brukerId, token.getTokenValue()).call());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import reactor.util.retry.Retry;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.Callable;

@RequiredArgsConstructor
public class BrukerServiceGetTilgangCommand implements Callable<Flux<String>> {
public class BrukerServiceGetTilgangCommand implements Callable<Mono<List<String>>> {

private static final String TILGANG_URL = "/api/v1/tilgang";

Expand All @@ -21,17 +22,18 @@ public class BrukerServiceGetTilgangCommand implements Callable<Flux<String>> {
private final String token;

@Override
public Flux<String> call() {
public Mono<List<String>> call() {

return webClient.get()
.uri(uriBuilder -> uriBuilder.path(TILGANG_URL)
.queryParam("brukerId", brukerId)
.build())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.retrieve()
.bodyToFlux(String.class)
.bodyToMono(String[].class)
.map(List::of)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(error -> Mono.just(brukerId))
.onErrorResume(error -> Mono.just(List.of(brukerId)))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ private void sjekkTilgang(Long gruppeId, Integer pageNo, Integer pageSize) {
if (bruker.getBrukertype() == BANKID) {
log.info("Sjekker tilgang for bruker: {}, brukertype: {}", bruker.getBrukerId(), bruker.getBrukertype());
brukerServiceConsumer.getKollegaerIOrganisasjon(bruker.getBrukerId())
.collectList()
.doOnNext(brukere -> log.info("BrukerServiceConsumer hentet {} kollegaer for bruker: {}",
String.join(",", brukere), bruker.getBrukerId()))
.map(testgruppeRepository::findAllByOpprettetAv_BrukerIdIn)
Expand All @@ -131,7 +130,6 @@ public Mono<Page<Testgruppe>> getAllTestgrupper(Integer pageNo, Integer pageSize
log.info("Henter testgrupper for bruker: {}, brukertype: {}", bruker.getBrukerId(), bruker.getBrukertype());
if (bruker.getBrukertype() == BANKID) {
return brukerServiceConsumer.getKollegaerIOrganisasjon(bruker.getBrukerId())
.collectList()
.doOnNext(brukere -> log.info("BrukerServiceConsumer hentet {} kollegaer for bruker: {}",
String.join(",", brukere), bruker.getBrukerId()))
.map(brukere -> testgruppeRepository.findAllByOpprettetAv_BrukerIdIn(brukere,
Expand Down

0 comments on commit 0e87874

Please sign in to comment.