From 18dc08e41775e654888fdebf416e07228aa326d1 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 16 Nov 2021 11:02:03 -0500 Subject: [PATCH 1/2] GH-3677: Doc for URL conn customization in FeedCA Fixes https://github.com/spring-projects/spring-integration/issues/3677 If there is need to have a `URLConnection` customized, the `UrlResource` has to be used instead of plain `URL` injection into the `FeedEntryMessageSource` --- src/reference/asciidoc/feed.adoc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/reference/asciidoc/feed.adoc b/src/reference/asciidoc/feed.adoc index cd4e97b9df6..eacb971b26b 100644 --- a/src/reference/asciidoc/feed.adoc +++ b/src/reference/asciidoc/feed.adoc @@ -7,8 +7,8 @@ The implementation is based on the https://rometools.github.io/rome/[ROME Framew You need to include this dependency into your project: ==== +[source, xml, subs="normal", role="primary"] .Maven -[source, xml, subs="normal"] ---- org.springframework.integration @@ -17,8 +17,8 @@ You need to include this dependency into your project: ---- +[source, groovy, subs="normal", role="secondary"] .Gradle -[source, groovy, subs="normal"] ---- compile "org.springframework.integration:spring-integration-feed:{project-version}" ---- @@ -127,3 +127,5 @@ public class FeedJavaApplication { } ---- ==== + +NOTE: If the connection to the feed needs some customization, e.g. connection and read timeouts, the `org.springframework.core.io.UrlResource` extension with its `customizeConnection(HttpURLConnection)` override has to be used instead of plain `URL` injection into the `FeedEntryMessageSource`. \ No newline at end of file From 2754ece02c6e08011fa4f934eda21ea859d7b963 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 16 Nov 2021 11:41:17 -0500 Subject: [PATCH 2/2] * Add a sample to docs for connection customization --- .../inbound/FeedEntryMessageSourceTests.java | 35 +++++++++++++------ src/reference/asciidoc/feed.adoc | 29 +++++++++++++-- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/spring-integration-feed/src/test/java/org/springframework/integration/feed/inbound/FeedEntryMessageSourceTests.java b/spring-integration-feed/src/test/java/org/springframework/integration/feed/inbound/FeedEntryMessageSourceTests.java index 68bf0783742..ad0ad8d63ad 100644 --- a/spring-integration-feed/src/test/java/org/springframework/integration/feed/inbound/FeedEntryMessageSourceTests.java +++ b/spring-integration-feed/src/test/java/org/springframework/integration/feed/inbound/FeedEntryMessageSourceTests.java @@ -17,16 +17,20 @@ package org.springframework.integration.feed.inbound; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import java.io.File; +import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URL; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.UrlResource; import org.springframework.integration.metadata.PropertiesPersistingMetadataStore; import org.springframework.messaging.Message; @@ -43,7 +47,7 @@ */ public class FeedEntryMessageSourceTests { - @Before + @BeforeEach public void prepare() { File metadataStoreFile = new File(System.getProperty("java.io.tmpdir") + "/spring-integration/", "metadata-store.properties"); @@ -52,17 +56,28 @@ public void prepare() { } } - @Test(expected = IllegalArgumentException.class) + @Test public void testFailureWhenNotInitialized() throws Exception { URL url = new ClassPathResource("org/springframework/integration/feed/sample.rss").getURL(); FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(url, "foo"); - feedEntrySource.receive(); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(feedEntrySource::receive); } @Test public void testReceiveFeedWithNoEntries() throws Exception { URL url = new ClassPathResource("org/springframework/integration/feed/empty.rss").getURL(); - FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(url, "foo"); + UrlResource urlResource = + new UrlResource(url) { + + @Override + protected void customizeConnection(HttpURLConnection connection) throws IOException { + super.customizeConnection(connection); + connection.setConnectTimeout(10000); + connection.setReadTimeout(5000); + } + }; + FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(urlResource, "foo"); feedEntrySource.setBeanName("feedReader"); feedEntrySource.setBeanFactory(mock(BeanFactory.class)); feedEntrySource.afterPropertiesSet(); @@ -70,7 +85,7 @@ public void testReceiveFeedWithNoEntries() throws Exception { } @Test - public void testReceiveFeedWithEntriesSorted() throws Exception { + public void testReceiveFeedWithEntriesSorted() { ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/sample.rss"); FeedEntryMessageSource source = new FeedEntryMessageSource(resource, "foo"); source.setBeanName("feedReader"); @@ -90,7 +105,7 @@ public void testReceiveFeedWithEntriesSorted() throws Exception { // verifies that when entry has been updated since publish, that is taken into // account when determining if the feed entry has been seen before @Test - public void testEntryHavingBeenUpdatedAfterPublishAndRepeat() throws Exception { + public void testEntryHavingBeenUpdatedAfterPublishAndRepeat() { ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/atom.xml"); FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(resource, "foo"); feedEntrySource.setBeanName("feedReader"); @@ -124,7 +139,7 @@ public void testEntryHavingBeenUpdatedAfterPublishAndRepeat() throws Exception { // will test that last feed entry is remembered between the sessions // and no duplicate entries are retrieved @Test - public void testReceiveFeedWithRealEntriesAndRepeatWithPersistentMetadataStore() throws Exception { + public void testReceiveFeedWithRealEntriesAndRepeatWithPersistentMetadataStore() { ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/sample.rss"); FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(resource, "foo"); feedEntrySource.setBeanName("feedReader"); @@ -166,7 +181,7 @@ public void testReceiveFeedWithRealEntriesAndRepeatWithPersistentMetadataStore() // will test that last feed entry is NOT remembered between the sessions, since // no persistent MetadataStore is provided and the same entries are retrieved again @Test - public void testReceiveFeedWithRealEntriesAndRepeatNoPersistentMetadataStore() throws Exception { + public void testReceiveFeedWithRealEntriesAndRepeatNoPersistentMetadataStore() { ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/sample.rss"); FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(resource, "foo"); feedEntrySource.setBeanName("feedReader"); diff --git a/src/reference/asciidoc/feed.adoc b/src/reference/asciidoc/feed.adoc index eacb971b26b..d88d7adf9bf 100644 --- a/src/reference/asciidoc/feed.adoc +++ b/src/reference/asciidoc/feed.adoc @@ -87,6 +87,31 @@ This is useful when the feed source is not an HTTP endpoint but is any other res In the `FeedEntryMessageSource` logic, such a resource (or provided `URL`) is parsed by the `SyndFeedInput` to the `SyndFeed` object for the processing mentioned earlier. You can also inject a customized `SyndFeedInput` (for example, with the `allowDoctypes` option) instance into the `FeedEntryMessageSource`. +[NOTE] +==== +If the connection to the feed needs some customization, e.g. connection and read timeouts, the `org.springframework.core.io.UrlResource` extension with its `customizeConnection(HttpURLConnection)` override has to be used instead of plain `URL` injection into the `FeedEntryMessageSource`. +For example: + +[source, java] +---- +@Bean +@InboundChannelAdapter("feedChannel") +FeedEntryMessageSource feedEntrySource() { + UrlResource urlResource = + new UrlResource(url) { + + @Override + protected void customizeConnection(HttpURLConnection connection) throws IOException { + super.customizeConnection(connection); + connection.setConnectTimeout(10000); + connection.setReadTimeout(5000); + } + }; + return new FeedEntryMessageSource(urlResource, "myKey"); +} +---- +==== + [[feed-java-configuration]] === Java DSL Configuration @@ -126,6 +151,4 @@ public class FeedJavaApplication { } ---- -==== - -NOTE: If the connection to the feed needs some customization, e.g. connection and read timeouts, the `org.springframework.core.io.UrlResource` extension with its `customizeConnection(HttpURLConnection)` override has to be used instead of plain `URL` injection into the `FeedEntryMessageSource`. \ No newline at end of file +==== \ No newline at end of file