This repository was archived by the owner on Jun 30, 2023. It is now read-only.
File tree Expand file tree Collapse file tree
main/java/com/google/api/server/spi/dispatcher
test/java/com/google/api/server/spi/dispatcher Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515 */
1616package 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}
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments