Fix an unintended regression in #1357 (#1429)

* Start working on a tweak to #1357 - Not yet complete

* Tweaks to avoid an unintended regression from #1357
This commit is contained in:
James Agnew 2019-08-15 08:35:50 -04:00 committed by GitHub
parent f29c37a654
commit 1e07fcd2b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 31 deletions

View File

@ -197,12 +197,25 @@ public enum EncodingEnum {
} }
} }
private static String getTypeWithoutCharset(final String theContentType) { static String getTypeWithoutCharset(final String theContentType) {
if (theContentType == null) { if (theContentType == null) {
return null; return null;
} else { } else {
String[] contentTypeSplitted = theContentType.split(";");
return contentTypeSplitted[0]; int start = 0;
for (; start < theContentType.length(); start++) {
if (theContentType.charAt(start) != ' ') {
break;
}
}
int end = start;
for (; end < theContentType.length(); end++) {
if (theContentType.charAt(end) == ' ' || theContentType.charAt(end) == ';') {
break;
}
}
return theContentType.substring(start, end);
} }
} }

View File

@ -0,0 +1,17 @@
package ca.uhn.fhir.rest.api;
import org.junit.Test;
import static org.junit.Assert.*;
public class EncodingEnumTest {
@Test
public void getTypeWithoutCharset() {
assertEquals("text/plain", EncodingEnum.getTypeWithoutCharset("text/plain"));
assertEquals("text/plain", EncodingEnum.getTypeWithoutCharset(" text/plain"));
assertEquals("text/plain", EncodingEnum.getTypeWithoutCharset(" text/plain; charset=utf-8"));
assertEquals("text/plain", EncodingEnum.getTypeWithoutCharset(" text/plain ; charset=utf-8"));
}
}

View File

@ -24,5 +24,5 @@ import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IIdType;
public interface IResourceRetriever { public interface IResourceRetriever {
IBaseResource getResource(IIdType id); IBaseResource getResource(IIdType theId);
} }

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.jpa.subscription.module.subscriber;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription;
import ca.uhn.fhir.jpa.subscription.module.ResourceModifiedMessage; import ca.uhn.fhir.jpa.subscription.module.ResourceModifiedMessage;
import ca.uhn.fhir.rest.api.EncodingEnum;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -97,15 +98,9 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
mySubscription = theSubscription; mySubscription = theSubscription;
} }
public void setPayload(FhirContext theCtx, IBaseResource thePayload, Boolean theXml) { public void setPayload(FhirContext theCtx, IBaseResource thePayload, EncodingEnum theEncoding) {
myPayload = thePayload; myPayload = thePayload;
myPayloadString = theEncoding.newParser(theCtx).encodeResourceToString(thePayload);
if (theXml) {
myPayloadString = theCtx.newXmlParser().encodeResourceToString(thePayload);
} else {
myPayloadString = theCtx.newJsonParser().encodeResourceToString(thePayload);
}
myPayloadId = thePayload.getIdElement().toUnqualified().getValue(); myPayloadId = thePayload.getIdElement().toUnqualified().getValue();
} }

View File

@ -155,10 +155,6 @@ public class SubscriptionDeliveringRestHookSubscriber extends BaseSubscriptionDe
String payloadString = subscription.getPayloadString(); String payloadString = subscription.getPayloadString();
EncodingEnum payloadType = null; EncodingEnum payloadType = null;
if (payloadString != null) { if (payloadString != null) {
if (payloadString.contains(";")) {
payloadString = payloadString.substring(0, payloadString.indexOf(';'));
}
payloadString = payloadString.trim();
payloadType = EncodingEnum.forContentType(payloadString); payloadType = EncodingEnum.forContentType(payloadString);
} }

View File

