GH-3421: Resolve throws Exception; in the API

Fixes https://github.com/spring-projects/spring-integration/issues/3421

Remove `throws Exception;` from production code to honor
the rule `Generic exceptions should never be thrown` which is enabled on SonarQube

* Rework affected usages to `try..catch` with throwing respective runtime exception
or just logging
* Some other refactoring for the affected classes
This commit is contained in:
Artem Bilan
2022-10-14 16:47:00 -04:00
committed by Gary Russell
parent a08713fe87
commit cfeaecaae8
4 changed files with 72 additions and 65 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -39,8 +39,7 @@ import org.springframework.messaging.Message;
* @author Gary Russell
* @author Artem Bilan
*/
public abstract class AbstractMailMessageTransformer<T> implements Transformer,
BeanFactoryAware {
public abstract class AbstractMailMessageTransformer<T> implements Transformer, BeanFactoryAware {
private BeanFactory beanFactory;
@@ -48,7 +47,6 @@ public abstract class AbstractMailMessageTransformer<T> implements Transformer,
private boolean messageBuilderFactorySet;
@Override
public final void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
@@ -67,28 +65,19 @@ public abstract class AbstractMailMessageTransformer<T> implements Transformer,
@Override
public Message<?> transform(Message<?> message) {
Object payload = message.getPayload();
if (!(payload instanceof jakarta.mail.Message)) {
if (!(payload instanceof jakarta.mail.Message mailMessage)) {
throw new MessageTransformationException(message, getClass().getSimpleName()
+ " requires a jakarta.mail.Message payload");
}
jakarta.mail.Message mailMessage = (jakarta.mail.Message) payload;
AbstractIntegrationMessageBuilder<T> builder;
try {
builder = this.doTransform(mailMessage);
}
catch (Exception e) {
throw new MessageTransformationException(message, "failed to transform mail message", e);
}
builder = doTransform(mailMessage);
if (builder == null) {
throw new MessageTransformationException(message, "failed to transform mail message");
}
builder.copyHeaders(extractHeaderMapFromMailMessage(mailMessage));
return builder.build();
return builder.copyHeaders(extractHeaderMapFromMailMessage(mailMessage)).build();
}
protected abstract AbstractIntegrationMessageBuilder<T> doTransform(jakarta.mail.Message mailMessage)
throws Exception; // NOSONAR
protected abstract AbstractIntegrationMessageBuilder<T> doTransform(jakarta.mail.Message mailMessage);
private static Map<String, Object> extractHeaderMapFromMailMessage(jakarta.mail.Message mailMessage) {
return MailUtils.extractStandardHeaders(mailMessage);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -23,6 +23,7 @@ import jakarta.mail.Multipart;
import jakarta.mail.Part;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.transformer.MessageTransformationException;
import org.springframework.util.Assert;
/**
@@ -51,27 +52,34 @@ public class MailToStringTransformer extends AbstractMailMessageTransformer<Stri
}
@Override
protected AbstractIntegrationMessageBuilder<String> doTransform(jakarta.mail.Message mailMessage)
throws Exception { // NOSONAR
protected AbstractIntegrationMessageBuilder<String> doTransform(jakarta.mail.Message mailMessage) {
try {
String payload;
Object content = mailMessage.getContent();
if (content instanceof String value) {
payload = value;
}
else if (content instanceof Multipart multipart) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
multipart.writeTo(outputStream);
payload = outputStream.toString(this.charset);
}
else if (content instanceof Part part) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
part.writeTo(outputStream);
payload = outputStream.toString(this.charset);
}
else {
throw new IllegalArgumentException("failed to transform contentType ["
+ mailMessage.getContentType() + "] to String.");
}
Object content = mailMessage.getContent();
if (content instanceof String) {
return this.getMessageBuilderFactory().withPayload((String) content);
return getMessageBuilderFactory().withPayload(payload);
}
if (content instanceof Multipart) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
((Multipart) content).writeTo(outputStream);
return this.getMessageBuilderFactory().withPayload(
new String(outputStream.toByteArray(), this.charset));
catch (Exception ex) {
throw new MessageTransformationException("Cannot transform mail message", ex);
}
else if (content instanceof Part) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
((Part) content).writeTo(outputStream);
return this.getMessageBuilderFactory().withPayload(
new String(outputStream.toByteArray(), this.charset));
}
throw new IllegalArgumentException("failed to transform contentType ["
+ mailMessage.getContentType() + "] to String.");
}
}