Sonar Fixes

Critical smells for packages `o.s.i.i*`.

* Polishing

* Polishing

* Missed saving a file.

* Polishing - PR Comments

* More PR comments.
This commit is contained in:
Gary Russell
2018-12-07 14:59:27 -05:00
committed by Artem Bilan
parent 4760c54097
commit 563f526ddc
17 changed files with 95 additions and 39 deletions

View File

@@ -62,8 +62,8 @@ public class PayloadsArgumentResolver extends AbstractExpressionEvaluator
Collection<Message<?>> messages = (Collection<Message<?>>) payload;
if (!this.expressionCache.containsKey(parameter)) {
Payloads payloads = parameter.getParameterAnnotation(Payloads.class); // NOSONAR never null - supportsParameter()
String expression = payloads.value();
Payloads payloads = parameter.getParameterAnnotation(Payloads.class);
String expression = payloads.value(); // NOSONAR never null - supportsParameter()
if (StringUtils.hasText(expression)) {
this.expressionCache.put(parameter, EXPRESSION_PARSER.parseExpression("![payload." + expression + "]"));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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,16 +16,19 @@
package org.springframework.integration.mapping;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
/**
* Strategy interface for mapping from a {@link Message} to an Object.
*
* @author Mark Fisher
* @author Gary Russell
*/
@FunctionalInterface
public interface OutboundMessageMapper<T> {
@Nullable
T fromMessage(Message<?> message) throws Exception;
}

View File

@@ -31,6 +31,7 @@ import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.MessageTimeoutException;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.expression.ValueExpression;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.ip.IpHeaders;
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
@@ -62,6 +63,8 @@ import org.springframework.util.Assert;
public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler
implements TcpSender, TcpListener, Lifecycle {
private static final long DEFAULT_REMOTE_TIMEOUT = 10_000L;
private static final int DEFAULT_SECOND_CHANCE_DELAY = 2;
private final Map<String, AsyncReply> pendingReplies = new ConcurrentHashMap<>();
@@ -72,10 +75,9 @@ public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler
private boolean isSingleUse;
private Expression remoteTimeoutExpression = new LiteralExpression("10000");
private Expression remoteTimeoutExpression = new ValueExpression<>(DEFAULT_REMOTE_TIMEOUT);
private long requestTimeout = 10000;
private EvaluationContext evaluationContext = new StandardEvaluationContext();
private boolean evaluationContextSet;
@@ -148,8 +150,16 @@ public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler
}
}
connection = this.connectionFactory.getConnection();
AsyncReply reply = new AsyncReply(this.remoteTimeoutExpression.getValue(this.evaluationContext,
requestMessage, Long.class));
Long remoteTimeout = this.remoteTimeoutExpression.getValue(this.evaluationContext, requestMessage,
Long.class);
if (remoteTimeout == null) {
remoteTimeout = DEFAULT_REMOTE_TIMEOUT;
if (logger.isWarnEnabled()) {
logger.warn("remoteTimeoutExpression evaluated to null; falling back to default for message "
+ requestMessage);
}
}
AsyncReply reply = new AsyncReply(remoteTimeout);
connectionId = connection.getConnectionId();
this.pendingReplies.put(connectionId, reply);
if (logger.isDebugEnabled()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -314,6 +314,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
/**
* @return the listener
*/
@Nullable
public TcpListener getListener() {
return this.listener;
}
@@ -321,6 +322,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
/**
* @return the sender
*/
@Nullable
public TcpSender getSender() {
return this.sender;
}

View File

@@ -154,6 +154,7 @@ public abstract class AbstractServerConnectionFactory extends AbstractConnection
*
* @return the localAddress
*/
@Nullable
public String getLocalAddress() {
return this.localAddress;
}

View File

@@ -81,9 +81,6 @@ public class DefaultTcpNioSSLConnectionSupport extends AbstractTcpConnectionSupp
postProcessSSLEngine(sslEngine);
if (this.sslVerifyHost) {
SSLParameters sslParameters = sslEngine.getSSLParameters();
if (sslParameters == null) {
sslParameters = new SSLParameters();
}
// HTTPS works for any TCP connection.
// It checks SAN (Subject Alternative Name) as well as CN.
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");

View File

@@ -66,9 +66,6 @@ public class DefaultTcpSocketSupport implements TcpSocketSupport {
if (this.sslVerifyHost && socket instanceof SSLSocket) {
SSLSocket sslSocket = (SSLSocket) socket;
SSLParameters sslParameters = sslSocket.getSSLParameters();
if (sslParameters == null) {
sslParameters = new SSLParameters();
}
// HTTPS works for any TCP connection.
// It checks SAN (Subject Alternative Name) as well as CN.
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");

View File

@@ -363,7 +363,16 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
messageBuilder.setHeader(IpHeaders.ACTUAL_CONNECTION_ID,
message.getHeaders().get(IpHeaders.CONNECTION_ID));
}
return this.getListener().onMessage(messageBuilder.build());
TcpListener listener = getListener();
if (listener == null) {
if (this.logger.isDebugEnabled()) {
logger.debug("No listener for " + message);
}
return false;
}
else {
return listener.onMessage(messageBuilder.build());
}
}
else {
if (logger.isDebugEnabled()) {

View File

@@ -61,6 +61,7 @@ public interface TcpConnection extends Runnable {
* @return The payload.
* @throws Exception Any Exception.
*/
@Nullable
Object getPayload() throws Exception;
/**
@@ -104,6 +105,7 @@ public interface TcpConnection extends Runnable {
/**
* @return this connection's listener
*/
@Nullable
TcpListener getListener();
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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,8 @@ package org.springframework.integration.ip.tcp.connection;
import java.util.Arrays;
import org.springframework.lang.Nullable;
/**
* @author Gary Russell
* @since 2.0
@@ -27,6 +29,7 @@ public class TcpConnectionInterceptorFactoryChain {
private TcpConnectionInterceptorFactory[] interceptorFactories;
@Nullable
public TcpConnectionInterceptorFactory[] getInterceptorFactories() {
return this.interceptorFactories; //NOSONAR
}

View File

@@ -165,11 +165,15 @@ public abstract class TcpConnectionSupport implements TcpConnection {
close();
}
else {
TcpConnectionInterceptor outerInterceptor = (TcpConnectionInterceptor) listener;
while (outerInterceptor.getListener() instanceof TcpConnectionInterceptor) {
outerInterceptor = (TcpConnectionInterceptor) outerInterceptor.getListener();
TcpConnectionInterceptor outerListener = (TcpConnectionInterceptor) listener;
while (outerListener.getListener() instanceof TcpConnectionInterceptor) {
TcpConnectionInterceptor nextListener = (TcpConnectionInterceptor) outerListener.getListener();
if (nextListener == null) {
break;
}
outerListener = nextListener;
}
outerInterceptor.close();
outerListener.close();
if (isException) {
// ensure physical close in case the interceptor did not close
this.close();
@@ -235,7 +239,7 @@ public abstract class TcpConnectionSupport implements TcpConnection {
* Set the listener that will receive incoming Messages.
* @param listener The listener.
*/
public void registerListener(TcpListener listener) {
public void registerListener(@Nullable TcpListener listener) {
this.listener = listener;
this.listenerRegisteredLatch.countDown();
}
@@ -257,7 +261,7 @@ public abstract class TcpConnectionSupport implements TcpConnection {
* to.
* @param sender the sender.
*/
public void registerSender(TcpSender sender) {
public void registerSender(@Nullable TcpSender sender) {
this.sender = sender;
if (sender != null) {
sender.addNewConnection(this);
@@ -342,11 +346,12 @@ public abstract class TcpConnectionSupport implements TcpConnection {
}
protected final void sendExceptionToListener(Exception e) {
if (!this.exceptionSent.getAndSet(true) && this.getListener() != null) {
TcpListener listenerForException = getListener();
if (!this.exceptionSent.getAndSet(true) && listenerForException != null) {
Map<String, Object> headers = Collections.singletonMap(IpHeaders.CONNECTION_ID,
(Object) this.getConnectionId());
ErrorMessage errorMessage = new ErrorMessage(e, headers);
this.getListener().onMessage(errorMessage);
listenerForException.onMessage(errorMessage);
}
}

View File

@@ -35,6 +35,7 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.scheduling.SchedulingAwareRunnable;
import org.springframework.util.Assert;
/**
* A TcpConnection that uses and underlying {@link Socket}.
@@ -103,10 +104,11 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
this.socketOutputStream = new BufferedOutputStream(this.socket.getOutputStream(),
writeBufferSize > 0 ? writeBufferSize : 8192);
}
Object object = this.getMapper().fromMessage(message);
Object object = getMapper().fromMessage(message);
Assert.state(object != null, "Mapper mapped the message to 'null'.");
this.lastSend = System.currentTimeMillis();
try {
((Serializer<Object>) this.getSerializer()).serialize(object, this.socketOutputStream);
((Serializer<Object>) getSerializer()).serialize(object, this.socketOutputStream);
this.socketOutputStream.flush();
}
catch (Exception e) {

View File

@@ -152,6 +152,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
writeBufferSize > 0 ? writeBufferSize : 8192);
}
Object object = this.getMapper().fromMessage(message);
Assert.state(object != null, "Mapper mapped the message to 'null'.");
this.lastSend = System.currentTimeMillis();
try {
((Serializer<Object>) this.getSerializer()).serialize(object, this.bufferedOutputStream);

View File

@@ -163,10 +163,13 @@ public class DatagramPacketMessageMapper implements InboundMessageMapper<Datagra
buffer.put((byte) '=');
buffer.put(this.ackAddress.getBytes(this.charset));
buffer.put((byte) ';');
buffer.put(MessageHeaders.ID.getBytes(this.charset));
buffer.put((byte) '=');
buffer.put(message.getHeaders().getId().toString().getBytes(this.charset));
buffer.put((byte) ';');
UUID id = message.getHeaders().getId();
if (id != null) {
buffer.put(MessageHeaders.ID.getBytes(this.charset));
buffer.put((byte) '=');
buffer.put(id.toString().getBytes(this.charset));
buffer.put((byte) ';');
}
int headersLength = buffer.position() - 4;
buffer.put(bytes);
if (this.lengthCheck) {
@@ -199,6 +202,7 @@ public class DatagramPacketMessageMapper implements InboundMessageMapper<Datagra
}
@Override
@Nullable
public Message<byte[]> toMessage(DatagramPacket object) throws Exception {
return toMessage(object, null);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2001-2016 the original author or authors.
* Copyright 2001-2018 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.
@@ -129,11 +129,11 @@ public class MulticastSendingMessageHandler extends UnicastSendingMessageHandler
if (getTheSocket() == null) {
createSocket();
}
return getTheSocket();
return super.getSocket();
}
private void createSocket() throws IOException {
if (this.getTheSocket() == null) {
if (getTheSocket() == null) {
MulticastSocket socket;
if (this.isAcknowledge()) {
int ackPort = this.getAckPort();

View File

@@ -31,6 +31,7 @@ import java.util.regex.Pattern;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.ip.AbstractInternetProtocolReceivingChannelAdapter;
import org.springframework.integration.ip.IpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
@@ -145,8 +146,12 @@ public class UnicastReceivingChannelAdapter extends AbstractInternetProtocolRece
protected void sendAck(Message<byte[]> message) {
MessageHeaders headers = message.getHeaders();
Object id = headers.get(IpHeaders.ACK_ID);
if (id == null) {
logger.error("No " + IpHeaders.ACK_ID + " header; cannot send ack");
return;
}
byte[] ack = id.toString().getBytes();
String ackAddress = ((String) headers.get(IpHeaders.ACK_ADDRESS)).trim();
String ackAddress = (headers.get(IpHeaders.ACK_ADDRESS, String.class)).trim(); // NOSONAR caller checks header
Matcher mat = addressPattern.matcher(ackAddress);
if (!mat.matches()) {
throw new MessagingException(message,
@@ -227,6 +232,7 @@ public class UnicastReceivingChannelAdapter extends AbstractInternetProtocolRece
this.socket = socket;
}
@Nullable
protected DatagramSocket getTheSocket() {
return this.socket;
}

View File

@@ -27,6 +27,7 @@ import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
@@ -37,6 +38,7 @@ import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.ip.AbstractInternetProtocolSendingMessageHandler;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageDeliveryException;
import org.springframework.messaging.MessageHandlingException;
@@ -258,7 +260,11 @@ public class UnicastSendingMessageHandler extends
startAckThread();
}
CountDownLatch countdownLatch = null;
String messageId = message.getHeaders().getId().toString();
UUID id = message.getHeaders().getId();
if (id == null) {
id = UUID.randomUUID();
}
String messageId = id.toString();
try {
boolean waitForAck = this.waitForAck;
if (waitForAck) {
@@ -345,10 +351,17 @@ public class UnicastSendingMessageHandler extends
destinationAddress = getDestinationAddress();
}
DatagramPacket packet = this.mapper.fromMessage(message);
packet.setSocketAddress(destinationAddress);
socket.send(packet);
if (logger.isDebugEnabled()) {
logger.debug("Sent packet for message " + message + " to " + packet.getSocketAddress());
if (packet != null) {
packet.setSocketAddress(destinationAddress);
socket.send(packet);
if (logger.isDebugEnabled()) {
logger.debug("Sent packet for message " + message + " to " + packet.getSocketAddress());
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Mapper created no packet for message " + message);
}
}
}
@@ -356,6 +369,7 @@ public class UnicastSendingMessageHandler extends
this.socket = socket;
}
@Nullable
protected DatagramSocket getTheSocket() {
return this.socket;
}