Skip to content

Commit

Permalink
Annotations moved;Adding headers and status change logic rework
Browse files Browse the repository at this point in the history
  • Loading branch information
steel-judoka committed Sep 17, 2021
1 parent d2b98bd commit 69ebd69
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import java.nio.file.Path;

import org.jboss.jandex.DotName;
import org.jboss.resteasy.reactive.ResponseHeader;
import org.jboss.resteasy.reactive.Status;
import org.jboss.resteasy.reactive.multipart.FileUpload;

import io.quarkus.resteasy.reactive.server.common.ResponseHeader;
import io.quarkus.resteasy.reactive.server.common.Status;

final class DotNames {

static final String POPULATE_METHOD_NAME = "populate";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public List<HandlerChainCustomizer> scan(MethodInfo method, ClassInfo actualEndp
ResponseStatusHandler handler = new ResponseStatusHandler();
handler.setStatus(responseStatusValue.asInt());
return Collections.singletonList(new FixedHandlerChainCustomizer(handler,
HandlerChainCustomizer.Phase.AFTER_RESPONSE_CREATED));
HandlerChainCustomizer.Phase.AFTER_METHOD_INVOKE));
}
});
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public List<HandlerChainCustomizer> scan(MethodInfo method, ClassInfo actualEndp
ResponseHeaderHandler handler = new ResponseHeaderHandler();
handler.setHeaders(headers);
return Collections.singletonList(new FixedHandlerChainCustomizer(handler,
HandlerChainCustomizer.Phase.AFTER_RESPONSE_CREATED));
HandlerChainCustomizer.Phase.AFTER_METHOD_INVOKE));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package io.quarkus.resteasy.reactive.server.test.responseheader;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.Header;
import org.jboss.resteasy.reactive.ResponseHeader;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.resteasy.reactive.server.common.Header;
import io.quarkus.resteasy.reactive.server.common.ResponseHeader;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.http.Headers;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;

