-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2018 Odnoklassniki Ltd, Mail.Ru Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package one.nio.http; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Finds a RequestHandler by the given @Path and HTTP method. | ||
* Uses an embedded HashMap for performance reasons. | ||
*/ | ||
public class PathMapper extends HashMap<String, RequestHandler[]> { | ||
|
||
// Add a new mapping | ||
public void add(String path, int[] methods, RequestHandler handler) { | ||
RequestHandler[] handlersByMethod = super.computeIfAbsent(path, p -> new RequestHandler[1]); | ||
if (methods == null) { | ||
handlersByMethod[0] = handler; | ||
} else { | ||
for (int method : methods) { | ||
if (method <= 0 || method >= Request.NUMBER_OF_METHODS) { | ||
throw new IllegalArgumentException("Invalid RequestMethod " + method + " for path " + path); | ||
} | ||
if (method >= handlersByMethod.length) { | ||
handlersByMethod = Arrays.copyOf(handlersByMethod, method + 1); | ||
super.put(path, handlersByMethod); | ||
} | ||
handlersByMethod[method] = handler; | ||
} | ||
} | ||
} | ||
|
||
// Return an existing handler for this HTTP request or null if not found | ||
public RequestHandler find(String path, int method) { | ||
RequestHandler[] handlersByMethod = super.get(path); | ||
if (handlersByMethod == null) { | ||
return null; | ||
} | ||
|
||
// First, try to find a handler with @RequestMethod annotation | ||
if (method > 0 && method < handlersByMethod.length && handlersByMethod[method] != null) { | ||
return handlersByMethod[method]; | ||
} | ||
// Otherwise return the universal handler for all methods | ||
return handlersByMethod[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2018 Odnoklassniki Ltd, Mail.Ru Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package one.nio.http; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface RequestMethod { | ||
int[] value(); // METHOD_GET, METHOD_POST etc. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters