Changes to subscriptions to include the resource that triggered the subscription in the payload, serialized as JSON or XML depending on the Subscription.channel.payload property's value

This commit is contained in:
Sean McIlvenna 2019-06-20 13:45:46 -07:00 committed by James Agnew
parent e1b43d811f
commit d92be90789
4 changed files with 62 additions and 11 deletions

View File

@ -112,9 +112,13 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
return observation; return observation;
} }
/**
* Tests an email subscription with payload set to XML. The email sent must include content in the body of the email that is formatted as XML.
* @throws Exception
*/
@Test @Test
public void testEmailSubscriptionNormal() throws Exception { public void testEmailSubscriptionNormal() throws Exception {
String payload = "This is the body"; String payload = "application/fhir+xml";
String code = "1000000050"; String code = "1000000050";
String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml"; String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml";
@ -138,13 +142,21 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
assertEquals(1, received.get(0).getAllRecipients().length); assertEquals(1, received.get(0).getAllRecipients().length);
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress()); assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType()); assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
assertEquals("This is the body", received.get(0).getContent().toString().trim());
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]); assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
// Expect the body of the email subscription to be an Observation formatted as XML
Observation parsedObservation = (Observation) ourClient.getFhirContext().newXmlParser().parseResource(received.get(0).getContent().toString().trim());
assertEquals("SNOMED-CT", parsedObservation.getCode().getCodingFirstRep().getSystem());
assertEquals("1000000050", parsedObservation.getCode().getCodingFirstRep().getCode());
} }
/**
* Tests an email subscription with payload set to JSON. The email sent must include content in the body of the email that is formatted as JSON.
* @throws Exception
*/
@Test @Test
public void testEmailSubscriptionWithCustom() throws Exception { public void testEmailSubscriptionWithCustom() throws Exception {
String payload = "This is the body"; String payload = "application/fhir+json";
String code = "1000000050"; String code = "1000000050";
String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml"; String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml";
@ -185,13 +197,21 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress()); assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType()); assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
assertEquals("This is a subject", received.get(0).getSubject().toString().trim()); assertEquals("This is a subject", received.get(0).getSubject().toString().trim());
assertEquals("This is the body", received.get(0).getContent().toString().trim());
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]); assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
// Expect the body of the email subscription to be an Observation formatted as JSON
Observation parsedObservation = (Observation) ourClient.getFhirContext().newJsonParser().parseResource(received.get(0).getContent().toString().trim());
assertEquals("SNOMED-CT", parsedObservation.getCode().getCodingFirstRep().getSystem());
assertEquals("1000000050", parsedObservation.getCode().getCodingFirstRep().getCode());
} }
/**
* Tests an email subscription with no payload. When the email is sent, the body of the email must be empty.
* @throws Exception
*/
@Test @Test
public void testEmailSubscriptionWithCustomNoMailtoOnFrom() throws Exception { public void testEmailSubscriptionWithCustomNoMailtoOnFrom() throws Exception {
String payload = "This is the body"; String payload = "";
String code = "1000000050"; String code = "1000000050";
String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml"; String criteria1 = "Observation?code=SNOMED-CT|" + code + "&_format=xml";
@ -231,7 +251,7 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test {
assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress()); assertEquals("foo@example.com", ((InternetAddress) received.get(0).getAllRecipients()[0]).getAddress());
assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType()); assertEquals("text/plain; charset=us-ascii", received.get(0).getContentType());
assertEquals("This is a subject", received.get(0).getSubject().toString().trim()); assertEquals("This is a subject", received.get(0).getSubject().toString().trim());
assertEquals("This is the body", received.get(0).getContent().toString().trim()); assertEquals("", received.get(0).getContent().toString().trim());
assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]); assertEquals(mySubscriptionIds.get(0).toUnqualifiedVersionless().getValue(), received.get(0).getHeader("X-FHIR-Subscription")[0]);
ourLog.info("Subscription: {}", myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(ourClient.history().onInstance(id).andReturnBundle(Bundle.class).execute())); ourLog.info("Subscription: {}", myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(ourClient.history().onInstance(id).andReturnBundle(Bundle.class).execute()));

View File

