Skip to content

Commit dfa3022

Browse files
committed
Add code to generate a sitemap
1 parent c77c3ba commit dfa3022

16 files changed

Lines changed: 417 additions & 17 deletions

File tree

‎gradle/dependencies.gradle‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ ext {
2222
hashids : '1.0.1', // Id hashing
2323
failsafe : '1.0.4', // retry and circuit breakers
2424
jsoup : '1.10.3', // DOM parsing library
25-
lombok : '1.16.16', // Code gen
25+
lombok : '1.16.16', // Code gen
26+
sitemapgen4j : '1.0.6', // Sitemap generator for SEO
2627

2728
junit : '4.12', // Unit Testing
2829
]
@@ -66,6 +67,7 @@ ext {
6667
failsafe : "net.jodah:failsafe:$versions.failsafe",
6768
jsoup : "org.jsoup:jsoup:$versions.jsoup",
6869
lombok : "org.projectlombok:lombok:$versions.lombok",
70+
sitemapgen4j : "com.github.dfabulich:sitemapgen4j:$versions.sitemapgen4j",
6971

7072
junit : "junit:junit:$versions.junit",
7173
]

‎stubbornjava-common/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232
compile libs.s3
3333
compile libs.failsafe
3434
compile libs.jsoup
35+
compile libs.sitemapgen4j
3536

