Skip to content

Commit 9add8ee

Browse files
committed
mediaType: clean up charset handle
- fix #3755
1 parent 75ace46 commit 9add8ee

File tree

19 files changed

+137
-204
lines changed

19 files changed

+137
-204
lines changed

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

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ default Locale locale() {
571571
* Recreates full/entire url of the current request using the <code>Host</code> header.
572572
*
573573
* <p>If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
574-
* please consider to set {@link Router#setTrustProxy(boolean)} option.
574+
* please consider to set {@link Router#setRouterOptions(RouterOptions)} {@link
575+
* RouterOptions#setTrustProxy(boolean)} option.
575576
*
576577
* @return Full/entire request url using the <code>Host</code> header.
577578
*/
@@ -581,7 +582,8 @@ default Locale locale() {
581582
* Recreates full/entire request url using the <code>Host</code> header with a custom path/suffix.
582583
*
583584
* <p>If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
584-
* please consider to set {@link Router#setTrustProxy(boolean)} option.
585+
* please consider to set {@link Router#setRouterOptions(RouterOptions)} {@link
586+
* RouterOptions#setTrustProxy(boolean)} option.
585587
*
586588
* @param path Path or suffix to use, can also include query string parameters.
587589
* @return Full/entire request url using the <code>Host</code> header.
@@ -592,7 +594,8 @@ default Locale locale() {
592594
* The IP address of the client or last proxy that sent the request.
593595
*
594596
* <p>If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
595-
* please consider to set {@link Router#setTrustProxy(boolean)} option.
597+
* please consider to set {@link Router#setRouterOptions(RouterOptions)} {@link
598+
* RouterOptions#setTrustProxy(boolean)} option.
596599
*
597600
* @return The IP address of the client or last proxy that sent the request or <code>empty string
598601
* </code> for interrupted requests.
@@ -613,7 +616,8 @@ default Locale locale() {
613616
* method.
614617
*
615618
* <p>If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
616-
* please consider to set {@link Router#setTrustProxy(boolean)} option.
619+
* please consider to set {@link Router#setRouterOptions(RouterOptions)} {@link
620+
* RouterOptions#setTrustProxy(boolean)} option.
617621
*
618622
* @return Return the host that this request was sent to, in general this will be the value of the
619623
* Host header, minus the port specifier.
@@ -635,7 +639,8 @@ default Locale locale() {
635639
* the Host.
636640
*
637641
* <p>If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
638-
* please consider to set {@link Router#setTrustProxy(boolean)} option.
642+
* please consider to set {@link Router#setRouterOptions(RouterOptions)} {@link
643+
* RouterOptions#setTrustProxy(boolean)} option.
639644
*
640645
* @return Return the host that this request was sent to, in general this will be the value of the
641646
* Host header.
@@ -1086,15 +1091,6 @@ default Value lookup(String name) {
10861091
*/
10871092
Context setResponseType(@NonNull MediaType contentType);
10881093

1089-
/**
1090-
* Set response content type header.
1091-
*
1092-
* @param contentType Content type.
1093-
* @param charset Charset.
1094-
* @return This context.
1095-
*/
1096-
Context setResponseType(@NonNull MediaType contentType, @Nullable Charset charset);
1097-
10981094
/**
10991095
* Set the default response content type header. It is used if the response content type header
11001096
* was not set yet.
@@ -1200,15 +1196,6 @@ Context responseStream(
12001196
*/
12011197
PrintWriter responseWriter(@NonNull MediaType contentType);
12021198

1203-
/**
1204-
* HTTP response channel as response writer.
1205-
*
1206-
* @param contentType Content type.
1207-
* @param charset Charset.
1208-
* @return HTTP channel as response writer. Usually for chunked response.
1209-
*/
1210-
PrintWriter responseWriter(@NonNull MediaType contentType, @Nullable Charset charset);
1211-
12121199
/**
12131200
* HTTP response channel as response writer.
12141201
*
@@ -1229,19 +1216,6 @@ Context responseStream(
12291216
Context responseWriter(
12301217
MediaType contentType, @NonNull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception;
12311218

1232-
/**
1233-
* HTTP response channel as response writer.
1234-
*
1235-
* @param contentType Content type.
1236-
* @param charset Charset.
1237-
* @param consumer Writer consumer.
1238-
* @return This context.
1239-
* @throws Exception Is something goes wrong.
1240-
*/
1241-
Context responseWriter(
1242-
MediaType contentType, @Nullable Charset charset, SneakyThrows.Consumer<PrintWriter> consumer)
1243-
throws Exception;
1244-
12451219
/**
12461220
* Send a <code>302</code> response.
12471221
*
@@ -1390,10 +1364,12 @@ Context responseWriter(
13901364

13911365
/**
13921366
* True if response headers are cleared on application error. If none set it uses the
1393-
* default/global value specified by {@link RouterOptions#RESET_HEADERS_ON_ERROR}.
1367+
* default/global value specified by {@link Router#setRouterOptions(RouterOptions)} {@link
1368+
* RouterOptions#setResetHeadersOnError(boolean)} option.
13941369
*
13951370
* @return True if response headers are cleared on application error. If none set it uses the
1396-
* default/global value specified by {@link RouterOptions#RESET_HEADERS_ON_ERROR}.
1371+
* default/global value specified by {@link Router#setRouterOptions(RouterOptions)} {@link
1372+
* RouterOptions#setResetHeadersOnError(boolean)} option.
13971373
*/
13981374
boolean getResetHeadersOnError();
13991375

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.lang.reflect.Type;
1717
import java.nio.ByteBuffer;
1818
import java.nio.channels.FileChannel;
19-
import java.nio.charset.Charset;
2019
import java.nio.charset.StandardCharsets;
2120
import java.nio.file.Path;
2221
import java.time.Instant;
@@ -503,11 +502,6 @@ default Context setResponseHeader(@NonNull String name, @NonNull Object value) {
503502
return setResponseHeader(name, value.toString());
504503
}
505504

506-
@Override
507-
default Context setResponseType(@NonNull MediaType contentType) {
508-
return setResponseType(contentType, contentType.getCharset());
509-
}
510-
511505
@Override
512506
default Context setResponseCode(@NonNull StatusCode statusCode) {
513507
return setResponseCode(statusCode.value());
@@ -560,11 +554,6 @@ default PrintWriter responseWriter() {
560554
return responseWriter(MediaType.text);
561555
}
562556

563-
@Override
564-
default PrintWriter responseWriter(@NonNull MediaType contentType) {
565-
return responseWriter(contentType, contentType.getCharset());
566-
}
567-
568557
@Override
569558
default Context responseWriter(@NonNull SneakyThrows.Consumer<PrintWriter> consumer)
570559
throws Exception {
@@ -575,16 +564,7 @@ default Context responseWriter(@NonNull SneakyThrows.Consumer<PrintWriter> consu
575564
default Context responseWriter(
576565
@NonNull MediaType contentType, @NonNull SneakyThrows.Consumer<PrintWriter> consumer)
577566
throws Exception {
578-
return responseWriter(contentType, contentType.getCharset(), consumer);
579-
}
580-
581-
@Override
582-
default Context responseWriter(
583-
@NonNull MediaType contentType,
584-
@Nullable Charset charset,
585-
@NonNull SneakyThrows.Consumer<PrintWriter> consumer)
586-
throws Exception {
587-
try (PrintWriter writer = responseWriter(contentType, charset)) {
567+
try (PrintWriter writer = responseWriter(contentType)) {
588568
consumer.accept(writer);
589569
}
590570
return this;

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,12 +1097,6 @@ public Context setResponseType(@NonNull MediaType contentType) {
10971097
return this;
10981098
}
10991099

1100-
@Override
1101-
public Context setResponseType(@NonNull MediaType contentType, @Nullable Charset charset) {
1102-
ctx.setResponseType(contentType, charset);
1103-
return this;
1104-
}
1105-
11061100
@Override
11071101
public Context setDefaultResponseType(@NonNull MediaType contentType) {
11081102
ctx.setResponseType(contentType);
@@ -1175,11 +1169,6 @@ public PrintWriter responseWriter(@NonNull MediaType contentType) {
11751169
return ctx.responseWriter(contentType);
11761170
}
11771171

1178-
@Override
1179-
public PrintWriter responseWriter(@NonNull MediaType contentType, @Nullable Charset charset) {
1180-
return ctx.responseWriter(contentType, charset);
1181-
}
1182-
11831172
@Override
11841173
public Context responseWriter(@NonNull SneakyThrows.Consumer<PrintWriter> consumer)
11851174
throws Exception {
@@ -1193,15 +1182,6 @@ public Context responseWriter(
11931182
return ctx.responseWriter(contentType, consumer);
11941183
}
11951184

1196-
@Override
1197-
public Context responseWriter(
1198-
@NonNull MediaType contentType,
1199-
@Nullable Charset charset,
1200-
@NonNull SneakyThrows.Consumer<PrintWriter> consumer)
1201-
throws Exception {
1202-
return ctx.responseWriter(contentType, charset, consumer);
1203-
}
1204-
12051185
@Override
12061186
public Context sendRedirect(@NonNull String location) {
12071187
ctx.sendRedirect(location);

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.ArrayList;
1414
import java.util.Collections;
1515
import java.util.List;
16-
import java.util.function.BinaryOperator;
1716

1817
import edu.umd.cs.findbugs.annotations.NonNull;
1918
import edu.umd.cs.findbugs.annotations.Nullable;
@@ -25,14 +24,6 @@
2524
*/
2625
public final class MediaType implements Comparable<MediaType> {
2726

28-
/** Computes and returns the most specific media type of both. */
29-
public static final BinaryOperator<MediaType> MOST_SPECIFIC =
30-
(a, b) -> {
31-
int aScore = a.getScore();
32-
int bScore = b.getScore();
33-
return aScore >= bScore ? a : b;
34-
};
35-
3627
/** APPLICATION_JSON. */
3728
public static final String JSON = "application/json";
3829

@@ -72,8 +63,12 @@ public final class MediaType implements Comparable<MediaType> {
7263
/** ALL. */
7364
public static final String ALL = "*/*";
7465

75-
/** APPLICATION_JSON. */
76-
public static final MediaType json = new MediaType(JSON, UTF_8);
66+
/**
67+
* APPLICATION_JSON. The application/json content type, as defined by RFC 8259, does not require a
68+
* charset parameter. This is because JSON text is typically expected to be encoded in UTF-8, and
69+
* the specification does not define other encodings or a charset parameter for this media type.
70+
*/
71+
public static final MediaType json = new MediaType(JSON, null);
7772

7873
/** YAML. */
7974
public static final MediaType yaml = new MediaType(YAML, UTF_8);
@@ -115,6 +110,8 @@ public final class MediaType implements Comparable<MediaType> {
115110

116111
private final String value;
117112

113+
private final String contentTypeHeader;
114+
118115
private MediaType(@NonNull String value, Charset charset) {
119116
this.raw = value;
120117
this.subtypeStart = value.indexOf('/');
@@ -130,6 +127,7 @@ private MediaType(@NonNull String value, Charset charset) {
130127
this.subtypeEnd = subtypeEnd;
131128
}
132129
this.charset = charset;
130+
this.contentTypeHeader = this.charset == null ? value : value + ";charset=" + charset.name();
133131
}
134132

135133
@Override
@@ -187,18 +185,10 @@ public int hashCode() {
187185
/**
188186
* Render a content type header and add the charset parameter (when present).
189187
*
190-
* @param charset Charset.
191188
* @return Content type header.
192189
*/
193-
public @NonNull String toContentTypeHeader(@Nullable Charset charset) {
194-
if (charset == null) {
195-
Charset paramCharset = getCharset();
196-
if (paramCharset == null) {
197-
return value;
198-
}
199-
charset = paramCharset;
200-
}
201-
return value + ";charset=" + charset.name();
190+
public @NonNull String toContentTypeHeader() {
191+
return contentTypeHeader;
202192
}
203193

204194
/**

jooby/src/main/java/io/jooby/internal/ReadOnlyContext.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.Date;
1818

1919
import edu.umd.cs.findbugs.annotations.NonNull;
20-
import edu.umd.cs.findbugs.annotations.Nullable;
2120
import io.jooby.Context;
2221
import io.jooby.Cookie;
2322
import io.jooby.FileDownload;
@@ -140,15 +139,6 @@ public Context responseWriter(@NonNull SneakyThrows.Consumer<PrintWriter> consum
140139
throw new IllegalStateException(MESSAGE);
141140
}
142141

143-
@NonNull @Override
144-
public Context responseWriter(
145-
@NonNull MediaType contentType,
146-
@Nullable Charset charset,
147-
@NonNull SneakyThrows.Consumer<PrintWriter> consumer)
148-
throws Exception {
149-
throw new IllegalStateException(MESSAGE);
150-
}
151-
152142
@NonNull @Override
153143
public OutputStream responseStream() {
154144
throw new IllegalStateException(MESSAGE);
@@ -169,11 +159,6 @@ public PrintWriter responseWriter(@NonNull MediaType contentType) {
169159
throw new IllegalStateException(MESSAGE);
170160
}
171161

172-
@NonNull @Override
173-
public PrintWriter responseWriter(@NonNull MediaType contentType, @Nullable Charset charset) {
174-
throw new IllegalStateException(MESSAGE);
175-
}
176-
177162
@NonNull @Override
178163
public Sender responseSender() {
179164
throw new IllegalStateException(MESSAGE);
@@ -234,11 +219,6 @@ public Context setResponseType(@NonNull MediaType contentType) {
234219
throw new IllegalStateException(MESSAGE);
235220
}
236221

237-
@NonNull @Override
238-
public Context setResponseType(@NonNull MediaType contentType, @Nullable Charset charset) {
239-
throw new IllegalStateException(MESSAGE);
240-
}
241-
242222
@NonNull @Override
243223
public Context setDefaultResponseType(@NonNull MediaType contentType) {
244224
throw new IllegalStateException(MESSAGE);

jooby/src/main/java/io/jooby/internal/WebSocketSender.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.Date;
1212

1313
import edu.umd.cs.findbugs.annotations.NonNull;
14-
import edu.umd.cs.findbugs.annotations.Nullable;
1514
import io.jooby.Context;
1615
import io.jooby.Cookie;
1716
import io.jooby.DefaultContext;
@@ -150,12 +149,6 @@ public Context setResponseType(@NonNull String contentType) {
150149
return this;
151150
}
152151

153-
@NonNull @Override
154-
public Context setResponseType(@NonNull MediaType contentType, @Nullable Charset charset) {
155-
// NOOP
156-
return this;
157-
}
158-
159152
@NonNull @Override
160153
public Context setResponseType(@NonNull MediaType contentType) {
161154
// NOOP

0 commit comments

Comments
 (0)