Skip to content

Commit 4b3bb56

Browse files
committed
Remove contentful and stubbornjava-private reference
1 parent 64ab7a0 commit 4b3bb56

9 files changed

Lines changed: 110 additions & 94 deletions

File tree

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ allprojects {
2121

2222
// Using Jitpack so I need the repo name in the group to match.
2323
group = 'com.stubbornjava.StubbornJava'
24-
version = '0.1.17-SNAPSHOT'
24+
version = '0.1.18-SNAPSHOT'
2525

2626
repositories {
2727
mavenLocal()

‎gradle/dependencies.gradle‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ ext {
1515
jool : '0.9.12', // Functional Utils
1616
hsqldb : '2.3.4', // In memory SQL db
1717
aws : '1.11.98', // AWS Java SDK
18-
contentful : '7.4.0', // Contentful java sdk
1918
flyway : '4.1.2', // DB migrations
2019
connectorj : '5.1.41', // JDBC MYSQL driver
2120
jooq : '3.9.1', // jOOQ
@@ -58,7 +57,6 @@ ext {
5857
jool : "org.jooq:jool:$versions.jool",
5958
hsqldb : "org.hsqldb:hsqldb:$versions.hsqldb",
6059
s3 : "com.amazonaws:aws-java-sdk-s3:$versions.aws",
61-
contentful : "com.contentful.java:java-sdk:$versions.contentful",
6260
flyway : "org.flywaydb:flyway-core:$versions.flyway",
6361
connectorj : "mysql:mysql-connector-java:$versions.connectorj",
6462
jooq : "org.jooq:jooq:$versions.jooq",

‎settings.gradle‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ include ':stubbornjava-common'
66
include ':stubbornjava-examples'
77
include ':stubbornjava-webapp'
88

9-
def rootProjectDescriptor = settings.rootProject
10-
settings.createProjectDescriptor(rootProjectDescriptor, 'stubbornjava-private', file('../stubbornjava'))
9+
//def rootProjectDescriptor = settings.rootProject
10+
//settings.createProjectDescriptor(rootProjectDescriptor, 'stubbornjava-private', file('../stubbornjava'))
1111
// {{end:settings}}

‎stubbornjava-common/src/main/java/com/stubbornjava/common/Templating.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import com.github.jknack.handlebars.io.FileTemplateLoader;
2020
import com.github.jknack.handlebars.io.TemplateLoader;
2121
import com.google.common.collect.Lists;
22+
import com.google.common.collect.Maps;
2223
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
2324
// {{start:templating}}
2425
public class Templating {
2526
private static final Logger log = LoggerFactory.getLogger(Templating.class);
27+
private static final Map<Object, Object> NO_DATA = Maps.newHashMapWithExpectedSize(0);
2628

2729
// Once again using static for convenience use your own DI method.
2830
private static final Templating DEFAULT;
@@ -54,11 +56,19 @@ public static Templating instance() {
5456
this.handlebars = handlebars;
5557
}
5658

59+
public String renderHtmlTemplate(String templateName) {
60+
return renderHtmlTemplate(templateName, NO_DATA);
61+
}
62+
5763
public String renderHtmlTemplate(String templateName, Object data) {
5864
String response = renderTemplate(templateName, data);
5965
return compressor.compress(response);
6066
}
6167

68+
public String renderTemplate(String templateName) {
69+
return renderTemplate(templateName, NO_DATA);
70+
}
71+
6272
public String renderTemplate(String templateName, Object data) {
6373
Template template;
6474
try {
@@ -69,11 +79,19 @@ public String renderTemplate(String templateName, Object data) {
6979
return render(template, data);
7080
}
7181

82+
public String renderRawHtmlTemplate(String rawTemplate) {
83+
return renderRawTemplate(rawTemplate, NO_DATA);
84+
}
85+
7286
public String renderRawHtmlTemplate(String rawTemplate, Object data) {
7387
String response = renderRawTemplate(rawTemplate, data);
7488
return compressor.compress(response);
7589
}
7690

91+
public String renderRawTemplate(String rawTemplate) {
92+
return renderRawTemplate(rawTemplate, NO_DATA);
93+
}
94+
7795
public String renderRawTemplate(String rawTemplate, Object data) {
7896
Template template;
7997
try {

‎stubbornjava-webapp/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dependencies {
33
// Project reference
44
compile project(':stubbornjava-undertow')
55
compile project(':stubbornjava-common')
6-
compile project(':stubbornjava-private')
6+
//compile project(':stubbornjava-private')
77

88
compile libs.lombok
99

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.stubbornjava.common.Metrics;
77
import com.stubbornjava.common.undertow.Exchange;
8-
import com.stubbornjava.utils.Logging;
98

109
import io.undertow.server.HttpServerExchange;
1110

@@ -16,10 +15,10 @@ public static void getMetrics(HttpServerExchange exchange) {
1615
Exchange.body().sendJson(exchange, Metrics.registry());
1716
}
1817

19-
public static void getLoggers(HttpServerExchange exchange) {
20-
Response data = Response.fromExchange(exchange).with("loggers", Logging.getLoggers());
21-
Exchange.body().sendHtmlTemplate(exchange, "templates/loggers", data);
22-
}
18+
// public static void getLoggers(HttpServerExchange exchange) {
19+
// Response data = Response.fromExchange(exchange).with("loggers", Logging.getLoggers());
20+
// Exchange.body().sendHtmlTemplate(exchange, "templates/loggers", data);
21+
// }
2322

2423
// public static void getProperties(HttpServerExchange exchange) {
2524
// boolean hidden = RequestUtil.getQueryParam(exchange, "hidden")

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/guide/Guides.java‎

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,50 @@
22

33
import java.util.List;
44
import java.util.Map;
5-
import java.util.concurrent.TimeUnit;
6-
import java.util.stream.Collectors;
75

8-
import com.contentful.java.cda.CDAEntry;
9-
import com.google.common.cache.CacheBuilder;
10-
import com.google.common.cache.CacheLoader;
11-
import com.google.common.cache.LoadingCache;
12-
import com.google.common.collect.Maps;
6+
import org.jooq.lambda.Seq;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import com.google.common.collect.ImmutableList;
11+
import com.stubbornjava.common.Json;
1312
import com.stubbornjava.common.Templating;
14-
import com.stubbornjava.webapp.ContentfulClient;
13+
import com.stubbornjava.webapp.post.SlugUtil;
1514

1615
public class Guides {
16+
private static final Logger log = LoggerFactory.getLogger(Guides.class);
17+
1718
private Guides() {}
18-
private static final String contentType = "guide";
19-
private static final LoadingCache<String, Guide> guideCache = CacheBuilder.newBuilder()
20-
.maximumSize(250)
21-
.expireAfterWrite(30, TimeUnit.MINUTES)
22-
.build(CacheLoader.from(Guides::loadGuide));
23-
private static final LoadingCache<String, List<GuideTitle>> guideTitlesCache = CacheBuilder.newBuilder()
24-
.maximumSize(1)
25-
.expireAfterWrite(30, TimeUnit.MINUTES)
26-
.build(CacheLoader.from(() -> Guides.loadTitles()));
19+
private static final Map<String, Guide> guideSlugIndex;
20+
private static final List<GuideTitle> titles;
21+
static {
22+
List<Guide> guides = Seq.of(
23+
buildGuide("Embedded Java Web Server")
24+
).toList();
25+
26+
guideSlugIndex = Seq.seq(guides).toMap(Guide::getSlug);
27+
titles = ImmutableList.copyOf(Seq.seq(guides).map(g -> new GuideTitle(g.getTitle(), g.getSlug())).toList());
28+
}
2729

28-
public static List<GuideTitle> findTitles() {
29-
return guideTitlesCache.getUnchecked("instance");
30+
private static Guide buildGuide(String title) {
31+
String slug = SlugUtil.toSlug(title);
32+
String content = Templating.instance().renderTemplate("templates/src/guides/" + slug);
33+
Guide guide = new Guide(title, slug, content);
34+
return guide;
3035
}
3136

32-
private static List<GuideTitle> loadTitles() {
33-
return ContentfulClient.dsl()
34-
.withContentType(contentType)
35-
.select("fields.title", "fields.slug")
36-
.all()
37-
.entries()
38-
.values()
39-
.stream()
40-
.map(Guides::guideTitleFromEntry)
41-
.collect(Collectors.toList());
37+
public static List<GuideTitle> findTitles() {
38+
return titles;
4239
}
4340

4441
public static Guide findBySlug(String slug) {
4542
if (null == slug) {
4643
return null;
4744
}
48-
return guideCache.getUnchecked(slug);
49-
}
50-
51-
private static Guide loadGuide(String slug) {
52-
Guide guide = ContentfulClient.dsl()
53-
.withContentType(contentType)
54-
.where("fields.slug", slug)
55-
.all()
56-
.entries()
57-
.values()
58-
.stream()
59-
.findFirst()
60-
.map(Guides::guideFromEntry).orElse(null);
61-
return guide;
62-
}
63-
64-
private static Guide guideFromEntry(CDAEntry entry) {
65-
String title = entry.getField("title");
66-
String slug = entry.getField("slug");
67-
String bodyMarkdown = entry.getField("body");
68-
Map<String, String> data = Maps.newHashMapWithExpectedSize(1);
69-
data.put("data", bodyMarkdown);
70-
String bodyHtml = Templating.instance().renderRawHtmlTemplate("{{md data}}", data);
71-
return new Guide(title, slug, bodyHtml);
72-
}
73-
74-
private static GuideTitle guideTitleFromEntry(CDAEntry entry) {
75-
String title = entry.getField("title");
76-
String slug = entry.getField("slug");
77-
return new GuideTitle(title, slug);
45+
return guideSlugIndex.get(slug);
7846
}
7947

8048
public static void main(String[] args) {
81-
findBySlug("webserver-guide");
49+
log.debug(Json.serializer().toPrettyString(findBySlug("embedded-java-web-server")));
8250
}
8351
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{{#assign "markdown"}}
2+
This guide is meant to be a resource for creating lightweight web servers in Java without a framework. You are expected to have Java and HTTP experience. It will cover the basics from a simple hello world server to a functional server side rendered website as well as a JSON web service. Along the way it will cover HTTP routing, verbs, query parameters, path parameters, headers, cookies, form data, json, logging and debugging techniques. Most topics will be split out into their own posts and this will serve as a directory / recommended reading order.
3+
4+
We will be using several libraries from our [Recommended Java Libraries](https://www.stubbornjava.com/java-libraries "Recommended Java Libraries"). If there are any topics missing you would like covered [Suggest a topic](https://github.com/StubbornJava/StubbornJava#suggest-a-topic). To see how dependencies are managed check out [multi-project builds with gradle](https://www.stubbornjava.com/posts/multi-project-builds-with-gradle-and-fat-jars-with-shadow). This site is built with these exact same techniques and a lot of the shared classes.
5+
6+
## Hello World
7+
For the impatient let's get a [minimal hello world](https://www.stubbornjava.com/posts/java-hello-world-embedded-http-server-using-undertow) up and running quickly. For whatever reason some developers like to see how few lines it takes to start a server. Let's see what that looks like.
8+
9+
<pre class="line-numbers"><code class="language-java">public static void main(String[] args) {
10+
Undertow.builder()
11+
.addHttpListener(8080, "localhost")
12+
.setHandler((exchange) -> exchange.getResponseSender().send("hello world"))
13+
.build().start();
14+
}</code></pre>
15+
16+
If we really wanted this could be a one liner. You can also pass the `HttpHandler` into the `addHttpListener` method as a third argument. This is required if you want different handlers to run on different ports.
17+
18+
## Logging
19+
Why logging? Logging is a critical tool for understanding what is happening and extremely useful for debugging. It also happens to be overlooked quite frequently. When projects are several years old without proper logging you can't even compute the developer hours wasted debugging issues.
20+
21+
- [Quick intro to SLF4J and Logback](https://www.stubbornjava.com/posts/logging-in-java-with-slf4j-and-logback)
22+
- [Access / Request logging with undertow](https://www.stubbornjava.com/posts/http-access-logging-with-undertow)
23+
24+
## Undertow HttpHandlers
25+
`HttpHandler`'s are the building blocks of Undertow. They can be used to send responses, change status codes, log, meter / time, handle exceptions and much more. Undertow's simplicity is the main reason we use it. Try looking at mose frameworks and many of the concepts listed above have their own object models. Filtering / Interceptors often have strange APIs that only work before or after a request and you need to add both if you want to wrap a request. Exception handlers sometimes follow very different APIs and you don't have access to the same data you would expect. Undertow is simple. Most building blocks are simply HttpHandlers. Handlers are meant to be composed together. This allows you to use as little or as much functionality as you need. Does your web service have a special case where it is only a single route? Cut out the routing logic you don't need it!
26+
27+
- [Creating custom HttpHandlers in Undertow](https://www.stubbornjava.com/posts/undertow-writing-custom-httphandlers) - This showcases a few different ways you can implement an `HttpHandler` thanks to Java8.
28+
- [Http Routing in Undertow](https://www.stubbornjava.com/posts/url-routing-with-undertow-embedded-http-server) - Undertow comes with many built in `HttpHandlers` the `RoutingHandler` provides a convienent way to group a HTTP verb with a URL template.
29+
- [Handling Exceptions in Undertow](https://www.stubbornjava.com/posts/handling-exceptions-in-undertow-with-composition)
30+
- [Middleware in Undertow](https://www.stubbornjava.com/posts/logging-gzip-blocking-exception-handling-metrics-middleware-chaining-in-undertow) - Logging, metrics, error handling, authentication and many other features are built as filters / interceptors. In Undertow we simply compose all of the logic we need and let each individual handler do its job.
31+
32+
## Request Parameters
33+
So far most of our handlers have been fairly static. We want some dynamic content, let's explore using out HTTP parameters.
34+
35+
- [Query and Path Parameters](/posts/query-parameters-and-path-parameters-in-undertow) - `/users/{pathParam}?sortBy=queryParam`
36+
- [Obfuscating and Shortening Sequential ids with HashIds](/posts/obfuscating-and-shortening-sequential-ids-with-hashids)
37+
38+
## Sending Responses
39+
HTTP responses have some extremely crucial data. Status codes, content types, setting cookies, sending headers and much more. Often times large frameworks try to abstract this away from you by sending different `Entity` types back as the response. We are big boys here HTTP doesn't scare us.
40+
41+
- [Sending Content Types with Undertow](https://www.stubbornjava.com/posts/handling-content-types-with-undertow)
42+
- [Sending HTTP Redirects with Undertow](https://www.stubbornjava.com/posts/http-redirects-with-undertow)
43+
44+
## JSON Web Service
45+
We won't call it REST because we are not sticklers on naming / conventions. We use Jackson for all JSON serialization.
46+
47+
- [Simple Jackson ObjectMapper Configuration](https://www.stubbornjava.com/posts/practical-jackson-objectmapper-configuration)
48+
- [Simple in memory REST (ehhhh web) Service](https://www.stubbornjava.com/posts/lightweight-embedded-java-rest-server-without-a-framework)
49+
- [OkHttp Example REST Client](https://www.stubbornjava.com/posts/okhttp-example-rest-client)
50+
51+
## Server Side HTML with Handlebars
52+
SPAs are all the rage these days. However you can't beat the complicity and speed of building server side rendered web apps (Especially if you are a server side developer and aren't a js guru). SPAs make sense in a lot of use cases. However, they don't make sense in all cases. There is nothing wrong with going back to our roots.
53+
54+
- [HTML Templating with Handlebars and Undertow](https://www.stubbornjava.com/posts/java-html-templating-with-handlebars-and-undertow)
55+
{{/assign}}
56+
{{md markdown}}

0 commit comments

Comments
 (0)