Skip to content

Commit

Permalink
Support @Head and @options in sub-resources
Browse files Browse the repository at this point in the history
This is the same exact support as exists for normal
Resource methods

Fixes: quarkusio#43422
  • Loading branch information
geoand committed Sep 23, 2024
1 parent ffd0c78 commit 898b038
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.container.CompletionCallback;
import jakarta.ws.rs.core.Response;
Expand Down Expand Up @@ -58,14 +59,34 @@ public void onComplete(Throwable throwable) {
if (target == null) {
throw new RuntimeException("Resource locator method returned object that was not a resource: " + locator);
}

RequestMapper<RuntimeResource> mapper = target.get(requestContext.getMethod());
boolean hadNullMethodMapper = false;
if (mapper == null) {
mapper = target.get(null); //another layer of resource locators maybe
// we set this without checking if we matched, but we only use it after
// we check for a null mapper, so by the time we use it, it must have meant that
// we had a matcher for a null method
hadNullMethodMapper = true;
String requestMethod = requestContext.getMethod();
if (requestMethod.equals(HttpMethod.HEAD)) {
mapper = target.get(HttpMethod.GET);
} else if (requestMethod.equals(HttpMethod.OPTIONS)) {
Set<String> allowedMethods = new HashSet<>();
for (String method : target.keySet()) {
if (method == null) {
continue;
}
allowedMethods.add(method);
}
allowedMethods.add(HttpMethod.OPTIONS);
allowedMethods.add(HttpMethod.HEAD);
requestContext.abortWith(Response.ok().allow(allowedMethods).build());
return;
}

if (mapper == null) {
mapper = target.get(null); //another layer of resource locators maybe
// we set this without checking if we matched, but we only use it after
// we check for a null mapper, so by the time we use it, it must have meant that
// we had a matcher for a null method
hadNullMethodMapper = true;
}
}
if (mapper == null) {
throw new WebApplicationException(Response.status(Response.Status.METHOD_NOT_ALLOWED.getStatusCode()).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ public void testSubResource() throws Exception {
Assertions.assertEquals("Boo! - fred", response.readEntity(String.class), "Wrong content of response");
}

@Test
@DisplayName("Test Sub Resource - HEAD")
public void testSubResourceHead() throws Exception {
Response response = client.target(generateURL("/path/sub/fred")).request().head();
Assertions.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
@DisplayName("Test Sub Resource - OPTIONS")
public void testSubResourceOptions() throws Exception {
Response response = client.target(generateURL("/path/sub/fred")).request().options();
Assertions.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
@DisplayName("Test Return Sub Resource As Class")
public void testReturnSubResourceAsClass() throws Exception {
Expand Down

0 comments on commit 898b038

Please sign in to comment.