Skip to content

Commit

Permalink
Merge pull request #280 from BRP-API/feature/#279-loggen-gezagsuitspr…
Browse files Browse the repository at this point in the history
…aak-van-minderjarige-kinderen-bij-bevragen-meerderjarige

Feature/#279 loggen gezagsuitspraak van minderjarige kinderen bij bevragen meerderjarige
  • Loading branch information
FrozenSync authored Jan 13, 2025
2 parents 54cf92e + de15e16 commit 9475f2a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 89 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
id("org.jooq.jooq-codegen-gradle") version "3.19.15"
id("org.openapi.generator") version "7.10.0"
// id("org.owasp.dependencycheck") version "10.0.3"
id("org.springframework.boot") version "3.4.0"
id("org.springframework.boot") version "3.4.1"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import nl.rijksoverheid.mev.gezagsmodule.domain.Persoon;
import nl.rijksoverheid.mev.gezagsmodule.domain.Persoonslijst;
import nl.rijksoverheid.mev.gezagsmodule.domain.gezagvraag.GezagsBepaling;
import nl.rijksoverheid.mev.logging.GezagResultaat;
import nl.rijksoverheid.mev.logging.LoggingContext;
import org.openapitools.model.AbstractGezagsrelatie;
import org.openapitools.model.GezagNietTeBepalen;
Expand All @@ -18,6 +19,8 @@

import java.util.*;

import static net.logstash.logback.argument.StructuredArguments.value;

/**
* Service voor bepalen gezag
*/
Expand Down Expand Up @@ -124,10 +127,20 @@ public List<AbstractGezagsrelatie> getGezagResultaat(final String burgerservicen
gezagsRelaties = gezagsrelatieService.bepaalGezagsrelaties(arAntwoordenModel, gezagsBepaling);
}

