Skip to content

Commit

Permalink
Oppdatert response i ordrestatus
Browse files Browse the repository at this point in the history
  • Loading branch information
krharum committed Sep 7, 2024
1 parent 03d6755 commit e085b44
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;


@Order(1)
@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
@Profile({"prod", "dev"})
public class SecurityConfig {

@Bean
Expand All @@ -24,15 +25,18 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
httpSecurity.sessionManagement(sessionConfig -> sessionConfig.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorizeConfig -> authorizeConfig.requestMatchers(
"/internal/**",
"/webjars/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/swagger-ui/**",
"/swagger",
"/error",
"/swagger-ui.html"
).permitAll().requestMatchers("/api/**").fullyAuthenticated())
"/internal/**",
"/webjars/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/swagger-ui/**",
"/swagger",
"/error",
"/swagger-ui.html")
.permitAll()
.requestMatchers("/api/**")
.fullyAuthenticated())
.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
.oauth2ResourceServer(oauth2RSConfig -> oauth2RSConfig.jwt(Customizer.withDefaults()));

return httpSecurity.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import org.springframework.cache.annotation.Cacheable;
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.Arrays;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -35,19 +39,16 @@ public MiljoerServiceConsumer(
this.tokenExchange = tokenExchange;
}

@Cacheable(CACHE_MILJOER)
// @Cacheable(CACHE_MILJOER)
public Set<String> getOrgMiljoer() {

try {
return Stream.of(tokenExchange.exchange(serverProperties)
.flatMap(token ->
new MiljoerServiceCommand(webClient, token.getTokenValue()).call()).block())
.filter(env -> !env.equals("u5") && !env.equals("qx"))
.collect(Collectors.toSet());

} catch (RuntimeException e) {
log.error("Feilet å hente miljøer fra miljoer-service", e);
return emptySet();
}
return tokenExchange.exchange(serverProperties)
.flatMap(token ->
new MiljoerServiceCommand(webClient, token.getTokenValue()).call())
.map(miljoer -> Flux.fromIterable(Arrays.asList(miljoer))
.filter(env -> !env.equals("t13") && !env.equals("qx"))
.collect(Collectors.toSet()))
.flatMap(Mono::from)
.block();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nav.organisasjonforvalter.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.organisasjonforvalter.consumer.command.OrganisasjonBestillingCommand;
import no.nav.organisasjonforvalter.consumer.command.OrganisasjonBestillingIdsCommand;
import no.nav.organisasjonforvalter.consumer.command.OrganisasjonBestillingStatusCommand;
import no.nav.organisasjonforvalter.dto.responses.BestillingStatus;
Expand All @@ -13,6 +13,7 @@
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Slf4j
@Service
public class OrganisasjonBestillingConsumer {

Expand Down Expand Up @@ -44,14 +45,4 @@ public Flux<Status> getBestillingId(Status status) {
.flatMap(token -> new OrganisasjonBestillingIdsCommand(webClient, status,
token.getTokenValue()).call()));
}

public BestillingStatus getBestillingStatus(String uuid) {

return tokenExchange.exchange(serverProperties)
.flatMap(token -> new OrganisasjonBestillingCommand(webClient, Status.builder()
.uuid(uuid)
.build(),
token.getTokenValue()).call())
.block();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public Mono<String[]> call() {
.retrieve()
.bodyToMono(String[].class)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
.filter(WebClientFilter::is5xxException))
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(throwable -> Mono.empty());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import no.nav.organisasjonforvalter.jpa.entity.Status;
import no.nav.testnav.libs.commands.utils.WebClientFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
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;

import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.stream.Stream;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -28,19 +26,25 @@ public class OrganisasjonBestillingIdsCommand implements Callable<Mono<Status>>
public Mono<Status> call() {

return webClient.get()
.uri(STATUS_URL.replace("{uuid}", status.getUuid()))
.uri(uriBuilder -> uriBuilder.path(STATUS_URL)
.build(status.getUuid()))
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String[].class)
.flatMap(value -> {
status.setBestId(Stream.of(value).findFirst().orElse(null));
return Mono.just(status);
})
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException))
.doOnError(throwable ->
log.error(throwable instanceof WebClientResponseException webClientResponseException ?
webClientResponseException.getResponseBodyAsString() :
throwable.getMessage()));
.doOnNext(resultat -> log.info("Melding mottatt {}", resultat))
.map(resultat -> Arrays.stream(resultat)
.mapToInt(Integer::parseInt)
.max()
.orElse(0))
.doOnNext(resultat -> log.info("Index funnet {}", resultat))
.map(index -> Status.builder()
.id(status.getId())
.bestId(index != 0 ? index.toString() : null)
.uuid(status.getUuid())
.miljoe(status.getMiljoe())
.organisasjonsnummer(status.getOrganisasjonsnummer())
.build())
.doOnError(WebClientFilter::logErrorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
package no.nav.organisasjonforvalter.controller;

import static java.util.Objects.nonNull;
import static no.nav.organisasjonforvalter.config.CacheConfig.CACHE_BEDRIFT;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

import no.nav.organisasjonforvalter.consumer.MiljoerServiceConsumer;
import no.nav.organisasjonforvalter.dto.requests.BestillingRequest;
import no.nav.organisasjonforvalter.dto.requests.DeployRequest;
import no.nav.organisasjonforvalter.dto.responses.BestillingResponse;
Expand All @@ -32,6 +18,20 @@
import no.nav.organisasjonforvalter.service.OrdreStatusService;
import no.nav.organisasjonforvalter.service.OrganisasjonService;
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedId;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Objects.nonNull;
import static no.nav.organisasjonforvalter.config.CacheConfig.CACHE_BEDRIFT;

@RestController
@RequestMapping("api/v2/organisasjoner")
Expand All @@ -45,6 +45,7 @@ public class OrganisasjonController {
private final ImportService importService;
private final DrivervirksomheterService drivervirksomheterService;
private final GetAuthenticatedId getAuthenticatedId;
private final MiljoerServiceConsumer miljoerServiceConsumer;

@CacheEvict(value = CACHE_BEDRIFT, allEntries = true)
@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,18 @@ private static Set<String> getAllOrgnr(Organisasjon organisasjon, Set<String> or
@Transactional
public Map<String, RsOrganisasjon> getOrganisasjoner(String orgnummer, Set<String> miljoer) {

var miljoerAaSjekke = nonNull(miljoer) &&
miljoer.stream().anyMatch(miljoe ->
miljoerServiceConsumer.getOrgMiljoer().stream().anyMatch(miljoe2 -> miljoe2.equals(miljoe))) ?

miljoer.stream().filter(miljoe ->
miljoerServiceConsumer.getOrgMiljoer().stream().anyMatch(miljoe2 -> miljoe2.equals(miljoe)))
var miljoerAaSjekke = nonNull(miljoer) && !miljoer.isEmpty() ?
miljoerServiceConsumer.getOrgMiljoer().stream()
.filter(miljoer::contains)
.collect(Collectors.toSet()) :

miljoerServiceConsumer.getOrgMiljoer();

var dbOrganisasjon = organisasjonRepository.findByOrganisasjonsnummer(orgnummer);

var organisasjoner =
organisasjonServiceConsumer.getStatus(dbOrganisasjon.isPresent() ?
getAllOrgnr(dbOrganisasjon.get(), new HashSet<>()) : Set.of(orgnummer), miljoerAaSjekke);
organisasjonServiceConsumer.getStatus(
dbOrganisasjon.map(organisasjon -> getAllOrgnr(organisasjon,
new HashSet<>())).orElseGet(() -> Set.of(orgnummer)), miljoerAaSjekke);

return organisasjoner.keySet().stream()
.filter(env -> !organisasjoner.get(env).entrySet().isEmpty())
Expand Down
Loading

0 comments on commit e085b44

Please sign in to comment.