37
37
import com .erudika .scoold .utils .ScooldUtils ;
38
38
import com .redfin .sitemapgenerator .WebSitemapGenerator ;
39
39
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 ;
48
40
import jakarta .inject .Inject ;
49
41
import jakarta .servlet .http .HttpServletRequest ;
42
+ import jakarta .servlet .http .HttpServletResponse ;
50
43
import java .io .IOException ;
51
44
import java .util .ArrayList ;
52
45
import java .util .Collections ;
55
48
import java .util .LinkedHashMap ;
56
49
import java .util .LinkedList ;
57
50
import java .util .List ;
51
+ import java .util .Locale ;
58
52
import java .util .Map ;
59
53
import java .util .Optional ;
60
54
import java .util .concurrent .TimeUnit ;
@@ -229,59 +223,40 @@ public ResponseEntity<String> webmanifest(HttpServletRequest req) {
229
223
body (json );
230
224
}
231
225
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 );
249
230
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 );
252
234
String baseurl = CONF .serverUrl () + CONF .serverContextPath ();
253
235
baseurl = baseurl .endsWith ("/" ) ? baseurl : baseurl + "/" ;
254
236
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 ));
262
241
263
242
for (Post post : questions ) {
264
- SyndEntry entry ;
265
- SyndContent description ;
266
243
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 ()).
274
248
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 );
282
254
}
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" ;
285
260
}
286
261
287
262
@ ResponseBody
@@ -303,7 +278,7 @@ public ResponseEntity<String> sitemap(HttpServletRequest req) {
303
278
body (sitemap );
304
279
}
305
280
306
- private String getSitemap (HttpServletRequest req ) throws IOException , FeedException {
281
+ private String getSitemap (HttpServletRequest req ) throws IOException {
307
282
boolean canList = utils .isDefaultSpacePublic () || utils .isAuthenticated (req );
308
283
if (canList ) {
309
284
List <Post > questions = new LinkedList <>();
0 commit comments