Skip to content

Commit bed82a8

Browse files
committed
add ctx.defaultResponseType
1 parent 8efec0a commit bed82a8

15 files changed

Lines changed: 73 additions & 34 deletions

File tree

jooby/src/main/java/io/jooby/Context.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ default boolean accept(MediaType contentType) {
151151
return contentType.matches(accept);
152152
}
153153

154-
@Nonnull default MediaType contentType() {
154+
@Nonnull default MediaType requestType() {
155155
return header("Content-Type").toOptional().map(MediaType::valueOf).orElse(null);
156156
}
157157

158-
default long contentLength() {
158+
default long requestLength() {
159159
return header("Content-Length").longValue(-1);
160160
}
161161

@@ -307,11 +307,11 @@ default long contentLength() {
307307

308308
@Nonnull Context detach(@Nonnull Runnable action);
309309

310-
@Nullable <T> T get(String name);
310+
@Nullable <T> T attribute(String name);
311311

312-
@Nonnull Context set(@Nonnull String name, @Nonnull Object value);
312+
@Nonnull Context attribute(@Nonnull String name, @Nonnull Object value);
313313

314-
@Nonnull Map<String, Object> locals();
314+
@Nonnull Map<String, Object> attributes();
315315

316316
/**
317317
* **********************************************************************************************
@@ -345,6 +345,8 @@ default long contentLength() {
345345
return responseType(contentType, contentType.charset());
346346
}
347347

348+
@Nonnull Context defaultResponseType(@Nonnull MediaType contentType);
349+
348350
@Nonnull Context responseType(@Nonnull MediaType contentType, @Nullable Charset charset);
349351

350352
@Nonnull MediaType responseType();
@@ -367,6 +369,17 @@ default long contentLength() {
367369

368370
@Nonnull OutputStream responseStream();
369371

372+
default @Nonnull OutputStream responseStream(MediaType contentType) {
373+
responseType(contentType);
374+
return responseStream();
375+
}
376+
377+
default @Nonnull Context responseStream(MediaType contentType,
378+
Throwing.Consumer<OutputStream> consumer) throws Exception {
379+
responseType(contentType);
380+
return responseStream(consumer);
381+
}
382+
370383
default @Nonnull Context responseStream(Throwing.Consumer<OutputStream> consumer)
371384
throws Exception {
372385
try (OutputStream out = responseStream()) {

jooby/src/main/java/io/jooby/Renderer.java

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

2323
Renderer TO_STRING = (ctx, value) -> {
24-
ctx.responseType(MediaType.text);
24+
ctx.defaultResponseType(MediaType.text);
2525
return value.toString().getBytes(StandardCharsets.UTF_8);
2626
};
2727

jooby/src/main/java/io/jooby/TemplateEngine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public interface TemplateEngine extends Renderer {
2323
String apply(Context ctx, ModelAndView modelAndView) throws Exception;
2424

2525
@Override default byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
26+
ctx.defaultResponseType(MediaType.html);
2627
String output = apply(ctx, (ModelAndView) value);
2728
return output.getBytes(StandardCharsets.UTF_8);
2829
}

jooby/src/test/java/io/jooby/MockContext.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class MockContext implements Context {
5151
private Map<String, Object> responseHeaders = new HashMap<>();
5252

5353
private long length;
54-
private MediaType responseContentType = MediaType.text;
54+
private MediaType responseType;
5555
private Charset responseCharset = StandardCharsets.UTF_8;
5656
private int responseStatusCode = 200;
5757
private Object result;
@@ -181,16 +181,16 @@ public MockContext dispatch(@Nonnull Executor executor, @Nonnull Runnable action
181181
return null;
182182
}
183183

184-
@Nullable @Override public <T> T get(String name) {
184+
@Nullable @Override public <T> T attribute(String name) {
185185
return (T) locals.get(name);
186186
}
187187

188-
@Nonnull @Override public MockContext set(@Nonnull String name, @Nonnull Object value) {
188+
@Nonnull @Override public MockContext attribute(@Nonnull String name, @Nonnull Object value) {
189189
locals.put(name, value);
190190
return this;
191191
}
192192

193-
@Nonnull @Override public Map<String, Object> locals() {
193+
@Nonnull @Override public Map<String, Object> attributes() {
194194
return locals;
195195
}
196196

@@ -225,7 +225,7 @@ public long getResponseLength() {
225225

226226
@Nonnull @Override
227227
public MockContext responseType(@Nonnull MediaType contentType, @Nullable Charset charset) {
228-
this.responseContentType = contentType;
228+
this.responseType = contentType;
229229
this.responseCharset = charset;
230230
return this;
231231
}
@@ -332,12 +332,16 @@ public String getResultText() {
332332
return this;
333333
}
334334

335-
public MediaType getResponseContentType() {
336-
return responseContentType;
335+
@Nonnull @Override public Context defaultResponseType(@Nonnull MediaType contentType) {
336+
if (responseType == null) {
337+
responseType = contentType;
338+
responseCharset = contentType.charset();
339+
}
340+
return this;
337341
}
338342

339343
@Nonnull @Override public MediaType responseType() {
340-
return responseContentType;
344+
return responseType == null ? MediaType.text : responseType;
341345
}
342346

343347
public int getResponseStatusCode() {

jooby/src/test/java/io/jooby/MockRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private Object route(Jooby router, String method, String path, Consumer<MockCont
114114
Result result = new Result(value, ctx.getResponseStatusCode());
115115

116116
/** Content-Type: */
117-
result.header("Content-Type", ctx.getResponseContentType().toContentTypeHeader(ctx.getResponseCharset()));
117+
result.header("Content-Type", ctx.responseType().toContentTypeHeader(ctx.getResponseCharset()));
118118

119119
/** Length: */
120120
long responseLength = ctx.getResponseLength();

modules/jooby-freemarker/src/main/java/io/jooby/freemarker/Freemarker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Freemarker(Configuration freemarker) {
119119
@Override public String apply(Context ctx, ModelAndView modelAndView) throws Exception {
120120
Template template = freemarker.getTemplate(modelAndView.view);
121121
StringWriter writer = new StringWriter();
122-
Map<String, Object> model = new HashMap<>(ctx.locals());
122+
Map<String, Object> model = new HashMap<>(ctx.attributes());
123123
model.putAll(modelAndView.model);
124124
template.process(model, writer);
125125
return writer.toString();

modules/jooby-freemarker/src/test/java/io/jooby/freemarker/FreemarkerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public String getLastname() {
3232
public void render() throws Exception {
3333
Freemarker freemarker = Freemarker.builder().build(Env.empty("test"));
3434
String output = freemarker
35-
.apply(new MockContext().set("local", "var"), new ModelAndView("index.ftl")
35+
.apply(new MockContext().attribute("local", "var"), new ModelAndView("index.ftl")
3636
.put("user", new User("foo", "bar"))
3737
.put("sign", "!"));
3838
assertEquals("Hello foo bar var!\n", output);

modules/jooby-handlebars/src/main/java/io/jooby/handlebars/Hbs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Hbs(Handlebars handlebars) {
158158

159159
@Override public String apply(Context ctx, ModelAndView modelAndView) throws Exception {
160160
Template template = handlebars.compile(modelAndView.view);
161-
Map<String, Object> model = new HashMap<>(ctx.locals());
161+
Map<String, Object> model = new HashMap<>(ctx.attributes());
162162
model.putAll(modelAndView.model);
163163
return template.apply(model);
164164
}

modules/jooby-handlebars/src/test/java/io/jooby/handlebars/HbsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public String getLastname() {
3030
public void render() throws Exception {
3131
Hbs engine = Hbs.builder().build();
3232
String output = engine
33-
.apply(new MockContext().set("local", "var"), new ModelAndView("index.hbs")
33+
.apply(new MockContext().attribute("local", "var"), new ModelAndView("index.hbs")
3434
.put("user", new User("foo", "bar"))
3535
.put("sign", "!"));
3636
assertEquals("Hello foo bar var!\n", output);

modules/jooby-jackson/src/main/java/io/jooby/json/Jackson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static final ObjectMapper defaultObjectMapper() {
7070
}
7171

7272
@Override public byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
73-
ctx.responseType(MediaType.json);
73+
ctx.defaultResponseType(MediaType.json);
7474
byte[] json;
7575
if (value instanceof CharSequence) {
7676
// Ignore string/charsequence responses, those are going to be processed by the default

0 commit comments

Comments
 (0)