From 65c428881a18e4aeff2ef9e34a00f2b8eb90d681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Ko=C5=A1ir?= Date: Thu, 24 Oct 2024 18:11:12 +0900 Subject: [PATCH 1/2] Add missing static constructors to `ResponseEntity` --- .../armeria/common/ResponseEntity.java | 43 ++++++++++++++----- ...AnnotatedServiceResponseConverterTest.java | 6 +-- .../annotation/AnnotatedServiceTest.java | 7 ++- .../annotation/ResponseEntityUtilTest.java | 2 +- ...servableResponseConverterFunctionTest.java | 11 +++++ 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java b/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java index ff43d3bc975..227ffcd76c9 100644 --- a/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java +++ b/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java @@ -30,7 +30,7 @@ public interface ResponseEntity extends HttpEntity { /** * Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders}. */ - static ResponseEntity of(ResponseHeaders headers) { + static ResponseEntity of(ResponseHeaders headers) { return of(headers, null, HttpHeaders.of()); } @@ -43,15 +43,6 @@ static ResponseEntity of(ResponseHeaders headers, T content) { return of(headers, content, HttpHeaders.of()); } - /** - * Returns a newly created {@link ResponseEntity} with the specified {@code content} and - * {@link HttpStatus#OK} status. - */ - static ResponseEntity of(T content) { - requireNonNull(content, "content"); - return of(ResponseHeaders.of(HttpStatus.OK), content, HttpHeaders.of()); - } - /** * Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders}, * {@code content} and {@linkplain HttpHeaders trailers}. @@ -62,6 +53,38 @@ static ResponseEntity of(ResponseHeaders headers, @Nullable T content, Ht return new DefaultResponseEntity<>(headers, content, trailers); } + /** + * Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus}. + */ + static ResponseEntity of(HttpStatus status) { + return of(ResponseHeaders.of(status)); + } + + /** + * Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus} and + * {@code content}. + */ + static ResponseEntity of(HttpStatus status, T content) { + return of(ResponseHeaders.of(status), content); + } + + /** + * Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus}, + * {@code content} and {@linkplain HttpHeaders trailers}. + */ + static ResponseEntity of(HttpStatus status, @Nullable T content, HttpHeaders trailers) { + return of(ResponseHeaders.of(status), content, trailers); + } + + /** + * Returns a newly created {@link ResponseEntity} with the specified {@code content} and + * {@link HttpStatus#OK} status. + */ + static ResponseEntity of(T content) { + requireNonNull(content, "content"); + return of(HttpStatus.OK, content); + } + /** * Returns the {@link ResponseHeaders} of this response. */ diff --git a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceResponseConverterTest.java b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceResponseConverterTest.java index 5f60ddb375c..723a1cedd33 100644 --- a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceResponseConverterTest.java +++ b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceResponseConverterTest.java @@ -416,15 +416,13 @@ public ResponseEntity expectSpecifiedNoContent() { public ResponseEntity expectNotModified() { // Will send '304 Not Modified' because ResponseEntity overrides the @StatusCode // annotation. - return ResponseEntity.of(ResponseHeaders.of(HttpStatus.NOT_MODIFIED)); + return ResponseEntity.of(HttpStatus.NOT_MODIFIED); } @Get("/expect-unauthorized") public ResponseEntity expectUnauthorized() { // Will send '401 Unauthorized' because the content of ResponseEntity is HttpResponse. - return ResponseEntity.of( - ResponseHeaders.of(HttpStatus.OK), - HttpResponse.of(HttpStatus.UNAUTHORIZED)); + return ResponseEntity.of(HttpStatus.OK, HttpResponse.of(HttpStatus.UNAUTHORIZED)); } @Get("/expect-no-content-from-converter") diff --git a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceTest.java b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceTest.java index e078a605b07..bb5f68a8403 100644 --- a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceTest.java +++ b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/AnnotatedServiceTest.java @@ -900,7 +900,7 @@ public static class MyAnnotatedService15 { @Path("/response-entity-void") public ResponseEntity responseEntityVoid(RequestContext ctx) { validateContext(ctx); - return ResponseEntity.of(ResponseHeaders.of(HttpStatus.OK)); + return ResponseEntity.of(HttpStatus.OK); } @Get @@ -914,15 +914,14 @@ public ResponseEntity responseEntityString(RequestContext ctx, @Param("n @Path("/response-entity-status") public ResponseEntity responseEntityResponseData(RequestContext ctx) { validateContext(ctx); - return ResponseEntity.of(ResponseHeaders.of(HttpStatus.MOVED_PERMANENTLY)); + return ResponseEntity.of(HttpStatus.MOVED_PERMANENTLY); } @Get @Path("/response-entity-http-response") public ResponseEntity responseEntityHttpResponse(RequestContext ctx) { validateContext(ctx); - return ResponseEntity.of(ResponseHeaders.of(HttpStatus.OK), - HttpResponse.of(HttpStatus.UNAUTHORIZED)); + return ResponseEntity.of(HttpStatus.OK, HttpResponse.of(HttpStatus.UNAUTHORIZED)); } } diff --git a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/ResponseEntityUtilTest.java b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/ResponseEntityUtilTest.java index 2417ebd9605..2a6b459464c 100644 --- a/core/src/test/java/com/linecorp/armeria/internal/server/annotation/ResponseEntityUtilTest.java +++ b/core/src/test/java/com/linecorp/armeria/internal/server/annotation/ResponseEntityUtilTest.java @@ -72,7 +72,7 @@ void useNegotiatedResponseMediaType() { .routingResult(routingResult) .build(); - final ResponseEntity result = ResponseEntity.of(ResponseHeaders.of(HttpStatus.OK)); + final ResponseEntity result = ResponseEntity.of(HttpStatus.OK); final ResponseHeaders actual = ResponseEntityUtil.buildResponseHeaders(ctx, result); assertThat(actual.contentType()).isEqualTo(MediaType.JSON_UTF_8); } diff --git a/rxjava3/src/test/java/com/linecorp/armeria/server/rxjava3/ObservableResponseConverterFunctionTest.java b/rxjava3/src/test/java/com/linecorp/armeria/server/rxjava3/ObservableResponseConverterFunctionTest.java index 12f1a282695..1801528c3b6 100644 --- a/rxjava3/src/test/java/com/linecorp/armeria/server/rxjava3/ObservableResponseConverterFunctionTest.java +++ b/rxjava3/src/test/java/com/linecorp/armeria/server/rxjava3/ObservableResponseConverterFunctionTest.java @@ -35,6 +35,7 @@ import com.linecorp.armeria.common.HttpResponse; import com.linecorp.armeria.common.HttpStatus; import com.linecorp.armeria.common.MediaType; +import com.linecorp.armeria.common.ResponseEntity; import com.linecorp.armeria.common.ResponseHeaders; import com.linecorp.armeria.internal.testing.AnticipatedException; import com.linecorp.armeria.internal.testing.GenerateNativeImageTrace; @@ -90,6 +91,11 @@ public Maybe httpResponse() { public Maybe> httpResult() { return Maybe.just(HttpResult.of("a")); } + + @Get("/response-entity") + public Maybe> responseEntity() { + return Maybe.just(ResponseEntity.of("a")); + } }); sb.annotatedService("/single", new Object() { @@ -118,6 +124,11 @@ public Single httpResponse() { public Single> httpResult() { return Single.just(HttpResult.of("a")); } + + @Get("/response-entity") + public Single> responseEntity() { + return Single.just(ResponseEntity.of("a")); + } }); sb.annotatedService("/completable", new Object() { From 0e51aeec632f99f823cb3ee70668106f3069826f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Ko=C5=A1ir?= Date: Thu, 24 Oct 2024 18:54:59 +0900 Subject: [PATCH 2/2] Update signatures --- .../com/linecorp/armeria/common/ResponseEntity.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java b/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java index 227ffcd76c9..a6ee974aa4f 100644 --- a/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java +++ b/core/src/main/java/com/linecorp/armeria/common/ResponseEntity.java @@ -18,7 +18,6 @@ import static java.util.Objects.requireNonNull; -import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.common.annotation.UnstableApi; /** @@ -31,7 +30,8 @@ public interface ResponseEntity extends HttpEntity { * Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders}. */ static ResponseEntity of(ResponseHeaders headers) { - return of(headers, null, HttpHeaders.of()); + requireNonNull(headers, "headers"); + return new DefaultResponseEntity<>(headers, null, HttpHeaders.of()); } /** @@ -39,7 +39,6 @@ static ResponseEntity of(ResponseHeaders headers) { * {@code content}. */ static ResponseEntity of(ResponseHeaders headers, T content) { - requireNonNull(content, "content"); return of(headers, content, HttpHeaders.of()); } @@ -47,8 +46,9 @@ static ResponseEntity of(ResponseHeaders headers, T content) { * Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders}, * {@code content} and {@linkplain HttpHeaders trailers}. */ - static ResponseEntity of(ResponseHeaders headers, @Nullable T content, HttpHeaders trailers) { + static ResponseEntity of(ResponseHeaders headers, T content, HttpHeaders trailers) { requireNonNull(headers, "headers"); + requireNonNull(content, "content"); requireNonNull(trailers, "trailers"); return new DefaultResponseEntity<>(headers, content, trailers); } @@ -72,7 +72,7 @@ static ResponseEntity of(HttpStatus status, T content) { * Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus}, * {@code content} and {@linkplain HttpHeaders trailers}. */ - static ResponseEntity of(HttpStatus status, @Nullable T content, HttpHeaders trailers) { + static ResponseEntity of(HttpStatus status, T content, HttpHeaders trailers) { return of(ResponseHeaders.of(status), content, trailers); } @@ -81,7 +81,6 @@ static ResponseEntity of(HttpStatus status, @Nullable T content, HttpHead * {@link HttpStatus#OK} status. */ static ResponseEntity of(T content) { - requireNonNull(content, "content"); return of(HttpStatus.OK, content); }