Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 41d2619

Browse files
committed
fix javadoc and simplify accepts/type on Renderer
1 parent 24749e3 commit 41d2619

File tree

8 files changed

+87
-59
lines changed

8 files changed

+87
-59
lines changed

coverage-report/src/test/java/org/jooby/BodyConverters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ public class BodyConverters {
66

77
public static final Renderer toJson = (body, ctx) -> {
88
if (ctx.accepts("json")) {
9-
ctx.type(MediaType.json)
9+
ctx.type("json")
1010
.send("{\"body\": \"" + body + "\"}");
1111
}
1212
};
1313

1414
public static final Renderer toHtml = (viewable, ctx) -> {
1515
if (ctx.accepts("html")) {
16-
ctx.type(MediaType.html)
16+
ctx.type("html")
1717
.send("<html><body>" + viewable + "</body></html>");
1818
}
1919
};

coverage-report/src/test/java/org/jooby/jackson/JsonCustomTypeFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class JsonCustomTypeFeature extends ServerFeature {
1818

1919
{
2020

21-
use(new Jackson().types(MediaType.valueOf("application/vnd.github.v3+json")));
21+
use(new Jackson().type(MediaType.valueOf("application/vnd.github.v3+json")));
2222

2323
get("/members",
2424
req -> Lists.newArrayList(ImmutableMap.<String, Object> of("id", 1, "name", "pablo")));

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.text.SimpleDateFormat;
2525
import java.util.LinkedHashSet;
26-
import java.util.List;
2726
import java.util.Locale;
2827
import java.util.Set;
2928
import java.util.TimeZone;
@@ -41,7 +40,6 @@
4140
import com.fasterxml.jackson.databind.ObjectMapper;
4241
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
4342
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
44-
import com.google.common.collect.ImmutableList;
4543
import com.google.inject.Binder;
4644
import com.google.inject.Key;
4745
import com.google.inject.multibindings.Multibinder;
@@ -132,7 +130,7 @@ public PostConfigurer(final ObjectMapper mapper, final Set<Module> jacksonModule
132130

133131
private final Set<Module> modules = new LinkedHashSet<>();
134132

135-
private List<MediaType> types = ImmutableList.of(MediaType.json);
133+
private MediaType type = MediaType.json;
136134

137135
public Jackson(final ObjectMapper mapper) {
138136
this.mapper = checkNotNull(mapper, "An object mapper is required.");
@@ -145,17 +143,13 @@ public Jackson() {
145143
this(new ObjectMapper());
146144
}
147145

148-
public Jackson types(final MediaType... types) {
149-
return types(ImmutableList.copyOf(types));
150-
}
151-
152-
public Jackson types(final List<MediaType> types) {
153-
this.types = ImmutableList.copyOf(types);
146+
public Jackson type(final MediaType type) {
147+
this.type = type;
154148
return this;
155149
}
156150

157-
public Jackson types(final String... types) {
158-
return types(MediaType.valueOf(types));
151+
public Jackson types(final String type) {
152+
return type(MediaType.valueOf(type));
159153
}
160154

161155
public Jackson doWith(final Consumer<ObjectMapper> block) {
@@ -181,8 +175,8 @@ public void configure(final Env mode, final Config config, final Binder binder)
181175
binder.bind(PostConfigurer.class).asEagerSingleton();
182176

183177
// json parser & renderer
184-
JacksonParser parser = new JacksonParser(mapper, types);
185-
JacksonRenderer renderer = new JacksonRenderer(mapper, types);
178+
JacksonParser parser = new JacksonParser(mapper, type);
179+
JacksonRenderer renderer = new JacksonRenderer(mapper, type);
186180

187181
Multibinder.newSetBinder(binder, Renderer.class)
188182
.addBinding()

jooby-jackson/src/main/java/org/jooby/json/JacksonParser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.jooby.json;
22

3-
import java.util.List;
4-
53
import org.jooby.MediaType;
64
import org.jooby.MediaType.Matcher;
75
import org.jooby.Parser;
@@ -16,9 +14,9 @@ class JacksonParser implements Parser {
1614

1715
private Matcher matcher;
1816

19-
public JacksonParser(final ObjectMapper mapper, final List<MediaType> types) {
17+
public JacksonParser(final ObjectMapper mapper, final MediaType type) {
2018
this.mapper = mapper;
21-
this.matcher = MediaType.matcher(types);
19+
this.matcher = MediaType.matcher(type);
2220
}
2321

2422
@Override

jooby-jackson/src/main/java/org/jooby/json/JacksonRenderer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.jooby.json;
22

3-
import java.util.List;
4-
53
import org.jooby.MediaType;
64
import org.jooby.Renderer;
75

@@ -11,17 +9,17 @@ class JacksonRenderer implements Renderer {
119

1210
private ObjectMapper mapper;
1311

14-
private List<MediaType> types;
12+
private MediaType type;
1513

16-
public JacksonRenderer(final ObjectMapper mapper, final List<MediaType> types) {
14+
public JacksonRenderer(final ObjectMapper mapper, final MediaType type) {
1715
this.mapper = mapper;
18-
this.types = types;
16+
this.type = type;
1917
}
2018

2119
@Override
2220
public void render(final Object value, final Context ctx) throws Exception {
23-
if (ctx.accepts(types) && mapper.canSerialize(value.getClass())) {
24-
ctx.type(types.get(0));
21+
if (ctx.accepts(type) && mapper.canSerialize(value.getClass())) {
22+
ctx.type(type);
2523
// use UTF-8 and get byte version
2624
byte[] bytes = mapper.writer().writeValueAsBytes(value);
2725
ctx.length(bytes.length)

jooby/src/main/java/org/jooby/Err.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@
4242
public class Err extends RuntimeException {
4343

4444
/**
45-
* Default err handler it does content negotation. On
46-
* <code>text/html<code> requests the err handler
47-
* creates an <code>err</code> view and set as model the {@link Err#toMap()}.
45+
* Default err handler it does content negotation. On <code>text/html</code> requests the err
46+
* handler creates an <code>err</code> view and set as model the {@link Err#toMap()}.
4847
*
4948
* @author edgar
5049
* @since 0.1.0

jooby/src/main/java/org/jooby/Renderer.java

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@
2424
import java.nio.CharBuffer;
2525
import java.nio.channels.FileChannel;
2626
import java.nio.charset.Charset;
27-
import java.util.List;
2827
import java.util.Map;
2928

30-
import com.google.common.collect.ImmutableList;
31-
3229
/**
3330
* Write a value into the HTTP response and apply a format, if need it.
3431
*
@@ -97,11 +94,13 @@ public interface Renderer {
9794
interface Context {
9895

9996
/**
100-
* @return Request attributes.
97+
* @return Request local attributes.
10198
*/
10299
Map<String, Object> locals();
103100

104101
/**
102+
* True if the given type matches the <code>Accept</code> header.
103+
*
105104
* @param type The type to check for.
106105
* @return True if the given type matches the <code>Accept</code> header.
107106
*/
@@ -110,18 +109,23 @@ default boolean accepts(final String type) {
110109
}
111110

112111
/**
112+
* True if the given type matches the <code>Accept</code> header.
113+
*
113114
* @param type The type to check for.
114115
* @return True if the given type matches the <code>Accept</code> header.
115116
*/
116-
default boolean accepts(final MediaType type) {
117-
return accepts(ImmutableList.of(type));
118-
}
117+
boolean accepts(final MediaType type);
119118

120119
/**
121-
* @param types Types to check for.
122-
* @return True if the given type matches the <code>Accept</code> header.
120+
* Set the <code>Content-Type</code> header IF and ONLY IF, no <code>Content-Type</code> was set
121+
* yet.
122+
*
123+
* @param type A suggested type to use if one is missing.
124+
* @return This context.
123125
*/
124-
boolean accepts(List<MediaType> types);
126+
default Context type(final String type) {
127+
return type(MediaType.valueOf(type));
128+
}
125129

126130
/**
127131
* Set the <code>Content-Type</code> header IF and ONLY IF, no <code>Content-Type</code> was set
@@ -147,57 +151,86 @@ default boolean accepts(final MediaType type) {
147151
Charset charset();
148152

149153
/**
150-
* Write bytes into the HTTP response body and close the resources.
154+
* Write bytes into the HTTP response body.
155+
*
156+
* It will set a <code>Content-Length</code> if none was set
157+
* It will set a <code>Content-Type</code> to {@link MediaType#octetstream} if none was set.
151158
*
152159
* @param bytes A bytes to write.
153160
* @throws Exception When the operation fails.
154161
*/
155162
void send(byte[] bytes) throws Exception;
156163

157164
/**
158-
* Write bytes into the HTTP response body and close the resources.
165+
* Write byte buffer into the HTTP response body.
159166
*
160-
* @param bytes A bytes to write.
167+
* It will set a <code>Content-Length</code> if none was set.
168+
* It will set a <code>Content-Type</code> to {@link MediaType#octetstream} if none was set.
169+
*
170+
* @param buffer A buffer to write.
161171
* @throws Exception When the operation fails.
162172
*/
163173
void send(ByteBuffer buffer) throws Exception;
164174

165175
/**
166-
* Write test into the HTTP response body.
176+
* Write text into the HTTP response body.
167177
*
168-
* @param data A text to write.
178+
* It will set a <code>Content-Length</code> if none was set.
179+
* It will set a <code>Content-Type</code> to {@link MediaType#html} if none was set.
180+
*
181+
* @param text A text to write.
169182
* @throws Exception When the operation fails.
170183
*/
171184
void send(String text) throws Exception;
172185

173186
/**
174-
* Write test into the HTTP response body.
187+
* Write bytes into the HTTP response body.
175188
*
176-
* @param text A text to write.
189+
* It will set a <code>Content-Length</code> if the response size is less than the
190+
* <code>server.ResponseBufferSize</code> (default is: 16k). If the response is larger than the
191+
* buffer size, it will set a <code>Transfer-Encoding: chunked</code> header.
192+
*
193+
* It will set a <code>Content-Type</code> to {@link MediaType#octetstream} if none was set.
194+
*
195+
* This method will check if the given input stream has a {@link FileChannel} and redirect to file
196+
*
197+
* @param stream Bytes to write.
177198
* @throws Exception When the operation fails.
178199
*/
179200
void send(InputStream stream) throws Exception;
180201

181202
/**
182-
* Write test into the HTTP response body.
203+
* Write text into the HTTP response body.
183204
*
184-
* @param text A text to write.
205+
* It will set a <code>Content-Length</code> if none was set.
206+
* It will set a <code>Content-Type</code> to {@link MediaType#html} if none was set.
207+
*
208+
* @param buffer A text to write.
185209
* @throws Exception When the operation fails.
186210
*/
187211
void send(CharBuffer buffer) throws Exception;
188212

189213
/**
190-
* Write test into the HTTP response body.
214+
* Write text into the HTTP response body.
191215
*
192-
* @param text A text to write.
216+
* It will set a <code>Content-Length</code> if the response size is less than the
217+
* <code>server.ResponseBufferSize</code> (default is: 16k). If the response is larger than the
218+
* buffer size, it will set a <code>Transfer-Encoding: chunked</code> header.
219+
*
220+
* It will set a <code>Content-Type</code> to {@link MediaType#html} if none was set.
221+
*
222+
* @param reader Text to write.
193223
* @throws Exception When the operation fails.
194224
*/
195225
void send(Reader reader) throws Exception;
196226

197227
/**
198-
* Write test into the HTTP response body.
228+
* Write file into the HTTP response body, using OS zero-copy transfer (if possible).
199229
*
200-
* @param text A text to write.
230+
* It will set a <code>Content-Length</code> if none was set.
231+
* It will set a <code>Content-Type</code> to {@link MediaType#html} if none was set.
232+
*
233+
* @param file A text to write.
201234
* @throws Exception When the operation fails.
202235
*/
203236
void send(FileChannel file) throws Exception;

jooby/src/main/java/org/jooby/internal/AbstractRendererContext.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.jooby.MediaType;
1919
import org.jooby.MediaType.Matcher;
2020
import org.jooby.Renderer;
21-
import org.jooby.Renderer.Context;
2221
import org.jooby.Status;
2322
import org.jooby.View;
2423

@@ -78,18 +77,18 @@ public Map<String, Object> locals() {
7877
}
7978

8079
@Override
81-
public boolean accepts(final List<MediaType> types) {
82-
return matcher.matches(types);
80+
public boolean accepts(final MediaType type) {
81+
return matcher.matches(type);
8382
}
8483

8584
@Override
86-
public Context type(final MediaType type) {
85+
public Renderer.Context type(final MediaType type) {
8786
// NOOP
8887
return this;
8988
}
9089

9190
@Override
92-
public Context length(final long length) {
91+
public Renderer.Context length(final long length) {
9392
// NOOP
9493
return this;
9594
}
@@ -101,45 +100,52 @@ public Charset charset() {
101100

102101
@Override
103102
public void send(final CharBuffer buffer) throws Exception {
103+
type(MediaType.html);
104104
send(charset.encode(buffer));
105105
}
106106

107107
@Override
108108
public void send(final Reader reader) throws Exception {
109+
type(MediaType.html);
109110
send(new ReaderInputStream(reader, charset));
110111
}
111112

112113
@Override
113114
public void send(final String text) throws Exception {
115+
type(MediaType.html);
114116
_send(text);
115117
committed = true;
116118
}
117119

118120
@Override
119121
public void send(final byte[] bytes) throws Exception {
122+
type(MediaType.octetstream);
120123
length(bytes.length);
121124
_send(bytes);
122125
committed = true;
123126
}
124127

125128
@Override
126129
public void send(final ByteBuffer buffer) throws Exception {
130+
type(MediaType.octetstream);
127131
length(buffer.remaining());
128132
_send(buffer);
129133
committed = true;
130134
}
131135

132136
@Override
133137
public void send(final FileChannel file) throws Exception {
138+
type(MediaType.octetstream);
134139
length(file.size());
135140
_send(file);
136141
committed = true;
137142
}
138143

139144
@Override
140145
public void send(final InputStream stream) throws Exception {
146+
type(MediaType.octetstream);
141147
if (stream instanceof FileInputStream) {
142-
_send(((FileInputStream) stream).getChannel());
148+
send(((FileInputStream) stream).getChannel());
143149
} else {
144150
_send(stream);
145151
}

0 commit comments

Comments
 (0)