1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

format code

Signed-off-by: caotc <250622148@qq.com>
This commit is contained in:
caotc 2019-09-03 13:38:19 +08:00
parent 0a1f712094
commit 2cacc97d94
3 changed files with 277 additions and 270 deletions

View File

@ -1,47 +1,48 @@
package com.alibaba.cloud.stream.binder.rocketmq.support; package com.alibaba.cloud.stream.binder.rocketmq.support;
import org.apache.rocketmq.common.message.MessageConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;
/** /**
* Base for RocketMQ header mappers. * Base for RocketMQ header mappers.
* *
* @author caotc * @author caotc
* @date 2019-08-22 * @date 2019-08-22
* @since 2.1.1 * @since 2.1.1
*/ */
public abstract class AbstractRocketMQHeaderMapper implements RocketMQHeaderMapper{ public abstract class AbstractRocketMQHeaderMapper implements RocketMQHeaderMapper {
private static final Charset DEFAULT_CHARSET=StandardCharsets.UTF_8; private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private Charset charset; private Charset charset;
public AbstractRocketMQHeaderMapper() { public AbstractRocketMQHeaderMapper() {
this(DEFAULT_CHARSET); this(DEFAULT_CHARSET);
} }
public AbstractRocketMQHeaderMapper(Charset charset) { public AbstractRocketMQHeaderMapper(Charset charset) {
Assert.notNull(charset, "'charset' cannot be null"); Assert.notNull(charset, "'charset' cannot be null");
this.charset = charset; this.charset = charset;
} }
protected boolean matches(String headerName) { protected boolean matches(String headerName) {
return !MessageConst.STRING_HASH_SET.contains(headerName) && !MessageHeaders.ID.equals(headerName) return !MessageConst.STRING_HASH_SET.contains(headerName)
&& !MessageHeaders.TIMESTAMP.equals(headerName) && !MessageHeaders.CONTENT_TYPE.equals(headerName) && !MessageHeaders.ID.equals(headerName)
&& !MessageHeaders.REPLY_CHANNEL.equals(headerName) && !MessageHeaders.ERROR_CHANNEL.equals(headerName); && !MessageHeaders.TIMESTAMP.equals(headerName)
} && !MessageHeaders.CONTENT_TYPE.equals(headerName)
&& !MessageHeaders.REPLY_CHANNEL.equals(headerName)
&& !MessageHeaders.ERROR_CHANNEL.equals(headerName);
}
public Charset getCharset() { public Charset getCharset() {
return charset; return charset;
} }
public void setCharset(Charset charset) { public void setCharset(Charset charset) {
Assert.notNull(charset, "'charset' cannot be null"); Assert.notNull(charset, "'charset' cannot be null");
this.charset = charset; this.charset = charset;
} }
} }

View File

