|
|
|
@ -1,38 +1,34 @@
|
|
|
|
|
package com.alibaba.cloud.stream.binder.rocketmq.support;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.lang.Nullable;
|
|
|
|
|
import org.springframework.messaging.MessageHeaders;
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* jackson header mapper for RocketMQ.
|
|
|
|
|
* Header types are added to a special header {@link #JSON_TYPES}.
|
|
|
|
|
* jackson header mapper for RocketMQ. Header types are added to a special header
|
|
|
|
|
* {@link #JSON_TYPES}.
|
|
|
|
|
*
|
|
|
|
|
* @author caotc
|
|
|
|
|
* @date 2019-08-22
|
|
|
|
|
* @since 2.1.1
|
|
|
|
|
*/
|
|
|
|
|
public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper {
|
|
|
|
|
private final static Logger log = LoggerFactory
|
|
|
|
|
.getLogger(JacksonRocketMQHeaderMapper.class);
|
|
|
|
|
|
|
|
|
|
private static final List<String> DEFAULT_TRUSTED_PACKAGES =
|
|
|
|
|
Arrays.asList(
|
|
|
|
|
"java.lang",
|
|
|
|
|
"java.net",
|
|
|
|
|
"java.util",
|
|
|
|
|
"org.springframework.util"
|
|
|
|
|
);
|
|
|
|
|
private static final List<String> DEFAULT_TRUSTED_PACKAGES = Arrays
|
|
|
|
|
.asList("java.lang", "java.net", "java.util", "org.springframework.util");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Header name for java types of other headers.
|
|
|
|
@ -40,7 +36,8 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
public static final String JSON_TYPES = "spring_json_header_types";
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
private final Set<String> trustedPackages = new LinkedHashSet<>(DEFAULT_TRUSTED_PACKAGES);
|
|
|
|
|
private final Set<String> trustedPackages = new LinkedHashSet<>(
|
|
|
|
|
DEFAULT_TRUSTED_PACKAGES);
|
|
|
|
|
|
|
|
|
|
public JacksonRocketMQHeaderMapper(ObjectMapper objectMapper) {
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
@ -52,21 +49,23 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String,String> fromHeaders(MessageHeaders headers) {
|
|
|
|
|
public Map<String, String> fromHeaders(MessageHeaders headers) {
|
|
|
|
|
final Map<String, String> target = Maps.newHashMap();
|
|
|
|
|
final Map<String, String> jsonHeaders = Maps.newHashMap();
|
|
|
|
|
headers.forEach((key, value) -> {
|
|
|
|
|
if (matches(key)) {
|
|
|
|
|
if (value instanceof String) {
|
|
|
|
|
target.put(key, (String) value);
|
|
|
|
|
}else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
try {
|
|
|
|
|
String className = value.getClass().getName();
|
|
|
|
|
target.put(key, objectMapper.writeValueAsString(value));
|
|
|
|
|
jsonHeaders.put(key, className);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
log.debug("Could not map " + key + " with type " + value.getClass().getName(),e);
|
|
|
|
|
log.debug("Could not map " + key + " with type "
|
|
|
|
|
+ value.getClass().getName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -76,17 +75,17 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
target.put(JSON_TYPES, objectMapper.writeValueAsString(jsonHeaders));
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalStateException | JsonProcessingException e) {
|
|
|
|
|
log.error( "Could not add json types header",e);
|
|
|
|
|
log.error("Could not add json types header", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public MessageHeaders toHeaders(Map<String,String> source) {
|
|
|
|
|
public MessageHeaders toHeaders(Map<String, String> source) {
|
|
|
|
|
final Map<String, Object> target = Maps.newHashMap();
|
|
|
|
|
final Map<String, String> jsonTypes = decodeJsonTypes(source);
|
|
|
|
|
source.forEach((key,value) -> {
|
|
|
|
|
source.forEach((key, value) -> {
|
|
|
|
|
if (matches(key) && !(key.equals(JSON_TYPES))) {
|
|
|
|
|
if (jsonTypes != null && jsonTypes.containsKey(key)) {
|
|
|
|
|
Class<?> type = Object.class;
|
|
|
|
@ -95,8 +94,9 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
if (trusted) {
|
|
|
|
|
try {
|
|
|
|
|
type = ClassUtils.forName(requestedType, null);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
log.error( "Could not load class for header: " + key,e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
log.error("Could not load class for header: " + key, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -106,14 +106,16 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
target.put(key, val);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
log.error("Could not decode json type: " + value + " for key: "
|
|
|
|
|
+ key,e);
|
|
|
|
|
log.error("Could not decode json type: " + value
|
|
|
|
|
+ " for key: " + key, e);
|
|
|
|
|
target.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
target.put(key, new NonTrustedHeaderType(value, requestedType));
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
target.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -126,17 +128,17 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
* @see #addTrustedPackages(Collection)
|
|
|
|
|
*/
|
|
|
|
|
public void addTrustedPackages(String... packagesToTrust) {
|
|
|
|
|
if(Objects.nonNull(packagesToTrust)){
|
|
|
|
|
if (Objects.nonNull(packagesToTrust)) {
|
|
|
|
|
addTrustedPackages(Arrays.asList(packagesToTrust));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add packages to the trusted packages list (default {@code java.util, java.lang}) used
|
|
|
|
|
* when constructing objects from JSON.
|
|
|
|
|
* If any of the supplied packages is {@code "*"}, all packages are trusted.
|
|
|
|
|
* If a class for a non-trusted package is encountered, the header is returned to the
|
|
|
|
|
* application with value of type {@link NonTrustedHeaderType}.
|
|
|
|
|
* Add packages to the trusted packages list (default {@code java.util, java.lang})
|
|
|
|
|
* used when constructing objects from JSON. If any of the supplied packages is
|
|
|
|
|
* {@code "*"}, all packages are trusted. If a class for a non-trusted package is
|
|
|
|
|
* encountered, the header is returned to the application with value of type
|
|
|
|
|
* {@link NonTrustedHeaderType}.
|
|
|
|
|
* @param packagesToTrust the packages to trust.
|
|
|
|
|
*/
|
|
|
|
|
public void addTrustedPackages(Collection<String> packagesToTrust) {
|
|
|
|
@ -161,7 +163,8 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
return objectMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Object decodeValue(String jsonString, Class<?> type) throws IOException, LinkageError {
|
|
|
|
|
private Object decodeValue(String jsonString, Class<?> type)
|
|
|
|
|
throws IOException, LinkageError {
|
|
|
|
|
Object value = objectMapper.readValue(jsonString, type);
|
|
|
|
|
if (type.equals(NonTrustedHeaderType.class)) {
|
|
|
|
|
// Upstream NTHT propagated; may be trusted here...
|
|
|
|
@ -172,7 +175,7 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
ClassUtils.forName(nth.getUntrustedType(), null));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
log.error("Could not decode header: " + nth,e);
|
|
|
|
|
log.error("Could not decode header: " + nth, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -181,13 +184,15 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
private Map<String, String> decodeJsonTypes(Map<String, String> source) {
|
|
|
|
|
if(source.containsKey(JSON_TYPES)){
|
|
|
|
|
String value=source.get(JSON_TYPES);
|
|
|
|
|
if (source.containsKey(JSON_TYPES)) {
|
|
|
|
|
String value = source.get(JSON_TYPES);
|
|
|
|
|
try {
|
|
|
|
|
return objectMapper.readValue(value,new TypeReference<Map<String,String>>(){});
|
|
|
|
|
return objectMapper.readValue(value,
|
|
|
|
|
new TypeReference<Map<String, String>>() {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
log.error("Could not decode json types: " + value,e);
|
|
|
|
|
log.error("Could not decode json types: " + value, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
@ -204,7 +209,8 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
}
|
|
|
|
|
String packageName = requestedType.substring(0, lastDot);
|
|
|
|
|
for (String trustedPackage : this.trustedPackages) {
|
|
|
|
|
if (packageName.equals(trustedPackage) || packageName.startsWith(trustedPackage + ".")) {
|
|
|
|
|
if (packageName.equals(trustedPackage)
|
|
|
|
|
|| packageName.startsWith(trustedPackage + ".")) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -231,7 +237,6 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
this.untrustedType = untrustedType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setHeaderValue(String headerValue) {
|
|
|
|
|
this.headerValue = headerValue;
|
|
|
|
|
}
|
|
|
|
@ -250,8 +255,8 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "NonTrustedHeaderType [headerValue=" + headerValue
|
|
|
|
|
+ ", untrustedType=" + this.untrustedType + "]";
|
|
|
|
|
return "NonTrustedHeaderType [headerValue=" + headerValue + ", untrustedType="
|
|
|
|
|
+ this.untrustedType + "]";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|