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