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

Support @HEAD and @OPTIONS in sub-resources #43440

Merged
merged 1 commit into from
Sep 23, 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 @@ -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
Loading