@ -11,6 +11,7 @@ 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 ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.EncodingEnum;
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;
@ -25,6 +26,7 @@ import org.springframework.stereotype.Service;
import java.util.Collection; import java.util.Collection;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank;
/*- /*-
@ -104,7 +106,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; Boolean isText = false;
Collection<ActiveSubscription> subscriptions = mySubscriptionRegistry.getAll(); Collection<ActiveSubscription> subscriptions = mySubscriptionRegistry.getAll();
@ -138,28 +140,19 @@ public class SubscriptionMatchingSubscriber implements MessageHandler {
IBaseResource payload = theMsg.getNewPayload(myFhirContext); IBaseResource payload = theMsg.getNewPayload(myFhirContext);
CanonicalSubscription subscription = nextActiveSubscription.getSubscription(); CanonicalSubscription subscription = nextActiveSubscription.getSubscription();
EncodingEnum encoding = null;
if (subscription.getPayloadString() != null && !subscription.getPayloadString().isEmpty()) { if (subscription.getPayloadString() != null && !subscription.getPayloadString().isEmpty()) {
isXml = subscription.getPayloadString().equals(Constants.CT_XML) || subscription.getPayloadString().equals(Constants.CT_FHIR_XML_NEW); encoding = EncodingEnum.forContentType(subscription.getPayloadString());
isJson = subscription.getPayloadString().equals(Constants.CT_JSON) || subscription.getPayloadString().equals(Constants.CT_FHIR_JSON_NEW);
isText = subscription.getPayloadString().equals(Constants.CT_TEXT); isText = subscription.getPayloadString().equals(Constants.CT_TEXT);
} }
encoding = defaultIfNull(encoding, EncodingEnum.JSON);
ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage(); ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage();
// Only include the payload if either XML or JSON was specified in the subscription's payload property deliveryMsg.setPayload(myFhirContext, payload, encoding);
// 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.setSubscription(subscription);
deliveryMsg.setOperationType(theMsg.getOperationType()); deliveryMsg.setOperationType(theMsg.getOperationType());
deliveryMsg.copyAdditionalPropertiesFrom(theMsg); deliveryMsg.copyAdditionalPropertiesFrom(theMsg);
if (payload == null) {
deliveryMsg.setPayloadId(theMsg.getId(myFhirContext));
}
// Interceptor call: SUBSCRIPTION_RESOURCE_MATCHED // Interceptor call: SUBSCRIPTION_RESOURCE_MATCHED
HookParams params = new HookParams() HookParams params = new HookParams()

View File

@ -20,10 +20,12 @@ package ca.uhn.fhir.jpa.subscription.module.subscriber.email;
* #L% * #L%
*/ */
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.model.entity.ModelConfig;
import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription;
import ca.uhn.fhir.jpa.subscription.module.subscriber.BaseSubscriptionDeliverySubscriber; import ca.uhn.fhir.jpa.subscription.module.subscriber.BaseSubscriptionDeliverySubscriber;
import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryMessage; import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryMessage;
import ca.uhn.fhir.rest.api.EncodingEnum;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -43,6 +45,8 @@ public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliv
@Autowired @Autowired
private ModelConfig myModelConfig; private ModelConfig myModelConfig;
@Autowired
private FhirContext myCtx;
private IEmailSender myEmailSender; private IEmailSender myEmailSender;
@ -66,13 +70,21 @@ public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliv
} }
} }
String payload = "";
if (isNotBlank(subscription.getPayloadString())) {
EncodingEnum encoding = EncodingEnum.forContentType(subscription.getPayloadString());
if (encoding != null) {
payload = theMessage.getPayloadString();
}
}
String from = processEmailAddressUri(defaultString(subscription.getEmailDetails().getFrom(), myModelConfig.getEmailFromAddress())); String from = processEmailAddressUri(defaultString(subscription.getEmailDetails().getFrom(), myModelConfig.getEmailFromAddress()));
String subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate()); String subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate());
EmailDetails details = new EmailDetails(); EmailDetails details = new EmailDetails();
details.setTo(destinationAddresses); details.setTo(destinationAddresses);
details.setFrom(from); details.setFrom(from);
details.setBodyTemplate(theMessage.getPayloadString()); details.setBodyTemplate(payload);
details.setSubjectTemplate(subjectTemplate); details.setSubjectTemplate(subjectTemplate);
details.setSubscription(subscription.getIdElement(myFhirContext)); details.setSubscription(subscription.getIdElement(myFhirContext));