public class ResponseHeaderTest {
Expand All @@ -23,18 +27,46 @@ public class ResponseHeaderTest {
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void should_return_added_headers() {
public void should_return_added_headers_uni() {
Map<String, String> expectedHeaders = Map.of(
"Access-Control-Allow-Origin", "*",
"Keep-Alive", "timeout=5, max=997");
RestAssured
.given()
.get("/test/multi")
.then()
.statusCode(200)
.headers(expectedHeaders);
}

@Test
public void should_return_added_headers_multi() {
Map<String, String> expectedHeaders = Map.of(
"Access-Control-Allow-Origin", "*",
"Keep-Alive", "timeout=5, max=997");
RestAssured
.given()
.get("/test")
.get("/test/multi")
.then()
.statusCode(200)
.headers(expectedHeaders);
}

@Test
public void should_throw_exception_without_headers_uni() {
Headers headers = RestAssured.given().get("/test/exception_uni")
.then().extract().headers();
assertFalse(headers.hasHeaderWithName("Access-Control-Allow-Origin"));

}

@Test
public void should_throw_exception_without_headers_multi() {
Headers headers = RestAssured.given().get("/test/exception_multi")
.then().extract().headers();
assertFalse(headers.hasHeaderWithName("Access-Control-Allow-Origin"));
}

@Path("/test")
public static class TestResource {

Expand All @@ -43,8 +75,37 @@ public static class TestResource {
@Header(name = "Keep-Alive", value = "timeout=5, max=997"),
})
@GET
public Uni<String> getTest() {
@Path(("/uni"))
public Uni<String> getTestUni() {
return Uni.createFrom().item("test");
}

@ResponseHeader(headers = {
@Header(name = "Access-Control-Allow-Origin", value = "*"),
@Header(name = "Keep-Alive", value = "timeout=5, max=997"),
})
@GET
@Path("/multi")
public Multi<String> getTestMulti() {
return Multi.createFrom().item("test");
}

@ResponseHeader(headers = {
@Header(name = "Access-Control-Allow-Origin", value = "*")
})
@GET
@Path(("/exception_uni"))
public Uni<String> throwExceptionUni() {
return Uni.createFrom().failure(new IllegalArgumentException());
}

@ResponseHeader(headers = {
@Header(name = "Access-Control-Allow-Origin", value = "*")
})
@GET
@Path("/exception_multi")
public Multi<String> throwExceptionMulti() {
return Multi.createFrom().failure(new IllegalArgumentException());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.jboss.resteasy.reactive.Status;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.resteasy.reactive.server.common.Status;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;

public class ResponseStatusTest {
Expand All @@ -20,11 +21,41 @@ public class ResponseStatusTest {
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
public void should_return_changed_status() {
public void should_return_changed_status_uni() {
int expectedStatus = 201;
RestAssured
.given()
.get("/test")
.get("/test/uni")
.then()
.statusCode(expectedStatus);
}

@Test
public void should_return_changed_status_multi() {
int expectedStatus = 201;
RestAssured
.given()
.get("/test/multi")
.then()
.statusCode(expectedStatus);
}

@Test
public void should_not_change_status_uni() {
int expectedStatus = 500;
RestAssured
.given()
.get("/test/exception_uni")
.then()
.statusCode(expectedStatus);
}

@Test
public void should_not_change_status_multi() {
int expectedStatus = 500;
RestAssured
.given()
.get("/test/exception_multi")
.then()
.statusCode(expectedStatus);
}
Expand All @@ -34,8 +65,31 @@ public static class TestResource {

@Status(201)
@GET
public Uni<String> getTest() {
@Path("/uni")
public Uni<String> getTestUni() {
return Uni.createFrom().item("test");
}

@Status(201)
@GET
@Path("/multi")
public Multi<String> getTestMulti() {
return Multi.createFrom().item("test");
}

@Status(201)
@GET
@Path(("/exception_uni"))
public Uni<String> throwExceptionUni() {
return Uni.createFrom().failure(new IllegalArgumentException());
}

@Status(201)
@GET
@Path("/exception_multi")
public Multi<String> throwExceptionMulti() {
return Multi.createFrom().failure(new IllegalArgumentException());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Map;

import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler;

Expand All @@ -20,11 +18,8 @@ public Map<String, String> getHeaders() {

@Override
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
Response response = requestContext.getResponse().get();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
response.getHeaders().add(header.getKey(), header.getValue());
}
requestContext.setResponseHeaders(headers);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.quarkus.resteasy.reactive.server.runtime.responsestatus;

import javax.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.core.LazyResponse;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler;

Expand All @@ -20,11 +17,6 @@ public int getStatus() {

@Override
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
Response response = requestContext.getResponse().get();

Response.ResponseBuilder responseBuilder = Response.fromResponse(response);
responseBuilder.status(status);
LazyResponse.Existing lazyResponse = new LazyResponse.Existing(responseBuilder.build());
requestContext.setResponse(lazyResponse);
requestContext.setResponseStatus(status);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.resteasy.reactive.server.common;
package org.jboss.resteasy.reactive;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.resteasy.reactive.server.common;
package org.jboss.resteasy.reactive;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.resteasy.reactive.server.common;
package org.jboss.resteasy.reactive;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import javax.ws.rs.core.Cookie;
Expand Down Expand Up @@ -132,6 +133,9 @@ public abstract class ResteasyReactiveRequestContext
*/
private List<UriMatch> matchedURIs;

private Map<String, String> responseHeaders;
private Integer responseStatus;

private AsyncResponseImpl asyncResponse;
private SseEventSinkImpl sseEventSink;
private List<PathSegment> pathSegments;
Expand Down Expand Up @@ -230,6 +234,22 @@ public void setMaxPathParams(int maxPathParams) {
}
}

public Map<String, String> getResponseHeaders() {
return responseHeaders;
}

public void setResponseHeaders(Map<String, String> responseHeaders) {
this.responseHeaders = responseHeaders;
}

public Integer getResponseStatus() {
return responseStatus;
}

public void setResponseStatus(Integer responseStatus) {
this.responseStatus = responseStatus;
}

public String getPathParam(int index) {
return doGetPathParam(index, pathParamValues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.ws.rs.RuntimeType;
Expand Down Expand Up @@ -56,7 +57,7 @@ private static byte[] serialiseEntity(ResteasyReactiveRequestContext context, Ob
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean wrote = false;
for (MessageBodyWriter<Object> writer : writers) {
// Spec(API) says we should use class/type/mediaType but doesn't talk about annotations
// Spec(API) says we should use class/type/mediaType but doesn't talk about annotations
if (writer.isWriteable(entityClass, entityType, Serialisers.NO_ANNOTATION, mediaType)) {
// FIXME: spec doesn't really say what headers we should use here
writer.writeTo(entity, entityClass, entityType, Serialisers.NO_ANNOTATION, mediaType,
Expand All @@ -76,10 +77,15 @@ public static void setHeaders(ResteasyReactiveRequestContext context, ServerHttp
// FIXME: spec says we should flush the headers when first message is sent or when the resource method returns, whichever
// happens first
if (!response.headWritten()) {
response.setStatusCode(Response.Status.OK.getStatusCode());
response.setStatusCode(
context.getResponseStatus() != null ? context.getResponseStatus() : Response.Status.OK.getStatusCode());
response.setResponseHeader(HttpHeaders.CONTENT_TYPE, context.getResponseContentType().toString());
response.setChunked(true);
// FIXME: other headers?
if (context.getResponseHeaders() != null) {
for (Map.Entry<String, String> header : context.getResponseHeaders().entrySet()) {
response.addResponseHeader(header.getKey(), header.getValue());
}
}
}
}
}
Loading

0 comments on commit 69ebd69

Please sign in to comment.