Skip to content

Commit 1d90f5d

Browse files
committed
Major refactor
* Move Body to his own file * Split BodyConverter into Body.Parser and Body.Formatter * Move route handlers inside Route: Route.Handler and Route.Filter * Rename Variant to Mutant * Rename Viewable to View * Move TemplateProcessor inside View and rename * TemplateProcessor is now View.Engine * Template is now Viewable * Add name to View and Viewable * Add a few more unit tests
1 parent dc7365e commit 1d90f5d

File tree

71 files changed

+2689
-3308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2689
-3308
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@ quickstart
3737

3838
Just paste this into a terminal:
3939

40-
mvn archetype:generate -DgroupId=[app package] -DartifactId=[app name] -Dversion=[app version] -B -DarchetypeArtifactId=jooby-archetype -DarchetypeGroupId=org.jooby -DarchetypeVersion=0.1.0-SNAPSHOT
40+
mvn archetype:generate -B -DgroupId=com.mycompany -DartifactId=my-app -Dversion=1.0-SNAPSHOT \
41+
-DarchetypeArtifactId=jooby-archetype -DarchetypeGroupId=org.jooby \
42+
-DarchetypeVersion=0.1.0-SNAPSHOT
4143

42-
You must provide a value for:
44+
You might want to edit/change:
4345

44-
* -DgroupId: like ```com.mycompany```, ```org.myorg```, etc... It must be a valid Java package's name.
46+
* -DgroupId: A Java package's name
4547

46-
* -DartifactId: like ```my-app```, usually in lower case and without spaces.
48+
* -DartifactId: A project's name in lower case and without spaces
4749

48-
* -Dversion: like ```1.0-SNAPSHOT``` or ```1.0.0-SNAPSHOT```
50+
* -Dversion: A project's version, like ```1.0-SNAPSHOT``` or ```1.0.0-SNAPSHOT```
4951

5052

5153
Let's try it!:
5254

53-
mvn archetype:generate -B -DgroupId=com.mycompany -DartifactId=my-app -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=jooby-archetype -DarchetypeGroupId=org.jooby -DarchetypeVersion=0.1.0-SNAPSHOT
55+
mvn archetype:generate -B -DgroupId=com.mycompany -DartifactId=my-app -Dversion=1.0-SNAPSHOT \
56+
-DarchetypeArtifactId=jooby-archetype -DarchetypeGroupId=org.jooby \
57+
-DarchetypeVersion=0.1.0-SNAPSHOT
5458
cd my-app
5559
mvn jooby:run
5660

TODO

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
* Split BodyConverter into parser/formatter.
2-
** TemplateProcessor -> TemplateEngine?
3-
** Viewable -> View?
4-
** View negotiator should get all the media types of template engines.
5-
** Add name to template engine
6-
** Model contributions for views
1+
* Default Err negotiator should get all the media types of template engines.
2+
* Default handler for favicon
3+
* Model contributions for views
74
* Document Public API & GitHub Page
85
* Signed cookies
96
* Tests for Web Socket
10-
* Support for Test
7+
* Test support

