001package ca.uhn.fhir.jpa.subscription.match.deliver.email;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2021 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.model.CanonicalSubscription;
026import ca.uhn.fhir.jpa.subscription.match.deliver.BaseSubscriptionDeliverySubscriber;
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;
033import org.springframework.context.annotation.Scope;
034
035import java.util.ArrayList;
036import java.util.List;
037
038import static org.apache.commons.lang3.StringUtils.*;
039
040public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliverySubscriber {
041        private Logger ourLog = LoggerFactory.getLogger(SubscriptionDeliveringEmailSubscriber.class);
042
043        @Autowired
044        private ModelConfig myModelConfig;
045        @Autowired
046        private FhirContext myCtx;
047
048        private IEmailSender myEmailSender;
049
050        @Autowired
051        public SubscriptionDeliveringEmailSubscriber(IEmailSender theEmailSender) {
052                myEmailSender = theEmailSender;
053        }
054
055        @Override
056        public void handleMessage(ResourceDeliveryMessage theMessage) throws Exception {
057                CanonicalSubscription subscription = theMessage.getSubscription();
058
059                // The Subscription.endpoint is treated as the email "to"
060                String endpointUrl = subscription.getEndpointUrl();
061                List<String> destinationAddresses = new ArrayList<>();
062                String[] destinationAddressStrings = StringUtils.split(endpointUrl, ",");
063                for (String next : destinationAddressStrings) {
064                        next = processEmailAddressUri(next);
065                        if (isNotBlank(next)) {
066                                destinationAddresses.add(next);
067                        }
068                }
069
070                String payload = "";
071                if (isNotBlank(subscription.getPayloadString())) {
072                        EncodingEnum encoding = EncodingEnum.forContentType(subscription.getPayloadString());
073                        if (encoding != null) {
074                                payload = theMessage.getPayloadString();
075                        }
076                }
077
078                String from = processEmailAddressUri(defaultString(subscription.getEmailDetails().getFrom(), myModelConfig.getEmailFromAddress()));
079                String subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate());
080
081                EmailDetails details = new EmailDetails();
082                details.setTo(destinationAddresses);
083                details.setFrom(from);
084                details.setBodyTemplate(payload);
085                details.setSubjectTemplate(subjectTemplate);
086                details.setSubscription(subscription.getIdElement(myFhirContext));
087
088                myEmailSender.send(details);
089        }
090
091        private String processEmailAddressUri(String next) {
092                next = trim(defaultString(next));
093                if (next.startsWith("mailto:")) {
094         next = next.substring("mailto:".length());
095      }
096                return next;
097        }
098
099        private String provideDefaultSubjectTemplate() {
100                return "HAPI FHIR Subscriptions";
101        }
102
103        public void setEmailSender(IEmailSender theEmailSender) {
104                myEmailSender = theEmailSender;
105        }
106}