3637
testCompile libs.junit
3738
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.stubbornjava.common;
2+
3+
import java.util.List;
4+
5+
import okhttp3.HttpUrl;
6+
7+
public class HttpUrls {
8+
9+
public static HttpUrl host(HttpUrl url) {
10+
List<String> pathSegments = url.pathSegments();
11+
HttpUrl.Builder urlBuilder = url.newBuilder();
12+
for (int i = pathSegments.size() - 1; i >= 0; i--) {
13+
urlBuilder.removePathSegment(i);
14+
}
15+
urlBuilder.query(null);
16+
return urlBuilder.build();
17+
}
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.stubbornjava.common.seo;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.function.Supplier;
8+
9+
import org.jooq.lambda.Seq;
10+
11+
import com.google.common.base.Suppliers;
12+
import com.google.common.collect.Maps;
13+
14+
// {{start:inMemorySitemap}}
15+
public class InMemorySitemap {
16+
private final Supplier<Map<String, String>> indexSupplier;
17+
private InMemorySitemap(Supplier<Map<String, String>> indexSupplier) {
18+
this.indexSupplier = indexSupplier;
19+
}
20+
21+
public String getIndex(String sitemapName) {
22+
return indexSupplier.get().get(sitemapName);
23+
}
24+
25+
public List<String> getIndexNames() {
26+
return Seq.seq(indexSupplier.get().keySet())
27+
.sorted()
28+
.toList();
29+
}
30+
31+
// Cache the sitemap for the lifetime of the JVM
32+
public static InMemorySitemap fromSupplier(Supplier<Map<String, List<String>>> supplier) {
33+
Supplier<Map<String, String>> sup = mapSupplier(supplier);
34+
Supplier<Map<String, String>> memoized = Suppliers.memoize(sup::get);
35+
return new InMemorySitemap(memoized);
36+
}
37+
38+
// Cache the sitemap but refresh after the given duration.
39+
public static InMemorySitemap fromSupplierWithExpiration(
40+
Supplier<Map<String, List<String>>> supplier,
41+
long duration,
42+
TimeUnit unit) {
43+
Supplier<Map<String, String>> sup = mapSupplier(supplier);
44+
Supplier<Map<String, String>> memoized = Suppliers.memoizeWithExpiration(sup::get, duration, unit);
45+
return new InMemorySitemap(memoized);
46+
}
47+
48+
private static Supplier<Map<String, String>> mapSupplier(Supplier<Map<String, List<String>>> supplier) {
49+
return () -> {
50+
Map<String, List<String>> originalMap = supplier.get();
51+
Map<String, String> newIndex = Maps.newHashMap();
52+
for (Entry<String, List<String>> entry : originalMap.entrySet()) {
53+
for (int i = 0; i < entry.getValue().size(); i++) {
54+
newIndex.put(entry.getKey() + "-" + i + ".xml", entry.getValue().get(i));
55+
}
56+
}
57+
return newIndex;
58+
};
59+
}
60+
}
61+
// {{end:inMemorySitemap}}
62+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.stubbornjava.common.seo;
2+
3+
import static com.stubbornjava.common.undertow.handlers.CustomHandlers.timed;
4+
5+
import com.stubbornjava.common.undertow.Exchange;
6+
7+
import io.undertow.server.HttpServerExchange;
8+
import io.undertow.server.RoutingHandler;
9+
10+
// {{start:SitemapRoutes}}
11+
public class SitemapRoutes {
12+
private final InMemorySitemap sitemap;
13+
private SitemapRoutes(InMemorySitemap sitemap) {
14+
this.sitemap = sitemap;
15+
}
16+
17+
public void getSitemap(HttpServerExchange exchange) {
18+
String sitemapName = Exchange.pathParams().pathParam(exchange, "sitemap").orElse(null);
19+
String content = sitemap.getIndex(sitemapName);
20+
if (null == content) {
21+
exchange.setStatusCode(404);
22+
Exchange.body().sendText(exchange, String.format("Sitemap %s doesn't exist", sitemapName));
23+
return;
24+
}
25+
Exchange.body().sendXml(exchange, content);
26+
}
27+
28+
/*
29+
* Routing Handlers can be reused and combined with each other
30+
* using the RoutingHandler.addAll() method.
31+
*/
32+
public static RoutingHandler router(InMemorySitemap sitemap) {
33+
SitemapRoutes routes = new SitemapRoutes(sitemap);
34+
RoutingHandler router = new RoutingHandler()
35+
.get("/sitemaps/{sitemap}", timed("getSitemap", routes::getSitemap))
36+
;
37+
return router;
38+
}
39+
}
40+
// {{end:SitemapRoutes}}
41+

‎stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/Urls.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package com.stubbornjava.common.undertow;
2+
import com.stubbornjava.common.HttpUrls;
3+
24
import io.undertow.server.HttpServerExchange;
35
import okhttp3.HttpUrl;
46

@@ -14,4 +16,9 @@ default HttpUrl currentUrl(HttpServerExchange exchange) {
1416
return urlBuilder.build();
1517
}
1618
// {{end:currentUrl}}
19+
20+
default HttpUrl host(HttpServerExchange exchange) {
21+
HttpUrl url = HttpUrl.parse(exchange.getRequestURL());
22+
return HttpUrls.host(url);
23+
}
1724
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.stubbornjava.common;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import okhttp3.HttpUrl;
8+
9+
public class HttpUrlsTest {
10+
11+
@Test
12+
public void testDomain() {
13+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com");
14+
HttpUrl actual = HttpUrls.host(url);
15+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
16+
17+
assertEquals(expected, actual);
18+
}
19+
20+
@Test
21+
public void testDomainTrailingSlash() {
22+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com/");
23+
HttpUrl actual = HttpUrls.host(url);
24+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
25+
26+
assertEquals(expected, actual);
27+
}
28+
29+
@Test
30+
public void testDomainWithPath() {
31+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com/path1");
32+
HttpUrl actual = HttpUrls.host(url);
33+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
34+
35+
assertEquals(expected, actual);
36+
}
37+
38+
@Test
39+
public void testDomainWithPaths() {
40+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com/path1/path2");
41+
HttpUrl actual = HttpUrls.host(url);
42+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
43+
44+
assertEquals(expected, actual);
45+
}
46+
47+
@Test
48+
public void testDomainWithEmptyQueryString() {
49+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com?");
50+
HttpUrl actual = HttpUrls.host(url);
51+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
52+
53+
assertEquals(expected, actual);
54+
}
55+
56+
@Test
57+
public void testDomainWithQueryString() {
58+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com?x=1");
59+
HttpUrl actual = HttpUrls.host(url);
60+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
61+
62+
assertEquals(expected, actual);
63+
}
64+
65+
@Test
66+
public void testDomainWithPathsAndQueryString() {
67+
HttpUrl url = HttpUrl.parse("http://www.stubbornjava.com/path1/path2?x=1");
68+
HttpUrl actual = HttpUrls.host(url);
69+
HttpUrl expected = HttpUrl.parse("http://www.stubbornjava.com");
70+
71+
assertEquals(expected, actual);
72+
}
73+
}

‎stubbornjava-undertow/src/main/java/com/stubbornjava/undertow/exchange/ContentTypeSenders.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ default void sendJson(HttpServerExchange exchange, byte[] bytes) {
1717
exchange.getResponseSender().send(ByteBuffer.wrap(bytes));
1818
}
1919

20+
default void sendXml(HttpServerExchange exchange, String xml) {
21+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/xml");
22+
exchange.getResponseSender().send(xml);
23+
}
24+
2025
default void sendHtml(HttpServerExchange exchange, String html) {
2126
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
2227
exchange.getResponseSender().send(html);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.stubbornjava.webapp;
22

3+
import java.util.List;
4+
35
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
57

@@ -65,7 +67,11 @@ public static void home(HttpServerExchange exchange) {
6567
}
6668

6769
public static void robots(HttpServerExchange exchange) {
68-
Response response = Response.fromExchange(exchange);
70+
String host = Exchange.urls().host(exchange).toString();
71+
List<String> sitemaps = StubbornJavaSitemapGenerator.getSitemap().getIndexNames();
72+
Response response = Response.fromExchange(exchange)
73+
.with("sitemaps", sitemaps)
74+
.with("host", host);
6975
Exchange.body().sendText(exchange, Templating.instance().renderTemplate("templates/src/pages/robots.txt", response));
7076
}
7177

0 commit comments

Comments
 (0)