diff --git a/pom.xml b/pom.xml index fe9fdfe1..76fe5a53 100755 --- a/pom.xml +++ b/pom.xml @@ -111,11 +111,6 @@ --> - - rome - rome - 1.0 - org.apache.httpcomponents.core5 httpcore5 diff --git a/src/main/java/com/erudika/scoold/controllers/SearchController.java b/src/main/java/com/erudika/scoold/controllers/SearchController.java index 57931ecc..b3c2dd79 100755 --- a/src/main/java/com/erudika/scoold/controllers/SearchController.java +++ b/src/main/java/com/erudika/scoold/controllers/SearchController.java @@ -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; @@ -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; @@ -229,59 +223,40 @@ public ResponseEntity webmanifest(HttpServletRequest req) { body(json); } - @ResponseBody - @GetMapping("/feed.xml") - public ResponseEntity 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 questions = canList ? utils.fullQuestionsSearch("*") : Collections.emptyList(); - List entries = new ArrayList(); + List questions = canList ? utils.fullQuestionsSearch(qs) : Collections.emptyList(); + List> entriez = new LinkedList<>(); + Map lang = utils.getLang(req); String baseurl = CONF.serverUrl() + CONF.serverContextPath(); baseurl = baseurl.endsWith("/") ? baseurl : baseurl + "/"; - Map 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 map = new HashMap(); + 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 @@ -303,7 +278,7 @@ public ResponseEntity 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 questions = new LinkedList<>(); diff --git a/src/main/resources/templates/feed.vm b/src/main/resources/templates/feed.vm new file mode 100644 index 00000000..e0430a65 --- /dev/null +++ b/src/main/resources/templates/feed.vm @@ -0,0 +1,18 @@ + + + ${baseurl}feed.xml + $title + $description + $updated + + #foreach($entry in $entries) + + $entry.id + $entry.title + $entry.body + $entry.url + $entry.created + + #end + + \ No newline at end of file