From fe5efd0c0181f8accda9358f97e3095b260076cb Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Thu, 30 Nov 2023 10:43:34 +0100 Subject: [PATCH] feat: audit is now dynamic to gtw-config --- .../CustomResponseBodyAdviceAdapter.java | 36 ++++++++--- .../gtw/dispatcher/client/IConfigClient.java | 4 ++ .../dispatcher/client/impl/ConfigClient.java | 58 +++++++++++------ .../client/routes/ConfigClientRoutes.java | 63 +++++++++++++++++++ .../client/routes/base/ClientRoutes.java | 31 +++++++-- .../ms/gtw/dispatcher/config/Constants.java | 30 ++------- .../fse2/ms/gtw/dispatcher/config/WebCFG.java | 5 +- .../dispatcher/config/audit/AuditManager.java | 2 - .../dispatcher/enums/ConfigItemTypeEnum.java | 7 +++ .../interceptor/LogInterceptor.java | 24 +++---- .../repository/mongo/impl/AuditRepo.java | 11 ++-- .../ms/gtw/dispatcher/service/IConfigSRV.java | 6 ++ .../dispatcher/service/impl/ConfigSRV.java | 61 ++++++++++++++++++ src/main/resources/application-dev.properties | 7 +-- .../resources/application-docker.properties | 7 +-- src/main/resources/application.properties | 7 +-- .../resources/application-test.properties | 5 -- 17 files changed, 258 insertions(+), 106 deletions(-) create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/ConfigClientRoutes.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/enums/ConfigItemTypeEnum.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/adapter/CustomResponseBodyAdviceAdapter.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/adapter/CustomResponseBodyAdviceAdapter.java index 30abc366..59740635 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/adapter/CustomResponseBodyAdviceAdapter.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/adapter/CustomResponseBodyAdviceAdapter.java @@ -12,8 +12,8 @@ package it.finanze.sanita.fse2.ms.gtw.dispatcher.adapter; import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.audit.AuditManager; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -29,29 +29,45 @@ @ControllerAdvice @Slf4j -@ConditionalOnProperty("ms.dispatcher.audit.enabled") public class CustomResponseBodyAdviceAdapter implements ResponseBodyAdvice { + @Autowired + private IConfigSRV config; + @Autowired private AuditManager manager; @Override - public boolean supports(MethodParameter methodParameter, Class> aClass) { + public boolean supports(MethodParameter param, Class> clazz) { return true; } @Override - public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, - Class> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { - if (serverHttpRequest instanceof ServletServerHttpRequest && serverHttpResponse instanceof ServletServerHttpResponse) { + public Object beforeBodyWrite( + Object body, + MethodParameter param, + MediaType type, + Class> clazz, + ServerHttpRequest req, + ServerHttpResponse res + ) { + if ( + req instanceof ServletServerHttpRequest && + res instanceof ServletServerHttpResponse + ) { try { - manager.process(((ServletServerHttpRequest) serverHttpRequest).getServletRequest(), o); + if (config.isAuditEnable()) { + // Cast to HTTP request + ServletServerHttpRequest out = (ServletServerHttpRequest) req; + // Invoke processor + manager.process(out.getServletRequest(), body); + } } catch(Exception ex) { - log.error("Errore nel before body write ", ex); - throw new BusinessException("Errore nel before body write " + ex); + log.error("Unable to invoke before-body write ", ex); + throw new BusinessException("Unable to invoke before-body write " + ex); } } - return o; + return body; } } \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java index d903184d..a858e32a 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java @@ -11,6 +11,8 @@ */ package it.finanze.sanita.fse2.ms.gtw.dispatcher.client; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; + /** * Interface of gtw-config Client. */ @@ -18,4 +20,6 @@ public interface IConfigClient { String getGatewayName(); + Boolean isAuditEnable(ConfigItemTypeEnum type, String props); + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java index b456c0d6..f82cf596 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java @@ -11,21 +11,24 @@ */ package it.finanze.sanita.fse2.ms.gtw.dispatcher.client.impl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.web.client.HttpStatusCodeException; -import org.springframework.web.client.ResourceAccessException; -import org.springframework.web.client.RestTemplate; - import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.impl.base.AbstractClient; import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.response.WhoIsResponseDTO; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.ConfigClientRoutes; import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.MicroservicesURLCFG; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.BusinessException; import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.ProfileUtility; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_AUDIT_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.*; /** * Implementation of gtw-config Client. @@ -35,22 +38,22 @@ public class ConfigClient extends AbstractClient implements IConfigClient { @Autowired - private MicroservicesURLCFG msUrlCFG; + private ConfigClientRoutes routes; @Autowired - private RestTemplate restTemplate; + private RestTemplate client; @Autowired - private ProfileUtility profileUtility; + private ProfileUtility profiles; @Override public String getGatewayName() { String gatewayName = null; try { log.debug("Config Client - Calling Config Client to get Gateway Name"); - final String endpoint = msUrlCFG.getConfigHost() + Constants.Client.Config.WHOIS_PATH; + final String endpoint = routes.whois(); - final boolean isTestEnvironment = profileUtility.isDevOrDockerProfile() || profileUtility.isTestProfile(); + final boolean isTestEnvironment = profiles.isDevOrDockerProfile() || profiles.isTestProfile(); // Check if the endpoint is reachable if (isTestEnvironment && !isReachable()) { @@ -58,8 +61,7 @@ public String getGatewayName() { return Constants.Client.Config.MOCKED_GATEWAY_NAME; } - final ResponseEntity response = restTemplate.getForEntity(endpoint, - WhoIsResponseDTO.class); + final ResponseEntity response = client.getForEntity(endpoint, WhoIsResponseDTO.class); WhoIsResponseDTO body = response.getBody(); @@ -82,14 +84,30 @@ public String getGatewayName() { return gatewayName; } + @Override + public Boolean isAuditEnable(ConfigItemTypeEnum type, String props) { + boolean out = false; + + String endpoint = routes.getConfigItem(GENERIC, PROPS_NAME_AUDIT_ENABLED); + log.debug("{} - Executing request: {}", routes.identifier(), endpoint); + + if(isReachable()){ + ResponseEntity response = client.getForEntity(endpoint, String.class); + out = Boolean.parseBoolean(response.getBody()); + } + + return out; + } + private boolean isReachable() { + boolean out; try { - final String endpoint = msUrlCFG.getConfigHost() + Constants.Client.Config.STATUS_PATH; - restTemplate.getForEntity(endpoint, String.class); - return true; - } catch (ResourceAccessException clientException) { - return false; + client.getForEntity(routes.status(), String.class); + out = true; + } catch (ResourceAccessException ex) { + out = false; } + return out; } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/ConfigClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/ConfigClientRoutes.java new file mode 100644 index 00000000..b1f47aaf --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/ConfigClientRoutes.java @@ -0,0 +1,63 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * Copyright (C) 2023 Ministero della Salute + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ +package it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.util.UriComponentsBuilder; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.*; + + +@Component +public final class ConfigClientRoutes { + + @Value("${ms.url.gtw-config}") + private String host; + + public UriComponentsBuilder base() { + return UriComponentsBuilder.fromHttpUrl(host); + } + + public String identifier() { + return IDENTIFIER; + } + + public String microservice() { + return IDENTIFIER_MS; + } + + public String status() { + return base() + .pathSegment(API_STATUS) + .build() + .toUriString(); + } + + public String whois() { + return base() + .pathSegment(API_WHOIS) + .build() + .toUriString(); + } + + public String getConfigItem(ConfigItemTypeEnum type, String props) { + return base() + .pathSegment(API_VERSION, API_CONFIG_ITEMS, API_PROPS) + .queryParam(QP_TYPE, type.name()) + .queryParam(QP_PROPS, props) + .build() + .toUriString(); + } + +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java index 7d5766a4..ffcef69d 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java @@ -11,18 +11,18 @@ */ package it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base; +import lombok.NoArgsConstructor; + +import static lombok.AccessLevel.PRIVATE; + +@NoArgsConstructor(access = PRIVATE) public final class ClientRoutes { - - private ClientRoutes() {} + @NoArgsConstructor(access = PRIVATE) public static final class Ini { - - private Ini() {} // COMMON public static final String IDENTIFIER_MS = "ini"; public static final String IDENTIFIER = "[INI]"; - // PATH PARAMS - public static final String ID_DOC_PATH_PARAM = "{id}"; // ENDPOINT public static final String API_VERSION = "v1"; public static final String DELETE_PATH = "ini-delete"; @@ -31,4 +31,23 @@ private Ini() {} public static final String METADATA_PATH = "get-merged-metadati"; } + @NoArgsConstructor(access = PRIVATE) + public static final class Config { + // COMMON + public static final String IDENTIFIER_MS = "cfg"; + public static final String IDENTIFIER = "[CFG]"; + // ENDPOINT + public static final String API_VERSION = "v1"; + public static final String API_CONFIG_ITEMS = "config-items"; + public static final String API_PROPS = "props"; + public static final String API_STATUS = "status"; + public static final String API_WHOIS = "whois"; + // QP + public static final String QP_TYPE = "type"; + public static final String QP_PROPS = "props"; + // VALUES + public static final String PROPS_NAME_AUDIT_ENABLED = "audit_enabled"; + + } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/Constants.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/Constants.java index 825e6b86..4aa0c575 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/Constants.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/Constants.java @@ -11,6 +11,10 @@ */ package it.finanze.sanita.fse2.ms.gtw.dispatcher.config; +import lombok.NoArgsConstructor; + +import static lombok.AccessLevel.PRIVATE; + /** * Constants application. */ @@ -84,18 +88,14 @@ public static final class App { public static final String BEARER_PREFIX = "Bearer "; public static final String SHA_ERROR = "Errore in fase di calcolo SHA-256"; - public static final String HOST_ERROR = "Error while retrieving host informations"; public static final String SHA_ALGORITHM = "SHA-256"; public static final String JWT_MISSING_ISSUER_PLACEHOLDER = "UNDEFINED_JWT_ISSUER"; - public static final String JWT_MISSING_SUBJECT_ROLE = "UNDEFINED_SUBJECT_ROLE"; public static final String JWT_MISSING_SUBJECT = "UNDEFINED_SUBJECT"; - public static final String JWT_MISSING_LOCALITY = "UNDEFINED_LOCALITY"; public static final String MISSING_WORKFLOW_PLACEHOLDER = "UNKNOWN_WORKFLOW_ID"; public static final String MISSING_DOC_TYPE_PLACEHOLDER = "UNKNOWN_DOCUMENT_TYPE"; - public static final String LOG_TYPE_KPI = "kpi-structured-log"; public static final String LOG_TYPE_CONTROL = "control-structured-log"; public static final int MAX_SIZE_WARNING = 200000; @@ -141,34 +141,16 @@ private Misc() { public static final class Client { private Client() {} - - public static final class Ini { - - private Ini() {} - - public static final String DELETE_PATH = "/v1/ini-delete"; - public static final String UPDATE_PATH = "/v1/ini-update"; - public static final String REFERENCE_PATH = "/v1/get-reference/{idDoc}"; - - - } + @NoArgsConstructor(access = PRIVATE) public static final class Eds { - - private Eds() {} public static final String DELETE_PATH = "/v1/documents/{idDoc}"; public static final String UPDATE_PATH = "/v1/documents/{idDoc}/metadata"; public static final String ID_DOC_PLACEHOLDER = "{idDoc}"; } + @NoArgsConstructor(access = PRIVATE) public static final class Config { - - private Config() {} - - public static final String WHOIS_PATH = "/v1/whois"; - - public static final String STATUS_PATH = "/status"; - public static final String MOCKED_GATEWAY_NAME = "mocked-gateway"; } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/WebCFG.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/WebCFG.java index 3b723d96..897f7292 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/WebCFG.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/WebCFG.java @@ -12,7 +12,6 @@ package it.finanze.sanita.fse2.ms.gtw.dispatcher.config; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -20,7 +19,6 @@ import it.finanze.sanita.fse2.ms.gtw.dispatcher.interceptor.LogInterceptor; @Configuration -@ConditionalOnProperty("ms.dispatcher.audit.enabled") public class WebCFG implements WebMvcConfigurer { @Autowired @@ -28,7 +26,8 @@ public class WebCFG implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(logInterceptor); + registry.addInterceptor(logInterceptor); } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/audit/AuditManager.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/audit/AuditManager.java index 70022c4c..856bc281 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/audit/AuditManager.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/config/audit/AuditManager.java @@ -16,7 +16,6 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @@ -28,7 +27,6 @@ @Slf4j @Component -@ConditionalOnProperty("ms.dispatcher.audit.enabled") public class AuditManager { @Autowired diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/enums/ConfigItemTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/enums/ConfigItemTypeEnum.java new file mode 100644 index 00000000..18edd253 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/enums/ConfigItemTypeEnum.java @@ -0,0 +1,7 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher.enums; + +public enum ConfigItemTypeEnum { + GENERIC, + GARBAGE, + STATUS_MANAGER +} \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/interceptor/LogInterceptor.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/interceptor/LogInterceptor.java index d51c739a..07b8a71f 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/interceptor/LogInterceptor.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/interceptor/LogInterceptor.java @@ -11,34 +11,36 @@ */ package it.finanze.sanita.fse2.ms.gtw.dispatcher.interceptor; -import java.util.Date; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Date; + @Component -@ConditionalOnProperty("ms.dispatcher.audit.enabled") public class LogInterceptor implements HandlerInterceptor { + + @Autowired + private IConfigSRV config; @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { - request.setAttribute("START_TIME", new Date()); + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + if (config.isAuditEnable()) request.setAttribute("START_TIME", new Date()); return true; } @Override - public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { //Questo metodo è lasciato intenzionalmente vuoto } @Override - public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { //Questo metodo è lasciato intenzionalmente vuoto } } \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/repository/mongo/impl/AuditRepo.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/repository/mongo/impl/AuditRepo.java index dc7fb5a0..703dd47c 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/repository/mongo/impl/AuditRepo.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/repository/mongo/impl/AuditRepo.java @@ -12,7 +12,6 @@ package it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.mongo.impl; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Repository; @@ -23,21 +22,19 @@ @Repository @Slf4j -@ConditionalOnProperty("ms.dispatcher.audit.enabled") public class AuditRepo implements IAuditRepo { @Autowired - private MongoTemplate mongoTemplate; - + private MongoTemplate mongo; /** * Salvataggio audit request and response. */ @Override - public void save(final AuditETY auditETY) { - try { - mongoTemplate.insert(auditETY); + public void save(final AuditETY audit) { + try { + mongo.insert(audit); } catch (final Exception ex) { log.error("Errore durante il salvataggio dell'audit", ex); throw new BusinessException("Errore durante il salvataggio dell'audit", ex); diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java new file mode 100644 index 00000000..e3a4d2d9 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java @@ -0,0 +1,6 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher.service; + +public interface IConfigSRV { + + Boolean isAuditEnable(); +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java new file mode 100644 index 00000000..938517ce --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java @@ -0,0 +1,61 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Service; + +import java.util.Date; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_AUDIT_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; + +@Slf4j +@Service +public class ConfigSRV implements IConfigSRV { + + private static final long DELTA_MS = 300_000L; + + @Autowired + private IConfigClient client; + + private Boolean audit; + private long lastUpdate; + + + @EventListener(ApplicationStartedEvent.class) + public void initialize() { + refreshAuditEnable(); + } + + private void refreshAuditEnable() { + // Keep track before updating + boolean previous = audit; + // Execute request and update last-updated + audit = client.isAuditEnable(GENERIC, PROPS_NAME_AUDIT_ENABLED); + lastUpdate = new Date().getTime(); + // Omit initialization but notify for subsequent changes + if(audit != null && previous != audit) { + log.info( + "[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", + PROPS_NAME_AUDIT_ENABLED, + audit, + previous + ); + } + } + + @Override + public Boolean isAuditEnable() { + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + synchronized(ConfigSRV.class) { + refreshAuditEnable(); + } + } + return audit; + } + +} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 84326233..d3ee892b 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -35,9 +35,4 @@ document-type.priority.medium=RIC,SUM,TAC,PRS,PDC document-type.priority.high=VAC,PRE,WOR,LDO,REF log.output.format=console -kafka.log.base-topic=MDS-SA-0004_FU_001_LOG - -####################################### -# AUDIT ENABLEMENT -####################################### -ms.dispatcher.audit.enabled=true +kafka.log.base-topic=MDS-SA-0004_FU_001_LOG \ No newline at end of file diff --git a/src/main/resources/application-docker.properties b/src/main/resources/application-docker.properties index a96db98e..d9cd6079 100644 --- a/src/main/resources/application-docker.properties +++ b/src/main/resources/application-docker.properties @@ -37,9 +37,4 @@ document-type.priority.medium=RIC,SUM,TAC,PRS,PDC document-type.priority.high=VAC,PRE,WOR,LDO,REF log.output.format=console -kafka.log.base-topic=MDS-SA-0004_FU_001_LOG - -####################################### -# AUDIT ENABLEMENT -####################################### -ms.dispatcher.audit.enabled=true +kafka.log.base-topic=MDS-SA-0004_FU_001_LOG \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2676f27c..5e7d2a62 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -71,7 +71,7 @@ kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerial kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer kafka.producer.transactional.id=gtwdispatcher.tx. kafka.producer.enable.idempotence=true -kafka.producer.ack=all +kafka.producer.ack=all ###### KAFKA CODE ########## @@ -125,11 +125,6 @@ ms.url.gtw-config= ms.url.ana-service= ms.ana-service.enable-validation=false -####################################### -# AUDIT ENABLEMENT -####################################### -ms.dispatcher.audit.enabled=${AUDIT_ENABLEMENT} - ####################################### # FISCAL CODE PROPERTY ####################################### diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties index 0df425d6..bb488bf0 100644 --- a/src/test/resources/application-test.properties +++ b/src/test/resources/application-test.properties @@ -33,11 +33,6 @@ document-type.priority.low=ESE,CER,VRB document-type.priority.medium=RIC,SUM,TAC,PRS,PDC document-type.priority.high=VAC,PRE,WOR,LDO,REF -####################################### -# AUDIT ENABLEMENT -####################################### -ms.dispatcher.audit.enabled=true - ####################################### # CLIENTS CONFIG #######################################