Add HapiMessageHeaders to BaseJsonMessage class

This commit is contained in:
Tadgh 2020-09-14 13:02:32 -04:00
parent 28a9a53917
commit c3ddf59d6f
4 changed files with 108 additions and 21 deletions

View File

@ -10,8 +10,6 @@ import ca.uhn.fhir.jpa.subscription.module.BaseSubscriptionDstu3Test;
import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.server.SimpleBundleProvider; import ca.uhn.fhir.rest.server.SimpleBundleProvider;
import ca.uhn.fhir.util.UrlUtil; import ca.uhn.fhir.util.UrlUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hl7.fhir.dstu3.model.BodySite; import org.hl7.fhir.dstu3.model.BodySite;
import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Coding;
@ -38,12 +36,9 @@ import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -115,17 +110,6 @@ public class InMemorySubscriptionMatcherR3Test extends BaseSubscriptionDstu3Test
assertNotMatched(pr, "ProcedureRequest?subject=Patient/123"); assertNotMatched(pr, "ProcedureRequest?subject=Patient/123");
} }
@Test
public void testSerializationOfMessageHeaders() throws JsonProcessingException {
Map<String, Object> headers = new HashMap<>();
headers.put("zoop", "zoop");
MessageHeaders mh = new MessageHeaders(headers);
ObjectMapper mapper = new ObjectMapper();
//Test that serialization of MessageHeaders works.
mapper.writeValueAsString(mh);
}
@Test @Test
public void testResourceById() { public void testResourceById() {

View File

@ -25,15 +25,17 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson { public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@JsonProperty("headers") @JsonProperty("headers")
private MessageHeaders myHeaders; private HapiMessageHeaders myHeaders;
private String RETRY_COUNT_HEADER = "retryCount"; private String RETRY_COUNT_HEADER = "retryCount";
private String FIRST_FAILURE_HEADER = "firstFailure"; private String FIRST_FAILURE_HEADER = "firstFailure";
@ -46,20 +48,98 @@ public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
super(); super();
setDefaultRetryHeaders(); setDefaultRetryHeaders();
} }
public static class HapiMessageHeaders implements Map<String, Object>{
private final Map<String, Object> headers;
public HapiMessageHeaders(Map<String, Object> theHeaders) {
headers = theHeaders;
}
public HapiMessageHeaders() {
headers = new HashMap<>();
}
@Override
public int size() {
return this.headers.size();
}
@Override
public boolean isEmpty() {
return this.headers.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.headers.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.headers.containsValue(value);
}
@Override
public Object get(Object key) {
return this.headers.get(key);
}
@Override
public Object put(String key, Object value) {
return this.headers.put(key, value);
}
@Override
public Object remove(Object key) {
return this.headers.remove(key);
}
@Override
public void putAll(Map<? extends String, ?> m) {
this.headers.putAll(m);
}
@Override
public void clear() {
this.headers.clear();
}
@Override
public Set<String> keySet() {
return this.headers.keySet();
}
@Override
public Collection<Object> values() {
return this.headers.values();
}
@Override
public Set<Entry<String, Object>> entrySet() {
return this.headers.entrySet();
}
}
protected void setDefaultRetryHeaders() { protected void setDefaultRetryHeaders() {
Map<String, Object> headers = new HashMap<>(); Map<String, Object> headers = new HashMap<>();
headers.put(RETRY_COUNT_HEADER, 0); headers.put(RETRY_COUNT_HEADER, 0);
headers.put(FIRST_FAILURE_HEADER, null); headers.put(FIRST_FAILURE_HEADER, null);
headers.put(LAST_FAILURE_HEADER, null); headers.put(LAST_FAILURE_HEADER, null);
MessageHeaders messageHeaders = new MessageHeaders(headers); HapiMessageHeaders messageHeaders = new HapiMessageHeaders(headers);
setHeaders(messageHeaders); setHeaders(messageHeaders);
} }
@Override @Override
public MessageHeaders getHeaders() { public MessageHeaders getHeaders() {
return new MessageHeaders(myHeaders);
}
public HapiMessageHeaders getHapiHeaders() {
return myHeaders; return myHeaders;
} }
public final Integer getRetryCount() { public final Integer getRetryCount() {
//TODO GGG this is not NPE-safe //TODO GGG this is not NPE-safe
return (Integer)this.getHeaders().get(RETRY_COUNT_HEADER); return (Integer)this.getHeaders().get(RETRY_COUNT_HEADER);
@ -77,7 +157,7 @@ public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
} }
public void setHeaders(MessageHeaders theHeaders) { public void setHeaders(HapiMessageHeaders theHeaders) {
myHeaders = theHeaders; myHeaders = theHeaders;
} }
} }

View File

@ -23,7 +23,6 @@ package ca.uhn.fhir.rest.server.messaging.json;
import ca.uhn.fhir.rest.server.messaging.BaseResourceModifiedMessage; import ca.uhn.fhir.rest.server.messaging.BaseResourceModifiedMessage;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.messaging.MessageHeaders;
public class BaseResourceModifiedJsonMessage extends BaseJsonMessage<BaseResourceModifiedMessage> { public class BaseResourceModifiedJsonMessage extends BaseJsonMessage<BaseResourceModifiedMessage> {
@ -46,7 +45,7 @@ public class BaseResourceModifiedJsonMessage extends BaseJsonMessage<BaseResourc
setDefaultRetryHeaders(); setDefaultRetryHeaders();
} }
public BaseResourceModifiedJsonMessage(MessageHeaders theRetryMessageHeaders, BaseResourceModifiedMessage thePayload) { public BaseResourceModifiedJsonMessage(HapiMessageHeaders theRetryMessageHeaders, BaseResourceModifiedMessage thePayload) {
myPayload = thePayload; myPayload = thePayload;
setHeaders(theRetryMessageHeaders); setHeaders(theRetryMessageHeaders);
} }

View File

@ -0,0 +1,24 @@
package ca.uhn.fhir.rest.server.messaging.json;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import org.springframework.messaging.MessageHeaders;
import java.io.IOException;
public class MessageHeaderDeserializer extends JsonDeserializer<MessageHeaders> {
@Override
public MessageHeaders deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = p.getCodec().readTree(p);
return null;
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return super.deserializeWithType(p, ctxt, typeDeserializer);
}
}