INT-4440: Support serialized UUID in headers

JIRA: https://jira.spring.io/browse/INT-4440

The previous fix eliminated an extra `generateId()` call, but at the
same introduced regression do not populate `id` and `timestamp` from
the serialized state, e.g. after JSON transferring over the network

* Introduce a couple utility methods in the `MutableMessageHeaders`
to extract and parse `id` and `timestamp` from the provided headers

**Cherry-pick to 5.0.x**
This commit is contained in:
Nathan Kurtyka
2018-03-22 15:49:46 +00:00
committed by Artem Bilan
parent a9eb922d35
commit ae2aa8b6d1
2 changed files with 73 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2017 the original author or authors.
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.integration.support;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.UUID;
@@ -30,6 +31,7 @@ import org.springframework.messaging.MessageHeaders;
* @author Stuart Williams
* @author David Turanski
* @author Artem Bilan
* @author Nathan Kurtyka
*
* @since 4.2
*/
@@ -38,16 +40,12 @@ public class MutableMessageHeaders extends MessageHeaders {
private static final long serialVersionUID = 3084692953798643018L;
public MutableMessageHeaders(@Nullable Map<String, Object> headers) {
this(headers,
(headers != null ?
(UUID) headers.get(MessageHeaders.ID)
: null),
(headers != null ?
(Long) headers.get(MessageHeaders.TIMESTAMP)
: null));
super(headers, extractId(headers), extractTimestamp(headers));
}
protected MutableMessageHeaders(@Nullable Map<String, Object> headers, @Nullable UUID id, @Nullable Long timestamp) {
protected MutableMessageHeaders(@Nullable Map<String, Object> headers, @Nullable UUID id,
@Nullable Long timestamp) {
super(headers, id, timestamp);
}
@@ -76,4 +74,31 @@ public class MutableMessageHeaders extends MessageHeaders {
return super.getRawHeaders().remove(key);
}
private static UUID extractId(@Nullable Map<String, Object> headers) {
if (headers != null && headers.containsKey(MessageHeaders.ID)) {
Object id = headers.get(MessageHeaders.ID);
if (id instanceof String) {
return UUID.fromString((String) id);
}
else if (id instanceof byte[]) {
ByteBuffer bb = ByteBuffer.wrap((byte[]) id);
return new UUID(bb.getLong(), bb.getLong());
}
else {
return (UUID) id;
}
}
return null;
}
private static Long extractTimestamp(@Nullable Map<String, Object> headers) {
if (headers != null && headers.containsKey(MessageHeaders.TIMESTAMP)) {
Object timestamp = headers.get(MessageHeaders.TIMESTAMP);
return (timestamp instanceof String) ? Long.parseLong((String) timestamp) : (Long) timestamp;
}
return null;
}
}