Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd21c6fa

Browse files
authored
Add an rss feed (#98)
1 parent8e60e06 commitd21c6fa

File tree

9 files changed

+99
-3
lines changed

9 files changed

+99
-3
lines changed

‎gradle/dependencies.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ext {
2424
lombok :'1.18.2',// Code gen
2525
sitemapgen4j :'1.0.6',// Sitemap generator for SEO
2626
jbcrypt :'0.4',// BCrypt salted hashing library
27+
romeRss :'1.0',// RSS Library
2728

2829
junit :'4.12',// Unit Testing
2930
]
@@ -69,6 +70,7 @@ ext {
6970
lombok :"org.projectlombok:lombok:$versions.lombok",
7071
sitemapgen4j :"com.github.dfabulich:sitemapgen4j:$versions.sitemapgen4j",
7172
jbcrypt :"org.mindrot:jbcrypt:$versions.jbcrypt",
73+
romeRss :"rome:rome:$versions.romeRss",
7274

7375
junit :"junit:junit:$versions.junit",
7476
]

‎stubbornjava-webapp/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
compile project(':stubbornjava-cms-server')
99

1010
compile libs.lombok
11+
compile libs.romeRss
1112

1213
testCompile libs.junit
1314
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagecom.stubbornjava.webapp;
2+
3+
publicclassSiteUrls {
4+
5+
publicstaticStringpostUrl(Stringslug) {
6+
returnString.format("/posts/%s",slug);
7+
}
8+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packagecom.stubbornjava.webapp;
2+
3+
importjava.io.StringWriter;
4+
importjava.time.ZoneId;
5+
importjava.util.Date;
6+
importjava.util.List;
7+
8+
importorg.jooq.lambda.Seq;
9+
importorg.jooq.lambda.Unchecked;
10+
11+
importcom.stubbornjava.common.undertow.Exchange;
12+
importcom.stubbornjava.webapp.post.PostRaw;
13+
importcom.stubbornjava.webapp.post.Posts;
14+
importcom.sun.syndication.feed.synd.SyndContentImpl;
15+
importcom.sun.syndication.feed.synd.SyndEntry;
16+
importcom.sun.syndication.feed.synd.SyndEntryImpl;
17+
importcom.sun.syndication.feed.synd.SyndFeed;
18+
importcom.sun.syndication.feed.synd.SyndFeedImpl;
19+
importcom.sun.syndication.io.SyndFeedOutput;
20+
21+
importio.undertow.server.HttpServerExchange;
22+
importokhttp3.HttpUrl;
23+
24+
classStubbornJavaRss {
25+
26+
27+
publicstaticvoidgetRssFeed(HttpServerExchangeexchange) {
28+
HttpUrlhost =Exchange.urls().host(exchange);
29+
Exchange.body().sendXml(exchange,getFeed(host));
30+
}
31+
32+
privatestaticStringgetFeed(HttpUrlhost) {
33+
SyndFeedfeed =newSyndFeedImpl();
34+
feed.setFeedType("rss_2.0");
35+
feed.setTitle("StubbornJava");
36+
feed.setLink(host.toString());
37+
feed.setDescription("Unconventional guides, examples, and blog utilizing modern Java");
38+
39+
List<PostRaw>posts =Posts.getAllRawPosts();
40+
List<SyndEntry>entries =Seq.seq(posts)
41+
.map(p -> {
42+
SyndEntryentry =newSyndEntryImpl();
43+
entry.setTitle(p.getTitle());
44+
entry.setLink(host.newBuilder().encodedPath(p.getUrl()).build().toString());
45+
entry.setPublishedDate(Date.from(p.getDateCreated()
46+
.toLocalDate()
47+
.atStartOfDay(ZoneId.systemDefault())
48+
.toInstant()));
49+
entry.setUpdatedDate(Date.from(p.getDateUpdated()
50+
.toLocalDate()
51+
.atStartOfDay(ZoneId.systemDefault())
52+
.toInstant()));
53+
SyndContentImpldescription =newSyndContentImpl();
54+
description.setType("text/plain");
55+
description.setValue(p.getMetaDesc());
56+
entry.setDescription(description);
57+
returnentry;
58+
}).toList();
59+
feed.setEntries(entries);
60+
61+
StringWriterwriter =newStringWriter();
62+
SyndFeedOutputoutput =newSyndFeedOutput();
63+
64+
returnUnchecked.supplier(() -> {
65+
output.output(feed,writer);
66+
returnwriter.toString();
67+
}).get();
68+
}
69+
}

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaSitemapGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
importokhttp3.HttpUrl;
2121

2222
// {{start:sitemapgen}}
23-
publicclassStubbornJavaSitemapGenerator {
23+
classStubbornJavaSitemapGenerator {
2424
privatestaticfinalStringHOST ="https://www.stubbornjava.com";
2525

2626
privatestaticfinalInMemorySitemapsitemap =InMemorySitemap.fromSupplier(StubbornJavaSitemapGenerator::generateSitemap);

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaWebApp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private static final HttpHandler getBasicRoutes() {
8787

8888
.get("/dev/metrics",timed("getMetrics",HelperRoutes::getMetrics))
8989

90+
.get("/rss/feed",StubbornJavaRss::getRssFeed)
9091
// addAll allows you to combine more than one RoutingHandler together.
9192
.addAll(SitemapRoutes.router(StubbornJavaSitemapGenerator.getSitemap()))
9293

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/PostRaw.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importjava.util.List;
55

66
importcom.stubbornjava.common.Slugs;
7+
importcom.stubbornjava.webapp.SiteUrls;
78
importcom.stubbornjava.webapp.github.FileReference;
89

910
importlombok.Builder;
@@ -27,4 +28,8 @@ public class PostRaw {
2728
publicStringgetSlug() {
2829
returnSlugs.toSlug(title);
2930
}
31+
32+
publicStringgetUrl() {
33+
returnSiteUrls.postUrl(getSlug());
34+
}
3035
}

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/Posts.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Posts {
3030

3131
recentPosts =Seq.seq(posts)
3232
.map(Posts::metaFromPost)
33-
.sorted(p ->p.getDateCreated(),Comparator.reverseOrder())
33+
.sorted(PostMeta::getDateCreated,Comparator.reverseOrder())
3434
.toList();
3535

3636
for (PostMetapost:recentPosts) {
@@ -90,7 +90,16 @@ public static Post findBySlug(String slug) {
9090
}
9191

9292
publicstaticList<String>getAllSlugs() {
93-
returnSeq.seq(slugIndex.keySet()).sorted().toList();
93+
returnSeq.seq(slugIndex.values())
94+
.sorted(PostRaw::getDateCreated,Comparator.reverseOrder())
95+
.map(PostRaw::getSlug)
96+
.toList();
97+
}
98+
99+
publicstaticList<PostRaw>getAllRawPosts() {
100+
returnSeq.seq(slugIndex.values())
101+
.sorted(PostRaw::getDateCreated,Comparator.reverseOrder())
102+
.toList();
94103
}
95104

96105
privatestaticList<TagOrLibrary>findFromIndex(Multimap<String,PostMeta>index,Typetype) {

‎stubbornjava-webapp/ui/src/widgets/nav/header.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
<divclass="followus">Follow</div>
2323
<ahref="https://twitter.com/StubbornJava"><iclass="fa fa-twitter"aria-hidden="true"></i></a>
2424
<ahref="https://github.com/StubbornJava/StubbornJava"><iclass="fa fa-github"aria-hidden="true"></i></a>
25+
<ahref="/rss/feed"><iclass="fa fa-rss"aria-hidden="true"></i></a>
2526
</div>
2627
</nav>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp