Skip to content

Commit

Permalink
Fjernet boilerplate fra organisasjon-forvalter sin bruk av ServerProp…
Browse files Browse the repository at this point in the history
…erties.
  • Loading branch information
rfc3092 committed Oct 26, 2023
1 parent 42480bc commit 8eac3b7
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package no.nav.organisasjonforvalter.config;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import static lombok.AccessLevel.PACKAGE;

/**
* Samler alle placeholders for ulike {@code consumers.*}-konfigurasjon her, dvs. subklasser av {@code ServerProperties}.
* <br/><br/>
* Husk at Spring Boot bruker <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding">relaxed binding</a>
* mellom configuration properties og field names.
*
* @see ServerProperties
*/
@Configuration
@ConfigurationProperties(prefix = "consumers")
@NoArgsConstructor(access = PACKAGE)
@Getter
@Setter(PACKAGE)
public class Consumers {

private ServerProperties testnavAdresseService;
private ServerProperties genererNavnService;
private ServerProperties testnavMiljoerService;
private ServerProperties organisasjonBestillingService;
private ServerProperties testnavOrganisasjonService;
private ServerProperties testnavOrgnummerService;


}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package no.nav.organisasjonforvalter.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.credentials.AdresseServiceProperties;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.organisasjonforvalter.consumer.command.AdresseServiceCommand;
import no.nav.testnav.libs.dto.adresseservice.v1.VegadresseDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -18,16 +19,16 @@
public class AdresseServiceConsumer {

private final WebClient webClient;
private final AdresseServiceProperties serviceProperties;
private final ServerProperties serverProperties;
private final TokenExchange tokenExchange;

public AdresseServiceConsumer(
AdresseServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavAdresseService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}
Expand All @@ -48,7 +49,7 @@ public List<VegadresseDTO> getAdresser(String query) {
long startTime = currentTimeMillis();

try {
var adresser = tokenExchange.exchange(serviceProperties)
var adresser = tokenExchange.exchange(serverProperties)
.flatMap(token -> new AdresseServiceCommand(webClient, query, token.getTokenValue()).call()).block();

log.info("Adresseoppslag tok {} ms", currentTimeMillis() - startTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package no.nav.organisasjonforvalter.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.credentials.GenererNavnServiceProperties;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.testnav.libs.commands.generernavnservice.v1.GenererNavnCommand;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -24,15 +25,15 @@ public class GenererNavnServiceConsumer {

private final TokenExchange tokenExchange;
private final WebClient webClient;
private final GenererNavnServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public GenererNavnServiceConsumer(
GenererNavnServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getGenererNavnService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}
Expand All @@ -41,7 +42,7 @@ public List<String> getOrgName(Integer antall) {

long startTime = currentTimeMillis();
try {
var accessToken = tokenExchange.exchange(serviceProperties);
var accessToken = tokenExchange.exchange(serverProperties);
var navn = new GenererNavnCommand(webClient, accessToken.block().getTokenValue(), antall).call();

log.info("Generer-navn-service svarte etter {} ms", currentTimeMillis() - startTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package no.nav.organisasjonforvalter.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.credentials.MiljoerServiceProperties;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.organisasjonforvalter.consumer.command.MiljoerServiceCommand;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand All @@ -21,15 +22,15 @@ public class MiljoerServiceConsumer {

private final TokenExchange tokenExchange;
private final WebClient webClient;
private final MiljoerServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public MiljoerServiceConsumer(
MiljoerServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavMiljoerService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}
Expand All @@ -38,7 +39,7 @@ public MiljoerServiceConsumer(
public Set<String> getOrgMiljoer() {

try {
return Stream.of(tokenExchange.exchange(serviceProperties)
return Stream.of(tokenExchange.exchange(serverProperties)
.flatMap(token ->
new MiljoerServiceCommand(webClient, token.getTokenValue()).call()).block())
.filter(env -> !env.equals("u5") && !env.equals("qx"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package no.nav.organisasjonforvalter.consumer;

import no.nav.organisasjonforvalter.config.credentials.OrganisasjonBestillingServiceProperties;
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;
import no.nav.organisasjonforvalter.jpa.entity.Status;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Service;

Expand All @@ -17,36 +18,36 @@ public class OrganisasjonBestillingConsumer {

private final TokenExchange tokenExchange;
private final WebClient webClient;
private final OrganisasjonBestillingServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public OrganisasjonBestillingConsumer(
OrganisasjonBestillingServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getOrganisasjonBestillingService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}

public Flux<BestillingStatus> getBestillingStatus(Status status) {

return Flux.from(tokenExchange.exchange(serviceProperties)
return Flux.from(tokenExchange.exchange(serverProperties)
.flatMap(token -> new OrganisasjonBestillingStatusCommand(webClient, status,
token.getTokenValue()).call()));
}

public Flux<Status> getBestillingId(Status status) {

return Flux.from(tokenExchange.exchange(serviceProperties)
return Flux.from(tokenExchange.exchange(serverProperties)
.flatMap(token -> new OrganisasjonBestillingIdsCommand(webClient, status,
token.getTokenValue()).call()));
}

public BestillingStatus getBestillingStatus(String uuid) {

return tokenExchange.exchange(serviceProperties)
return tokenExchange.exchange(serverProperties)
.flatMap(token -> new OrganisasjonBestillingCommand(webClient, Status.builder()
.uuid(uuid)
.build(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package no.nav.organisasjonforvalter.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.credentials.TestnavOrgnummerServiceProperties;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.organisasjonforvalter.consumer.command.OrganisasjonOrgnummerServiceCommand;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -23,15 +24,15 @@ public class OrganisasjonOrgnummerServiceConsumer {

private final TokenExchange tokenExchange;
private final WebClient webClient;
private final TestnavOrgnummerServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public OrganisasjonOrgnummerServiceConsumer(
TestnavOrgnummerServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavOrgnummerService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}
Expand All @@ -40,7 +41,7 @@ public List<String> getOrgnummer(Integer antall) {

long startTime = currentTimeMillis();
try {
var response = tokenExchange.exchange(serviceProperties)
var response = tokenExchange.exchange(serverProperties)
.flatMap(token -> new OrganisasjonOrgnummerServiceCommand(webClient, antall, token.getTokenValue()).call())
.block();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.config.credentials.OrganisasjonServiceProperties;
import no.nav.organisasjonforvalter.config.Consumers;
import no.nav.testnav.libs.commands.organisasjonservice.v1.GetOrganisasjonCommand;
import no.nav.testnav.libs.dto.organisasjon.v1.OrganisasjonDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Service;

Expand All @@ -30,19 +31,18 @@ public class OrganisasjonServiceConsumer {

private final TokenExchange tokenExchange;
private final WebClient webClient;
private final OrganisasjonServiceProperties serviceProperties;
private final ServerProperties serverProperties;
private final ExecutorService executorService;

public OrganisasjonServiceConsumer(
OrganisasjonServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavOrganisasjonService();
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
this.executorService = Executors.newFixedThreadPool(serviceProperties.getThreads());
this.executorService = Executors.newFixedThreadPool(serverProperties.getThreads());
}

public Optional<OrganisasjonDTO> getStatus(String orgnummer, String miljoe) {
Expand All @@ -59,7 +59,7 @@ public Map<String, Map<String, Optional<OrganisasjonDTO>>> getStatus(Set<String>

long startTime = currentTimeMillis();

var token = tokenExchange.exchange(serviceProperties).block().getTokenValue();
var token = tokenExchange.exchange(serverProperties).block().getTokenValue();

var completables = miljoer.stream()
.map(miljoe -> OrgFutureDTO.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class ServerProperties {
@NotBlank
private String namespace = "dolly";

/**
* Requested number of threads used to run requests to this target service. Not used by all clients.
*/
private int threads = 1;

public String toTokenXScope() {
return "%s:%s:%s".formatted(cluster, namespace, name);
}
Expand Down

0 comments on commit 8eac3b7

Please sign in to comment.