Make Mail supplier as auto-config

* Fix all their Checkstyle violations
* Fix Checkstyle violations in the `spring-kafka-supplier`, too
This commit is contained in:
Artem Bilan
2024-01-09 14:09:01 -05:00
parent 8ef105b9c5
commit a0e6d7c7d4
12 changed files with 76 additions and 199 deletions

View File

@@ -126,7 +126,7 @@ public class KafkaSupplierConfiguration {
StandardEvaluationContext evaluationContext = IntegrationContextUtils.getEvaluationContext(beanFactory);
return consumerRecord -> Boolean.TRUE.equals(
return (consumerRecord) -> Boolean.TRUE.equals(
kafkaSupplierProperties.getRecordFilter().getValue(evaluationContext, consumerRecord, Boolean.class));
}

View File

@@ -0,0 +1,4 @@
/**
* The Apache Kafka supplier auto-configuration support.
*/
package org.springframework.cloud.fn.supplier.kafka;

View File

@@ -9,7 +9,7 @@ Users have to subscribe to this `Flux` and receive the data.
## Beans for injection
You can import the `MailSupplierConfiguration` in the application and then inject the following bean.
The `MailSupplierConfiguration` auto-configuration provides the following bean:
`mailSupplier`

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 the original author or authors.
* Copyright 2020-2024 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.
@@ -25,11 +25,11 @@ import jakarta.mail.URLName;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.fn.common.config.ComponentCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.MessageProducerSpec;
@@ -38,27 +38,28 @@ import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.endpoint.ReactiveMessageSourceProducer;
import org.springframework.integration.mail.MailHeaders;
import org.springframework.integration.mail.dsl.ImapIdleChannelAdapterSpec;
import org.springframework.integration.mail.dsl.ImapMailInboundChannelAdapterSpec;
import org.springframework.integration.mail.dsl.Mail;
import org.springframework.integration.mail.dsl.MailInboundChannelAdapterSpec;
import org.springframework.integration.mail.dsl.Pop3MailInboundChannelAdapterSpec;
import org.springframework.integration.transformer.support.AbstractHeaderValueMessageProcessor;
import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
/**
* Mail supplier components.
* Mail supplier auto-configuration.
*
* @author Amol
* @author Artem Bilan
* @author Chris Schaefer
* @author Soby Chacko
* @author Corneil du Plessis
* @author Soby Chacko
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MailSupplierProperties.class)
@AutoConfiguration
public class MailSupplierConfiguration {
final private MailSupplierProperties properties;
private final MailSupplierProperties properties;
public MailSupplierConfiguration(MailSupplierProperties properties) {
this.properties = properties;
@@ -66,10 +67,9 @@ public class MailSupplierConfiguration {
@Bean
public Publisher<Message<Object>> mailInboundFlow(MessageProducerSupport messageProducer) {
return IntegrationFlow.from(messageProducer)
.transform(Mail.toStringTransformer(this.properties.getCharset()))
.enrichHeaders(h -> h.defaultOverwrite(true)
.enrichHeaders((h) -> h.defaultOverwrite(true)
.header(MailHeaders.TO, arrayToListProcessor(MailHeaders.TO))
.header(MailHeaders.CC, arrayToListProcessor(MailHeaders.CC))
.header(MailHeaders.BCC, arrayToListProcessor(MailHeaders.BCC)))
@@ -81,7 +81,7 @@ public class MailSupplierConfiguration {
return () -> Flux.from(messagePublisher);
}
private HeaderValueMessageProcessor<?> arrayToListProcessor(final String header) {
private HeaderValueMessageProcessor<?> arrayToListProcessor(String header) {
return new AbstractHeaderValueMessageProcessor<List<String>>() {
@Override
@@ -103,7 +103,8 @@ public class MailSupplierConfiguration {
.userFlag(this.properties.getUserFlag())
.javaMailProperties(getJavaMailProperties(urlName))
.selectorExpression(this.properties.getExpression())
.shouldMarkMessagesAsRead(this.properties.isMarkAsRead());
.shouldMarkMessagesAsRead(this.properties.isMarkAsRead())
.autoStartup(false);
if (imapIdleChannelAdapterSpecCustomizer != null) {
imapIdleChannelAdapterSpecCustomizer.customize(imapIdleChannelAdapterSpec);
@@ -118,18 +119,11 @@ public class MailSupplierConfiguration {
MailInboundChannelAdapterSpec<?, ?> adapterSpec;
URLName urlName = this.properties.getUrl();
switch (urlName.getProtocol().toUpperCase()) {
case "IMAP":
case "IMAPS":
adapterSpec = getImapChannelAdapterSpec(urlName);
break;
case "POP3":
case "POP3S":
adapterSpec = getPop3ChannelAdapterSpec(urlName);
break;
default:
throw new IllegalArgumentException("Unsupported mail protocol: " + urlName.getProtocol());
}
adapterSpec = switch (urlName.getProtocol().toUpperCase()) {
case "IMAP", "IMAPS" -> getImapChannelAdapterSpec(urlName);
case "POP3", "POP3S" -> getPop3ChannelAdapterSpec(urlName);
default -> throw new IllegalArgumentException("Unsupported mail protocol: " + urlName.getProtocol());
};
adapterSpec.javaMailProperties(getJavaMailProperties(urlName))
.userFlag(this.properties.getUserFlag())
.selectorExpression(this.properties.getExpression())
@@ -145,26 +139,17 @@ public class MailSupplierConfiguration {
@Bean("mailChannelAdapter")
@ConditionalOnProperty(value = "mail.supplier.idle-imap", matchIfMissing = true, havingValue = "false")
MessageProducerSupport mailMessageProducer(MessageSource<?> mailMessageSource) {
return new ReactiveMessageSourceProducer(mailMessageSource);
ReactiveMessageSourceProducer reactiveMessageSourceProducer = new ReactiveMessageSourceProducer(
mailMessageSource);
reactiveMessageSourceProducer.setAutoStartup(false);
return reactiveMessageSourceProducer;
}
/**
* Method to build Mail Channel Adapter for POP3.
* @param urlName Mail source URL.
* @return Mail Channel for POP3
*/
@SuppressWarnings("rawtypes")
private MailInboundChannelAdapterSpec getPop3ChannelAdapterSpec(URLName urlName) {
private Pop3MailInboundChannelAdapterSpec getPop3ChannelAdapterSpec(URLName urlName) {
return Mail.pop3InboundAdapter(urlName.toString());
}
/**
* Method to build Mail Channel Adapter for IMAP.
* @param urlName Mail source URL.
* @return Mail Channel for IMAP
*/
@SuppressWarnings("rawtypes")
private MailInboundChannelAdapterSpec getImapChannelAdapterSpec(URLName urlName) {
private ImapMailInboundChannelAdapterSpec getImapChannelAdapterSpec(URLName urlName) {
return Mail.imapInboundAdapter(urlName.toString()).shouldMarkMessagesAsRead(this.properties.isMarkAsRead());
}
@@ -172,29 +157,26 @@ public class MailSupplierConfiguration {
Properties javaMailProperties = new Properties();
switch (urlName.getProtocol().toUpperCase()) {
case "IMAP":
case "IMAP" -> {
javaMailProperties.setProperty("mail.imap.socketFactory.class", "javax.net.SocketFactory");
javaMailProperties.setProperty("mail.imap.socketFactory.fallback", "false");
javaMailProperties.setProperty("mail.store.protocol", "imap");
break;
case "IMAPS":
}
case "IMAPS" -> {
javaMailProperties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javaMailProperties.setProperty("mail.imap.socketFactory.fallback", "false");
javaMailProperties.setProperty("mail.store.protocol", "imaps");
break;
case "POP3":
}
case "POP3" -> {
javaMailProperties.setProperty("mail.pop3.socketFactory.class", "javax.net.SocketFactory");
javaMailProperties.setProperty("mail.pop3.socketFactory.fallback", "false");
javaMailProperties.setProperty("mail.store.protocol", "pop3");
break;
case "POP3S":
}
case "POP3S" -> {
javaMailProperties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javaMailProperties.setProperty("mail.pop3.socketFactory.fallback", "false");
javaMailProperties.setProperty("mail.store.protocol", "pop3s");
break;
}
}
javaMailProperties.putAll(this.properties.getJavaMailProperties());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -26,7 +26,7 @@ import org.springframework.integration.mail.AbstractMailReceiver;
import org.springframework.validation.annotation.Validated;
/**
* Properties for the file supplier.
* Configuration properties for Mail supplier.
*
* @author Gary Russell
* @author Artem Bilan
@@ -58,7 +58,7 @@ public class MailSupplierProperties {
private boolean idleImap = false;
/**
* JavaMail properties as a new line delimited string of name-value pairs, e.g.
* Java Mail properties as a new line delimited string of name-value pairs, e.g.
* 'foo=bar\n baz=car'.
*/
private Properties javaMailProperties = new Properties();
@@ -78,89 +78,53 @@ public class MailSupplierProperties {
*/
private String userFlag = AbstractMailReceiver.DEFAULT_SI_USER_FLAG;
/**
* @return the markAsRead
*/
public boolean isMarkAsRead() {
return this.markAsRead;
}
/**
* @param markAsRead the markAsRead to set
*/
public void setMarkAsRead(boolean markAsRead) {
this.markAsRead = markAsRead;
}
/**
* @return the delete
*/
public boolean isDelete() {
return this.delete;
}
/**
* @param delete the delete to set
*/
public void setDelete(boolean delete) {
this.delete = delete;
}
/**
* @return the idleImap
*/
public boolean isIdleImap() {
return this.idleImap;
}
/**
* @param idleImap the idleImap to set
*/
public void setIdleImap(boolean idleImap) {
this.idleImap = idleImap;
}
/**
* @return the javaMailProperties
*/
@NotNull
public Properties getJavaMailProperties() {
return this.javaMailProperties;
}
/**
* @param javaMailProperties the javaMailProperties to set
*/
public void setJavaMailProperties(Properties javaMailProperties) {
this.javaMailProperties = javaMailProperties;
}
/**
* @return the url
*/
@NotNull
public URLName getUrl() {
return this.url;
}
/**
* @param url the url to set
*/
public void setUrl(URLName url) {
this.url = url;
}
/**
* @return the expression
*/
@NotNull
public String getExpression() {
return this.expression;
}
/**
* @param expression the expression to set
*/
public void setExpression(String expression) {
this.expression = expression;
}

View File

@@ -0,0 +1,4 @@
/**
* The MQTT supplier auto-configuration support.
*/
package org.springframework.cloud.fn.supplier.mail;

View File

@@ -0,0 +1 @@
org.springframework.cloud.fn.supplier.mail.MailSupplierConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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 com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import reactor.core.publisher.Flux;
@@ -35,8 +36,8 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { "mail.supplier.mark-as-read=true",
"mail.supplier.delete=false", "mail.supplier.user-flag=testSIUserFlag",
@SpringBootTest(properties = { "mail.supplier.mark-as-read=true", "mail.supplier.delete=false",
"mail.supplier.user-flag=testSIUserFlag",
"mail.supplier.java-mail-properties=mail.imap.socketFactory.fallback=true\\n mail.store.protocol=imap\\n mail.debug=true" })
@DirtiesContext
public abstract class AbstractMailSupplierTests {
@@ -70,6 +71,11 @@ public abstract class AbstractMailSupplierTests {
mailServer.start();
}
@AfterAll
static void tearDown() {
mailServer.stop();
}
@DynamicPropertySource
static void mongoDbProperties(DynamicPropertyRegistry registry) {
registry.add("test.mail.server.imap.port", mailServer.getImap().getServerSetup()::getPort);

View File

@@ -1,51 +0,0 @@
/*
* Copyright 2020-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.fn.supplier.mail;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mail.transformer.MailToStringTransformer;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.test.context.TestPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
@TestPropertySource(properties = { "mail.supplier.url=imap://user:pw@localhost:${test.mail.server.imap.port}/INBOX",
"mail.supplier.charset=cp1251" })
public class ImapFailTests extends AbstractMailSupplierTests {
@Autowired
protected MailToStringTransformer mailToStringTransformer;
@Test
public void testSimpleTest() {
// given
sendMessage("test", "foo");
// when
final Flux<Message<?>> messageFlux = mailSupplier.get();
// then
assertThat(TestUtils.getPropertyValue(mailToStringTransformer, "charset").equals("cp1251")).isTrue();
StepVerifier.create(messageFlux).assertNext((message) -> {
assertThat(((String) message.getPayload())).isNotEqualTo("Test Mail");
}).thenCancel().verify();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -20,26 +20,36 @@ import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mail.transformer.MailToStringTransformer;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.test.context.TestPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
@TestPropertySource(properties = { "mail.supplier.idle-imap=true",
"mail.supplier.url=imap://user:pw@localhost:${test.mail.server.imap.port}/INBOX" })
"mail.supplier.url=imap://user:pw@localhost:${test.mail.server.imap.port}/INBOX",
"mail.supplier.charset=cp1251" })
public class ImapIdlePassTests extends AbstractMailSupplierTests {
@Autowired
MailToStringTransformer mailToStringTransformer;
@Test
public void testSimpleTest() {
// given
sendMessage("test", "foo");
// when
final Flux<Message<?>> messageFlux = mailSupplier.get();
final Flux<Message<?>> messageFlux = this.mailSupplier.get();
// then
StepVerifier.create(messageFlux).assertNext((message) -> {
System.out.println("Message:" + message);
assertThat(((String) message.getPayload())).isEqualTo("foo");
}).thenCancel().verify();
assertThat(TestUtils.getPropertyValue(this.mailToStringTransformer, "charset").equals("cp1251")).isTrue();
StepVerifier.create(messageFlux)
.assertNext((message) -> assertThat(((String) message.getPayload())).isEqualTo("foo"))
.thenCancel()
.verify();
}
}

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2020-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.fn.supplier.mail;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.messaging.Message;
import org.springframework.test.context.TestPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
@TestPropertySource(properties = "mail.supplier.url=pop3://user:pw@localhost:${test.mail.server.pop3.port}/INBOX")
public class Pop3FailTests extends AbstractMailSupplierTests {
@Test
public void testSimpleTest() {
// given
sendMessage("test", "foo");
// when
final Flux<Message<?>> messageFlux = mailSupplier.get();
// then
StepVerifier.create(messageFlux).assertNext((message) -> {
assertThat(((String) message.getPayload())).isNotEqualTo("Test Mail");
}).thenCancel().verify();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -34,10 +34,10 @@ public class Pop3PassTests extends AbstractMailSupplierTests {
sendMessage("test", "foo");
final Flux<Message<?>> messageFlux = mailSupplier.get();
StepVerifier.create(messageFlux).assertNext((message) -> {
assertThat(((String) message.getPayload())).contains("foo");
}).thenCancel().verify();
StepVerifier.create(messageFlux)
.assertNext((message) -> assertThat(((String) message.getPayload())).contains("foo"))
.thenCancel()
.verify();
}
}