loggingContext.addGezagType(arAntwoordenModel.getSoortGezag(), burgerservicenummer);
loggingContext.addRoute(route, burgerservicenummer);
loggingContext.addToelichting(arAntwoordenModel.getUitleg(), burgerservicenummer);
logger.info("Gezag bepaald voor persoon \"{}\": {}", burgerservicenummer, arAntwoordenModel);
if (logger.isInfoEnabled()) {
var gezagResultaat = new GezagResultaat(
loggingContext.getPlIdBy(burgerservicenummer),
arAntwoordenModel.getSoortGezag(),
arAntwoordenModel.getUitleg(),
route
);
logger.info(
"""
Gezag bepaald voor persoon "{}": {}
{}""",
burgerservicenummer, value("gezag_resultaat", gezagResultaat), value("antwoorden_model", arAntwoordenModel)
);
}
return gezagsRelaties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public BevoegdheidTotGezagService(
*/
public List<Persoon> bepaalBevoegdheidTotGezag(final GezagRequest gezagRequest) throws GezagException {
List<String> burgerservicenummers = gezagRequest.getBurgerservicenummer();
loggingContext.addBurgerservicenummers(burgerservicenummers);

return burgerservicenummers.stream()
.map(this::bepaalGezagVoorPersoon)
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/nl/rijksoverheid/mev/logging/GezagResultaat.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package nl.rijksoverheid.mev.logging;

import lombok.Data;

@Data
public final class GezagResultaat {
private long plId;
private String type;
private String toelichting;
private String route;
public record GezagResultaat(
Long plId,
String type,
String toelichting,
String route
) {
}
76 changes: 13 additions & 63 deletions src/main/java/nl/rijksoverheid/mev/logging/LoggingContext.java
Original file line number Diff line number Diff line change
@@ -1,88 +1,38 @@
package nl.rijksoverheid.mev.logging;

import nl.rijksoverheid.mev.gezagsmodule.model.Burgerservicenummer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class LoggingContext {

private static final Logger logger = LoggerFactory.getLogger(LoggingContext.class);

private final Instant eventStart;
private final List<Long> plIds;
private final Map<String, GezagResultaat> gezagResultaten;
private final Map<Long, Long> plIdByBurgerservicenummer;

public LoggingContext() {
this.eventStart = Instant.now();
this.plIds = new ArrayList<>();
this.gezagResultaten = new HashMap<>();
}

public void addBurgerservicenummers(Collection<String> burgerservicenummers) {
burgerservicenummers.forEach(burgerservicenummer ->
gezagResultaten.put(burgerservicenummer, new GezagResultaat())
);
this.plIdByBurgerservicenummer = new HashMap<>();
}

public void addPlId(long plId, Burgerservicenummer burgerservicenummer) {
gezagResultaten.computeIfPresent(burgerservicenummer.asString(), (unused, gezagResultaat) -> {
gezagResultaat.setPlId(plId);
return gezagResultaat;
});
plIds.add(plId);
plIdByBurgerservicenummer.put(burgerservicenummer.value(), plId);
}

public void addGezagType(String gezagType, String burgerservicenummer) {
gezagResultaten.computeIfPresent(burgerservicenummer, (unused, gezagResultaat) -> {
var translatedGezagType = mapGezagType(gezagType);
gezagResultaat.setType(translatedGezagType);
return gezagResultaat;
});
}

public void addRoute(String route, String burgerservicenummer) {
gezagResultaten.computeIfPresent(burgerservicenummer, (unused, gezagResultaat) -> {
gezagResultaat.setRoute(route);
return gezagResultaat;
});
}

public void addToelichting(String toelichting, String burgerservicenummer) {
gezagResultaten.computeIfPresent(burgerservicenummer, (unused, gezagResultaat) -> {
gezagResultaat.setToelichting(toelichting);
return gezagResultaat;
});
public Long getPlIdBy(String burgerservicenummerAsString) {
var burgerservicenummerAsLong = Long.valueOf(burgerservicenummerAsString);
return plIdByBurgerservicenummer.get(burgerservicenummerAsLong);
}

public Instant getEventStart() {
return eventStart;
}

public List<Long> getPlIds() {
return Collections.unmodifiableList(plIds);
}

public Collection<GezagResultaat> getGezagResultaten() {
return Collections.unmodifiableCollection(gezagResultaten.values());
}

private static String mapGezagType(String gezagType) {
// temporary until GezagService returns typed responses
return switch (gezagType) {
case "OG1" -> "EenhoofdigOuderlijkGezag";
case "OG2" -> "TweehoofdigOuderlijkGezag";
case "GG" -> "GezamenlijkGezag";
case "V" -> "Voogdij";
case "G" -> "TijdelijkGeenGezag";
case "N" -> "GezagNietTeBepalen";
case "NVT" -> "GezagNietVanToepassing*";
default -> { // should never happen
logger.error("Unknown gezag type: {}", gezagType);
yield "OnbekendGezagType";
}
};
public Set<Long> getPlIds() {
return plIdByBurgerservicenummer.values().stream()
.collect(Collectors.toUnmodifiableSet());
}
}
27 changes: 15 additions & 12 deletions src/main/java/nl/rijksoverheid/mev/logging/LoggingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import static net.logstash.logback.marker.Markers.append;

public class LoggingFilter extends OncePerRequestFilter implements ApplicationContextAware {

/**
Expand Down Expand Up @@ -81,10 +83,10 @@ private void afterRequest(HttpServletRequest request, HttpServletResponse respon
var event = processEvent();
var http = processHttp(request, response);
var url = processUrl(request);

processMetadata(request, response);
var metadata = processMetadata(request, response);

log.info(
append("metadata", metadata),
"HTTP {} {} responded {} in {} ms",
http.requestMethod, url.path, http.responseStatusCode, event.duration.toMillis()
);
Expand Down Expand Up @@ -136,7 +138,7 @@ private Url processUrl(HttpServletRequest request) {
private record Url(String path) {
}

private void processMetadata(HttpServletRequest request, HttpServletResponse response) {
private Map<String, Object> processMetadata(HttpServletRequest request, HttpServletResponse response) {
var loggingContext = applicationContext.getBean(LoggingContext.class);

var requestHeaders = headersToMap(Collections.list(request.getHeaderNames()), request::getHeader);
Expand All @@ -154,23 +156,24 @@ private void processMetadata(HttpServletRequest request, HttpServletResponse res
}
var responseHeaders = cachedResponse == null ? null : headersToMap(cachedResponse.getHeaderNames(), cachedResponse::getHeader);

var isError = response.getStatus() >= 400;

var metadata = new HashMap<String, Object>();
metadata.put("gezag_resultaten", loggingContext.getGezagResultaten());
metadata.put("pl_ids", loggingContext.getPlIds());
if (isError) metadata.put("response.body", responseBodyAsJsonString);
metadata.put("response.headers", responseHeaders);
metadata.put("request.body", requestBodyAsJsonString);
metadata.put("request.headers", requestHeaders);

try {
var metadataAsJsonString = objectMapper.writeValueAsString(metadata);
MDC.put("metadata", metadataAsJsonString);
var responseBody = objectMapper.readValue(responseBodyAsJsonString, Object.class);
var requestBody = objectMapper.readValue(requestBodyAsJsonString, Object.class);

metadata.put("response.body", responseBody);
metadata.put("request.body", requestBody);
} catch (JsonProcessingException e) {
log.error("Failed to serialize headers to JSON; fallback to writing headers as Java object", e);
MDC.put("metadata", metadata.toString());
log.error("Failed to serialize HTTP body to JSON; fallback to writing headers as Java object", e);
metadata.put("response.body", responseBodyAsJsonString);
metadata.put("request.body", requestBodyAsJsonString);
}

return metadata;
}

private Map<String, String> headersToMap(Collection<String> headerNames, UnaryOperator<String> headerValueResolver) {
Expand Down

0 comments on commit 9475f2a

Please sign in to comment.