@ -73,6 +73,14 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
return retVal; return retVal;
} }
public String getPayloadString() {
if (this.myPayloadString != null) {
return this.myPayloadString;
}
return "";
}
public IIdType getPayloadId(FhirContext theCtx) { public IIdType getPayloadId(FhirContext theCtx) {
IIdType retVal = null; IIdType retVal = null;
if (myPayloadId != null) { if (myPayloadId != null) {
@ -89,9 +97,15 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
mySubscription = theSubscription; mySubscription = theSubscription;
} }
public void setPayload(FhirContext theCtx, IBaseResource thePayload) { public void setPayload(FhirContext theCtx, IBaseResource thePayload, Boolean isXml) {
myPayload = thePayload; myPayload = thePayload;
if (isXml) {
myPayloadString = theCtx.newXmlParser().encodeResourceToString(thePayload);
} else {
myPayloadString = theCtx.newJsonParser().encodeResourceToString(thePayload); myPayloadString = theCtx.newJsonParser().encodeResourceToString(thePayload);
}
myPayloadId = thePayload.getIdElement().toUnqualified().getValue(); myPayloadId = thePayload.getIdElement().toUnqualified().getValue();
} }

View File

@ -10,6 +10,7 @@ import ca.uhn.fhir.jpa.subscription.module.ResourceModifiedMessage;
import ca.uhn.fhir.jpa.subscription.module.cache.ActiveSubscription; import ca.uhn.fhir.jpa.subscription.module.cache.ActiveSubscription;
import ca.uhn.fhir.jpa.subscription.module.cache.SubscriptionRegistry; import ca.uhn.fhir.jpa.subscription.module.cache.SubscriptionRegistry;
import ca.uhn.fhir.jpa.subscription.module.matcher.ISubscriptionMatcher; import ca.uhn.fhir.jpa.subscription.module.matcher.ISubscriptionMatcher;
import ca.uhn.fhir.rest.api.Constants;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
@ -103,6 +104,7 @@ public class SubscriptionMatchingSubscriber implements MessageHandler {
private void doMatchActiveSubscriptionsAndDeliver(ResourceModifiedMessage theMsg) { private void doMatchActiveSubscriptionsAndDeliver(ResourceModifiedMessage theMsg) {
IIdType resourceId = theMsg.getId(myFhirContext); IIdType resourceId = theMsg.getId(myFhirContext);
Boolean isXml = false, isJson = false, isText = false;
Collection<ActiveSubscription> subscriptions = mySubscriptionRegistry.getAll(); Collection<ActiveSubscription> subscriptions = mySubscriptionRegistry.getAll();
@ -134,10 +136,25 @@ public class SubscriptionMatchingSubscriber implements MessageHandler {
matchResult.isInMemory() ? "in-memory" : "by querying the repository"); matchResult.isInMemory() ? "in-memory" : "by querying the repository");
IBaseResource payload = theMsg.getNewPayload(myFhirContext); IBaseResource payload = theMsg.getNewPayload(myFhirContext);
CanonicalSubscription subscription = nextActiveSubscription.getSubscription();
if (subscription.getPayloadString() != null && !subscription.getPayloadString().isEmpty()) {
isXml = subscription.getPayloadString().equals(Constants.CT_XML) || subscription.getPayloadString().equals(Constants.CT_FHIR_XML_NEW);
isJson = subscription.getPayloadString().equals(Constants.CT_JSON) || subscription.getPayloadString().equals(Constants.CT_FHIR_JSON_NEW);
isText = subscription.getPayloadString().equals(Constants.CT_TEXT);
}
ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage(); ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage();
deliveryMsg.setPayload(myFhirContext, payload);
deliveryMsg.setSubscription(nextActiveSubscription.getSubscription()); // Only include the payload if either XML or JSON was specified in the subscription's payload property
// See http://hl7.org/fhir/subscription-definitions.html#Subscription.channel.payload
if (isXml || isJson) {
deliveryMsg.setPayload(myFhirContext, payload, isXml);
} else if (isText) {
// TODO: Handle payload mimetype of text/plain (for just the .text representation of the resource being updated?)
}
deliveryMsg.setSubscription(subscription);
deliveryMsg.setOperationType(theMsg.getOperationType()); deliveryMsg.setOperationType(theMsg.getOperationType());
deliveryMsg.copyAdditionalPropertiesFrom(theMsg); deliveryMsg.copyAdditionalPropertiesFrom(theMsg);
if (payload == null) { if (payload == null) {

View File

@ -72,7 +72,7 @@ public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliv
EmailDetails details = new EmailDetails(); EmailDetails details = new EmailDetails();
details.setTo(destinationAddresses); details.setTo(destinationAddresses);
details.setFrom(from); details.setFrom(from);
details.setBodyTemplate(subscription.getPayloadString()); details.setBodyTemplate(theMessage.getPayloadString());
details.setSubjectTemplate(subjectTemplate); details.setSubjectTemplate(subjectTemplate);
details.setSubscription(subscription.getIdElement(myFhirContext)); details.setSubscription(subscription.getIdElement(myFhirContext));