Avoid throws Exception where possible - Phase II
Final phase III to follow. * Polishing * Polishing
This commit is contained in:
committed by
Artem Bilan
parent
3d87ac6463
commit
b138ab80f8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
* Copyright 2013-2019 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
@@ -36,7 +37,7 @@ import com.jayway.jsonpath.Predicate;
|
||||
*/
|
||||
public final class JsonPathUtils {
|
||||
|
||||
public static <T> T evaluate(Object json, String jsonPath, Predicate... predicates) throws Exception {
|
||||
public static <T> T evaluate(Object json, String jsonPath, Predicate... predicates) throws IOException {
|
||||
if (json instanceof String) {
|
||||
return JsonPath.read((String) json, jsonPath, predicates);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -35,10 +35,9 @@ public interface InboundMessageMapper<T> {
|
||||
* Convert a provided object to the {@link Message}.
|
||||
* @param object the object for message payload or some other conversion logic
|
||||
* @return the message as a result of mapping
|
||||
* @throws Exception the exception thrown by the underlying mapper implementation
|
||||
*/
|
||||
@Nullable
|
||||
default Message<?> toMessage(T object) throws Exception { // NOSONAR - TODO remove Exception in 5.2
|
||||
default Message<?> toMessage(T object) {
|
||||
return toMessage(object, null);
|
||||
}
|
||||
|
||||
@@ -48,10 +47,9 @@ public interface InboundMessageMapper<T> {
|
||||
* @param object the object for message payload or some other conversion logic
|
||||
* @param headers additional headers for building message. Can be null
|
||||
* @return the message as a result of mapping
|
||||
* @throws Exception the exception thrown by the underlying mapper implementation
|
||||
* @since 5.0
|
||||
*/
|
||||
@Nullable
|
||||
Message<?> toMessage(T object, @Nullable Map<String, Object> headers) throws Exception; // NOSONAR
|
||||
Message<?> toMessage(T object, @Nullable Map<String, Object> headers);
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,6 @@ import org.springframework.messaging.Message;
|
||||
public interface OutboundMessageMapper<T> {
|
||||
|
||||
@Nullable
|
||||
T fromMessage(Message<?> message) throws Exception; // NOSONAR
|
||||
T fromMessage(Message<?> message);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -91,7 +91,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
File baseDir = new File(this.baseDirectory);
|
||||
baseDir.mkdirs();
|
||||
this.file = new File(baseDir, this.fileName);
|
||||
@@ -195,7 +195,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
public void close() {
|
||||
flush();
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
public void destroy() {
|
||||
flush();
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e2) {
|
||||
catch (@SuppressWarnings("unused") Exception e2) {
|
||||
// non fatal
|
||||
this.logger.warn("Failed to close InputStream for: " + this.file.getAbsolutePath());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 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.
|
||||
@@ -90,7 +90,7 @@ public class ExpressionEvaluatingRoutingSlipRouteStrategy
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
if (this.evaluationContext == null) {
|
||||
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -90,12 +90,12 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
Assert.state(this.messageGroupStore != null, "A MessageGroupStore must be provided");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
public void destroy() {
|
||||
if (this.expireOnDestroy) {
|
||||
if (this.isRunning()) {
|
||||
logger.info("Expiring all messages from message group store: " + this.messageGroupStore);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -134,7 +134,7 @@ public class SimpleMessageConverter implements MessageConverter, BeanFactoryAwar
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> toMessage(Object object, @Nullable Map<String, Object> headers) throws Exception {
|
||||
public Message<?> toMessage(Object object, @Nullable Map<String, Object> headers) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -157,7 +157,7 @@ public class SimpleMessageConverter implements MessageConverter, BeanFactoryAwar
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object fromMessage(Message<?> message) throws Exception {
|
||||
public Object fromMessage(Message<?> message) {
|
||||
return (message != null) ? message.getPayload() : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -69,15 +69,15 @@ abstract class AbstractJacksonJsonMessageParser<P> implements JsonInboundMessage
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> doInParser(JsonInboundMessageMapper messageMapper, String jsonMessage,
|
||||
@Nullable Map<String, Object> headers) throws Exception {
|
||||
public Message<?> doInParser(JsonInboundMessageMapper messageMapperToUse, String jsonMessage,
|
||||
@Nullable Map<String, Object> headers) {
|
||||
|
||||
if (this.messageMapper == null) {
|
||||
this.messageMapper = messageMapper;
|
||||
this.messageMapper = messageMapperToUse;
|
||||
}
|
||||
P parser = this.createJsonParser(jsonMessage);
|
||||
|
||||
if (messageMapper.isMapToPayload()) {
|
||||
if (messageMapperToUse.isMapToPayload()) {
|
||||
Object payload = readPayload(parser, jsonMessage);
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(payload)
|
||||
@@ -89,7 +89,7 @@ abstract class AbstractJacksonJsonMessageParser<P> implements JsonInboundMessage
|
||||
}
|
||||
}
|
||||
|
||||
protected Object readPayload(P parser, String jsonMessage) throws Exception {
|
||||
protected Object readPayload(P parser, String jsonMessage) {
|
||||
try {
|
||||
return this.objectMapper.fromJson(parser, this.messageMapper.getPayloadType());
|
||||
}
|
||||
@@ -99,7 +99,7 @@ abstract class AbstractJacksonJsonMessageParser<P> implements JsonInboundMessage
|
||||
}
|
||||
}
|
||||
|
||||
protected Object readHeader(P parser, String headerName, String jsonMessage) throws Exception {
|
||||
protected Object readHeader(P parser, String headerName, String jsonMessage) {
|
||||
Class<?> headerType = this.messageMapper.getHeaderTypes().getOrDefault(headerName, Object.class);
|
||||
try {
|
||||
return this.objectMapper.fromJson(parser, (Type) headerType);
|
||||
@@ -111,8 +111,8 @@ abstract class AbstractJacksonJsonMessageParser<P> implements JsonInboundMessage
|
||||
}
|
||||
|
||||
protected abstract Message<?> parseWithHeaders(P parser, String jsonMessage,
|
||||
@Nullable Map<String, Object> headers) throws Exception;
|
||||
@Nullable Map<String, Object> headers);
|
||||
|
||||
protected abstract P createJsonParser(String jsonMessage) throws Exception;
|
||||
protected abstract P createJsonParser(String jsonMessage);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2019 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.support.json;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
@@ -57,17 +58,17 @@ public abstract class AbstractJacksonJsonObjectMapper<N, P, J> extends JsonObjec
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object json, Class<T> valueType) throws Exception {
|
||||
return this.fromJson(json, this.constructType(valueType));
|
||||
public <T> T fromJson(Object json, Class<T> valueType) throws IOException {
|
||||
return fromJson(json, this.constructType(valueType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
|
||||
J javaType = this.extractJavaType(javaTypes);
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException {
|
||||
J javaType = extractJavaType(javaTypes);
|
||||
return this.fromJson(json, javaType);
|
||||
}
|
||||
|
||||
protected J createJavaType(Map<String, Object> javaTypes, String javaTypeKey) throws Exception {
|
||||
protected J createJavaType(Map<String, Object> javaTypes, String javaTypeKey) {
|
||||
Object classValue = javaTypes.get(javaTypeKey);
|
||||
if (classValue == null) {
|
||||
throw new IllegalArgumentException("Could not resolve '" + javaTypeKey + "' in 'javaTypes'.");
|
||||
@@ -78,16 +79,21 @@ public abstract class AbstractJacksonJsonObjectMapper<N, P, J> extends JsonObjec
|
||||
aClass = (Class<?>) classValue;
|
||||
}
|
||||
else {
|
||||
aClass = ClassUtils.forName(classValue.toString(), this.classLoader);
|
||||
try {
|
||||
aClass = ClassUtils.forName(classValue.toString(), this.classLoader);
|
||||
}
|
||||
catch (ClassNotFoundException | LinkageError e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
return this.constructType(aClass);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract <T> T fromJson(Object json, J type) throws Exception;
|
||||
protected abstract <T> T fromJson(Object json, J type) throws IOException;
|
||||
|
||||
protected abstract J extractJavaType(Map<String, Object> javaTypes) throws Exception;
|
||||
protected abstract J extractJavaType(Map<String, Object> javaTypes);
|
||||
|
||||
protected abstract J constructType(Type type);
|
||||
|
||||
|
||||
@@ -66,8 +66,8 @@ public abstract class AbstractJsonInboundMessageMapper<P> implements InboundMess
|
||||
this.mapToPayload = mapToPayload;
|
||||
}
|
||||
|
||||
protected abstract Object readPayload(P parser, String jsonMessage) throws Exception;
|
||||
protected abstract Object readPayload(P parser, String jsonMessage);
|
||||
|
||||
protected abstract Map<String, Object> readHeaders(P parser, String jsonMessage) throws Exception;
|
||||
protected abstract Map<String, Object> readHeaders(P parser, String jsonMessage);
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.support.json;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
@@ -80,7 +81,7 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object value) throws Exception {
|
||||
public String toJson(Object value) {
|
||||
return this.objectMapper.writeValueAsString(value);
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> toJsonNode(final Object value) throws Exception {
|
||||
public Map<String, Object> toJsonNode(final Object value) throws IOException {
|
||||
PipedReader in = new PipedReader();
|
||||
final PipedWriter out = new PipedWriter(in);
|
||||
Executors.newSingleThreadExecutor().execute(() -> toJson(value, out));
|
||||
@@ -99,7 +100,7 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object json, Class<T> type) throws Exception {
|
||||
public <T> T fromJson(Object json, Class<T> type) {
|
||||
if (json instanceof String) {
|
||||
return this.objectMapper.readValue((String) json, type);
|
||||
}
|
||||
@@ -126,14 +127,14 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException {
|
||||
JsonParserAndMapper parser = this.objectMapper.parser();
|
||||
|
||||
Class<?> classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID);
|
||||
Class<?> classType = createJavaType(javaTypes, JsonHeaders.TYPE_ID);
|
||||
|
||||
Class<?> contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
|
||||
Class<?> contentClassType = createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
|
||||
|
||||
Class<?> keyClassType = this.createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
|
||||
Class<?> keyClassType = createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
|
||||
|
||||
if (keyClassType != null) {
|
||||
logger.warn("Boon doesn't support the Map 'key' conversion. Will be returned raw Map<String, Object>");
|
||||
@@ -190,13 +191,18 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
return (T) fromJson(json, classType);
|
||||
}
|
||||
|
||||
protected Class<?> createJavaType(Map<String, Object> javaTypes, String javaTypeKey) throws Exception {
|
||||
protected Class<?> createJavaType(Map<String, Object> javaTypes, String javaTypeKey) {
|
||||
Object classValue = javaTypes.get(javaTypeKey);
|
||||
if (classValue instanceof Class<?>) {
|
||||
return (Class<?>) classValue;
|
||||
}
|
||||
else if (classValue != null) {
|
||||
return ClassUtils.forName(classValue.toString(), this.classLoader);
|
||||
try {
|
||||
return ClassUtils.forName(classValue.toString(), this.classLoader);
|
||||
}
|
||||
catch (ClassNotFoundException | LinkageError e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
@@ -204,7 +210,7 @@ public class BoonJsonObjectMapper extends JsonObjectMapperAdapter<Map<String, Ob
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object parser, Type valueType) throws Exception {
|
||||
public <T> T fromJson(Object parser, Type valueType) {
|
||||
throw new UnsupportedOperationException("Boon doesn't support JSON reader parser abstraction");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2018 the original author or authors.
|
||||
* Copyright 2017-2019 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.support.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -36,6 +37,7 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
@@ -154,9 +156,8 @@ public class EmbeddedJsonHeadersMessageMapper implements BytesMessageMapper {
|
||||
return Arrays.asList(this.headerPatterns);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public byte[] fromMessage(Message<?> message) throws Exception {
|
||||
public byte[] fromMessage(Message<?> message) {
|
||||
Map<String, Object> headersToEncode =
|
||||
this.allHeaders
|
||||
? message.getHeaders()
|
||||
@@ -179,7 +180,12 @@ public class EmbeddedJsonHeadersMessageMapper implements BytesMessageMapper {
|
||||
messageToEncode = new MutableMessage<>(message.getPayload(), headersToEncode);
|
||||
}
|
||||
|
||||
return this.objectMapper.writeValueAsBytes(messageToEncode);
|
||||
try {
|
||||
return this.objectMapper.writeValueAsBytes(messageToEncode);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,14 +203,19 @@ public class EmbeddedJsonHeadersMessageMapper implements BytesMessageMapper {
|
||||
: PatternMatchUtils.smartMatchIgnoreCase(header, this.headerPatterns));
|
||||
}
|
||||
|
||||
private byte[] fromBytesPayload(byte[] payload, Map<String, Object> headersToEncode) throws Exception {
|
||||
byte[] headers = this.objectMapper.writeValueAsBytes(headersToEncode);
|
||||
ByteBuffer buffer = ByteBuffer.wrap(new byte[8 + headers.length + payload.length]);
|
||||
buffer.putInt(headers.length);
|
||||
buffer.put(headers);
|
||||
buffer.putInt(payload.length);
|
||||
buffer.put(payload);
|
||||
return buffer.array();
|
||||
private byte[] fromBytesPayload(byte[] payload, Map<String, Object> headersToEncode) {
|
||||
try {
|
||||
byte[] headers = this.objectMapper.writeValueAsBytes(headersToEncode);
|
||||
ByteBuffer buffer = ByteBuffer.wrap(new byte[8 + headers.length + payload.length]);
|
||||
buffer.putInt(headers.length);
|
||||
buffer.put(headers);
|
||||
buffer.putInt(payload.length);
|
||||
buffer.put(payload);
|
||||
return buffer.array();
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -213,7 +224,7 @@ public class EmbeddedJsonHeadersMessageMapper implements BytesMessageMapper {
|
||||
try {
|
||||
message = decodeNativeFormat(bytes, headers);
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (@SuppressWarnings("unused") Exception e) {
|
||||
// empty
|
||||
}
|
||||
if (message == null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.integration.support.json;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -49,42 +51,52 @@ public class Jackson2JsonMessageParser extends AbstractJacksonJsonMessageParser<
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonParser createJsonParser(String jsonMessage) throws Exception {
|
||||
return new JsonFactory().createParser(jsonMessage);
|
||||
protected JsonParser createJsonParser(String jsonMessage) {
|
||||
try {
|
||||
return new JsonFactory().createParser(jsonMessage);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> parseWithHeaders(JsonParser parser, String jsonMessage,
|
||||
@Nullable Map<String, Object> headersToAdd) throws Exception {
|
||||
@Nullable Map<String, Object> headersToAdd) {
|
||||
|
||||
String error = AbstractJsonInboundMessageMapper.MESSAGE_FORMAT_ERROR + jsonMessage;
|
||||
Assert.isTrue(JsonToken.START_OBJECT == parser.nextToken(), error);
|
||||
Map<String, Object> headers = null;
|
||||
Object payload = null;
|
||||
while (JsonToken.END_OBJECT != parser.nextToken()) {
|
||||
Assert.isTrue(JsonToken.FIELD_NAME == parser.getCurrentToken(), error);
|
||||
boolean isHeadersToken = "headers".equals(parser.getCurrentName());
|
||||
boolean isPayloadToken = "payload".equals(parser.getCurrentName());
|
||||
Assert.isTrue(isHeadersToken || isPayloadToken, error);
|
||||
if (isHeadersToken) {
|
||||
Assert.isTrue(parser.nextToken() == JsonToken.START_OBJECT, error);
|
||||
headers = readHeaders(parser, jsonMessage);
|
||||
}
|
||||
else if (isPayloadToken) {
|
||||
parser.nextToken();
|
||||
payload = this.readPayload(parser, jsonMessage);
|
||||
try {
|
||||
String error = AbstractJsonInboundMessageMapper.MESSAGE_FORMAT_ERROR + jsonMessage;
|
||||
Assert.isTrue(JsonToken.START_OBJECT == parser.nextToken(), error);
|
||||
Map<String, Object> headers = null;
|
||||
Object payload = null;
|
||||
while (JsonToken.END_OBJECT != parser.nextToken()) {
|
||||
Assert.isTrue(JsonToken.FIELD_NAME == parser.getCurrentToken(), error);
|
||||
boolean isHeadersToken = "headers".equals(parser.getCurrentName());
|
||||
boolean isPayloadToken = "payload".equals(parser.getCurrentName());
|
||||
Assert.isTrue(isHeadersToken || isPayloadToken, error);
|
||||
if (isHeadersToken) {
|
||||
Assert.isTrue(parser.nextToken() == JsonToken.START_OBJECT, error);
|
||||
headers = readHeaders(parser, jsonMessage);
|
||||
}
|
||||
else if (isPayloadToken) {
|
||||
parser.nextToken();
|
||||
payload = this.readPayload(parser, jsonMessage);
|
||||
}
|
||||
}
|
||||
Assert.notNull(headers, error);
|
||||
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(payload)
|
||||
.copyHeaders(headers)
|
||||
.copyHeadersIfAbsent(headersToAdd)
|
||||
.build();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
Assert.notNull(headers, error);
|
||||
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(payload)
|
||||
.copyHeaders(headers)
|
||||
.copyHeadersIfAbsent(headersToAdd)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Map<String, Object> readHeaders(JsonParser parser, String jsonMessage) throws Exception {
|
||||
private Map<String, Object> readHeaders(JsonParser parser, String jsonMessage) throws IOException {
|
||||
Map<String, Object> headers = new LinkedHashMap<>();
|
||||
while (JsonToken.END_OBJECT != parser.nextToken()) {
|
||||
String headerName = parser.getCurrentName();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
* Copyright 2013-2019 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.support.json;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -82,17 +84,17 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object value) throws Exception {
|
||||
public String toJson(Object value) throws JsonProcessingException {
|
||||
return this.objectMapper.writeValueAsString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toJson(Object value, Writer writer) throws Exception {
|
||||
public void toJson(Object value, Writer writer) throws IOException {
|
||||
this.objectMapper.writeValue(writer, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode toJsonNode(Object json) throws Exception {
|
||||
public JsonNode toJsonNode(Object json) throws IOException {
|
||||
try {
|
||||
if (json instanceof String) {
|
||||
return this.objectMapper.readTree((String) json);
|
||||
@@ -124,7 +126,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T> T fromJson(Object json, JavaType type) throws Exception {
|
||||
protected <T> T fromJson(Object json, JavaType type) throws IOException {
|
||||
if (json instanceof String) {
|
||||
return this.objectMapper.readValue((String) json, type);
|
||||
}
|
||||
@@ -150,13 +152,13 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(JsonParser parser, Type valueType) throws Exception {
|
||||
public <T> T fromJson(JsonParser parser, Type valueType) throws IOException {
|
||||
return this.objectMapper.readValue(parser, constructType(valueType));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
protected JavaType extractJavaType(Map<String, Object> javaTypes) throws Exception {
|
||||
protected JavaType extractJavaType(Map<String, Object> javaTypes) {
|
||||
JavaType classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID);
|
||||
if (!classType.isContainerType() || classType.isArrayType()) {
|
||||
return classType;
|
||||
@@ -169,7 +171,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
contentClassType);
|
||||
}
|
||||
|
||||
JavaType keyClassType = this.createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
|
||||
JavaType keyClassType = createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
|
||||
return this.objectMapper.getTypeFactory()
|
||||
.constructMapType((Class<? extends Map<?, ?>>) classType.getRawClass(), keyClassType, contentClassType);
|
||||
}
|
||||
@@ -186,7 +188,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk7.Jdk7Module", getClassLoader());
|
||||
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk7Module));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
|
||||
// jackson-datatype-jdk7 not available
|
||||
}
|
||||
|
||||
@@ -195,7 +197,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", getClassLoader());
|
||||
this.objectMapper.registerModule(BeanUtils.instantiateClass(jdk8Module));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
|
||||
// jackson-datatype-jdk8 not available
|
||||
}
|
||||
|
||||
@@ -204,7 +206,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", getClassLoader());
|
||||
this.objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
|
||||
// jackson-datatype-jsr310 not available
|
||||
}
|
||||
|
||||
@@ -215,7 +217,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.joda.JodaModule", getClassLoader());
|
||||
this.objectMapper.registerModule(BeanUtils.instantiateClass(jodaModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
|
||||
// jackson-datatype-joda not available
|
||||
}
|
||||
}
|
||||
@@ -227,7 +229,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
|
||||
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", getClassLoader());
|
||||
this.objectMapper.registerModule(BeanUtils.instantiateClass(kotlinModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
catch (@SuppressWarnings("unused") ClassNotFoundException ex) {
|
||||
//jackson-module-kotlin not available
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -65,26 +65,26 @@ public class JsonInboundMessageMapper extends AbstractJsonInboundMessageMapper<J
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> toMessage(String jsonMessage, @Nullable Map<String, Object> headers) throws Exception {
|
||||
public Message<?> toMessage(String jsonMessage, @Nullable Map<String, Object> headers) {
|
||||
return this.messageParser.doInParser(this, jsonMessage, headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> readHeaders(JsonMessageParser<?> parser, String jsonMessage) throws Exception {
|
||||
protected Map<String, Object> readHeaders(JsonMessageParser<?> parser, String jsonMessage) {
|
||||
//No-op
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object readPayload(JsonMessageParser<?> parser, String jsonMessage) throws Exception {
|
||||
protected Object readPayload(JsonMessageParser<?> parser, String jsonMessage) {
|
||||
//No-op
|
||||
return null;
|
||||
}
|
||||
|
||||
public interface JsonMessageParser<P> {
|
||||
public interface JsonMessageParser<P> { // NOSONAR unused P
|
||||
|
||||
Message<?> doInParser(JsonInboundMessageMapper messageMapper, String jsonMessage,
|
||||
@Nullable Map<String, Object> headers) throws Exception;
|
||||
@Nullable Map<String, Object> headers);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2019 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.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
@@ -32,17 +33,17 @@ import java.util.Map;
|
||||
*/
|
||||
public interface JsonObjectMapper<N, P> {
|
||||
|
||||
String toJson(Object value) throws Exception;
|
||||
String toJson(Object value) throws IOException;
|
||||
|
||||
void toJson(Object value, Writer writer) throws Exception;
|
||||
void toJson(Object value, Writer writer) throws IOException;
|
||||
|
||||
N toJsonNode(Object value) throws Exception;
|
||||
N toJsonNode(Object value) throws IOException;
|
||||
|
||||
<T> T fromJson(Object json, Class<T> valueType) throws Exception;
|
||||
<T> T fromJson(Object json, Class<T> valueType) throws IOException;
|
||||
|
||||
<T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception;
|
||||
<T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException;
|
||||
|
||||
<T> T fromJson(P parser, Type valueType) throws Exception;
|
||||
<T> T fromJson(P parser, Type valueType) throws IOException;
|
||||
|
||||
void populateJavaTypes(Map<String, Object> map, Object object);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
* Copyright 2013-2019 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.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
@@ -34,31 +35,31 @@ import org.springframework.integration.mapping.support.JsonHeaders;
|
||||
public abstract class JsonObjectMapperAdapter<N, P> implements JsonObjectMapper<N, P> {
|
||||
|
||||
@Override
|
||||
public String toJson(Object value) throws Exception {
|
||||
public String toJson(Object value) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toJson(Object value, Writer writer) throws Exception {
|
||||
public void toJson(Object value, Writer writer) throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public N toJsonNode(Object value) throws Exception {
|
||||
public N toJsonNode(Object value) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object json, Class<T> valueType) throws Exception {
|
||||
public <T> T fromJson(Object json, Class<T> valueType) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(P parser, Type valueType) throws Exception {
|
||||
public <T> T fromJson(P parser, Type valueType) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
|
||||
public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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,9 @@
|
||||
|
||||
package org.springframework.integration.support.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
import org.springframework.integration.mapping.OutboundMessageMapper;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -53,8 +56,13 @@ public class JsonOutboundMessageMapper implements OutboundMessageMapper<String>
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fromMessage(Message<?> message) throws Exception {
|
||||
return this.jsonObjectMapper.toJson(this.shouldExtractPayload ? message.getPayload() : message);
|
||||
public String fromMessage(Message<?> message) {
|
||||
try {
|
||||
return this.jsonObjectMapper.toJson(this.shouldExtractPayload ? message.getPayload() : message);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user