Skip to content

Commit f01c0a5

Browse files
committed
rename ctx.sendText to ctx.sendString
- update doc for parser and renderer
1 parent 3d5c1e3 commit f01c0a5

File tree

22 files changed

+83
-77
lines changed

22 files changed

+83
-77
lines changed

docs/body.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ Request body parsing is achieved using the javadoc:Parser[] functional interface
3636
----
3737
public interface Parser {
3838
39-
<T> T parse(Context ctx, Reified<T> reified) throws Exception;
39+
<T> T parse(Context ctx, Type type) throws Exception;
4040
}
4141
----
4242

43-
javadoc:Parser[] has a single `parse` method that takes two input arguments: `(context, reified)`
44-
and returns a single result of the given reified type.
43+
javadoc:Parser[] has a single `parse` method that takes two input arguments: `(context, type)`
44+
and returns a single result of the given type.
4545

4646
.JSON example:
4747
[source, java]
4848
----
4949
{
5050
FavoriteJson lib = ...; // <1>
5151
52-
parser("appplication/json", (ctx, reified) -> { // <2>
52+
parser(MediaType.json, (ctx, type) -> { // <2>
5353
5454
String body = ctx.body().value(); // <3>
5555
56-
return lib.fromJson(body, reified.getType()); // <4>
56+
return lib.fromJson(body, type); // <4>
5757
});
5858
5959
post("/", ctx -> {
@@ -89,7 +89,7 @@ Response body is generated from `handler` function:
8989
get("/", ctx -> {
9090
ctx.statusCode(200); // <1>
9191
92-
ctx.type("text/plain"); // <2>
92+
ctx.type(MediaType.text); // <2>
9393
9494
ctx.header("Date", new Date()); // <3>
9595
@@ -125,11 +125,11 @@ to operate via side-effects.
125125
{
126126
FavoriteJson lib = ...; // <1>
127127
128-
renderer("application/json", (ctx, result) -> { // <2>
128+
renderer(MediaType.json, (ctx, result) -> { // <2>
129129
130130
String json = lib.toJson(result); // <3>
131131
132-
ctx.type("application/json"); // <4>
132+
ctx.type(MediaType.json); // <4>
133133
134134
ctx.sendText(json); // <5>
135135
});

examples/src/main/java/examples/BenchApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Message(String message) {
3636

3737
decorate(Decorators.defaultHeaders());
3838

39-
get("/", ctx -> ctx.sendText(MESSAGE));
39+
get("/", ctx -> ctx.sendString(MESSAGE));
4040

4141
get("/json", ctx -> Thread.currentThread().getName());
4242

examples/src/main/java/examples/HelloApp.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import io.jooby.FileUpload;
2222
import io.jooby.json.Jackson;
2323

24-
import java.util.concurrent.Executors;
25-
2624
public class HelloApp extends Jooby {
2725

2826
public static class User {
@@ -54,7 +52,7 @@ public Message(String message) {
5452

5553
decorate(Decorators.defaultHeaders());
5654

57-
get("/", ctx -> ctx.sendText(MESSAGE));
55+
get("/", ctx -> ctx.sendString(MESSAGE));
5856

5957
// get("/{foo}", ctx -> ctx.sendText("Hello World!"));
6058

jooby/src/main/java/io/jooby/ContentNegotiation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ContentNegotiation accept(Throwing.Supplier<Object> fallback) {
4242
}
4343

4444
public Object render(String accept) {
45-
List<MediaType> types = MediaType.fromAcceptHeader(accept);
45+
List<MediaType> types = MediaType.parse(accept);
4646
int maxScore = Integer.MIN_VALUE;
4747
Throwing.Supplier<Object> result = fallback;
4848
for (Map.Entry<MediaType, Throwing.Supplier<Object>> entry : options.entrySet()) {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.nio.channels.FileChannel;
2929
import java.nio.charset.Charset;
3030
import java.nio.charset.StandardCharsets;
31-
import java.nio.file.Files;
3231
import java.nio.file.Path;
3332
import java.time.Instant;
3433
import java.time.ZoneId;
@@ -147,9 +146,9 @@ public interface Context {
147146
return headers().toMultimap();
148147
}
149148

150-
default boolean accept(String contentType) {
149+
default boolean accept(MediaType contentType) {
151150
String accept = header(ACCEPT).value(MediaType.ALL);
152-
return MediaType.matches(contentType, accept);
151+
return contentType.matches(accept);
153152
}
154153

155154
/* **********************************************************************************************
@@ -382,11 +381,11 @@ default boolean accept(String contentType) {
382381
return this;
383382
}
384383

385-
default @Nonnull Context sendText(@Nonnull String data) {
386-
return sendText(data, StandardCharsets.UTF_8);
384+
default @Nonnull Context sendString(@Nonnull String data) {
385+
return sendString(data, StandardCharsets.UTF_8);
387386
}
388387

389-
@Nonnull Context sendText(@Nonnull String data, @Nonnull Charset charset);
388+
@Nonnull Context sendString(@Nonnull String data, @Nonnull Charset charset);
390389

391390
@Nonnull Context sendBytes(@Nonnull byte[] data);
392391

jooby/src/main/java/io/jooby/ErrorHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static ErrorHandler log(Logger log, StatusCode... quiet) {
5252
String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason());
5353
return ctx.type(json)
5454
.statusCode(statusCode)
55-
.sendText("{\"message\":\"" + message + "\",\"statusCode\":" + statusCode.value()
55+
.sendString("{\"message\":\"" + message + "\",\"statusCode\":" + statusCode.value()
5656
+ ",\"reason\":\"" + statusCode.reason() + "\"}");
5757
})
5858
.accept("text/html", () -> {
@@ -89,7 +89,7 @@ static ErrorHandler log(Logger log, StatusCode... quiet) {
8989
return ctx
9090
.type(MediaType.html)
9191
.statusCode(statusCode)
92-
.sendText(html.toString());
92+
.sendString(html.toString());
9393
}).render(ctx);
9494
};
9595

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ public Jooby use(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
172172
return this;
173173
}
174174

175-
@Nonnull @Override public Jooby parser(@Nonnull String contentType, @Nonnull Parser parser) {
175+
@Nonnull @Override public Jooby parser(@Nonnull MediaType contentType, @Nonnull Parser parser) {
176176
router.parser(contentType, parser);
177177
return this;
178178
}
179179

180180
@Nonnull @Override
181-
public Jooby renderer(@Nonnull String contentType, @Nonnull Renderer renderer) {
181+
public Jooby renderer(@Nonnull MediaType contentType, @Nonnull Renderer renderer) {
182182
router.renderer(contentType, renderer);
183183
return this;
184184
}

jooby/src/main/java/io/jooby/MediaType.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import javax.annotation.Nullable;
2020
import java.io.File;
2121
import java.nio.charset.Charset;
22-
import java.nio.charset.StandardCharsets;
2322
import java.nio.file.Path;
2423
import java.util.ArrayList;
2524
import java.util.Collections;
@@ -69,22 +68,30 @@ public class MediaType implements Comparable<MediaType> {
6968

7069
public static final MediaType all = new MediaType(ALL, null);
7170

72-
private final String value;
71+
private final String raw;
7372

7473
private final Charset charset;
7574

7675
private final int subtypeStart;
7776

7877
private final int subtypeEnd;
7978

79+
private final String value;
80+
8081
private MediaType(@Nonnull String value, Charset charset) {
81-
this.value = value;
82+
this.raw = value;
8283
this.subtypeStart = value.indexOf('/');
8384
if (subtypeStart < 0) {
8485
throw new IllegalArgumentException("Invalid media type: " + value);
8586
}
8687
int subtypeEnd = value.indexOf(';');
87-
this.subtypeEnd = subtypeEnd < 0 ? value.length() : subtypeEnd;
88+
if (subtypeEnd < 0) {
89+
this.value = raw;
90+
this.subtypeEnd = value.length();
91+
} else {
92+
this.value = raw.substring(0, subtypeEnd);
93+
this.subtypeEnd = subtypeEnd;
94+
}
8895
this.charset = charset;
8996
}
9097

@@ -97,21 +104,21 @@ private MediaType(@Nonnull String value, Charset charset) {
97104
}
98105

99106
@Override public int hashCode() {
100-
return value.hashCode();
107+
return raw.hashCode();
101108
}
102109

103110
public @Nullable String param(@Nonnull String name) {
104111
int paramStart = subtypeEnd + 1;
105-
for (int i = subtypeEnd; i < value.length(); i++) {
106-
char ch = value.charAt(i);
112+
for (int i = subtypeEnd; i < raw.length(); i++) {
113+
char ch = raw.charAt(i);
107114
if (ch == '=') {
108-
String pname = value.substring(paramStart, i).trim();
109-
int paramValueEnd = value.indexOf(';', i);
115+
String pname = raw.substring(paramStart, i).trim();
116+
int paramValueEnd = raw.indexOf(';', i);
110117
if (paramValueEnd < 0) {
111-
paramValueEnd = value.length();
118+
paramValueEnd = raw.length();
112119
}
113120
if (pname.equals(name)) {
114-
return value.substring(i + 1, paramValueEnd).trim();
121+
return raw.substring(i + 1, paramValueEnd).trim();
115122
}
116123
paramStart = paramValueEnd + 1;
117124
i = paramStart;
@@ -121,18 +128,18 @@ private MediaType(@Nonnull String value, Charset charset) {
121128
}
122129

123130
public @Nonnull String value() {
124-
return value.substring(0, subtypeEnd);
131+
return value;
125132
}
126133

127-
public @Nonnull String toContenTypeHeader(@Nullable Charset charset) {
134+
public @Nonnull String toContentTypeHeader(@Nullable Charset charset) {
128135
if (charset == null) {
129136
Charset paramCharset = charset();
130137
if (paramCharset == null) {
131-
return value();
138+
return value;
132139
}
133140
charset = paramCharset;
134141
}
135-
return value() + ";charset=" + charset.name();
142+
return value + ";charset=" + charset.name();
136143
}
137144

138145
@Nonnull public float quality() {
@@ -176,19 +183,19 @@ private Charset _charset(Charset charset) {
176183
}
177184

178185
public @Nonnull String type() {
179-
return value.substring(0, subtypeStart).trim();
186+
return raw.substring(0, subtypeStart).trim();
180187
}
181188

182189
public @Nonnull String subtype() {
183-
return value.substring(subtypeStart + 1, subtypeEnd).trim();
190+
return raw.substring(subtypeStart + 1, subtypeEnd).trim();
184191
}
185192

186193
public boolean matches(@Nonnull String contentType) {
187-
return matches(value(), contentType);
194+
return matches(value, contentType);
188195
}
189196

190197
public boolean matches(@Nonnull MediaType type) {
191-
return matches(type.value());
198+
return matches(value, type.value);
192199
}
193200

194201
public int score() {
@@ -204,8 +211,8 @@ public int score() {
204211

205212
public int paramSize() {
206213
int p = 0;
207-
for (int i = subtypeEnd; i < value.length(); i++) {
208-
char ch = value.charAt(i);
214+
for (int i = subtypeEnd; i < raw.length(); i++) {
215+
char ch = raw.charAt(i);
209216
if (ch == '=') {
210217
p += 1;
211218
}
@@ -247,7 +254,7 @@ public int paramSize() {
247254
return new MediaType(value, null);
248255
}
249256

250-
public static @Nonnull List<MediaType> fromAcceptHeader(@Nullable String value) {
257+
public static @Nonnull List<MediaType> parse(@Nullable String value) {
251258
if (value == null || value.length() == 0) {
252259
return Collections.emptyList();
253260
}
@@ -739,6 +746,6 @@ private static boolean matchOne(String expected, int len1, String contentType) {
739746
}
740747

741748
@Override public String toString() {
742-
return value;
749+
return raw;
743750
}
744751
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
public interface Renderer {
2121

2222
Renderer TO_STRING = (ctx, value) -> {
23-
ctx.sendText(value.toString());
23+
ctx.sendString(value.toString());
2424
return true;
2525
};
2626

2727
boolean render(@Nonnull Context ctx, @Nonnull Object result) throws Exception;
2828

29-
@Nonnull default Renderer accept(String contentType) {
29+
@Nonnull default Renderer accept(@Nonnull MediaType contentType) {
3030
return (ctx, result) -> {
3131
if (ctx.accept(contentType)) {
3232
return render(ctx, result);

jooby/src/main/java/io/jooby/Router.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ interface Match {
7070

7171
@Nonnull Router renderer(@Nonnull Renderer renderer);
7272

73-
@Nonnull Router renderer(@Nonnull String contentType, @Nonnull Renderer renderer);
73+
@Nonnull Router renderer(@Nonnull MediaType contentType, @Nonnull Renderer renderer);
7474

7575
@Nonnull Path tmpdir();
7676

77-
@Nonnull Router parser(@Nonnull String contentType, @Nonnull Parser parser);
77+
@Nonnull Router parser(@Nonnull MediaType contentType, @Nonnull Parser parser);
7878

7979
@Nonnull Executor worker();
8080

0 commit comments

Comments
 (0)