Skip to content

Commit

Permalink
fix: refactor props
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 7, 2023
1 parent 35d0627 commit aecd144
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
*/
package it.finanze.sanita.fse2.ms.gtw.statusmanager.client;

public interface IConfigClient {
import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum;

Integer getExpirationDate();
Boolean isCfOnIssuerAllowed();
Boolean isSubjectPersistenceEnabled();
public interface IConfigClient {
ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type);
String getProps(ConfigItemTypeEnum type, String props, String previous);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.IConfigClient;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.ConfigClientRoutes;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum;
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.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.*;

@Slf4j
@Component
public class ConfigClient implements IConfigClient {
Expand All @@ -32,49 +31,6 @@ public class ConfigClient implements IConfigClient {
@Autowired
private ConfigClientRoutes routes;

@Override
public Integer getExpirationDate() {
int output = 5;

String endpoint = routes.getStatusManagerConfig(PROPS_NAME_EXP_DAYS);
log.debug("{} - Executing request: {}", routes.identifier(), endpoint);

if(isReachable()) {
ResponseEntity<String> response = client.getForEntity(endpoint, String.class);
if(response.getBody() != null) output = Integer.parseInt(response.getBody());
}

return output;
}

@Override
public Boolean isSubjectPersistenceEnabled() {
boolean output = false;
String endpoint = routes.getStatusManagerConfig(PROPS_NAME_SUBJECT);
log.debug("{} - Executing request: {}", routes.identifier(), endpoint);

if (isReachable()){
ResponseEntity<String> response = client.getForEntity(endpoint, String.class);
if (response.getBody() != null) output = Boolean.parseBoolean(response.getBody());
}

return output;
}

@Override
public Boolean isCfOnIssuerAllowed() {
boolean output = false;
String endpoint = routes.getStatusManagerConfig(PROPS_NAME_ISSUER_CF);
log.debug("{} - Executing request: {}", routes.identifier(), endpoint);

if(isReachable()) {
ResponseEntity<String> response = client.getForEntity(endpoint, String.class);
if(response.getBody() != null) output = Boolean.parseBoolean(response.getBody());
}

return output;
}

private boolean isReachable() {
boolean out;
try {
Expand All @@ -85,4 +41,20 @@ private boolean isReachable() {
}
return out;
}

@Override
public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) {
return client.getForObject(routes.getConfigItems(type), ConfigItemDTO.class);
}

@Override
public String getProps(ConfigItemTypeEnum type, String props, String previous) {
String out = previous;
String endpoint = routes.getConfigItem(type, props);
if (isReachable()) out = client.getForObject(endpoint, String.class);
if(out == null || !out.equals(previous)) {
log.info("[GTW-CFG] Property {} is set as {} (previously: {})", props, out, previous);
}
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes;

import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -43,21 +44,22 @@ public String status() {
.toUriString();
}

public String getStatusManagerConfig(String props) {
return getConfigItemByName(props, QP_TYPE_STATUS_MANAGER);
}

public String getGenericProps(String props) {
return getConfigItemByName(props, QP_TYPE_GENERIC);
}

private String getConfigItemByName(String props, String type) {
public String getConfigItem(ConfigItemTypeEnum type, String props) {
return base()
.pathSegment(API_VERSION, API_CONFIG_ITEMS, API_PROPS)
.queryParam(QP_TYPE, type)
.queryParam(QP_TYPE, type.name())
.queryParam(QP_PROPS, props)
.build()
.toUriString();
}

public String getConfigItems(ConfigItemTypeEnum type) {
return base()
.pathSegment(API_VERSION, API_CONFIG_ITEMS)
.queryParam(QP_TYPE, type.name())
.build()
.toUriString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ public static final class Config {
// QP
public static final String QP_TYPE = "type";
public static final String QP_PROPS = "props";
public static final String QP_TYPE_GENERIC = "GENERIC";
public static final String QP_TYPE_STATUS_MANAGER = "STATUS_MANAGER";
// VALUES
public static final String PROPS_NAME_EXP_DAYS = "expiring_date_day";
public static final String PROPS_NAME_EXP_DAYS = "expiring-date-day";
public static final String PROPS_NAME_ISSUER_CF = "issuer-cf-cleaning";
public static final String PROPS_NAME_SUBJECT = "subject-persistence";

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.finanze.sanita.fse2.ms.gtw.statusmanager.dto;

import lombok.Getter;
import lombok.Setter;

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

@Getter
@Setter
public class ConfigItemDTO {

private String traceId;
private String spanId;

private List<ConfigDataItemDTO> configurationItems;

@Getter
@Setter
public static class ConfigDataItemDTO {
private String key;
private Map<String, String> items;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package it.finanze.sanita.fse2.ms.gtw.statusmanager.enums;

public enum ConfigItemTypeEnum {
GENERIC,
STATUS_MANAGER
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package it.finanze.sanita.fse2.ms.gtw.statusmanager.service.impl;

import it.finanze.sanita.fse2.ms.gtw.statusmanager.client.IConfigClient;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum;
import it.finanze.sanita.fse2.ms.gtw.statusmanager.service.IConfigSRV;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -22,118 +24,91 @@

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.*;
import static it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO.*;
import static it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum.STATUS_MANAGER;

@Slf4j
@Service
public class ConfigSRV implements IConfigSRV {

private static final Long DELTA_MS = 300000L;

private final Map<String, Pair<Long, Object>> props;

@Autowired
private IConfigClient client;

private final Map<String, Pair<Long, String>> props;

public ConfigSRV() {
this.props = new HashMap<>();
}

@EventListener(ApplicationStartedEvent.class)
void initialize() {
refreshExpirationDate();
refreshIsCfOnIssuerAllowed();
refreshIsSubjectPersistenceEnabled();
runningConfiguration();
}

private void refreshExpirationDate() {
int days = client.getExpirationDate();
props.put(PROPS_NAME_EXP_DAYS, Pair.of(new Date().getTime(), days));
}

private void refreshIsSubjectPersistenceEnabled(){
boolean out = client.isSubjectPersistenceEnabled();
props.put(PROPS_NAME_SUBJECT, Pair.of(new Date().getTime(), out));
}

private void refreshIsCfOnIssuerAllowed() {
boolean out = client.isCfOnIssuerAllowed();
props.put(PROPS_NAME_ISSUER_CF, Pair.of(new Date().getTime(), out));
for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) {
log.info("[GTW-CFG] Retrieving {} properties ...", en.name());
ConfigItemDTO items = client.getConfigurationItems(en);
List<ConfigDataItemDTO> opts = items.getConfigurationItems();
for(ConfigDataItemDTO opt : opts) {
opt.getItems().forEach((key, value) -> {
log.info("[GTW-CFG] Property {} is set as {}", key, value);
props.put(key, Pair.of(new Date().getTime(), value));
});
}
}
}

@Override
public Integer getExpirationDate() {
Pair<Long, Object> pair = props.getOrDefault(
PROPS_NAME_EXP_DAYS,
Pair.of(0L, null)
);
if (new Date().getTime() - pair.getKey() >= DELTA_MS) {
long lastUpdate = props.get(PROPS_NAME_EXP_DAYS).getKey();
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
synchronized(PROPS_NAME_EXP_DAYS) {
refreshExpirationDate();
verifyExpirationDate(pair);
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
refresh(STATUS_MANAGER, PROPS_NAME_EXP_DAYS);
}
}
}
return (Integer) props.get(PROPS_NAME_EXP_DAYS).getValue();
return Integer.parseInt(
props.get(PROPS_NAME_EXP_DAYS).getValue()
);
}

@Override
public Boolean isSubjectPersistenceEnabled() {
Pair<Long, Object> pair = props.getOrDefault(
PROPS_NAME_SUBJECT,
Pair.of(0L, null)
);
if (new Date().getTime() - pair.getKey() >= DELTA_MS) {
long lastUpdate = props.get(PROPS_NAME_SUBJECT).getKey();
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
synchronized (PROPS_NAME_SUBJECT) {
refreshIsSubjectPersistenceEnabled();
verifyIsSubjectPersistenceEnabled(pair);
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
refresh(STATUS_MANAGER, PROPS_NAME_SUBJECT);
}
}
}
return (Boolean) props.get(PROPS_NAME_SUBJECT).getValue();
return Boolean.parseBoolean(
props.get(PROPS_NAME_SUBJECT).getValue()
);
}

@Override
public Boolean isCfOnIssuerNotAllowed() {
Pair<Long, Object> pair = props.getOrDefault(
PROPS_NAME_ISSUER_CF,
Pair.of(0L, null)
);
if (new Date().getTime() - pair.getKey() >= DELTA_MS) {
long lastUpdate = props.get(PROPS_NAME_ISSUER_CF).getKey();
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
synchronized(PROPS_NAME_ISSUER_CF) {
refreshIsCfOnIssuerAllowed();
verifyIsCfOnIssuerAllowed(pair);
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
refresh(STATUS_MANAGER, PROPS_NAME_ISSUER_CF);
}
}
}
return (Boolean) props.get(PROPS_NAME_ISSUER_CF).getValue();
}

private void runningConfiguration() {
props.forEach((id, pair) -> log.info("[GTW-CONFIG] key: {} | value: {}", id, pair.getValue()));
}

private void verifyExpirationDate(Pair<Long, Object> pair) {
int previous = (int) pair.getValue();
int current = (int) props.get(PROPS_NAME_EXP_DAYS).getValue();
if(previous != current) {
log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_EXP_DAYS, current, previous);
}
}

private void verifyIsSubjectPersistenceEnabled(Pair<Long, Object> pair){
Boolean previous = (Boolean) pair.getValue();
boolean current = (boolean) props.get(PROPS_NAME_SUBJECT).getValue();
if (previous != current){
log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_SUBJECT, current, previous);
}
return Boolean.parseBoolean(
props.get(PROPS_NAME_ISSUER_CF).getValue()
);
}

private void verifyIsCfOnIssuerAllowed(Pair<Long, Object> pair) {
Boolean previous = (Boolean) pair.getValue();
boolean current = (boolean) props.get(PROPS_NAME_ISSUER_CF).getValue();
if(previous != current) {
log.info("[GTW-CONFIG][UPDATE] key: {} | value: {} (previous: {})", PROPS_NAME_ISSUER_CF, current, previous);
}
private void refresh(ConfigItemTypeEnum type, String name) {
String previous = props.getOrDefault(name, Pair.of(0L, null)).getValue();
String prop = client.getProps(type, name, previous);
props.put(name, Pair.of(new Date().getTime(), prop));
}
}

0 comments on commit aecd144

Please sign in to comment.