Skip to content

Commit

Permalink
feat: Add confi remove eds
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo-ingenito committed Dec 19, 2023
1 parent 5eb5697 commit dcc4e4a
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static final class Config {
public static final String PROPS_NAME_CONTROL_LOG_ENABLED = "control-log-persistence-enabled";
public static final String PROPS_NAME_ISSUER_CF = "issuer-cf-cleaning";
public static final String PROPS_NAME_SUBJECT = "subject-cleaning";
public static final String PROPS_NAME_REMOVE_EDS_ENABLE = "remove-eds-enable";


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,105 @@
*/
package it.finanze.sanita.fse2.ms.gtw.dispatcher.controller.impl;

import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.App.MISSING_DOC_TYPE_PLACEHOLDER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.App.MISSING_WORKFLOW_PLACEHOLDER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventStatusEnum.BLOCKING_ERROR;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventStatusEnum.SUCCESS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.EDS_DELETE;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.EDS_UPDATE;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.INI_DELETE;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.INI_UPDATE;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.RIFERIMENTI_INI;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.FHIR_MAPPING_ERROR;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.INI_EXCEPTION;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.get;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.createMasterIdError;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.createReqMasterIdError;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.createWorkflowInstanceId;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.extractFieldCda;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.getDocumentType;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.isValidMasterId;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility.encodeSHA256;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility.isNullOrEmpty;

import java.util.Date;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Size;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;

import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IEdsClient;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IIniClient;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.AccreditationSimulationCFG;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.Misc;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.ValidationCFG;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.controller.IPublicationCTL;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.AccreditamentoSimulationDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.IndexerValueDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.JWTPayloadDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ResourceDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ValidationCreationInputDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ValidationDataDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.DeleteRequestDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.EdsMetadataUpdateReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.IniMetadataUpdateReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.IniReferenceRequestDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.MergedMetadatiRequestDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.PublicationCreationReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.PublicationFatherCreationReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.PublicationMetadataReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.PublicationUpdateReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.EdsResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.ErrorResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.GetMergedMetadatiDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.IniReferenceResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.IniTraceResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.LogTraceInfoDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.PublicationResDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.ResponseWifDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ActivityEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.DestinationTypeEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ErrorInstanceEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventStatusEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.OperationLogEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.PriorityTypeEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ProcessorOperationEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ResultLogEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.SystemTypeEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.BusinessException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.ConnectionRefusedException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.EdsException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.IniException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.MockEnabledException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.NoRecordFoundException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.ValidationException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.logging.LoggerHelper;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IAccreditamentoSimulationSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IDocumentReferenceSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IErrorHandlerSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IJwtSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IKafkaSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.ISignSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.facade.ICdaFacadeSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl.IniEdsInvocationSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.ValidationUtility;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Size;
import java.util.Date;

import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.App.MISSING_DOC_TYPE_PLACEHOLDER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.App.MISSING_WORKFLOW_PLACEHOLDER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventStatusEnum.BLOCKING_ERROR;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventStatusEnum.SUCCESS;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.EventTypeEnum.*;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.*;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.CdaUtility.*;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility.encodeSHA256;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility.isNullOrEmpty;

/**
* Publication controller.
Expand Down Expand Up @@ -89,13 +147,19 @@ public class PublicationCTL extends AbstractCTL implements IPublicationCTL {

@Autowired
private AccreditationSimulationCFG accreditationSimulationCFG;

@Autowired
private ISignSRV signSRV;

@Autowired
private IJwtSRV jwtSRV;

@Autowired
private IEdsClient edsClient;

@Autowired
private IConfigSRV configSRV;


@Override
public ResponseEntity<PublicationResDTO> create(final PublicationCreationReqDTO requestBody, final MultipartFile file, final HttpServletRequest request) {
Expand All @@ -118,7 +182,7 @@ public ResponseEntity<PublicationResDTO> create(final PublicationCreationReqDTO
}

String warning = "";

if (validationInfo.getJsonObj().getMode() == null) {
warning = Misc.WARN_EXTRACTION_SELECTION;
}
Expand Down Expand Up @@ -161,7 +225,7 @@ public ResponseEntity<PublicationResDTO> replace(final String idDoc, final Publi
try {
if(!isValidMasterId(idDoc)) throw new ValidationException(createReqMasterIdError());
validationInfo = publicationAndReplace(file, request, true,idDoc,traceInfoDTO);

log.info("[START] {}() with arguments {}={}, {}={}, {}={}","replace","traceId", traceInfoDTO.getTraceID(),"wif", validationInfo.getValidationData().getWorkflowInstanceId(),"idDoc", idDoc);

IniReferenceRequestDTO iniReq = new IniReferenceRequestDTO(idDoc, validationInfo.getJwtPayloadToken());
Expand Down Expand Up @@ -218,7 +282,7 @@ public ResponseWifDTO updateMetadata(final String idDoc, final PublicationMetada
log.info("[START] {}() with arguments {}={}, {}={}, {}={}","update","traceId", logTraceDTO.getTraceID(),"wif", wif,"idDoc", idDoc);

String warning = null;

if(!isValidMasterId(idDoc)) throw new ValidationException(createMasterIdError());

try {
Expand All @@ -240,30 +304,30 @@ public ResponseWifDTO updateMetadata(final String idDoc, final PublicationMetada
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, SUCCESS, jwtPayloadToken, "Merge metadati effettuato correttamente", RIFERIMENTI_INI);
}

// Keep going independently of the strategy
if (regimeDiMock) {
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, SUCCESS, jwtPayloadToken, "Regime di mock",
INI_UPDATE);
if(!configSRV.isRemoveEds()) {
EdsResponseDTO edsResponse = edsClient.update(new EdsMetadataUpdateReqDTO(idDoc, wif, requestBody));
if(edsResponse.isEsito()) {
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, SUCCESS, jwtPayloadToken, "Update EDS effettuato correttamente", EDS_UPDATE);
} else {
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, BLOCKING_ERROR, jwtPayloadToken, "Update EDS fallito", EDS_UPDATE);
throw new EdsException(edsResponse.getMessageError());
}
}

if(regimeDiMock) {
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, SUCCESS, jwtPayloadToken, "Regime di mock", INI_UPDATE);
} else {
IniTraceResponseDTO res = iniClient.update(
new IniMetadataUpdateReqDTO(
metadatiToUpdate.getMarshallResponse(),
jwtPayloadToken,
metadatiToUpdate.getDocumentType(),
wif,
requestBody.getAdministrativeRequest().getCode(),
metadatiToUpdate.getAuthorInstitution()
));
IniTraceResponseDTO res = iniClient.update(new IniMetadataUpdateReqDTO(metadatiToUpdate.getMarshallResponse(), jwtPayloadToken,metadatiToUpdate.getDocumentType(),wif,"ADM_REQ","AUTH_INST"));
// Check response errors
if (!StringUtility.isNullOrEmpty(res.getErrorMessage())) {
if(!StringUtility.isNullOrEmpty(res.getErrorMessage())) {
// Send to indexer
kafkaSRV.sendUpdateRequest(wif, new IniMetadataUpdateReqDTO(metadatiToUpdate.getMarshallResponse(), jwtPayloadToken, metadatiToUpdate.getDocumentType(), wif, requestBody.getAdministrativeRequest().getCode(), metadatiToUpdate.getAuthorInstitution()));
kafkaSRV.sendUpdateRequest(wif, new IniMetadataUpdateReqDTO(metadatiToUpdate.getMarshallResponse(), jwtPayloadToken, metadatiToUpdate.getDocumentType(), wif,"ADM_REQ","AUTH_INST"));
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, EventStatusEnum.ASYNC_RETRY, jwtPayloadToken, "Transazione presa in carico", INI_UPDATE);
warning = Misc.WARN_ASYNC_TRANSACTION;
} else {
kafkaSRV.sendUpdateStatus(logTraceDTO.getTraceID(), wif, idDoc, SUCCESS, jwtPayloadToken, "Update ini effettuato correttamente", INI_UPDATE);
}
}
}

}

Expand All @@ -286,7 +350,7 @@ public ResponseWifDTO updateMetadata(final String idDoc, final PublicationMetada

log.info("[EXIT] {}() with arguments {}={}, {}={}, {}={}","update",
"traceId", logTraceDTO.getTraceID(),"wif", wif,"idDoc", idDoc);

return new ResponseWifDTO(wif, logTraceDTO, warning);
}

Expand Down Expand Up @@ -334,12 +398,12 @@ private ValidationCreationInputDTO publicationAndReplaceValidation(final Multipa
kafkaSRV.sendReplaceStatus(traceInfoDTO.getTraceID(), "", EventStatusEnum.BLOCKING_ERROR, "Id documento non presente", jsonObj, jwtPayloadToken);
throw noRecordFound;
}

if(simulatedResult!=null) {
jsonObj.setWorkflowInstanceId(simulatedResult.getWorkflowInstanceId());
}
}

if(!SystemTypeEnum.TS.equals(jwtSRV.getSystemByIssuer(jwtPayloadToken.getIss()))){
signSRV.checkPades(bytePDF,eventTypeEnum);
}
Expand Down Expand Up @@ -441,7 +505,25 @@ public ResponseWifDTO delete(String idDoc, HttpServletRequest request) {
kafkaSRV.sendDeleteStatus(info.getTraceID(), workflowInstanceId, idDoc, "Riferimenti trovati: " +iniReference.getUuid(), SUCCESS, jwtPayloadToken, RIFERIMENTI_INI);
}

EdsResponseDTO edsResponse = new EdsResponseDTO(true,"EDS Mock","EDS Mock");
// ==============================
// [2] Send delete request to EDS
// ==============================
EdsResponseDTO edsResponse = new EdsResponseDTO(true,"EDS_MOCK", "EDS_MOCK");
if(!configSRV.isRemoveEds()) {
edsResponse = edsClient.delete(idDoc);
// Exit if necessary
Objects.requireNonNull(edsResponse, "PublicationCTL returned an error - edsResponse is null!");

if (!edsResponse.isEsito()) {
// Update transaction status
kafkaSRV.sendDeleteStatus(info.getTraceID(), workflowInstanceId, idDoc, edsResponse.getMessageError(), BLOCKING_ERROR, jwtPayloadToken, EDS_DELETE);
throw new EdsException("Error encountered while sending delete information to EDS client");
} else {
// Update transaction status
kafkaSRV.sendDeleteStatus(info.getTraceID(), workflowInstanceId, idDoc, "Delete effettuata su eds", SUCCESS, jwtPayloadToken, EDS_DELETE);
}
}


// ==============================
// [3] Send delete request to INI
Expand Down Expand Up @@ -565,7 +647,7 @@ public ResponseEntity<PublicationResDTO> validateAndCreate(PublicationFatherCrea
} catch (final ValidationException e) {
errorHandlerSRV.validationExceptionHandler(startDateOperationValidation, traceInfoDTO, workflowInstanceId, validationResult.getJwtPayloadToken(), e, CdaUtility.getDocumentType(docT));
}

final Date startDateOperationPublication = new Date();
try {
//Eseguo le operazione di creazione
Expand All @@ -585,7 +667,7 @@ public ResponseEntity<PublicationResDTO> validateAndCreate(PublicationFatherCrea
warning = StringUtility.isNullOrEmpty(warning) ? null : warning;
return new ResponseEntity<>(new PublicationResDTO(traceInfoDTO, warning, validationResult.getValidationData().getWorkflowInstanceId()), HttpStatus.CREATED);
}

@Override
public ResponseEntity<PublicationResDTO> validateAndReplace(@Size(min = 1, max = 256) String idDoc,
PublicationUpdateReqDTO requestBody, MultipartFile file, HttpServletRequest request) {
Expand Down Expand Up @@ -654,8 +736,8 @@ public ResponseEntity<PublicationResDTO> validateAndReplace(@Size(min = 1, max =
} catch (final ValidationException e) {
errorHandlerSRV.publicationValidationExceptionHandler(startDateReplacenOperation, validationResult.getValidationData(), validationResult.getJwtPayloadToken(), validationResult.getJsonObj(), traceInfoDTO, e, true, getDocumentType(validationResult.getDocument()));
}

return new ResponseEntity<>(new PublicationResDTO(traceInfoDTO, warning, validationResult.getValidationData().getWorkflowInstanceId()), HttpStatus.OK);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface IConfigSRV {

Boolean isSubjectNotAllowed();

Boolean isRemoveEds();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.*;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.DISPATCHER;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_REMOVE_EDS_ENABLE;

@Slf4j
@Service
Expand Down Expand Up @@ -45,6 +46,20 @@ public void postConstruct() {
log.info("Skipping gtw-config initialization due to test profile");
}
}


@Override
public Boolean isRemoveEds() {
long lastUpdate = props.get(PROPS_NAME_REMOVE_EDS_ENABLE).getKey();
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
synchronized(PROPS_NAME_REMOVE_EDS_ENABLE) {
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
refresh(PROPS_NAME_REMOVE_EDS_ENABLE);
}
}
}
return Boolean.parseBoolean(props.get(PROPS_NAME_REMOVE_EDS_ENABLE).getValue());
}

@Override
public Boolean isAuditEnable() {
Expand Down Expand Up @@ -118,7 +133,8 @@ private void integrity() {
PROPS_NAME_AUDIT_ENABLED,
PROPS_NAME_CONTROL_LOG_ENABLED,
PROPS_NAME_SUBJECT,
PROPS_NAME_ISSUER_CF
PROPS_NAME_ISSUER_CF,
PROPS_NAME_REMOVE_EDS_ENABLE
};
for (String prop : out) {
if(!props.containsKey(prop)) throw new IllegalStateException(err.replace("{}", prop));
Expand Down

0 comments on commit dcc4e4a

Please sign in to comment.