001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.subscription.match.deliver.email;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
024import jakarta.annotation.Nonnull;
025import org.apache.commons.lang3.StringUtils;
026import org.hl7.fhir.instance.model.api.IIdType;
027import org.simplejavamail.api.email.Email;
028import org.simplejavamail.email.EmailBuilder;
029import org.thymeleaf.context.Context;
030import org.thymeleaf.spring6.SpringTemplateEngine;
031import org.thymeleaf.spring6.dialect.SpringStandardDialect;
032import org.thymeleaf.templatemode.TemplateMode;
033import org.thymeleaf.templateresolver.StringTemplateResolver;
034
035import java.util.List;
036import java.util.stream.Collectors;
037
038public class EmailDetails {
039        private final SpringTemplateEngine myTemplateEngine;
040        private final Context myContext;
041
042        private String mySubjectTemplate;
043        private String myBodyTemplate;
044        private List<String> myTo;
045        private String myFrom;
046        private IIdType mySubscription;
047
048        public EmailDetails() {
049                myTemplateEngine = makeTemplateEngine();
050                myContext = new Context();
051        }
052
053        public String getBody() {
054                return myTemplateEngine.process(myBodyTemplate, myContext);
055        }
056
057        public void setBodyTemplate(String theBodyTemplate) {
058                myBodyTemplate = theBodyTemplate;
059        }
060
061        public String getFrom() {
062                return StringUtils.trim(myFrom);
063        }
064
065        public void setFrom(String theFrom) {
066                myFrom = theFrom;
067        }
068
069        public String getSubject() {
070                return myTemplateEngine.process(mySubjectTemplate, myContext);
071        }
072
073        public void setSubjectTemplate(String theSubjectTemplate) {
074                mySubjectTemplate = theSubjectTemplate;
075        }
076
077        public String getSubscriptionId() {
078                return mySubscription.toUnqualifiedVersionless().getValue();
079        }
080
081        public void setSubscription(IIdType theSubscription) {
082                mySubscription = theSubscription;
083        }
084
085        public String getTo() {
086                return myTo.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(","));
087        }
088
089        public void setTo(List<String> theTo) {
090                myTo = theTo;
091        }
092
093        public Email toEmail() {
094                try {
095                        return EmailBuilder.startingBlank()
096                                        .from(getFrom())
097                                        .to(getTo())
098                                        .withSubject(getSubject())
099                                        .withPlainText(getBody())
100                                        .withHeader("X-FHIR-Subscription", getSubscriptionId())
101                                        .buildEmail();
102                } catch (IllegalArgumentException e) {
103                        throw new InternalErrorException(Msg.code(3) + "Failed to create email message", e);
104                }
105        }
106
107        @Nonnull
108        private SpringTemplateEngine makeTemplateEngine() {
109                StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
110                stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);
111
112                SpringStandardDialect springStandardDialect = new SpringStandardDialect();
113                springStandardDialect.setEnableSpringELCompiler(true);
114
115                SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
116                springTemplateEngine.setDialect(springStandardDialect);
117                springTemplateEngine.setEnableSpringELCompiler(true);
118                springTemplateEngine.setTemplateResolver(stringTemplateResolver);
119
120                return springTemplateEngine;
121        }
122}