Make XMPP functions as auto-config

* Fix all the Checkstyle violations for those modules
This commit is contained in:
Artem Bilan
2024-01-03 17:13:51 -05:00
parent 7b06b8f4ba
commit 7b9bd3f3a9
17 changed files with 84 additions and 66 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 the original author or authors.
* Copyright 2016-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.
@@ -24,16 +24,16 @@ import io.awspring.cloud.autoconfigure.s3.properties.S3Properties;
import software.amazon.awssdk.services.s3.S3Client;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.aws.support.S3SessionFactory;
/**
* The auto-configuration for {@link S3SessionFactory}.
*
* @author Artem Bilan
*/
@AutoConfiguration
@AutoConfigureAfter({ S3AutoConfiguration.class, S3CrtAsyncClientAutoConfiguration.class })
@AutoConfiguration(after = { S3AutoConfiguration.class, S3CrtAsyncClientAutoConfiguration.class })
public class AmazonS3Configuration {
@Bean

View File

@@ -0,0 +1,4 @@
/**
* The common classes for AWS S3 functions.
*/
package org.springframework.cloud.fn.common.aws.s3;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 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,7 +20,6 @@ import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration;
import io.awspring.cloud.autoconfigure.s3.S3AutoConfiguration;
import io.awspring.cloud.autoconfigure.s3.S3CrtAsyncClientAutoConfiguration;
import io.awspring.cloud.core.region.StaticRegionProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
@@ -36,6 +35,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.test.util.TestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Timo Salm
* @author Artem Bilan
@@ -51,27 +52,24 @@ public class AmazonS3ConfigurationTests {
@Test
public void testAmazonS3Configuration() {
runner.withPropertyValues().run(context -> {
runner.withPropertyValues().run((context) -> {
S3Client amazonS3 = context.getBean(S3Client.class);
Assertions.assertNotNull(amazonS3);
assertThat(amazonS3).isNotNull();
S3Utilities utilities = amazonS3.utilities();
Assertions.assertEquals(TEST_REGION_NAME,
TestUtils.getPropertyValue(utilities, "region", Region.class).id());
Assertions.assertTrue(utilities.getUrl(GetUrlRequest.builder().bucket("b").key("k").build())
.toString()
.startsWith("https://s3.eu-central-1.amazonaws.com"));
assertThat(TestUtils.getPropertyValue(utilities, "region", Region.class).id()).isEqualTo(TEST_REGION_NAME);
assertThat(utilities.getUrl(GetUrlRequest.builder().bucket("b").key("k").build()).toString())
.startsWith("https://s3.eu-central-1.amazonaws.com");
});
}
@Test
public void testAmazonS3ConfigurationForS3CompatibleStorage() {
runner.withPropertyValues("spring.cloud.aws.s3.endpoint=http://localhost:8080").run(context -> {
runner.withPropertyValues("spring.cloud.aws.s3.endpoint=http://localhost:8080").run((context) -> {
S3Client amazonS3 = context.getBean(S3Client.class);
Assertions.assertNotNull(amazonS3);
assertThat(amazonS3).isNotNull();
S3Utilities utilities = amazonS3.utilities();
Assertions.assertTrue(utilities.getUrl(GetUrlRequest.builder().bucket("b").key("k").build())
.toString()
.startsWith("http://localhost:8080"));
assertThat(utilities.getUrl(GetUrlRequest.builder().bucket("b").key("k").build()).toString())
.startsWith("http://localhost:8080");
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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,14 +20,19 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.xmpp.config.XmppConnectionFactoryBean;
import org.springframework.util.StringUtils;
@Configuration
/**
* The XMPP connection factory auto-configuration.
*
* @author Daniel Frey
*/
@AutoConfiguration
@EnableConfigurationProperties(XmppConnectionFactoryProperties.class)
public class XmppConnectionFactoryConfiguration {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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.
@@ -24,6 +24,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
/**
* The properties for XMPP connection factory.
*
* @author Daniel Frey
* @since 4.0.0
*/
@@ -71,7 +73,7 @@ public class XmppConnectionFactoryProperties {
}
public String getResource() {
return resource;
return this.resource;
}
public void setUser(String user) {
@@ -80,7 +82,7 @@ public class XmppConnectionFactoryProperties {
@NotEmpty(message = "user is required")
public String getUser() {
return user;
return this.user;
}
public void setPassword(String password) {
@@ -89,7 +91,7 @@ public class XmppConnectionFactoryProperties {
@NotEmpty(message = "password is required")
public String getPassword() {
return password;
return this.password;
}
public void setServiceName(String serviceName) {
@@ -97,7 +99,7 @@ public class XmppConnectionFactoryProperties {
}
public String getServiceName() {
return serviceName;
return this.serviceName;
}
public void setHost(String host) {
@@ -106,7 +108,7 @@ public class XmppConnectionFactoryProperties {
@NotEmpty(message = "host is required")
public String getHost() {
return host;
return this.host;
}
public void setPort(int port) {
@@ -114,7 +116,7 @@ public class XmppConnectionFactoryProperties {
}
public int getPort() {
return port;
return this.port;
}
public void setSubscriptionMode(Roster.SubscriptionMode subscriptionMode) {
@@ -122,7 +124,7 @@ public class XmppConnectionFactoryProperties {
}
public Roster.SubscriptionMode getSubscriptionMode() {
return subscriptionMode;
return this.subscriptionMode;
}
public void setSecurityMode(ConnectionConfiguration.SecurityMode securityMode) {
@@ -130,7 +132,7 @@ public class XmppConnectionFactoryProperties {
}
public ConnectionConfiguration.SecurityMode getSecurityMode() {
return securityMode;
return this.securityMode;
}
}

View File

@@ -0,0 +1,4 @@
/**
* The XMPP connection factory auto-configuration.
*/
package org.springframework.cloud.fn.common.xmpp;

View File

@@ -0,0 +1 @@
org.springframework.cloud.fn.common.xmpp.XmppConnectionFactoryConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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,35 +20,35 @@ import java.util.function.Consumer;
import org.jivesoftware.smack.XMPPConnection;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.fn.common.xmpp.XmppConnectionFactoryConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.integration.xmpp.XmppHeaders;
import org.springframework.integration.xmpp.outbound.ChatMessageSendingMessageHandler;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
/**
* The XMPP consumer auto-configuration.
*
* @author Daniel Frey
* @since 4.0.0
*/
@Configuration
@AutoConfiguration(after = XmppConnectionFactoryConfiguration.class)
@EnableConfigurationProperties(XmppConsumerProperties.class)
@Import(XmppConnectionFactoryConfiguration.class)
public class XmppConsumerConfiguration {
@Bean
public ChatMessageSendingMessageHandler chatMessageSendingMessageHandler(XMPPConnection xmppConnection) {
return new ChatMessageSendingMessageHandler(xmppConnection);
}
@Bean
public Consumer<Message<?>> xmppConsumer(ChatMessageSendingMessageHandler chatMessageSendingMessageHandler,
XmppConsumerProperties properties) {
return message -> {
return (message) -> {
var send = MessageBuilder.fromMessage(message)
.setHeaderIfAbsent(XmppHeaders.TO, properties.getChatTo())

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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,6 +20,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
/**
* Properties for XMPP consumer.
*
* @author Daniel Frey
* @since 4.0.0
*/
@@ -37,7 +39,7 @@ public class XmppConsumerProperties {
}
public String getChatTo() {
return chatTo;
return this.chatTo;
}
}

View File

@@ -0,0 +1,4 @@
/**
* The XMPP consumer classes.
*/
package org.springframework.cloud.fn.consumer.xmpp;

View File

@@ -0,0 +1 @@
org.springframework.cloud.fn.consumer.xmpp.XmppConsumerConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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.
@@ -33,11 +33,9 @@ import org.junit.jupiter.api.Test;
import org.jxmpp.stringprep.XmppStringprepException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.fn.test.support.xmpp.XmppTestContainerSupport;
import org.springframework.context.annotation.Import;
import org.springframework.integration.xmpp.XmppHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
@@ -62,8 +60,8 @@ public class XmppConsumerConfigurationTests implements XmppTestContainerSupport
@DynamicPropertySource
static void registerConfigurationProperties(DynamicPropertyRegistry registry) {
registry.add("xmpp.factory.host", () -> XmppTestContainerSupport.getXmppHost());
registry.add("xmpp.factory.port", () -> XmppTestContainerSupport.getXmppMappedPort());
registry.add("xmpp.factory.host", XmppTestContainerSupport::getXmppHost);
registry.add("xmpp.factory.port", XmppTestContainerSupport::getXmppMappedPort);
}
@Autowired
@@ -139,9 +137,7 @@ public class XmppConsumerConfigurationTests implements XmppTestContainerSupport
assertThat(stanza.getFrom().asBareJid().asUnescapedString()).isEqualTo(JOHN_USER + "@" + SERVICE_NAME);
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(XmppConsumerConfiguration.class)
@SpringBootApplication
static class XmppConsumerTestApplication {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-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.
@@ -21,11 +21,10 @@ import java.util.function.Supplier;
import org.jivesoftware.smack.XMPPConnection;
import reactor.core.publisher.Flux;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.fn.common.xmpp.XmppConnectionFactoryConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.integration.channel.FluxMessageChannel;
import org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpoint;
import org.springframework.messaging.Message;
@@ -36,12 +35,11 @@ import org.springframework.messaging.Message;
* @author Daniel Frey
* @since 4.0.0
*/
@Configuration
@AutoConfiguration(after = XmppConnectionFactoryConfiguration.class)
@EnableConfigurationProperties(XmppSupplierProperties.class)
@Import(XmppConnectionFactoryConfiguration.class)
public class XmppSupplierConfiguration {
private FluxMessageChannel output = new FluxMessageChannel();
private final FluxMessageChannel output = new FluxMessageChannel();
@Bean
public ChatMessageListeningEndpoint chatMessageListeningEndpoint(XMPPConnection xmppConnection,
@@ -54,7 +52,7 @@ public class XmppSupplierConfiguration {
}
chatMessageListeningEndpoint.setStanzaFilter(properties.getStanzaFilter());
chatMessageListeningEndpoint.setOutputChannel(output);
chatMessageListeningEndpoint.setOutputChannel(this.output);
chatMessageListeningEndpoint.setAutoStartup(false);
return chatMessageListeningEndpoint;
@@ -62,7 +60,7 @@ public class XmppSupplierConfiguration {
@Bean
public Supplier<Flux<Message<?>>> xmppSupplier(ChatMessageListeningEndpoint chatMessageListeningEndpoint) {
return () -> Flux.from(output).doOnSubscribe(subscription -> chatMessageListeningEndpoint.start());
return () -> Flux.from(this.output).doOnSubscribe((subscription) -> chatMessageListeningEndpoint.start());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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.
@@ -24,6 +24,8 @@ import org.springframework.expression.Expression;
import org.springframework.validation.annotation.Validated;
/**
* The properties for XMPP supplier.
*
* @author Daniel Frey
* @since 4.0.0
*/
@@ -40,7 +42,7 @@ public class XmppSupplierProperties {
}
public StanzaFilter getStanzaFilter() {
return stanzaFilter;
return this.stanzaFilter;
}
public void setPayloadExpression(Expression payloadExpression) {
@@ -48,7 +50,7 @@ public class XmppSupplierProperties {
}
public Expression getPayloadExpression() {
return payloadExpression;
return this.payloadExpression;
}
}

View File

@@ -0,0 +1,4 @@
/**
* The XMPP supplier classes.
*/
package org.springframework.cloud.fn.supplier.xmpp;

View File

@@ -0,0 +1 @@
org.springframework.cloud.fn.supplier.xmpp.XmppSupplierConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-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.
@@ -36,11 +36,9 @@ import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.fn.test.support.xmpp.XmppTestContainerSupport;
import org.springframework.context.annotation.Import;
import org.springframework.integration.xmpp.XmppHeaders;
import org.springframework.messaging.Message;
import org.springframework.test.context.DynamicPropertyRegistry;
@@ -122,9 +120,7 @@ public class XmppSupplierConfigurationTests implements XmppTestContainerSupport
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(XmppSupplierConfiguration.class)
@SpringBootApplication
static class XmppSupplierTestApplication {
}