View Javadoc
1   package ca.uhn.fhir.jpa.subscription.email;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
24  import ca.uhn.fhir.jpa.subscription.BaseSubscriptionDeliverySubscriber;
25  import ca.uhn.fhir.jpa.subscription.CanonicalSubscription;
26  import ca.uhn.fhir.jpa.subscription.ResourceDeliveryMessage;
27  import org.apache.commons.lang3.StringUtils;
28  import org.hl7.fhir.r4.model.Subscription;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import static org.apache.commons.lang3.StringUtils.*;
36  
37  public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliverySubscriber {
38  	private Logger ourLog = LoggerFactory.getLogger(SubscriptionDeliveringEmailSubscriber.class);
39  
40  	private SubscriptionEmailInterceptor mySubscriptionEmailInterceptor;
41  
42  	public SubscriptionDeliveringEmailSubscriber(IFhirResourceDao<?> theSubscriptionDao, Subscription.SubscriptionChannelType theChannelType, SubscriptionEmailInterceptor theSubscriptionEmailInterceptor) {
43  		super(theSubscriptionDao, theChannelType, theSubscriptionEmailInterceptor);
44  
45  		mySubscriptionEmailInterceptor = theSubscriptionEmailInterceptor;
46  	}
47  
48  	@Override
49  	public void handleMessage(ResourceDeliveryMessage theMessage) throws Exception {
50  		CanonicalSubscription subscription = theMessage.getSubscription();
51  
52  		// The Subscription.endpoint is treated as the email "to"
53  		String endpointUrl = subscription.getEndpointUrl();
54  		List<String> destinationAddresses = new ArrayList<>();
55  		String[] destinationAddressStrings = StringUtils.split(endpointUrl, ",");
56  		for (String next : destinationAddressStrings) {
57  			next = processEmailAddressUri(next);
58  			if (isNotBlank(next)) {
59  				destinationAddresses.add(next);
60  			}
61  		}
62  
63  		String from = processEmailAddressUri(defaultString(subscription.getEmailDetails().getFrom(), mySubscriptionEmailInterceptor.getDefaultFromAddress()));
64  		String subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate());
65  
66  		EmailDetails details = new EmailDetails();
67  		details.setTo(destinationAddresses);
68  		details.setFrom(from);
69  		details.setBodyTemplate(subscription.getPayloadString());
70  		details.setSubjectTemplate(subjectTemplate);
71  		details.setSubscription(subscription.getIdElement(getContext()));
72  
73  		IEmailSender emailSender = mySubscriptionEmailInterceptor.getEmailSender();
74  		emailSender.send(details);
75  	}
76  
77  	private String processEmailAddressUri(String next) {
78  		next = trim(defaultString(next));
79  		if (next.startsWith("mailto:")) {
80           next = next.substring("mailto:".length());
81        }
82  		return next;
83  	}
84  
85  
86  	private String provideDefaultSubjectTemplate() {
87  		return "HAPI FHIR Subscriptions";
88  	}
89  }