jooby-archetype/src/main/resources/archetype-resources/src/main/java/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class App extends Jooby {
1010
{
1111
assets("/assets/**");
1212

13-
get("/", html("welcome.html"));
13+
get("/", file("welcome.html"));
1414
}
1515

1616
public static void main(final String[] args) throws Exception {

jooby-hbm/src/main/java/org/jooby/Hbm.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void configure(final Mode mode, final Config config, final Binder binder)
306306
this.emf = emf;
307307
}
308308

309-
private Filter readWriteTrx(final Key<EntityManager> key) {
309+
private Route.Filter readWriteTrx(final Key<EntityManager> key) {
310310
return (req, resp, chain) -> {
311311
EntityManager em = req.getInstance(key);
312312
Session session = (Session) em.getDelegate();
@@ -377,14 +377,6 @@ public void send(final Object body) throws Exception {
377377

378378
}
379379

380-
@Override
381-
public void send(final Object body, final BodyConverter converter) throws Exception {
382-
transactionalSend(() -> {
383-
super.send(body, converter);
384-
return null;
385-
});
386-
}
387-
388380
@Override
389381
public void send(final Body body) throws Exception {
390382
transactionalSend(() -> {
@@ -393,14 +385,6 @@ public void send(final Body body) throws Exception {
393385
});
394386
}
395387

396-
@Override
397-
public void send(final Body body, final BodyConverter converter) throws Exception {
398-
transactionalSend(() -> {
399-
super.send(body, converter);
400-
return null;
401-
});
402-
}
403-
404388
});
405389
} finally {
406390
try {

jooby-hbs/src/main/java/org/jooby/Hbs.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,6 @@
205205

206206
import static java.util.Objects.requireNonNull;
207207

208-
import org.jooby.BodyConverter;
209-
import org.jooby.BodyWriter;
210-
import org.jooby.Jooby;
211-
import org.jooby.Mode;
212-
import org.jooby.TemplateProcessor;
213-
import org.jooby.Viewable;
214-
215208
import com.github.jknack.handlebars.Context;
216209
import com.github.jknack.handlebars.Handlebars;
217210
import com.github.jknack.handlebars.Template;
@@ -226,7 +219,7 @@
226219

227220
public class Hbs extends Jooby.Module {
228221

229-
private static class Engine extends TemplateProcessor {
222+
private static class Engine implements View.Engine {
230223

231224
private Handlebars handlebars;
232225

@@ -235,7 +228,12 @@ public Engine(final Handlebars handlebars) {
235228
}
236229

237230
@Override
238-
public void render(final Viewable view, final BodyWriter writer) throws Exception {
231+
public String name() {
232+
return "hbs";
233+
}
234+
235+
@Override
236+
public void render(final View view, final Body.Writer writer) throws Exception {
239237
Template template = handlebars.compile(view.name());
240238

241239
final Context context;
@@ -257,7 +255,7 @@ public void render(final Viewable view, final BodyWriter writer) throws Exceptio
257255

258256
@Override
259257
public String toString() {
260-
return "Hbs";
258+
return name();
261259
}
262260
}
263261

@@ -276,7 +274,7 @@ public void configure(final Mode mode, final Config config, final Binder binder)
276274
throws Exception {
277275
binder.bind(Handlebars.class).toInstance(handlebars);
278276

279-
Multibinder.newSetBinder(binder, BodyConverter.class).addBinding()
277+
Multibinder.newSetBinder(binder, Body.Formatter.class).addBinding()
280278
.toInstance(new Engine(handlebars));
281279

282280
}

jooby-jackson/src/main/java/org/jooby/JSON.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,10 @@
208208

209209
import java.util.List;
210210

211-
import org.jooby.BodyConverter;
212-
import org.jooby.BodyReader;
213-
import org.jooby.BodyWriter;
214-
import org.jooby.MediaType;
215-
216211
import com.fasterxml.jackson.databind.ObjectMapper;
217212
import com.google.inject.TypeLiteral;
218213

219-
class JSON implements BodyConverter {
214+
class JSON implements Body.Formatter, Body.Parser {
220215

221216
private ObjectMapper mapper;
222217
private List<MediaType> types;
@@ -232,22 +227,22 @@ public List<MediaType> types() {
232227
}
233228

234229
@Override
235-
public boolean canRead(final TypeLiteral<?> type) {
230+
public boolean canParse(final TypeLiteral<?> type) {
236231
return mapper.canDeserialize(mapper.constructType(type.getType()));
237232
}
238233

239234
@Override
240-
public boolean canWrite(final Class<?> type) {
235+
public boolean canFormat(final Class<?> type) {
241236
return mapper.canSerialize(type);
242237
}
243238

244239
@Override
245-
public <T> T read(final TypeLiteral<T> type, final BodyReader reader) throws Exception {
240+
public <T> T parse(final TypeLiteral<T> type, final Body.Reader reader) throws Exception {
246241
return reader.text(in -> mapper.readValue(in, mapper.constructType(type.getType())));
247242
}
248243

249244
@Override
250-
public void write(final Object body, final BodyWriter writer) throws Exception {
245+
public void format(final Object body, final Body.Writer writer) throws Exception {
251246
writer.text(out -> mapper.writeValue(out, body));
252247
}
253248

jooby-jackson/src/main/java/org/jooby/Jackson.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@
207207

208208
import java.util.List;
209209

210-
import org.jooby.BodyConverter;
211-
import org.jooby.Jooby;
212-
import org.jooby.MediaType;
213-
import org.jooby.Mode;
214-
215210
import com.fasterxml.jackson.databind.ObjectMapper;
216211
import com.google.common.collect.ImmutableList;
217212
import com.google.inject.Binder;
@@ -240,9 +235,14 @@ public Jackson with(final MediaType... types) {
240235
@Override
241236
public void configure(final Mode mode, final Config config, final Binder binder) {
242237
binder.bind(ObjectMapper.class).toInstance(mapper);
243-
Multibinder.newSetBinder(binder, BodyConverter.class)
238+
JSON json = new JSON(mapper, types);
239+
Multibinder.newSetBinder(binder, Body.Formatter.class)
240+
.addBinding()
241+
.toInstance(json);
242+
243+
Multibinder.newSetBinder(binder, Body.Parser.class)
244244
.addBinding()
245-
.toInstance(new JSON(mapper, types));
245+
.toInstance(json);
246246
}
247247

248248
}

jooby-tests/src/test/java/org/jooby/AllParamsFeature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
import org.apache.http.client.fluent.Request;
88
import org.apache.http.message.BasicNameValuePair;
9-
import org.jooby.Variant;
9+
import org.jooby.Mutant;
1010
import org.junit.Test;
1111

1212
public class AllParamsFeature extends ServerFeature {
1313

1414
{
1515

1616
post("/:name", (req, res) -> {
17-
Map<String, Variant> params = req.params();
17+
Map<String, Mutant> params = req.params();
1818
assertEquals("a", params.get("name").toList(String.class).get(0));
1919
assertEquals("b", params.get("name").toList(String.class).get(1));
2020
assertEquals("c", params.get("name").toList(String.class).get(2));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.jooby;
2+
3+
import java.util.List;
4+
5+
import com.google.common.collect.ImmutableList;
6+
import com.google.common.io.CharStreams;
7+
import com.google.inject.TypeLiteral;
8+
9+
public class BodyConverters {
10+
11+
public static final Body.Parser fromJson = new Body.Parser() {
12+
13+
@Override
14+
public boolean canParse(final TypeLiteral<?> type) {
15+
return true;
16+
}
17+
18+
@Override
19+
public <T> T parse(final TypeLiteral<T> type, final Body.Reader reader) throws Exception {
20+
return reader.text(r -> CharStreams.toString(r));
21+
}
22+
23+
@Override
24+
public List<MediaType> types() {
25+
return ImmutableList.of(MediaType.json);
26+
}
27+
28+
};
29+
30+
public static final Body.Formatter toJson = new Body.Formatter() {
31+
32+
@Override
33+
public boolean canFormat(final Class<?> type) {
34+
return true;
35+
}
36+
37+
@Override
38+
public void format(final Object body, final Body.Writer writer)
39+
throws Exception {
40+
writer.text(w -> w.write("{\"body\": \"" + body + "\"}"));
41+
}
42+
43+
@Override
44+
public List<MediaType> types() {
45+
return ImmutableList.of(MediaType.json);
46+
}
47+
};
48+
49+
public static final Body.Formatter toHtml = new View.Engine() {
50+
@Override
51+
public void render(final View viewable, final Body.Writer writer) throws Exception {
52+
writer.text(w -> w.write("<html><body>" + viewable + "</body></html>"));
53+
}
54+
};
55+
}

jooby-tests/src/test/java/org/jooby/BodyParamFeature.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import org.apache.http.client.fluent.Request;
1010
import org.apache.http.entity.ContentType;
1111
import org.apache.http.entity.mime.MultipartEntityBuilder;
12-
import org.jooby.BodyConverter;
13-
import org.jooby.BodyReader;
14-
import org.jooby.BodyWriter;
15-
import org.jooby.MediaType;
1612
import org.jooby.mvc.POST;
1713
import org.jooby.mvc.Path;
1814
import org.junit.Test;
@@ -41,10 +37,30 @@ public Bean body(final Bean param) throws IOException {
4137

4238
{
4339

44-
use(new BodyConverter() {
40+
use(new Body.Parser() {
41+
@SuppressWarnings("unchecked")
42+
@Override
43+
public <T> T parse(final TypeLiteral<T> type, final Body.Reader reader) throws Exception {
44+
String body = reader.text(in -> CharStreams.toString(in));
45+
assertEquals("{}", body);
46+
return (T) new Bean();
47+
}
4548

4649
@Override
47-
public void write(final Object body, final BodyWriter writer) throws Exception {
50+
public boolean canParse(final TypeLiteral<?> type) {
51+
return type.getRawType() == Bean.class;
52+
}
53+
54+
@Override
55+
public List<MediaType> types() {
56+
return ImmutableList.of(MediaType.json);
57+
}
58+
});
59+
60+
use(new Body.Formatter() {
61+
62+
@Override
63+
public void format(final Object body, final Body.Writer writer) throws Exception {
4864
writer.text(out -> new CharSink() {
4965
@Override
5066
public Writer openStream() throws IOException {
@@ -58,23 +74,11 @@ public List<MediaType> types() {
5874
return ImmutableList.of(MediaType.json);
5975
}
6076

61-
@SuppressWarnings("unchecked")
62-
@Override
63-
public <T> T read(final TypeLiteral<T> type, final BodyReader reader) throws Exception {
64-
String body = reader.text(in ->CharStreams.toString(in));
65-
assertEquals("{}", body);
66-
return (T) new Bean();
67-
}
68-
6977
@Override
70-
public boolean canWrite(final Class<?> type) {
78+
public boolean canFormat(final Class<?> type) {
7179
return type == Bean.class;
7280
}
7381

74-
@Override
75-
public boolean canRead(final TypeLiteral<?> type) {
76-
return type.getRawType() == Bean.class;
77-
}
7882
});
7983

8084
post("/body", (req, resp) -> {

0 commit comments

Comments
 (0)