Skip to content

Commit 4ccc760

Browse files
committed
removed Rome dependency and upgraded RSS feed to 2.0
1 parent 8bd1d17 commit 4ccc760

File tree

3 files changed

+46
-58
lines changed

3 files changed

+46
-58
lines changed

Diff for: pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@
111111
</dependency>-->
112112

113113
<!-- MISC -->
114-
<dependency>
115-
<groupId>rome</groupId>
116-
<artifactId>rome</artifactId>
117-
<version>1.0</version>
118-
</dependency>
119114
<dependency>
120115
<groupId>org.apache.httpcomponents.core5</groupId>
121116
<artifactId>httpcore5</artifactId>

Diff for: src/main/java/com/erudika/scoold/controllers/SearchController.java

+28-53
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,9 @@
3737
import com.erudika.scoold.utils.ScooldUtils;
3838
import com.redfin.sitemapgenerator.WebSitemapGenerator;
3939
import com.redfin.sitemapgenerator.WebSitemapUrl;
40-
import com.sun.syndication.feed.synd.SyndContent;
41-
import com.sun.syndication.feed.synd.SyndContentImpl;
42-
import com.sun.syndication.feed.synd.SyndEntry;
43-
import com.sun.syndication.feed.synd.SyndEntryImpl;
44-
import com.sun.syndication.feed.synd.SyndFeed;
45-
import com.sun.syndication.feed.synd.SyndFeedImpl;
46-
import com.sun.syndication.io.FeedException;
47-
import com.sun.syndication.io.SyndFeedOutput;
4840
import jakarta.inject.Inject;
4941
import jakarta.servlet.http.HttpServletRequest;
42+
import jakarta.servlet.http.HttpServletResponse;
5043
import java.io.IOException;
5144
import java.util.ArrayList;
5245
import java.util.Collections;
@@ -55,6 +48,7 @@
5548
import java.util.LinkedHashMap;
5649
import java.util.LinkedList;
5750
import java.util.List;
51+
import java.util.Locale;
5852
import java.util.Map;
5953
import java.util.Optional;
6054
import java.util.concurrent.TimeUnit;
@@ -229,59 +223,40 @@ public ResponseEntity<String> webmanifest(HttpServletRequest req) {
229223
body(json);
230224
}
231225

