Skip to content

Commit

Permalink
removed Rome dependency and upgraded RSS feed to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Jun 9, 2024
1 parent 8bd1d17 commit 4ccc760
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 58 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@
</dependency>-->

<!-- MISC -->
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
Expand Down
81 changes: 28 additions & 53 deletions src/main/java/com/erudika/scoold/controllers/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@
import com.erudika.scoold.utils.ScooldUtils;
import com.redfin.sitemapgenerator.WebSitemapGenerator;
import com.redfin.sitemapgenerator.WebSitemapUrl;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -55,6 +48,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -229,59 +223,40 @@ public ResponseEntity<String> webmanifest(HttpServletRequest req) {
body(json);
}

@ResponseBody
@GetMapping("/feed.xml")
public ResponseEntity<String> feed(HttpServletRequest req) {
String feed = "";
try {
feed = new SyndFeedOutput().outputString(getFeed(req));
} catch (Exception ex) {
logger.error("Could not generate feed", ex);
}
return ResponseEntity.ok().
contentType(MediaType.APPLICATION_ATOM_XML).
cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)).
eTag(Utils.md5(feed)).
body(feed);
}

private SyndFeed getFeed(HttpServletRequest req) throws IOException, FeedException {
@GetMapping(path = "/feed.xml", produces = "application/rss+xml")
public String feed(Model model, HttpServletRequest req, HttpServletResponse res) {
// [space query filter] + original query string
String qs = utils.sanitizeQueryString("*", req);
boolean canList = utils.isDefaultSpacePublic() || utils.isAuthenticated(req);
List<Post> questions = canList ? utils.fullQuestionsSearch("*") : Collections.emptyList();
List<SyndEntry> entries = new ArrayList<SyndEntry>();
List<Post> questions = canList ? utils.fullQuestionsSearch(qs) : Collections.emptyList();
List<Map<String, String>> entriez = new LinkedList<>();
Map<String, String> lang = utils.getLang(req);
String baseurl = CONF.serverUrl() + CONF.serverContextPath();
baseurl = baseurl.endsWith("/") ? baseurl : baseurl + "/";

Map<String, String> lang = utils.getLang(req);

SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("atom_1.0");
feed.setTitle(Utils.formatMessage(lang.get("feed.title"), CONF.appName()));
feed.setLink(baseurl);
feed.setDescription(Utils.formatMessage(lang.get("feed.description"), CONF.appName()));
model.addAttribute("title", Utils.formatMessage(lang.get("feed.title"), CONF.appName()));
model.addAttribute("description", Utils.formatMessage(lang.get("feed.description"), CONF.appName()));
model.addAttribute("baseurl", baseurl);
model.addAttribute("updated", Utils.formatDate(Utils.timestamp(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));

for (Post post : questions) {
SyndEntry entry;
SyndContent description;
String baselink = baseurl.concat("question/").concat(post.getId());

entry = new SyndEntryImpl();
entry.setTitle(post.getTitle());
entry.setLink(baselink);
entry.setPublishedDate(new Date(post.getTimestamp()));
entry.setAuthor(baseurl.concat("profile/").concat(post.getCreatorid()));
entry.setUri(baselink.concat("/").concat(Utils.stripAndTrim(post.getTitle()).
Map<String, String> map = new HashMap<String, String>();
map.put("url", baselink);
map.put("title", post.getTitle());
map.put("id", baselink.concat("/").concat(Utils.stripAndTrim(post.getTitle()).
replaceAll("\\p{Z}+", "-").toLowerCase()));

description = new SyndContentImpl();
description.setType("text/html");
description.setValue(Utils.markdownToHtml(post.getBody()));

entry.setDescription(description);
entries.add(entry);
map.put("created", Utils.formatDate(post.getTimestamp(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
map.put("updated", Utils.formatDate(post.getUpdated(), "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
map.put("author", baseurl.concat("profile/").concat(post.getCreatorid()));
map.put("body", StringUtils.removeEnd(Utils.markdownToHtml(post.getBody()), "\n"));
entriez.add(map);
}
feed.setEntries(entries);
return feed;
model.addAttribute("entries", entriez);
res.setCharacterEncoding("UTF-8");
res.setContentType("application/rss+xml");
res.addHeader("Cache-Control", "max-age=3600");
return "feed";
}

@ResponseBody
Expand All @@ -303,7 +278,7 @@ public ResponseEntity<String> sitemap(HttpServletRequest req) {
body(sitemap);
}

private String getSitemap(HttpServletRequest req) throws IOException, FeedException {
private String getSitemap(HttpServletRequest req) throws IOException {
boolean canList = utils.isDefaultSpacePublic() || utils.isAuthenticated(req);
if (canList) {
List<Post> questions = new LinkedList<>();
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/templates/feed.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<link>${baseurl}feed.xml</link>
<title>$title</title>
<description>$description</description>
<lastBuildDate>$updated</lastBuildDate>
<atom:link href="${baseurl}feed.xml" rel="self" type="application/rss+xml" />
#foreach($entry in $entries)
<item>
<guid isPermaLink="true">$entry.id</guid>
<title>$entry.title</title>
<description>$entry.body</description>
<link>$entry.url</link>
<pubDate>$entry.created</pubDate>
</item>
#end
</channel>
</rss>

0 comments on commit 4ccc760

Please sign in to comment.