Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 921f95a

Browse files
committed
gracefully fail with unsupported HTTP methods
1 parent 8af49d7 commit 921f95a

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

endpoints-framework/src/main/java/com/google/api/server/spi/dispatcher/HttpMethod.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.google.api.server.spi.dispatcher;
1717

18+
import com.google.common.base.Preconditions;
19+
import com.google.common.collect.ImmutableMap;
20+
1821
/**
1922
* Possible HTTP methods for use in the dispatcher.
2023
*/
@@ -23,5 +26,22 @@ enum HttpMethod {
2326
POST,
2427
PUT,
2528
DELETE,
26-
PATCH
29+
PATCH;
30+
31+
private static final ImmutableMap<String, HttpMethod> STRING_TO_ENUM =
32+
ImmutableMap.<String, HttpMethod>builder()
33+
.put("GET", GET)
34+
.put("POST", POST)
35+
.put("PUT", PUT)
36+
.put("DELETE", DELETE)
37+
.put("PATCH", PATCH)
38+
.build();
39+
40+
/**
41+
* Returns an {@link HttpMethod} corresponding to a string value, or null if it doesn't exist.
42+
*/
43+
public static HttpMethod fromString(String method) {
44+
Preconditions.checkNotNull(method, "method");
45+
return STRING_TO_ENUM.get(method.toUpperCase());
46+
}
2747
}

endpoints-framework/src/main/java/com/google/api/server/spi/dispatcher/PathDispatcher.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ private PathDispatcher(Builder<ContextT> builder) {
4242
public boolean dispatch(String httpMethod, String path, ContextT context) throws IOException {
4343
Preconditions.checkNotNull(httpMethod, "httpMethod");
4444
Preconditions.checkNotNull(path, "path");
45-
Result<DispatcherHandler<ContextT>> result =
46-
trie.resolve(HttpMethod.valueOf(httpMethod.toUpperCase()), path);
47-
if (result != null) {
48-
context.setRawPathParameters(result.getRawParameters());
49-
result.getResult().handle(context);
50-
return true;
45+
HttpMethod method = HttpMethod.fromString(httpMethod);
46+
if (method != null) {
47+
Result<DispatcherHandler<ContextT>> result = trie.resolve(method, path);
48+
if (result != null) {
49+
context.setRawPathParameters(result.getRawParameters());
50+
result.getResult().handle(context);
51+
return true;
52+
}
5153
}
5254
return false;
5355
}

endpoints-framework/src/test/java/com/google/api/server/spi/dispatcher/PathDispatcherTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,10 @@ public void builderNullHandler() {
121121
// expected
122122
}
123123
}
124+
125+
@Test
126+
public void methodNotKnown() throws IOException {
127+
PathDispatcher<DispatcherContext> dispatcher = PathDispatcher.builder().build();
128+
assertThat(dispatcher.dispatch("INVALID", "test/one/two/3", context)).isFalse();
129+
}
124130
}

0 commit comments

Comments
 (0)