Skip to content

Commit

Permalink
Tweaks to avoid an unintended regression from #1357
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Aug 15, 2019
1 parent b57c919 commit 9024df2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,25 @@ public static EncodingEnum forContentTypeStrict(final String theContentType) {
}
}

private static String getTypeWithoutCharset(final String theContentType) {
static String getTypeWithoutCharset(final String theContentType) {
if (theContentType == null) {
return null;
} 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
import org.hl7.fhir.instance.model.api.IIdType;

public interface IResourceRetriever {
IBaseResource getResource(IIdType id);
IBaseResource getResource(IIdType theId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ public void handleMessage(ResourceDeliveryMessage theMessage) throws MessagingEx
String payloadString = subscription.getPayloadString();
EncodingEnum payloadType = null;
if (payloadString != null) {
if (payloadString.contains(";")) {
payloadString = payloadString.substring(0, payloadString.indexOf(';'));
}
payloadString = payloadString.trim();
payloadType = EncodingEnum.forContentType(payloadString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Collection;

import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/*-
Expand Down Expand Up @@ -144,23 +145,14 @@ private void doMatchActiveSubscriptionsAndDeliver(ResourceModifiedMessage theMsg
encoding = EncodingEnum.forContentType(subscription.getPayloadString());
isText = subscription.getPayloadString().equals(Constants.CT_TEXT);
}
encoding = defaultIfNull(encoding, EncodingEnum.JSON);

ResourceDeliveryMessage deliveryMsg = new ResourceDeliveryMessage();

// 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 (encoding != null) {
deliveryMsg.setPayload(myFhirContext, payload, encoding);
} else if (isText) {
// TODO: Handle payload mimetype of text/plain (for just the .text representation of the resource being updated?)
}

deliveryMsg.setPayload(myFhirContext, payload, encoding);
deliveryMsg.setSubscription(subscription);
deliveryMsg.setOperationType(theMsg.getOperationType());
deliveryMsg.copyAdditionalPropertiesFrom(theMsg);
if (deliveryMsg.getPayload(myFhirContext) == null) {
deliveryMsg.setPayloadId(theMsg.getId(myFhirContext));
}

// Interceptor call: SUBSCRIPTION_RESOURCE_MATCHED
HookParams params = new HookParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
* #L%
*/

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.model.entity.ModelConfig;
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.ResourceDeliveryMessage;
import ca.uhn.fhir.rest.api.EncodingEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,6 +45,8 @@ public class SubscriptionDeliveringEmailSubscriber extends BaseSubscriptionDeliv

@Autowired
private ModelConfig myModelConfig;
@Autowired
private FhirContext myCtx;

private IEmailSender myEmailSender;

Expand All @@ -66,13 +70,21 @@ public void handleMessage(ResourceDeliveryMessage theMessage) throws Exception {
}
}

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 subjectTemplate = defaultString(subscription.getEmailDetails().getSubjectTemplate(), provideDefaultSubjectTemplate());

EmailDetails details = new EmailDetails();
details.setTo(destinationAddresses);
details.setFrom(from);
details.setBodyTemplate(theMessage.getPayloadString());
details.setBodyTemplate(payload);
details.setSubjectTemplate(subjectTemplate);
details.setSubscription(subscription.getIdElement(myFhirContext));

Expand Down

0 comments on commit 9024df2

Please sign in to comment.