@ -1,258 +1,263 @@
package com.alibaba.cloud.stream.binder.rocketmq.support; package com.alibaba.cloud.stream.binder.rocketmq.support;
import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException;
import com.fasterxml.jackson.core.type.TypeReference; import java.nio.charset.Charset;
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.*;
import com.google.common.collect.Maps;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import java.io.IOException; import com.fasterxml.jackson.core.JsonProcessingException;
import java.nio.charset.Charset; import com.fasterxml.jackson.core.type.TypeReference;
import java.util.*; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
/** /**
* jackson header mapper for RocketMQ. * jackson header mapper for RocketMQ. Header types are added to a special header
* Header types are added to a special header {@link #JSON_TYPES}. * {@link #JSON_TYPES}.
* *
* @author caotc * @author caotc
* @date 2019-08-22 * @date 2019-08-22
* @since 2.1.1 * @since 2.1.1
*/ */
public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper {
private final static Logger log = LoggerFactory private final static Logger log = LoggerFactory
.getLogger(JacksonRocketMQHeaderMapper.class); .getLogger(JacksonRocketMQHeaderMapper.class);
private static final List<String> DEFAULT_TRUSTED_PACKAGES = private static final List<String> DEFAULT_TRUSTED_PACKAGES = Arrays
Arrays.asList( .asList("java.lang", "java.net", "java.util", "org.springframework.util");
"java.lang",
"java.net",
"java.util",
"org.springframework.util"
);
/** /**
* Header name for java types of other headers. * Header name for java types of other headers.
*/ */
public static final String JSON_TYPES = "spring_json_header_types"; public static final String JSON_TYPES = "spring_json_header_types";
private final ObjectMapper objectMapper; 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) { public JacksonRocketMQHeaderMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
} }
public JacksonRocketMQHeaderMapper(Charset charset, ObjectMapper objectMapper) { public JacksonRocketMQHeaderMapper(Charset charset, ObjectMapper objectMapper) {
super(charset); super(charset);
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
} }
@Override @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> target = Maps.newHashMap();
final Map<String, String> jsonHeaders = Maps.newHashMap(); final Map<String, String> jsonHeaders = Maps.newHashMap();
headers.forEach((key, value) -> { headers.forEach((key, value) -> {
if (matches(key)) { if (matches(key)) {
if (value instanceof String) { if (value instanceof String) {
target.put(key, (String) value); target.put(key, (String) value);
}else { }
try { else {
String className = value.getClass().getName(); try {
target.put(key, objectMapper.writeValueAsString(value)); String className = value.getClass().getName();
jsonHeaders.put(key, className); 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); catch (Exception e) {
} log.debug("Could not map " + key + " with type "
} + value.getClass().getName(), e);
} }
}); }
if (jsonHeaders.size() > 0) { }
try { });
target.put(JSON_TYPES, objectMapper.writeValueAsString(jsonHeaders)); if (jsonHeaders.size() > 0) {
} try {
catch (IllegalStateException | JsonProcessingException e) { target.put(JSON_TYPES, objectMapper.writeValueAsString(jsonHeaders));
log.error( "Could not add json types header",e); }
} catch (IllegalStateException | JsonProcessingException e) {
} log.error("Could not add json types header", e);
return target; }
} }
return target;
}
@Override @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, Object> target = Maps.newHashMap();
final Map<String, String> jsonTypes = decodeJsonTypes(source); final Map<String, String> jsonTypes = decodeJsonTypes(source);
source.forEach((key,value) -> { source.forEach((key, value) -> {
if (matches(key) && !(key.equals(JSON_TYPES))) { if (matches(key) && !(key.equals(JSON_TYPES))) {
if (jsonTypes != null && jsonTypes.containsKey(key)) { if (jsonTypes != null && jsonTypes.containsKey(key)) {
Class<?> type = Object.class; Class<?> type = Object.class;
String requestedType = jsonTypes.get(key); String requestedType = jsonTypes.get(key);
boolean trusted = trusted(requestedType); boolean trusted = trusted(requestedType);
if (trusted) { if (trusted) {
try { try {
type = ClassUtils.forName(requestedType, null); 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);
} }
}
if (trusted) { if (trusted) {
try { try {
Object val = decodeValue(value, type); Object val = decodeValue(value, type);
target.put(key, val); target.put(key, val);
} }
catch (IOException e) { catch (IOException e) {
log.error("Could not decode json type: " + value + " for key: " log.error("Could not decode json type: " + value
+ key,e); + " for key: " + key, e);
target.put(key, value); target.put(key, value);
} }
}else { }
target.put(key, new NonTrustedHeaderType(value, requestedType)); else {
} target.put(key, new NonTrustedHeaderType(value, requestedType));
}else { }
target.put(key, value); }
} else {
} target.put(key, value);
}); }
return new MessageHeaders(target); }
} });
return new MessageHeaders(target);
}
/** /**
* @param packagesToTrust the packages to trust. * @param packagesToTrust the packages to trust.
* @see #addTrustedPackages(Collection) * @see #addTrustedPackages(Collection)
*/ */
public void addTrustedPackages(String... packagesToTrust) { public void addTrustedPackages(String... packagesToTrust) {
if(Objects.nonNull(packagesToTrust)){ if (Objects.nonNull(packagesToTrust)) {
addTrustedPackages(Arrays.asList(packagesToTrust)); addTrustedPackages(Arrays.asList(packagesToTrust));
} }
} }
/** /**
* Add packages to the trusted packages list (default {@code java.util, java.lang}) used * Add packages to the trusted packages list (default {@code java.util, java.lang})
* when constructing objects from JSON. * used when constructing objects from JSON. If any of the supplied packages is
* If any of the supplied packages is {@code "*"}, all packages are trusted. * {@code "*"}, all packages are trusted. If a class for a non-trusted package is
* If a class for a non-trusted package is encountered, the header is returned to the * encountered, the header is returned to the application with value of type
* application with value of type {@link NonTrustedHeaderType}. * {@link NonTrustedHeaderType}.
* @param packagesToTrust the packages to trust. * @param packagesToTrust the packages to trust.
*/ */
public void addTrustedPackages(Collection<String> packagesToTrust) { public void addTrustedPackages(Collection<String> packagesToTrust) {
if (packagesToTrust != null) { if (packagesToTrust != null) {
for (String whiteList : packagesToTrust) { for (String whiteList : packagesToTrust) {
if ("*".equals(whiteList)) { if ("*".equals(whiteList)) {
this.trustedPackages.clear(); this.trustedPackages.clear();
break; break;
} }
else { else {
this.trustedPackages.add(whiteList); this.trustedPackages.add(whiteList);
} }
} }
} }
} }
public Set<String> getTrustedPackages() { public Set<String> getTrustedPackages() {
return this.trustedPackages; return this.trustedPackages;
} }
public ObjectMapper getObjectMapper() { public ObjectMapper getObjectMapper() {
return objectMapper; return objectMapper;
} }
private Object decodeValue(String jsonString, Class<?> type) throws IOException, LinkageError { private Object decodeValue(String jsonString, Class<?> type)
Object value = objectMapper.readValue(jsonString, type); throws IOException, LinkageError {
if (type.equals(NonTrustedHeaderType.class)) { Object value = objectMapper.readValue(jsonString, type);
// Upstream NTHT propagated; may be trusted here... if (type.equals(NonTrustedHeaderType.class)) {
NonTrustedHeaderType nth = (NonTrustedHeaderType) value; // Upstream NTHT propagated; may be trusted here...
if (trusted(nth.getUntrustedType())) { NonTrustedHeaderType nth = (NonTrustedHeaderType) value;
try { if (trusted(nth.getUntrustedType())) {
value = objectMapper.readValue(nth.getHeaderValue(), try {
ClassUtils.forName(nth.getUntrustedType(), null)); value = objectMapper.readValue(nth.getHeaderValue(),
} ClassUtils.forName(nth.getUntrustedType(), null));
catch (Exception e) { }
log.error("Could not decode header: " + nth,e); catch (Exception e) {
} log.error("Could not decode header: " + nth, e);
} }
} }
return value; }
} return value;
}
@Nullable @Nullable
private Map<String, String> decodeJsonTypes(Map<String, String> source) { private Map<String, String> decodeJsonTypes(Map<String, String> source) {
if(source.containsKey(JSON_TYPES)){ if (source.containsKey(JSON_TYPES)) {
String value=source.get(JSON_TYPES); String value = source.get(JSON_TYPES);
try { 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); }
} catch (IOException e) {
} log.error("Could not decode json types: " + value, e);
return null; }
} }
return null;
}
protected boolean trusted(String requestedType) { protected boolean trusted(String requestedType) {
if (requestedType.equals(NonTrustedHeaderType.class.getName())) { if (requestedType.equals(NonTrustedHeaderType.class.getName())) {
return true; return true;
} }
if (!this.trustedPackages.isEmpty()) { if (!this.trustedPackages.isEmpty()) {
int lastDot = requestedType.lastIndexOf('.'); int lastDot = requestedType.lastIndexOf('.');
if (lastDot < 0) { if (lastDot < 0) {
return false; return false;
} }
String packageName = requestedType.substring(0, lastDot); String packageName = requestedType.substring(0, lastDot);
for (String trustedPackage : this.trustedPackages) { for (String trustedPackage : this.trustedPackages) {
if (packageName.equals(trustedPackage) || packageName.startsWith(trustedPackage + ".")) { if (packageName.equals(trustedPackage)
return true; || packageName.startsWith(trustedPackage + ".")) {
} return true;
} }
return false; }
} return false;
return true; }
} return true;
}
/** /**
* Represents a header that could not be decoded due to an untrusted type. * Represents a header that could not be decoded due to an untrusted type.
*/ */
public static class NonTrustedHeaderType { public static class NonTrustedHeaderType {
private String headerValue; private String headerValue;
private String untrustedType; private String untrustedType;
public NonTrustedHeaderType() { public NonTrustedHeaderType() {
super(); super();
} }
NonTrustedHeaderType(String headerValue, String untrustedType) { NonTrustedHeaderType(String headerValue, String untrustedType) {
this.headerValue = headerValue; this.headerValue = headerValue;
this.untrustedType = untrustedType; this.untrustedType = untrustedType;
} }
public void setHeaderValue(String headerValue) {
this.headerValue = headerValue;
}
public void setHeaderValue(String headerValue) { public String getHeaderValue() {
this.headerValue = headerValue; return this.headerValue;
} }
public String getHeaderValue() { public void setUntrustedType(String untrustedType) {
return this.headerValue; this.untrustedType = untrustedType;
} }
public void setUntrustedType(String untrustedType) { public String getUntrustedType() {
this.untrustedType = untrustedType; return this.untrustedType;
} }
public String getUntrustedType() { @Override
return this.untrustedType; public String toString() {
} return "NonTrustedHeaderType [headerValue=" + headerValue + ", untrustedType="
+ this.untrustedType + "]";
}
@Override }
public String toString() {
return "NonTrustedHeaderType [headerValue=" + headerValue
+ ", untrustedType=" + this.untrustedType + "]";
}
}
} }

