Skip to content

Commit

Permalink
feat: Add control log config
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo-ingenito committed Dec 6, 2023
1 parent 4d89559 commit e52b471
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package it.finanze.sanita.fse2.ms.gtw.dispatcher.client;

import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum;

/**
Expand All @@ -20,6 +21,8 @@ public interface IConfigClient {

String getGatewayName();

Boolean isAuditEnable(ConfigItemTypeEnum type, String props);

ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type);

Object getProps(ConfigItemTypeEnum type, String props, Object previous);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +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 org.springframework.web.util.UriComponentsBuilder;

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.dto.ConfigItemDTO;
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.
Expand All @@ -39,75 +39,100 @@ public class ConfigClient extends AbstractClient implements IConfigClient {

@Autowired
private ConfigClientRoutes routes;

@Autowired
private RestTemplate client;

@Autowired
private ProfileUtility profiles;

@Override
public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(routes.base() + "/config-items").queryParam("type", type);
return client.getForObject(builder.toUriString(), ConfigItemDTO.class);
}

@Override
public String getGatewayName() {
String gatewayName = null;
try {
log.debug("Config Client - Calling Config Client to get Gateway Name");
final String endpoint = routes.whois();

final boolean isTestEnvironment = profiles.isDevOrDockerProfile() || profiles.isTestProfile();

// Check if the endpoint is reachable
if (isTestEnvironment && !isReachable()) {
log.warn("Config Client - Config Client is not reachable, mocking for testing purpose");
return Constants.Client.Config.MOCKED_GATEWAY_NAME;
}

final ResponseEntity<WhoIsResponseDTO> response = client.getForEntity(endpoint, WhoIsResponseDTO.class);

WhoIsResponseDTO body = response.getBody();

if(body!=null) {
if (response.getStatusCode().is2xxSuccessful()) {
gatewayName = body.getGatewayName();
} else {
log.error("Config Client - Error calling Config Client to get Gateway Name");
throw new BusinessException("The Config Client has returned an error");
}
} else {
throw new BusinessException("The Config Client has returned an error - The body is null");
}
} catch (HttpStatusCodeException clientException) {
errorHandler("config", clientException, "/config/whois");
} catch (Exception e) {
log.error("Error encountered while retrieving Gateway name", e);
throw e;
}
return gatewayName;
}

private boolean isReachable() {
boolean out;
try {
client.getForEntity(routes.status(), String.class);
out = true;
} catch (ResourceAccessException ex) {
out = false;
}
return out;
}

@Autowired
private RestTemplate client;

@Autowired
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 = routes.whois();

final boolean isTestEnvironment = profiles.isDevOrDockerProfile() || profiles.isTestProfile();

// Check if the endpoint is reachable
if (isTestEnvironment && !isReachable()) {
log.warn("Config Client - Config Client is not reachable, mocking for testing purpose");
return Constants.Client.Config.MOCKED_GATEWAY_NAME;
}

final ResponseEntity<WhoIsResponseDTO> response = client.getForEntity(endpoint, WhoIsResponseDTO.class);

WhoIsResponseDTO body = response.getBody();

if(body!=null) {
if (response.getStatusCode().is2xxSuccessful()) {
gatewayName = body.getGatewayName();
} else {
log.error("Config Client - Error calling Config Client to get Gateway Name");
throw new BusinessException("The Config Client has returned an error");
}
} else {
throw new BusinessException("The Config Client has returned an error - The body is null");
}
} catch (HttpStatusCodeException clientException) {
errorHandler("config", clientException, "/config/whois");
} catch (Exception e) {
log.error("Error encountered while retrieving Gateway name", e);
throw e;
}
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<String> response = client.getForEntity(endpoint, String.class);
out = Boolean.parseBoolean(response.getBody());
}

return out;
}

private boolean isReachable() {
boolean out;
try {
client.getForEntity(routes.status(), String.class);
out = true;
} catch (ResourceAccessException ex) {
out = false;
}
return out;
}

@Override
public Object getProps(ConfigItemTypeEnum type, String props, Object previous) {
Object out = previous;

String endpoint = routes.getConfigItem(type, props);

if (isReachable()) {
Object response = client.getForObject(endpoint, Object.class);
out = convertResponse(response, previous);
}

return out;
}

@SuppressWarnings("unchecked")
private <T> T convertResponse(Object response, Object previous) {
try {
Class<T> targetType = (Class<T>) previous.getClass();

if (targetType == Integer.class) {
return (T) Integer.valueOf(response.toString());
} else if (targetType == Boolean.class) {
return (T) Boolean.valueOf(response.toString());
} else if (targetType == String.class) {
return (T) response.toString();
} else {
return (T) response;
}
} catch (Exception e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@
*/
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 static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.API_CONFIG_ITEMS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.API_PROPS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.API_STATUS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.API_VERSION;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.API_WHOIS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.IDENTIFIER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.IDENTIFIER_MS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.QP_PROPS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.QP_TYPE;

import org.springframework.beans.factory.annotation.Autowired;
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.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.MicroservicesURLCFG;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum;


@Component
public final class ConfigClientRoutes {

@Value("${ms.url.gtw-config}")
private String host;
@Autowired
private MicroservicesURLCFG msUrlCFG;

public UriComponentsBuilder base() {
return UriComponentsBuilder.fromHttpUrl(host);
return UriComponentsBuilder.fromHttpUrl(msUrlCFG.getConfigHost());
}

public String identifier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static final class Config {
public static final String QP_PROPS = "props";
// VALUES
public static final String PROPS_NAME_AUDIT_ENABLED = "audit-enabled";
public static final String PROPS_NAME_CONTROL_LOG_ENABLED = "control-log-persistence-enabled";

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.finanze.sanita.fse2.ms.gtw.dispatcher.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class ConfigItemDTO {
private String key;
private Map<String, String> items;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum ConfigItemTypeEnum {
GENERIC,
GARBAGE,
STATUS_MANAGER
DISPATCHER,
}
Loading

0 comments on commit e52b471

Please sign in to comment.