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,14 +1,12 @@
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.StandardCharsets;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;
/**
* Base for RocketMQ header mappers.
*
@ -31,9 +29,12 @@ public abstract class AbstractRocketMQHeaderMapper implements RocketMQHeaderMapp
}
protected boolean matches(String headerName) {
return !MessageConst.STRING_HASH_SET.contains(headerName) && !MessageHeaders.ID.equals(headerName)
&& !MessageHeaders.TIMESTAMP.equals(headerName) && !MessageHeaders.CONTENT_TYPE.equals(headerName)
&& !MessageHeaders.REPLY_CHANNEL.equals(headerName) && !MessageHeaders.ERROR_CHANNEL.equals(headerName);
return !MessageConst.STRING_HASH_SET.contains(headerName)
&& !MessageHeaders.ID.equals(headerName)
&& !MessageHeaders.TIMESTAMP.equals(headerName)
&& !MessageHeaders.CONTENT_TYPE.equals(headerName)
&& !MessageHeaders.REPLY_CHANNEL.equals(headerName)
&& !MessageHeaders.ERROR_CHANNEL.equals(headerName);
}
public Charset getCharset() {

View File

@ -1,22 +1,23 @@
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
@ -26,13 +27,8 @@ 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;
@ -59,14 +56,16 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
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);
}
}
}
@ -95,7 +94,8 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
if (trusted) {
try {
type = ClassUtils.forName(requestedType, null);
}catch (Exception 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);
}
}
@ -132,11 +134,11 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
}
/**
* 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...
@ -184,7 +187,9 @@ public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper{
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);
@ -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 + "]";
}
}

View File

@ -1,9 +1,9 @@
package com.alibaba.cloud.stream.binder.rocketmq.support;
import org.springframework.messaging.MessageHeaders;
import java.util.Map;
import org.springframework.messaging.MessageHeaders;
/**
* header value mapper for RocketMQ
*
@ -18,6 +18,7 @@ public interface RocketMQHeaderMapper {
* @return the native target message.
*/
Map<String, String> fromHeaders(MessageHeaders headers);
/**
* Map from the given target message to abstracted {@link MessageHeaders}.
* @param source the native target message.