View File

@ -1,27 +1,28 @@
package com.alibaba.cloud.stream.binder.rocketmq.support; package com.alibaba.cloud.stream.binder.rocketmq.support;
import org.springframework.messaging.MessageHeaders;
import java.util.Map; import java.util.Map;
import org.springframework.messaging.MessageHeaders;
/** /**
* header value mapper for RocketMQ * header value mapper for RocketMQ
* *
* @author caotc * @author caotc
* @date 2019-08-22 * @date 2019-08-22
* @since 2.1.1 * @since 2.1.1
*/ */
public interface RocketMQHeaderMapper { public interface RocketMQHeaderMapper {
/** /**
* Map from the given {@link MessageHeaders} to the specified target message. * Map from the given {@link MessageHeaders} to the specified target message.
* @param headers the abstracted MessageHeaders. * @param headers the abstracted MessageHeaders.
* @return the native target message. * @return the native target message.
*/ */
Map<String,String> fromHeaders(MessageHeaders headers); Map<String, String> fromHeaders(MessageHeaders headers);
/**
* Map from the given target message to abstracted {@link MessageHeaders}. /**
* @param source the native target message. * Map from the given target message to abstracted {@link MessageHeaders}.
* @return the target headers. * @param source the native target message.
*/ * @return the target headers.
MessageHeaders toHeaders(Map<String,String> source); */
MessageHeaders toHeaders(Map<String, String> source);
} }