|
| 1 | +package org.highmed.dsf.bpe.mail; |
| 2 | + |
| 3 | +import static org.highmed.dsf.bpe.ConstantsBase.NAMINGSYSTEM_HIGHMED_ENDPOINT_IDENTIFIER; |
| 4 | + |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +import org.highmed.dsf.bpe.service.MailService; |
| 10 | +import org.highmed.dsf.fhir.client.FhirWebserviceClientProvider; |
| 11 | +import org.highmed.dsf.fhir.variables.Target; |
| 12 | +import org.hl7.fhir.r4.model.Bundle; |
| 13 | +import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; |
| 14 | +import org.hl7.fhir.r4.model.Endpoint; |
| 15 | +import org.hl7.fhir.r4.model.IdType; |
| 16 | +import org.hl7.fhir.r4.model.Identifier; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | +import org.springframework.beans.factory.InitializingBean; |
| 20 | + |
| 21 | +public class ErrorMailService implements InitializingBean |
| 22 | +{ |
| 23 | + private static final Logger pingProcessErrorLogger = LoggerFactory.getLogger("ping-process-error-logger"); |
| 24 | + private static final Logger pongProcessErrorLogger = LoggerFactory.getLogger("pong-process-error-logger"); |
| 25 | + |
| 26 | + private static final String SUBJECT_PING_PROCESS_FAILED = "Ping Process Failed"; |
| 27 | + private static final String SUBJECT_PONG_PROCESS_FAILED = "Pong Process Failed"; |
| 28 | + |
| 29 | + private final String localOrganizationIdentifierValue; |
| 30 | + |
| 31 | + private final MailService mailService; |
| 32 | + private final FhirWebserviceClientProvider clientProvider; |
| 33 | + |
| 34 | + private final boolean sendPingProcessFailedMail; |
| 35 | + private final boolean sendPongProcessFailedMail; |
| 36 | + |
| 37 | + public ErrorMailService(MailService mailService, FhirWebserviceClientProvider clientProvider, |
| 38 | + String localOrganizationIdentifierValue, boolean sendPingProcessFailedMail, |
| 39 | + boolean sendPongProcessFailedMail) |
| 40 | + { |
| 41 | + this.mailService = mailService; |
| 42 | + this.clientProvider = clientProvider; |
| 43 | + this.localOrganizationIdentifierValue = localOrganizationIdentifierValue; |
| 44 | + |
| 45 | + this.sendPingProcessFailedMail = sendPingProcessFailedMail; |
| 46 | + this.sendPongProcessFailedMail = sendPongProcessFailedMail; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void afterPropertiesSet() throws Exception |
| 51 | + { |
| 52 | + Objects.requireNonNull(mailService, "mailService"); |
| 53 | + Objects.requireNonNull(clientProvider, "clientProvider"); |
| 54 | + Objects.requireNonNull(localOrganizationIdentifierValue, "localOrganizationIdentifierValue"); |
| 55 | + } |
| 56 | + |
| 57 | + private String localEndpointIdentifierValue() |
| 58 | + { |
| 59 | + Bundle result = clientProvider.getLocalWebserviceClient().searchWithStrictHandling(Endpoint.class, |
| 60 | + Map.of("address", Collections.singletonList(clientProvider.getLocalBaseUrl()))); |
| 61 | + |
| 62 | + if (result.getTotal() != 1) |
| 63 | + return "?"; |
| 64 | + |
| 65 | + return result.getEntry().stream().filter(BundleEntryComponent::hasResource) |
| 66 | + .map(BundleEntryComponent::getResource).filter(r -> r instanceof Endpoint).map(r -> (Endpoint) r) |
| 67 | + .findFirst().stream().flatMap(e -> e.getIdentifier().stream()) |
| 68 | + .filter(i -> NAMINGSYSTEM_HIGHMED_ENDPOINT_IDENTIFIER.equals(i.getSystem())).findFirst() |
| 69 | + .map(Identifier::getValue).orElse("?"); |
| 70 | + } |
| 71 | + |
| 72 | + private String createMessage(Target target, String message, String messageDetails, IdType taskId) |
| 73 | + { |
| 74 | + StringBuilder b = new StringBuilder(); |
| 75 | + |
| 76 | + b.append(localOrganizationIdentifierValue); |
| 77 | + b.append('/'); |
| 78 | + b.append(localEndpointIdentifierValue()); |
| 79 | + |
| 80 | + b.append(" -> "); |
| 81 | + |
| 82 | + b.append(target.getOrganizationIdentifierValue()); |
| 83 | + b.append('/'); |
| 84 | + b.append(target.getEndpointIdentifierValue()); |
| 85 | + |
| 86 | + b.append(": "); |
| 87 | + b.append(message); |
| 88 | + |
| 89 | + if (messageDetails != null) |
| 90 | + { |
| 91 | + b.append("\n\t"); |
| 92 | + b.append(messageDetails); |
| 93 | + } |
| 94 | + |
| 95 | + b.append("\n\nProcess started by: "); |
| 96 | + b.append(taskId.toVersionless().withServerBase(clientProvider.getLocalBaseUrl(), "Task").getValue()); |
| 97 | + |
| 98 | + return b.toString(); |
| 99 | + } |
| 100 | + |
| 101 | + public void pongMessageNotReceived(IdType taskId, Target target) |
| 102 | + { |
| 103 | + pingProcessErrorLogger.debug("No pong from organization '{}', Endpoint '{}' received", |
| 104 | + target.getOrganizationIdentifierValue(), target.getEndpointIdentifierValue()); |
| 105 | + |
| 106 | + if (sendPingProcessFailedMail) |
| 107 | + { |
| 108 | + mailService.send(SUBJECT_PING_PROCESS_FAILED, |
| 109 | + createMessage(target, "No pong message received", null, taskId)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void endpointNotReachableForPing(IdType taskId, Target target, String errorMessage) |
| 114 | + { |
| 115 | + pingProcessErrorLogger.debug("Endpoint '{}' at organization '{}' not reachable with ping: {}", |
| 116 | + target.getOrganizationIdentifierValue(), target.getEndpointIdentifierValue(), errorMessage); |
| 117 | + |
| 118 | + if (sendPingProcessFailedMail) |
| 119 | + { |
| 120 | + mailService.send(SUBJECT_PING_PROCESS_FAILED, |
| 121 | + createMessage(target, "Not reachable with ping", errorMessage, taskId)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public void endpointReachablePingForbidden(IdType taskId, Target target, String errorMessage) |
| 126 | + { |
| 127 | + pingProcessErrorLogger.debug("Endpoint '{}' at organization '{}' reachable, ping forbidden: {}", |
| 128 | + target.getOrganizationIdentifierValue(), target.getEndpointIdentifierValue(), errorMessage); |
| 129 | + |
| 130 | + if (sendPongProcessFailedMail) |
| 131 | + { |
| 132 | + mailService.send(SUBJECT_PING_PROCESS_FAILED, |
| 133 | + createMessage(target, "Ping forbidden", errorMessage, taskId)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void endpointNotReachableForPong(IdType taskId, Target target, String errorMessage) |
| 138 | + { |
| 139 | + pongProcessErrorLogger.debug("Endpoint '{}' at organization '{}' not reachable with pong: {}", |
| 140 | + target.getOrganizationIdentifierValue(), target.getEndpointIdentifierValue(), errorMessage); |
| 141 | + |
| 142 | + if (sendPongProcessFailedMail) |
| 143 | + { |
| 144 | + mailService.send(SUBJECT_PONG_PROCESS_FAILED, |
| 145 | + createMessage(target, "Not reachable with pong", errorMessage, taskId)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public void endpointReachablePongForbidden(IdType taskId, Target target, String errorMessage) |
| 150 | + { |
| 151 | + pongProcessErrorLogger.debug("Endpoint '{}' at organization '{}' reachable, pong forbidden: {}", |
| 152 | + target.getOrganizationIdentifierValue(), target.getEndpointIdentifierValue(), errorMessage); |
| 153 | + |
| 154 | + if (sendPongProcessFailedMail) |
| 155 | + { |
| 156 | + mailService.send(SUBJECT_PONG_PROCESS_FAILED, |
| 157 | + createMessage(target, "Pong forbidden", errorMessage, taskId)); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments