INT-1530 reviewing
This commit is contained in:
@@ -45,10 +45,9 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
|
||||
|
||||
private static final String MESSAGE_FORMAT_ERROR = "JSON message is invalid. Expected a message in the format of either {\"headers\":{...},\"payload\":{...}} or {\"payload\":{...}.\"headers\":{...}} but was ";
|
||||
|
||||
private static Map<String, Class<?>> DEFAULT_HEADER_TYPES;
|
||||
private static final Map<String, Class<?>> DEFAULT_HEADER_TYPES = new HashMap<String, Class<?>>();
|
||||
|
||||
static {
|
||||
DEFAULT_HEADER_TYPES = new HashMap<String, Class<?>>();
|
||||
DEFAULT_HEADER_TYPES.put(MessageHeaders.ID, UUID.class);
|
||||
DEFAULT_HEADER_TYPES.put(MessageHeaders.TIMESTAMP, Long.class);
|
||||
DEFAULT_HEADER_TYPES.put(MessageHeaders.EXPIRATION_DATE, Long.class);
|
||||
@@ -72,6 +71,7 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
|
||||
this.payloadType = TypeFactory.type(typeReference);
|
||||
}
|
||||
|
||||
|
||||
public void setHeaderTypes(Map<String, Class<?>> headerTypes) {
|
||||
this.headerTypes.putAll(headerTypes);
|
||||
}
|
||||
@@ -87,28 +87,32 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
|
||||
return MessageBuilder.withPayload(readPayload(parser, jsonMessage)).build();
|
||||
}
|
||||
catch (JsonMappingException ex) {
|
||||
throw new IllegalArgumentException("Mapping of JSON message "+jsonMessage+" directly to payload of type "+payloadType.getRawClass().getName()+" failed.", ex);
|
||||
throw new IllegalArgumentException("Mapping of JSON message " + jsonMessage +
|
||||
" directly to payload of type " + payloadType.getRawClass().getName() + " failed.", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
String error = MESSAGE_FORMAT_ERROR + jsonMessage;
|
||||
Assert.isTrue(parser.nextToken() == JsonToken.START_OBJECT, error);
|
||||
|
||||
Map<String, Object> headers = null;
|
||||
Object payload = null;
|
||||
while(parser.nextToken() != JsonToken.END_OBJECT) {
|
||||
Assert.isTrue(parser.getCurrentToken() == JsonToken.FIELD_NAME, error);
|
||||
Assert.isTrue(parser.getCurrentName().equals("headers") || parser.getCurrentName().equals("payload"), error);
|
||||
if (parser.getCurrentName().equals("headers")) {
|
||||
boolean isHeadersToken = "headers".equals(parser.getCurrentName());
|
||||
boolean isPayloadToken = "payload".equals(parser.getCurrentName());
|
||||
Assert.isTrue(isHeadersToken || isPayloadToken, error);
|
||||
if (isHeadersToken) {
|
||||
Assert.isTrue(parser.nextToken() == JsonToken.START_OBJECT, error);
|
||||
headers = readHeaders(parser, jsonMessage);
|
||||
} else if (parser.getCurrentName().equals("payload")) {
|
||||
}
|
||||
else if (isPayloadToken) {
|
||||
parser.nextToken();
|
||||
try {
|
||||
payload = readPayload(parser, jsonMessage);
|
||||
}
|
||||
catch (JsonMappingException ex) {
|
||||
throw new IllegalArgumentException("Mapping payload of JSON message "+jsonMessage+" to payload type "+payloadType.getRawClass().getName()+" failed.", ex);
|
||||
throw new IllegalArgumentException("Mapping payload of JSON message " + jsonMessage +
|
||||
" to payload type " + this.payloadType.getRawClass().getName() + " failed.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,11 +120,7 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
|
||||
return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
|
||||
}
|
||||
}
|
||||
|
||||
protected Object readPayload(JsonParser parser, String jsonMessage) throws Exception {
|
||||
return objectMapper.readValue(parser, payloadType);
|
||||
}
|
||||
|
||||
|
||||
protected Map<String, Object> readHeaders(JsonParser parser, String jsonMessage) throws Exception{
|
||||
Map<String, Object> headers = new LinkedHashMap<String, Object>();
|
||||
while (parser.nextToken() != JsonToken.END_OBJECT) {
|
||||
@@ -132,9 +132,15 @@ public class JsonInboundMessageMapper implements InboundMessageMapper<String> {
|
||||
headers.put(headerName, this.objectMapper.readValue(parser, headerType));
|
||||
}
|
||||
catch (JsonMappingException ex) {
|
||||
throw new IllegalArgumentException("Mapping header \""+headerName+"\" of JSON message "+jsonMessage+" to header type "+payloadType.getRawClass().getName()+" failed.", ex);
|
||||
throw new IllegalArgumentException("Mapping header \"" + headerName + "\" of JSON message " +
|
||||
jsonMessage + " to header type " + this.payloadType.getRawClass().getName() + " failed.", ex);
|
||||
}
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
protected Object readPayload(JsonParser parser, String jsonMessage) throws Exception {
|
||||
return this.objectMapper.readValue(parser, this.payloadType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user