Skip to content

Commit

Permalink
Make Mail supplier as auto-config
Browse files Browse the repository at this point in the history
* Fix all their Checkstyle violations
* Fix Checkstyle violations in the `spring-kafka-supplier`, too
  • Loading branch information
artembilan committed Jan 9, 2024
1 parent 8ef105b commit a0e6d7c
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ RecordFilterStrategy<Object, Object> recordFilterStrategy(KafkaSupplierPropertie

StandardEvaluationContext evaluationContext = IntegrationContextUtils.getEvaluationContext(beanFactory);

return consumerRecord -> Boolean.TRUE.equals(
return (consumerRecord) -> Boolean.TRUE.equals(
kafkaSupplierProperties.getRecordFilter().getValue(evaluationContext, consumerRecord, Boolean.class));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The Apache Kafka supplier auto-configuration support.
*/
package org.springframework.cloud.fn.supplier.kafka;
2 changes: 1 addition & 1 deletion supplier/spring-mail-supplier/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,11 +25,11 @@
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;
Expand All @@ -38,38 +38,38 @@
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;
}

@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)))
Expand All @@ -81,7 +81,7 @@ public Supplier<Flux<Message<?>>> mailSupplier(Publisher<Message<Object>> messag
return () -> Flux.from(messagePublisher);
}

private HeaderValueMessageProcessor<?> arrayToListProcessor(final String header) {
private HeaderValueMessageProcessor<?> arrayToListProcessor(String header) {
return new AbstractHeaderValueMessageProcessor<List<String>>() {

@Override
Expand All @@ -103,7 +103,8 @@ public List<String> processMessage(Message<?> message) {
.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);
Expand All @@ -118,18 +119,11 @@ public List<String> processMessage(Message<?> message) {

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())
Expand All @@ -145,56 +139,44 @@ public List<String> processMessage(Message<?> message) {
@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());
}

private Properties getJavaMailProperties(URLName urlName) {
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,7 +26,7 @@
import org.springframework.validation.annotation.Validated;

/**
* Properties for the file supplier.
* Configuration properties for Mail supplier.
*
* @author Gary Russell
* @author Artem Bilan
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The MQTT supplier auto-configuration support.
*/
package org.springframework.cloud.fn.supplier.mail;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.cloud.fn.supplier.mail.MailSupplierConfiguration
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,6 +23,7 @@
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;

Expand All @@ -35,8 +36,8 @@
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 {
Expand Down Expand Up @@ -70,6 +71,11 @@ public static void setup() {
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);
Expand Down
Loading

0 comments on commit a0e6d7c

Please sign in to comment.