From 7d5df95542fadfc27284979e5bec831e3a0da5e4 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 21 Nov 2026 12:25:20 +0100 Subject: [PATCH] JSPWIKI-1226 Add a guid to the items and a link to itself to the RSS 2.0 feed --- ChangeLog.md | 6 +++++ .../java/org/apache/wiki/api/Release.java | 2 +- .../java/org/apache/wiki/rss/AtomFeed.java | 6 +---- .../main/java/org/apache/wiki/rss/Feed.java | 20 ++++++++++++++++ .../java/org/apache/wiki/rss/RSS20Feed.java | 9 +++++++ .../org/apache/wiki/rss/RSSGeneratorTest.java | 24 +++++++++++++++++++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 7f9a870113..c579a8cc53 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -16,6 +16,12 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> +**2026-11-21 Arturo Bernal (abernal AT apache DOT org)** + +* _3.0.2-git-2_ + +* [JSPWIKI-1226](https://issues.apache.org/jira/browse/JSPWIKI-1226): every item of the RSS 2.0 feed has a `guid`, and the channel has an `atom:link` with `rel="self"` that tells a feed reader where the feed is, as feed validators ask. The link to the feed itself in the Atom feed is absolute too. + **2026-11-19 Arturo Bernal (abernal AT apache DOT org)** * _3.0.2-git-1_ diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java index 26cb6a0efa..ad73c8ffa7 100644 --- a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java +++ b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java @@ -69,7 +69,7 @@ public final class Release { *

* If the build identifier is empty, it is not added. */ - public static final String BUILD = "1"; + public static final String BUILD = "2"; /** * This is the generic version string you should use when printing out the version. It is of diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/AtomFeed.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/AtomFeed.java index 384acdb140..b641aafedc 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/AtomFeed.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/AtomFeed.java @@ -160,12 +160,8 @@ public String getString() { root.addContent( getElement( "link" ).setAttribute( "href", engine.getBaseURL() ) ); root.addContent( getElement( "generator" ).setText( "JSPWiki " + Release.VERSTR ) ); - final String rssFeedURL = engine.getURL( ContextEnum.PAGE_NONE.getRequestContext(), "rss.jsp", - "page=" + engine.encodeName( m_wikiContext.getPage().getName() ) + - "&mode=" + m_mode + - "&type=atom" ); final Element self = getElement( "link" ).setAttribute( "rel","self" ); - self.setAttribute( "href", rssFeedURL ); + self.setAttribute( "href", getSelfURL( RSSGenerator.ATOM ) ); root.addContent( self ); // Items diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/Feed.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/Feed.java index 379602b9d3..777c65be25 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/Feed.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/Feed.java @@ -20,8 +20,10 @@ Licensed to the Apache Software Foundation (ASF) under one import org.apache.commons.text.StringEscapeUtils; import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.core.ContextEnum; import org.apache.wiki.api.core.Engine; import org.apache.wiki.api.exceptions.NoSuchVariableException; +import org.apache.wiki.util.HttpUtil; import org.apache.wiki.variables.VariableManager; import jakarta.servlet.ServletContext; @@ -172,6 +174,24 @@ public void setFeedURL( final String feedurl) { m_feedURL = feedurl; } + /** + * Returns the address of this feed, for the link that tells a feed reader where the feed is. The address is absolute when the + * request is known, because a feed reader has no page to resolve a relative one against. + * + * @param type the type of the feed, one of the types of {@link RSSGenerator}. + * @return the URL of this feed. + * @since 3.0.2 + */ + protected String getSelfURL( final String type ) { + final Engine engine = m_wikiContext.getEngine(); + final String url = engine.getURL( ContextEnum.PAGE_NONE.getRequestContext(), "rss.jsp", + "page=" + engine.encodeName( m_wikiContext.getPage().getName() ) + "&mode=" + m_mode + "&type=" + type ); + if( m_wikiContext.getHttpRequest() == null || !url.startsWith( "/" ) ) { + return url; + } + return HttpUtil.getAbsoluteUrl( m_wikiContext.getHttpRequest(), url ); + } + /** * A helper method for figuring out the MIME type for an enclosure. * diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java index def3375b8e..4f52d02a21 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSS20Feed.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one import org.apache.wiki.attachment.AttachmentManager; import org.apache.wiki.variables.VariableManager; import org.jdom2.Element; +import org.jdom2.Namespace; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; @@ -52,6 +53,8 @@ public class RSS20Feed extends Feed { private static final Logger LOG = LogManager.getLogger( RSS20Feed.class ); + private static final Namespace NS_ATOM = Namespace.getNamespace( "atom", "http://www.w3.org/2005/Atom" ); + /** * Creates an RSS 2.0 feed for the specified Context. * @@ -78,6 +81,8 @@ private List getItems() final String url = e.getURL(); final Element item = new Element( "item" ); item.addContent( new Element( "link" ).setText( url ) ); + // the address of an entry includes its version, so it never identifies another entry + item.addContent( new Element( "guid" ).setAttribute( "isPermaLink", "true" ).setText( url ) ); item.addContent( new Element( "title" ).setText( e.getTitle() ) ); item.addContent( new Element( "description" ).setText( e.getContent() ) ); @@ -126,6 +131,7 @@ public String getString() final Engine engine = m_wikiContext.getEngine(); final Element root = new Element("rss"); root.setAttribute("version","2.0"); + root.addNamespaceDeclaration( NS_ATOM ); final Element channel = new Element("channel"); root.addContent( channel ); @@ -142,6 +148,9 @@ public String getString() channel.addContent( new Element("link").setText("https://0.0.0.0/")); } channel.addContent( new Element("description").setText( getChannelDescription() )); + channel.addContent( new Element( "link", NS_ATOM ).setAttribute( "href", getSelfURL( RSSGenerator.RSS20 ) ) + .setAttribute( "rel", "self" ) + .setAttribute( "type", "application/rss+xml" ) ); // // Optional diff --git a/jspwiki-main/src/test/java/org/apache/wiki/rss/RSSGeneratorTest.java b/jspwiki-main/src/test/java/org/apache/wiki/rss/RSSGeneratorTest.java index 2e52a0f319..2f3c8734ea 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/rss/RSSGeneratorTest.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/rss/RSSGeneratorTest.java @@ -207,6 +207,30 @@ public void testEntryLinksAreAbsolute() throws Exception { } /** The feed is cached and shared, so an entry that the anonymous user may not view must not be listed in it. */ + /** JSPWIKI-1226: a feed validator wants a guid in every item and a link to the feed itself. */ + @Test + public void testRss20HasGuidAndLinkToItself() throws Exception { + final TestEngine engine = buildRssEngine(); + engine.saveText( "RssGuid", "Foo" ); + + final HttpServletRequest request = HttpMockFactory.createHttpRequest(); + Mockito.doReturn( "http" ).when( request ).getScheme(); + Mockito.doReturn( "localhost" ).when( request ).getServerName(); + Mockito.doReturn( 8080 ).when( request ).getServerPort(); + final Page page = engine.getManager( PageManager.class ).getPage( "RssGuid" ); + final Context context = Wiki.context().create( engine, request, page ); + final RSSGenerator gen = engine.getManager( RSSGenerator.class ); + + final String rss20 = gen.generateWikiPageRSS( context, new ArrayList<>( List.of( page ) ), new RSS20Feed( context ) ); + final String atom = gen.generateWikiPageRSS( context, new ArrayList<>( List.of( page ) ), new AtomFeed( context ) ); + + Assertions.assertTrue( rss20.contains( "xmlns:atom=\"http://www.w3.org/2005/Atom\"" ), rss20 ); + Assertions.assertTrue( rss20.contains( "" ), rss20 ); + Assertions.assertTrue( rss20.contains( "http://localhost:8080/test/Wiki.jsp?page=RssGuid" ), rss20 ); + Assertions.assertTrue( atom.contains( "rel=\"self\" href=\"http://localhost:8080/test/rss.jsp?page=RssGuid&mode=wiki&type=atom\"" ), atom ); + engine.shutdown(); + } + @Test public void testBlogRSSSkipsEntriesTheAnonymousUserMayNotView() throws Exception { final Properties props = TestEngine.getTestProperties();