001package ca.uhn.fhir.jpa.subscription.match.deliver.email;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.jpa.model.entity.ModelConfig;
025import ca.uhn.fhir.jpa.subscription.match.deliver.BaseSubscriptionDeliverySubscriber;
026import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
027import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryMessage;
028import ca.uhn.fhir.rest.api.EncodingEnum;
029import org.apache.commons.lang3.StringUtils;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.beans.factory.annotation.Autowired;
033
034import java.util.ArrayList;
035import java.util.List;
036
037import static org.apache.commons.lang3.StringUtils.defaultString;
038import static org.apache.commons.lang3.StringUtils.isNotBlank;
039import static org.apache.commons.lang3.StringUtils.trim;
040
041public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliverySubscriber {
042        private Logger ourLog = LoggerFactory.getLogger(SubscriptionDeliveringEmailSubscriber.class);
043
044        @Autowired
045        private ModelConfig myModelConfig;
046        @Autowired
047        private FhirContext myCtx;
048
049        private IEmailSender myEmailSender;
050
051        @Autowired
052        public SubscriptionDeliveringEmailSubscriber(IEmailSender theEmailSender) {
053                myEmailSender = theEmailSender;
054        }
055
056        @Override
057        public void handleMessage(ResourceDeliveryMessage theMessage) throws Exception {
058                CanonicalSubscription subscription = theMessage.getSubscription();
059
060                // The Subscription.endpoint is treated as the email "to"
061                String endpointUrl = subscription.getEndpointUrl();
062                List<String> destinationAddresses = new ArrayList<>();
063                String[] destinationAddressStrings = StringUtils.split(endpointUrl, ",");
064                for (String next : destinationAddressStrings) {
065                        next = processEmailAddressUri(next);
066                        if (isNotBlank(next)) {
067                                destinationAddresses.add(next);
068                        }
069                }
070
071                String payload = "";
072                if (isNotBlank(subscription.getPayloadString())) {
073                        EncodingEnum encoding = EncodingEnum.forContentType(subscription.getPayloadString());
074                        if (encoding != null) {
075                                payload = theMessage.getPayloadString();
076                        }
077                }
078
079                String from = processEmailAddressUri(defaultString(subscription.getEmailDetails().getFrom(), myModelConfig.getEmailFromAddress()));
080                String subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate());
081
082                EmailDetails details = new EmailDetails();
083                details.setTo(destinationAddresses);
084                details.setFrom(from);
085                details.setBodyTemplate(payload);
086                details.setSubjectTemplate(subjectTemplate);
087                details.setSubscription(subscription.getIdElement(myFhirContext));
088
089                myEmailSender.send(details);
090        }
091
092        private String processEmailAddressUri(String next) {
093                next = trim(defaultString(next));
094                if (next.startsWith("mailto:")) {
095         next = next.substring("mailto:".length());
096      }
097                return next;
098        }
099
100        private String provideDefaultSubjectTemplate() {
101                return "HAPI FHIR Subscriptions";
102        }
103
104        public void setEmailSender(IEmailSender theEmailSender) {
105                myEmailSender = theEmailSender;
106        }
107}