Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing static constructors to ResponseEntity #5954

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static java.util.Objects.requireNonNull;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.annotation.UnstableApi;

/**
Expand All @@ -30,36 +29,59 @@ public interface ResponseEntity<T> extends HttpEntity<T> {
/**
* Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders}.
*/
static ResponseEntity<Void> of(ResponseHeaders headers) {
return of(headers, null, HttpHeaders.of());
static <T> ResponseEntity<T> of(ResponseHeaders headers) {
requireNonNull(headers, "headers");
return new DefaultResponseEntity<>(headers, null, HttpHeaders.of());
}

/**
* Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders} and
* {@code content}.
*/
static <T> ResponseEntity<T> of(ResponseHeaders headers, T content) {
requireNonNull(content, "content");
return of(headers, content, HttpHeaders.of());
}

/**
* Returns a newly created {@link ResponseEntity} with the specified {@code content} and
* {@link HttpStatus#OK} status.
* Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders},
* {@code content} and {@linkplain HttpHeaders trailers}.
*/
static <T> ResponseEntity<T> of(T content) {
static <T> ResponseEntity<T> of(ResponseHeaders headers, T content, HttpHeaders trailers) {
requireNonNull(headers, "headers");
requireNonNull(content, "content");
return of(ResponseHeaders.of(HttpStatus.OK), content, HttpHeaders.of());
requireNonNull(trailers, "trailers");
return new DefaultResponseEntity<>(headers, content, trailers);
}

/**
* Returns a newly created {@link ResponseEntity} with the specified {@link ResponseHeaders},
* Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus}.
*/
static <T> ResponseEntity<T> of(HttpStatus status) {
return of(ResponseHeaders.of(status));
}

/**
* Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus} and
* {@code content}.
*/
static <T> ResponseEntity<T> of(HttpStatus status, T content) {
return of(ResponseHeaders.of(status), content);
KarboniteKream marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns a newly created {@link ResponseEntity} with the specified {@link HttpStatus},
* {@code content} and {@linkplain HttpHeaders trailers}.
*/
static <T> ResponseEntity<T> of(ResponseHeaders headers, @Nullable T content, HttpHeaders trailers) {
requireNonNull(headers, "headers");
requireNonNull(trailers, "trailers");
return new DefaultResponseEntity<>(headers, content, trailers);
static <T> ResponseEntity<T> of(HttpStatus status, 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 <T> ResponseEntity<T> of(T content) {
return of(HttpStatus.OK, content);
KarboniteKream marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,13 @@ public ResponseEntity<Void> expectSpecifiedNoContent() {
public ResponseEntity<Void> 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<HttpResponse> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ public static class MyAnnotatedService15 {
@Path("/response-entity-void")
public ResponseEntity<Void> responseEntityVoid(RequestContext ctx) {
validateContext(ctx);
return ResponseEntity.of(ResponseHeaders.of(HttpStatus.OK));
return ResponseEntity.of(HttpStatus.OK);
}

@Get
Expand All @@ -914,15 +914,14 @@ public ResponseEntity<String> responseEntityString(RequestContext ctx, @Param("n
@Path("/response-entity-status")
public ResponseEntity<Void> 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<HttpResponse> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void useNegotiatedResponseMediaType() {
.routingResult(routingResult)
.build();

final ResponseEntity<Void> result = ResponseEntity.of(ResponseHeaders.of(HttpStatus.OK));
final ResponseEntity<Void> result = ResponseEntity.of(HttpStatus.OK);
final ResponseHeaders actual = ResponseEntityUtil.buildResponseHeaders(ctx, result);
assertThat(actual.contentType()).isEqualTo(MediaType.JSON_UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,11 @@ public Maybe<HttpResponse> httpResponse() {
public Maybe<HttpResult<String>> httpResult() {
return Maybe.just(HttpResult.of("a"));
}

@Get("/response-entity")
public Maybe<ResponseEntity<String>> responseEntity() {
return Maybe.just(ResponseEntity.of("a"));
}
});

sb.annotatedService("/single", new Object() {
Expand Down Expand Up @@ -118,6 +124,11 @@ public Single<HttpResponse> httpResponse() {
public Single<HttpResult<String>> httpResult() {
return Single.just(HttpResult.of("a"));
}

@Get("/response-entity")
public Single<ResponseEntity<String>> responseEntity() {
return Single.just(ResponseEntity.of("a"));
}
});

sb.annotatedService("/completable", new Object() {
Expand Down
Loading