Fix sonar findings (#3015)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Bosch.IO GmbH and others
|
||||
*
|
||||
* This program and the accompanying materials are made
|
||||
* available under the terms of the Eclipse Public License 2.0
|
||||
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.hawkbit.amqp;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
|
||||
|
||||
/**
|
||||
* Class that composes a meaningful error message and enhances it with properties from failed message
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public final class AmqpErrorMessageComposer {
|
||||
|
||||
/**
|
||||
* Constructs an error message based on failed message content
|
||||
*
|
||||
* @param throwable the throwable containing failed message content
|
||||
* @return meaningful error message
|
||||
*/
|
||||
public static String constructErrorMessage(final Throwable throwable) {
|
||||
final String mainErrorMsg = throwable.getCause().getMessage();
|
||||
if (throwable instanceof ListenerExecutionFailedException listenerExecutionFailedException) {
|
||||
Collection<Message> failedMessages = listenerExecutionFailedException.getFailedMessages();
|
||||
// since the intended message content is always on top of the collection, we only extract the first one
|
||||
final Message failedMessage = failedMessages.iterator().next();
|
||||
final byte[] amqpFailedMsgBody = failedMessage.getBody();
|
||||
final Map<String, Object> amqpFailedMsgHeaders = failedMessage.getMessageProperties().getHeaders();
|
||||
|
||||
final String amqpFailedMsgConcatenatedHeaders = amqpFailedMsgHeaders.keySet().stream()
|
||||
.map(key -> key + "=" + amqpFailedMsgHeaders.get(key))
|
||||
.collect(Collectors.joining(", ", "{", "}"));
|
||||
return mainErrorMsg + new String(amqpFailedMsgBody) + amqpFailedMsgConcatenatedHeaders;
|
||||
}
|
||||
return mainErrorMsg;
|
||||
}
|
||||
}
|
||||
@@ -182,6 +182,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
updateAttributesEvent.getTargetAddress());
|
||||
}
|
||||
|
||||
@SuppressWarnings("java:S4449") // false positive - setCorrelationId param is @Nullable - but in spring - can't annotate it
|
||||
protected void sendPingResponseToDmfReceiver(final Message ping, final String tenant, final String virtualHost) {
|
||||
final Message message = MessageBuilder
|
||||
.withBody(String.valueOf(System.currentTimeMillis()).getBytes())
|
||||
@@ -200,10 +201,8 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
return;
|
||||
}
|
||||
|
||||
final DmfActionRequest actionRequest = new DmfActionRequest(actionId);
|
||||
final Message message = getMessageConverter().toMessage(
|
||||
actionRequest,
|
||||
createConnectorMessagePropertiesEvent(tenant, controllerId, EventTopic.CANCEL_DOWNLOAD));
|
||||
new DmfActionRequest(actionId), createConnectorMessagePropertiesEvent(tenant, controllerId, EventTopic.CANCEL_DOWNLOAD));
|
||||
|
||||
amqpSenderService.sendMessage(message, address);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import org.springframework.amqp.AmqpRejectAndDontRequeueException;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
|
||||
import org.springframework.amqp.support.converter.DefaultJacksonJavaTypeMapper;
|
||||
import org.springframework.amqp.support.converter.MessageConversionException;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -50,16 +50,17 @@ public class BaseAmqpService {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T convertMessage(@NotNull final Message message, final Class<T> clazz) {
|
||||
checkMessageBody(message);
|
||||
message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, clazz.getName());
|
||||
message.getMessageProperties().getHeaders().put(DefaultJacksonJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, clazz.getName());
|
||||
return (T) rabbitTemplate.getMessageConverter().fromMessage(message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("java:S2589") // messageProperties.getContentType() could be null via setContentType
|
||||
protected static void checkContentTypeJson(final Message message) {
|
||||
final MessageProperties messageProperties = message.getMessageProperties();
|
||||
if (messageProperties.getContentType() != null && messageProperties.getContentType().contains("json")) {
|
||||
return;
|
||||
final String contentType = messageProperties.getContentType();
|
||||
if (contentType == null || !contentType.contains("json")) {
|
||||
throw new AmqpRejectAndDontRequeueException("Content-Type is not JSON compatible");
|
||||
}
|
||||
throw new AmqpRejectAndDontRequeueException("Content-Type is not JSON compatible");
|
||||
}
|
||||
|
||||
protected static boolean isMessageBodyEmpty(final Message message) {
|
||||
@@ -101,6 +102,6 @@ public class BaseAmqpService {
|
||||
* @param message the message to cleaned up
|
||||
*/
|
||||
protected void cleanMessageHeaderProperties(final Message message) {
|
||||
message.getMessageProperties().getHeaders().remove(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME);
|
||||
message.getMessageProperties().getHeaders().remove(DefaultJacksonJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME);
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class DmfApiConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@link RabbitTemplate} with automatic retry, published confirms and {@link Jackson2JsonMessageConverter}.
|
||||
* @return {@link RabbitTemplate} with automatic retry, published confirms and {@link JacksonJsonMessageConverter}.
|
||||
*/
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate(final JsonMapper jsonMapper) {
|
||||
|
||||
Reference in New Issue
Block a user