232-
@ResponseBody
233-
@GetMapping("/feed.xml")
234-
public ResponseEntity<String> feed(HttpServletRequest req) {
235-
String feed = "";
236-
try {
237-
feed = new SyndFeedOutput().outputString(getFeed(req));
238-
} catch (Exception ex) {
239-
logger.error("Could not generate feed", ex);
240-
}
241-
return ResponseEntity.ok().
242-
contentType(MediaType.APPLICATION_ATOM_XML).
243-
cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)).
244-
eTag(Utils.md5(feed)).
245-
body(feed);
246-
}
247-
248-
private SyndFeed getFeed(HttpServletRequest req) throws IOException, FeedException {
226+
@GetMapping(path = "/feed.xml", produces = "application/rss+xml")
227+
public String feed(Model model, HttpServletRequest req, HttpServletResponse res) {
228+
// [space query filter] + original query string
229+
String qs = utils.sanitizeQueryString("*", req);
249230
boolean canList = utils.isDefaultSpacePublic() || utils.isAuthenticated(req);
250-
List<Post> questions = canList ? utils.fullQuestionsSearch("*") : Collections.emptyList();
251-
List<SyndEntry> entries = new ArrayList<SyndEntry>();
231+
List<Post> questions = canList ? utils.fullQuestionsSearch(qs) : Collections.emptyList();
232+
List<Map<String, String>> entriez = new LinkedList<>();
233+
Map<String, String> lang = utils.getLang(req);
252234
String baseurl = CONF.serverUrl() + CONF.serverContextPath();
253235
baseurl = baseurl.endsWith("/") ? baseurl : baseurl + "/";
254236

255-
Map<String, String> lang = utils.getLang(req);
256-
257-
SyndFeed feed = new SyndFeedImpl();
258-
feed.setFeedType("atom_1.0");
259-
feed.setTitle(Utils.formatMessage(lang.get("feed.title"), CONF.appName()));
260-
feed.setLink(baseurl);
261-
feed.setDescription(Utils.formatMessage(lang.get("feed.description"), CONF.appName()));
237+
model.addAttribute("title", Utils.formatMessage(lang.get("feed.title"), CONF.appName()));
238+
model.addAttribute("description", Utils.formatMessage(lang.get("feed.description"), CONF.appName()));
239+
model.addAttribute("baseurl", baseurl);
240+
model.addAttribute("updated", Utils.formatDate(Utils.timestamp(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
262241

263242
for (Post post : questions) {
264-
SyndEntry entry;
265-
SyndContent description;
266243
String baselink = baseurl.concat("question/").concat(post.getId());
267-
268-
entry = new SyndEntryImpl();
269-
entry.setTitle(post.getTitle());
270-
entry.setLink(baselink);
271-
entry.setPublishedDate(new Date(post.getTimestamp()));
272-
entry.setAuthor(baseurl.concat("profile/").concat(post.getCreatorid()));
273-
entry.setUri(baselink.concat("/").concat(Utils.stripAndTrim(post.getTitle()).
244+
Map<String, String> map = new HashMap<String, String>();
245+
map.put("url", baselink);
246+
map.put("title", post.getTitle());
247+
map.put("id", baselink.concat("/").concat(Utils.stripAndTrim(post.getTitle()).
274248
replaceAll("\\p{Z}+", "-").toLowerCase()));
275-
276-
description = new SyndContentImpl();
277-
description.setType("text/html");
278-
description.setValue(Utils.markdownToHtml(post.getBody()));
279-
280-
entry.setDescription(description);
281-
entries.add(entry);
249+
map.put("created", Utils.formatDate(post.getTimestamp(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
250+
map.put("updated", Utils.formatDate(post.getUpdated(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
251+
map.put("author", baseurl.concat("profile/").concat(post.getCreatorid()));
252+
map.put("body", StringUtils.removeEnd(Utils.markdownToHtml(post.getBody()), "\n"));
253+
entriez.add(map);
282254
}
283-
feed.setEntries(entries);
284-
return feed;
255+
model.addAttribute("entries", entriez);
256+
res.setCharacterEncoding("UTF-8");
257+
res.setContentType("application/rss+xml");
258+
res.addHeader("Cache-Control", "max-age=3600");
259+
return "feed";
285260
}
286261

287262
@ResponseBody
@@ -303,7 +278,7 @@ public ResponseEntity<String> sitemap(HttpServletRequest req) {
303278
body(sitemap);
304279
}
305280

306-
private String getSitemap(HttpServletRequest req) throws IOException, FeedException {
281+
private String getSitemap(HttpServletRequest req) throws IOException {
307282
boolean canList = utils.isDefaultSpacePublic() || utils.isAuthenticated(req);
308283
if (canList) {
309284
List<Post> questions = new LinkedList<>();

Diff for: src/main/resources/templates/feed.vm

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
2+
<channel>
3+
<link>${baseurl}feed.xml</link>
4+
<title>$title</title>
5+
<description>$description</description>
6+
<lastBuildDate>$updated</lastBuildDate>
7+
<atom:link href="${baseurl}feed.xml" rel="self" type="application/rss+xml" />
8+
#foreach($entry in $entries)
9+
<item>
10+
<guid isPermaLink="true">$entry.id</guid>
11+
<title>$entry.title</title>
12+
<description>$entry.body</description>
13+
<link>$entry.url</link>
14+
<pubDate>$entry.created</pubDate>
15+
</item>
16+
#end
17+
</channel>
18+
</rss>

0 commit comments

Comments
 (0)