Skip to content

Commit

Permalink
GH-3677: Doc for URL conn customization in FeedCA (#3678)
Browse files Browse the repository at this point in the history
* GH-3677: Doc for URL conn customization in FeedCA

Fixes #3677

If there is need to have a `URLConnection` customized, the `UrlResource`
has to be used instead of plain `URL` injection into the `FeedEntryMessageSource`

* * Add a sample to docs for connection customization
  • Loading branch information
artembilan committed Nov 16, 2021
1 parent fb14976 commit de0f91b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand All @@ -52,25 +56,36 @@ 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();
assertThat(feedEntrySource.receive()).isNull();
}

@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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
31 changes: 28 additions & 3 deletions src/reference/asciidoc/feed.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
----
<dependency>
<groupId>org.springframework.integration</groupId>
Expand All @@ -17,8 +17,8 @@ You need to include this dependency into your project:
</dependency>
----
[source, groovy, subs="normal", role="secondary"]
.Gradle
[source, groovy, subs="normal"]
----
compile "org.springframework.integration:spring-integration-feed:{project-version}"
----
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -126,4 +151,4 @@ public class FeedJavaApplication {
}
----
====
====

0 comments on commit de0f91b

Please sign in to comment.