From 2cacc97d945598b78c8033b9e61957fb8f044640 Mon Sep 17 00:00:00 2001 From: caotc <250622148@qq.com> Date: Tue, 3 Sep 2019 13:38:19 +0800 Subject: [PATCH] format code Signed-off-by: caotc <250622148@qq.com> --- .../support/AbstractRocketMQHeaderMapper.java | 79 ++-- .../support/JacksonRocketMQHeaderMapper.java | 437 +++++++++--------- .../support/RocketMQHeaderMapper.java | 41 +- 3 files changed, 282 insertions(+), 275 deletions(-) diff --git a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/AbstractRocketMQHeaderMapper.java b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/AbstractRocketMQHeaderMapper.java index e91b6e2f7..be14bb389 100644 --- a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/AbstractRocketMQHeaderMapper.java +++ b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/AbstractRocketMQHeaderMapper.java @@ -1,47 +1,48 @@ package com.alibaba.cloud.stream.binder.rocketmq.support; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + 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; - /** -* Base for RocketMQ header mappers. -* -* @author caotc -* @date 2019-08-22 -* @since 2.1.1 -*/ -public abstract class AbstractRocketMQHeaderMapper implements RocketMQHeaderMapper{ - private static final Charset DEFAULT_CHARSET=StandardCharsets.UTF_8; - - private Charset charset; - - public AbstractRocketMQHeaderMapper() { - this(DEFAULT_CHARSET); - } - - public AbstractRocketMQHeaderMapper(Charset charset) { - Assert.notNull(charset, "'charset' cannot be null"); - this.charset = charset; - } - - 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); - } - - public Charset getCharset() { - return charset; - } - - public void setCharset(Charset charset) { - Assert.notNull(charset, "'charset' cannot be null"); - this.charset = charset; - } + * Base for RocketMQ header mappers. + * + * @author caotc + * @date 2019-08-22 + * @since 2.1.1 + */ +public abstract class AbstractRocketMQHeaderMapper implements RocketMQHeaderMapper { + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + + private Charset charset; + + public AbstractRocketMQHeaderMapper() { + this(DEFAULT_CHARSET); + } + + public AbstractRocketMQHeaderMapper(Charset charset) { + Assert.notNull(charset, "'charset' cannot be null"); + this.charset = charset; + } + + 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); + } + + public Charset getCharset() { + return charset; + } + + public void setCharset(Charset charset) { + Assert.notNull(charset, "'charset' cannot be null"); + this.charset = charset; + } } diff --git a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/JacksonRocketMQHeaderMapper.java b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/JacksonRocketMQHeaderMapper.java index db5d446ba..91c5abdf8 100644 --- a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/JacksonRocketMQHeaderMapper.java +++ b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/JacksonRocketMQHeaderMapper.java @@ -1,258 +1,263 @@ 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{ - private final static Logger log = LoggerFactory - .getLogger(JacksonRocketMQHeaderMapper.class); - - private static final List DEFAULT_TRUSTED_PACKAGES = - Arrays.asList( - "java.lang", - "java.net", - "java.util", - "org.springframework.util" - ); +public class JacksonRocketMQHeaderMapper extends AbstractRocketMQHeaderMapper { + private final static Logger log = LoggerFactory + .getLogger(JacksonRocketMQHeaderMapper.class); - /** - * Header name for java types of other headers. - */ - public static final String JSON_TYPES = "spring_json_header_types"; + private static final List DEFAULT_TRUSTED_PACKAGES = Arrays + .asList("java.lang", "java.net", "java.util", "org.springframework.util"); - private final ObjectMapper objectMapper; - private final Set trustedPackages = new LinkedHashSet<>(DEFAULT_TRUSTED_PACKAGES); + /** + * Header name for java types of other headers. + */ + public static final String JSON_TYPES = "spring_json_header_types"; - public JacksonRocketMQHeaderMapper(ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } + private final ObjectMapper objectMapper; + private final Set trustedPackages = new LinkedHashSet<>( + DEFAULT_TRUSTED_PACKAGES); - public JacksonRocketMQHeaderMapper(Charset charset, ObjectMapper objectMapper) { - super(charset); - this.objectMapper = objectMapper; - } + public JacksonRocketMQHeaderMapper(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } - @Override - public Map fromHeaders(MessageHeaders headers) { - final Map target = Maps.newHashMap(); - final Map jsonHeaders = Maps.newHashMap(); - headers.forEach((key, value) -> { - if (matches(key)) { - if (value instanceof String) { - target.put(key, (String) value); - }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); - } - } - } - }); - if (jsonHeaders.size() > 0) { - try { - target.put(JSON_TYPES, objectMapper.writeValueAsString(jsonHeaders)); - } - catch (IllegalStateException | JsonProcessingException e) { - log.error( "Could not add json types header",e); - } - } - return target; - } + public JacksonRocketMQHeaderMapper(Charset charset, ObjectMapper objectMapper) { + super(charset); + this.objectMapper = objectMapper; + } - @Override - public MessageHeaders toHeaders(Map source) { - final Map target = Maps.newHashMap(); - final Map jsonTypes = decodeJsonTypes(source); - source.forEach((key,value) -> { - if (matches(key) && !(key.equals(JSON_TYPES))) { - if (jsonTypes != null && jsonTypes.containsKey(key)) { - Class type = Object.class; - String requestedType = jsonTypes.get(key); - boolean trusted = trusted(requestedType); - if (trusted) { - try { - type = ClassUtils.forName(requestedType, null); - }catch (Exception e) { - log.error( "Could not load class for header: " + key,e); - } - } + @Override + public Map fromHeaders(MessageHeaders headers) { + final Map target = Maps.newHashMap(); + final Map jsonHeaders = Maps.newHashMap(); + headers.forEach((key, value) -> { + if (matches(key)) { + if (value instanceof String) { + target.put(key, (String) value); + } + 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); + } + } + } + }); + if (jsonHeaders.size() > 0) { + try { + target.put(JSON_TYPES, objectMapper.writeValueAsString(jsonHeaders)); + } + catch (IllegalStateException | JsonProcessingException e) { + log.error("Could not add json types header", e); + } + } + return target; + } - if (trusted) { - try { - Object val = decodeValue(value, type); - target.put(key, val); - } - catch (IOException e) { - log.error("Could not decode json type: " + value + " for key: " - + key,e); - target.put(key, value); - } - }else { - target.put(key, new NonTrustedHeaderType(value, requestedType)); - } - }else { - target.put(key, value); - } - } - }); - return new MessageHeaders(target); - } + @Override + public MessageHeaders toHeaders(Map source) { + final Map target = Maps.newHashMap(); + final Map jsonTypes = decodeJsonTypes(source); + source.forEach((key, value) -> { + if (matches(key) && !(key.equals(JSON_TYPES))) { + if (jsonTypes != null && jsonTypes.containsKey(key)) { + Class type = Object.class; + String requestedType = jsonTypes.get(key); + boolean trusted = trusted(requestedType); + if (trusted) { + try { + type = ClassUtils.forName(requestedType, null); + } + catch (Exception e) { + log.error("Could not load class for header: " + key, e); + } + } - /** - * @param packagesToTrust the packages to trust. - * @see #addTrustedPackages(Collection) - */ - public void addTrustedPackages(String... packagesToTrust) { - if(Objects.nonNull(packagesToTrust)){ - addTrustedPackages(Arrays.asList(packagesToTrust)); - } - } + if (trusted) { + try { + Object val = decodeValue(value, type); + target.put(key, val); + } + catch (IOException e) { + log.error("Could not decode json type: " + value + + " for key: " + key, e); + target.put(key, value); + } + } + else { + target.put(key, new NonTrustedHeaderType(value, requestedType)); + } + } + else { + target.put(key, value); + } + } + }); + return new MessageHeaders(target); + } - /** - * 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 packagesToTrust) { - if (packagesToTrust != null) { - for (String whiteList : packagesToTrust) { - if ("*".equals(whiteList)) { - this.trustedPackages.clear(); - break; - } - else { - this.trustedPackages.add(whiteList); - } - } - } - } + /** + * @param packagesToTrust the packages to trust. + * @see #addTrustedPackages(Collection) + */ + public void addTrustedPackages(String... packagesToTrust) { + if (Objects.nonNull(packagesToTrust)) { + addTrustedPackages(Arrays.asList(packagesToTrust)); + } + } - public Set getTrustedPackages() { - return this.trustedPackages; - } + /** + * 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 packagesToTrust) { + if (packagesToTrust != null) { + for (String whiteList : packagesToTrust) { + if ("*".equals(whiteList)) { + this.trustedPackages.clear(); + break; + } + else { + this.trustedPackages.add(whiteList); + } + } + } + } - public ObjectMapper getObjectMapper() { - return objectMapper; - } + public Set getTrustedPackages() { + return this.trustedPackages; + } - 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... - NonTrustedHeaderType nth = (NonTrustedHeaderType) value; - if (trusted(nth.getUntrustedType())) { - try { - value = objectMapper.readValue(nth.getHeaderValue(), - ClassUtils.forName(nth.getUntrustedType(), null)); - } - catch (Exception e) { - log.error("Could not decode header: " + nth,e); - } - } - } - return value; - } + public ObjectMapper getObjectMapper() { + return objectMapper; + } - @Nullable - private Map decodeJsonTypes(Map source) { - if(source.containsKey(JSON_TYPES)){ - String value=source.get(JSON_TYPES); - try { - return objectMapper.readValue(value,new TypeReference>(){}); - } - catch (IOException e) { - log.error("Could not decode json types: " + value,e); - } - } - return null; - } + 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... + NonTrustedHeaderType nth = (NonTrustedHeaderType) value; + if (trusted(nth.getUntrustedType())) { + try { + value = objectMapper.readValue(nth.getHeaderValue(), + ClassUtils.forName(nth.getUntrustedType(), null)); + } + catch (Exception e) { + log.error("Could not decode header: " + nth, e); + } + } + } + return value; + } - protected boolean trusted(String requestedType) { - if (requestedType.equals(NonTrustedHeaderType.class.getName())) { - return true; - } - if (!this.trustedPackages.isEmpty()) { - int lastDot = requestedType.lastIndexOf('.'); - if (lastDot < 0) { - return false; - } - String packageName = requestedType.substring(0, lastDot); - for (String trustedPackage : this.trustedPackages) { - if (packageName.equals(trustedPackage) || packageName.startsWith(trustedPackage + ".")) { - return true; - } - } - return false; - } - return true; - } + @Nullable + private Map decodeJsonTypes(Map source) { + if (source.containsKey(JSON_TYPES)) { + String value = source.get(JSON_TYPES); + try { + return objectMapper.readValue(value, + new TypeReference>() { + }); + } + catch (IOException e) { + log.error("Could not decode json types: " + value, e); + } + } + return null; + } - /** - * Represents a header that could not be decoded due to an untrusted type. - */ - public static class NonTrustedHeaderType { + protected boolean trusted(String requestedType) { + if (requestedType.equals(NonTrustedHeaderType.class.getName())) { + return true; + } + if (!this.trustedPackages.isEmpty()) { + int lastDot = requestedType.lastIndexOf('.'); + if (lastDot < 0) { + return false; + } + String packageName = requestedType.substring(0, lastDot); + for (String trustedPackage : this.trustedPackages) { + if (packageName.equals(trustedPackage) + || packageName.startsWith(trustedPackage + ".")) { + return true; + } + } + return false; + } + return true; + } - private String headerValue; + /** + * Represents a header that could not be decoded due to an untrusted type. + */ + public static class NonTrustedHeaderType { - private String untrustedType; + private String headerValue; - public NonTrustedHeaderType() { - super(); - } + private String untrustedType; - NonTrustedHeaderType(String headerValue, String untrustedType) { - this.headerValue = headerValue; - this.untrustedType = untrustedType; - } + public NonTrustedHeaderType() { + super(); + } + NonTrustedHeaderType(String headerValue, String untrustedType) { + this.headerValue = headerValue; + this.untrustedType = untrustedType; + } - public void setHeaderValue(String headerValue) { - this.headerValue = headerValue; - } + public void setHeaderValue(String headerValue) { + this.headerValue = headerValue; + } - public String getHeaderValue() { - return this.headerValue; - } + public String getHeaderValue() { + return this.headerValue; + } - public void setUntrustedType(String untrustedType) { - this.untrustedType = untrustedType; - } + public void setUntrustedType(String untrustedType) { + this.untrustedType = untrustedType; + } - public String getUntrustedType() { - return this.untrustedType; - } + public String getUntrustedType() { + return this.untrustedType; + } - @Override - public String toString() { - return "NonTrustedHeaderType [headerValue=" + headerValue - + ", untrustedType=" + this.untrustedType + "]"; - } + @Override + public String toString() { + return "NonTrustedHeaderType [headerValue=" + headerValue + ", untrustedType=" + + this.untrustedType + "]"; + } - } + } } diff --git a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/RocketMQHeaderMapper.java b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/RocketMQHeaderMapper.java index d34af0eac..ee5994442 100644 --- a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/RocketMQHeaderMapper.java +++ b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/support/RocketMQHeaderMapper.java @@ -1,27 +1,28 @@ 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 -* -* @author caotc -* @date 2019-08-22 -* @since 2.1.1 -*/ + * header value mapper for RocketMQ + * + * @author caotc + * @date 2019-08-22 + * @since 2.1.1 + */ public interface RocketMQHeaderMapper { - /** - * Map from the given {@link MessageHeaders} to the specified target message. - * @param headers the abstracted MessageHeaders. - * @return the native target message. - */ - Map fromHeaders(MessageHeaders headers); - /** - * Map from the given target message to abstracted {@link MessageHeaders}. - * @param source the native target message. - * @return the target headers. - */ - MessageHeaders toHeaders(Map source); + /** + * Map from the given {@link MessageHeaders} to the specified target message. + * @param headers the abstracted MessageHeaders. + * @return the native target message. + */ + Map fromHeaders(MessageHeaders headers); + + /** + * Map from the given target message to abstracted {@link MessageHeaders}. + * @param source the native target message. + * @return the target headers. + */ + MessageHeaders toHeaders(Map source); }