Skip to content

Commit 38736d2

Browse files
committed
make clear difference between request and response attributes
1 parent 32a560b commit 38736d2

File tree

18 files changed

+67
-68
lines changed

18 files changed

+67
-68
lines changed

jooby/src/main/java/io/jooby/AssetHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public AssetHandler(AssetSource... sources) {
8787

8888
long length = asset.getSize();
8989
if (length != -1) {
90-
ctx.setContentLength(length);
90+
ctx.setResponseLength(length);
9191
}
92-
ctx.setContentType(asset.getContentType());
92+
ctx.setResponseType(asset.getContentType());
9393
return ctx.sendStream(asset.stream());
9494
}
9595

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ default MediaType accept(@Nonnull List<MediaType> produceTypes) {
400400
*
401401
* @return Request <code>Content-Type</code> header or <code>null</code> when missing.
402402
*/
403-
@Nullable default MediaType getContentType() {
403+
@Nullable default MediaType getRequestType() {
404404
Value contentType = header("Content-Type");
405405
return contentType.isMissing() ? null : MediaType.valueOf(contentType.value());
406406
}
@@ -411,7 +411,7 @@ default MediaType accept(@Nonnull List<MediaType> produceTypes) {
411411
* @param defaults Default content type to use when the header is missing.
412412
* @return Request <code>Content-Type</code> header or <code>null</code> when missing.
413413
*/
414-
@Nonnull default MediaType getContentType(MediaType defaults) {
414+
@Nonnull default MediaType getRequestType(MediaType defaults) {
415415
Value contentType = header("Content-Type");
416416
return contentType.isMissing() ? defaults : MediaType.valueOf(contentType.value());
417417
}
@@ -421,7 +421,7 @@ default MediaType accept(@Nonnull List<MediaType> produceTypes) {
421421
*
422422
* @return Request <code>Content-Length</code> header or <code>-1</code> when missing.
423423
*/
424-
default long getContentLength() {
424+
default long getRequestLength() {
425425
Value contentLength = header("Content-Length");
426426
return contentLength.isMissing() ? -1 : contentLength.longValue();
427427
}
@@ -671,7 +671,7 @@ default long getContentLength() {
671671
* @return Instance of conversion type.
672672
*/
673673
default @Nonnull <T> T body(@Nonnull Reified<T> type) {
674-
return body(type, getContentType(MediaType.text));
674+
return body(type, getRequestType(MediaType.text));
675675
}
676676

677677
/**
@@ -698,7 +698,7 @@ default long getContentLength() {
698698
* @return Instance of conversion type.
699699
*/
700700
default @Nonnull <T> T body(@Nonnull Class type) {
701-
return body(type, getContentType(MediaType.text));
701+
return body(type, getRequestType(MediaType.text));
702702
}
703703

704704
/**
@@ -863,24 +863,24 @@ default long getContentLength() {
863863
* @param length Response length.
864864
* @return This context.
865865
*/
866-
@Nonnull Context setContentLength(long length);
866+
@Nonnull Context setResponseLength(long length);
867867

868868
/**
869869
* Set response content type header.
870870
*
871871
* @param contentType Content type.
872872
* @return This context.
873873
*/
874-
@Nonnull Context setContentType(@Nonnull String contentType);
874+
@Nonnull Context setResponseType(@Nonnull String contentType);
875875

876876
/**
877877
* Set response content type header.
878878
*
879879
* @param contentType Content type.
880880
* @return This context.
881881
*/
882-
@Nonnull default Context setContentType(@Nonnull MediaType contentType) {
883-
return setContentType(contentType, contentType.getCharset());
882+
@Nonnull default Context setResponseType(@Nonnull MediaType contentType) {
883+
return setResponseType(contentType, contentType.getCharset());
884884
}
885885

886886
/**
@@ -890,7 +890,7 @@ default long getContentLength() {
890890
* @param charset Charset.
891891
* @return This context.
892892
*/
893-
@Nonnull Context setContentType(@Nonnull MediaType contentType, @Nullable Charset charset);
893+
@Nonnull Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset charset);
894894

895895
/**
896896
* Set the default response content type header. It is used if the response content type header
@@ -899,14 +899,14 @@ default long getContentLength() {
899899
* @param contentType Content type.
900900
* @return This context.
901901
*/
902-
@Nonnull Context setDefaultContentType(@Nonnull MediaType contentType);
902+
@Nonnull Context setDefaultResponseType(@Nonnull MediaType contentType);
903903

904904
/**
905905
* Get response content type.
906906
*
907907
* @return Response content type.
908908
*/
909-
@Nonnull MediaType getResponseContentType();
909+
@Nonnull MediaType getResponseType();
910910

911911
/**
912912
* Set response status code.
@@ -965,7 +965,7 @@ default long getContentLength() {
965965
* @return HTTP channel as output stream. Usually for chunked responses.
966966
*/
967967
default @Nonnull OutputStream responseStream(@Nonnull MediaType contentType) {
968-
setContentType(contentType);
968+
setResponseType(contentType);
969969
return responseStream();
970970
}
971971

@@ -979,7 +979,7 @@ default long getContentLength() {
979979
*/
980980
default @Nonnull Context responseStream(@Nonnull MediaType contentType,
981981
@Nonnull Throwing.Consumer<OutputStream> consumer) throws Exception {
982-
setContentType(contentType);
982+
setResponseType(contentType);
983983
return responseStream(consumer);
984984
}
985985

@@ -1169,9 +1169,9 @@ default long getContentLength() {
11691169
InputStream content = file.stream();
11701170
long length = file.getFileSize();
11711171
if (length > 0) {
1172-
setContentLength(length);
1172+
setResponseLength(length);
11731173
}
1174-
setDefaultContentType(file.getContentType());
1174+
setDefaultResponseType(file.getContentType());
11751175
if (content instanceof FileInputStream) {
11761176
sendFile(((FileInputStream) content).getChannel());
11771177
} else {
@@ -1188,7 +1188,7 @@ default long getContentLength() {
11881188
*/
11891189
default @Nonnull Context sendFile(@Nonnull Path file) {
11901190
try {
1191-
setDefaultContentType(MediaType.byFile(file));
1191+
setDefaultResponseType(MediaType.byFile(file));
11921192
return sendFile(FileChannel.open(file));
11931193
} catch (IOException x) {
11941194
throw Throwing.sneakyThrow(x);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public interface ErrorHandler {
7979
.append("</html>");
8080

8181
ctx
82-
.setContentType(MediaType.html)
82+
.setResponseType(MediaType.html)
8383
.setStatusCode(statusCode)
8484
.sendString(html.toString());
8585
} else {
8686
String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason());
87-
ctx.setContentType(json)
87+
ctx.setResponseType(json)
8888
.setStatusCode(statusCode)
8989
.sendString("{\"message\":\"" + message + "\",\"statusCode\":" + statusCode.value()
9090
+ ",\"reason\":\"" + statusCode.reason() + "\"}");

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

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

2929
/** To string renderer. */
3030
Renderer TO_STRING = (ctx, value) -> {
31-
if (ctx.accept(ctx.getResponseContentType())) {
31+
if (ctx.accept(ctx.getResponseType())) {
3232
return value.toString().getBytes(StandardCharsets.UTF_8);
3333
}
3434
throw new Err(StatusCode.NOT_ACCEPTABLE);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface TemplateEngine extends Renderer {
3737
String apply(Context ctx, ModelAndView modelAndView) throws Exception;
3838

3939
@Override default byte[] render(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
40-
ctx.setDefaultContentType(MediaType.html);
40+
ctx.setDefaultResponseType(MediaType.html);
4141
String output = apply(ctx, (ModelAndView) value);
4242
return output.getBytes(StandardCharsets.UTF_8);
4343
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Stack executor(Executor executor) {
122122
};
123123

124124
static final Route.Before SUPPORT_MEDIA_TYPE = ctx -> {
125-
MediaType contentType = ctx.getContentType();
125+
MediaType contentType = ctx.getRequestType();
126126
if (contentType == null) {
127127
throw new Err(StatusCode.UNSUPPORTED_MEDIA_TYPE);
128128
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public SingleByteRange(String value, long start, long end, long contentLength,
116116
@Override public @Nonnull ByteRange apply(@Nonnull Context ctx) {
117117
ctx.setHeader("Accept-Ranges", "bytes");
118118
ctx.setHeader("Content-Range", contentRange);
119-
ctx.setContentLength(contentLength);
119+
ctx.setResponseLength(contentLength);
120120
ctx.setStatusCode(StatusCode.PARTIAL_CONTENT);
121121
return this;
122122
}

jooby/src/main/java/io/jooby/internal/handler/SendFileChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public SendFileChannel(Route.Handler next) {
4242
}
4343
if (file instanceof Path) {
4444
if (Files.exists((Path) file)) {
45-
ctx.setDefaultContentType(MediaType.byFile((Path) file));
45+
ctx.setDefaultResponseType(MediaType.byFile((Path) file));
4646
file = FileChannel.open((Path) file, StandardOpenOption.READ);
4747
} else {
4848
throw new FileNotFoundException(file.toString());

jooby/src/main/java/io/jooby/internal/handler/reactive/ChunkedSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void onNext(Object item) {
5050
byte[] data = renderer.render(ctx, item);
5151

5252
if (responseType == null) {
53-
responseType = ctx.getResponseContentType();
53+
responseType = ctx.getResponseType();
5454
if (responseType.isJson()) {
5555
data = prepend(data, JSON_LBRACKET);
5656
}

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
@@ -67,7 +67,7 @@ public static final ObjectMapper defaultObjectMapper() {
6767
}
6868

6969
@Override public byte[] render(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
70-
ctx.setDefaultContentType(MediaType.json);
70+
ctx.setDefaultResponseType(MediaType.json);
7171
return mapper.writeValueAsBytes(value);
7272
}
7373

0 commit comments

Comments
 (0)