Skip to content

Commit

Permalink
Relax the validation of HTTP methods on route registration
Browse files Browse the repository at this point in the history
Despite the availability of API in Router to add a handler for
any HttpMethod, DefaultRouter rejects routes if they do not
specify one of HEAD, GET, PATCH, POST, PUT, or DELETE.
  • Loading branch information
gitblit committed Jan 7, 2015
1 parent 82d885a commit 6474885
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Expand Up @@ -15,7 +15,7 @@
*/ */
package ro.fortsoft.pippo.core.route; package ro.fortsoft.pippo.core.route;


import ro.fortsoft.pippo.core.HttpConstants; import ro.fortsoft.pippo.core.PippoRuntimeException;
import ro.fortsoft.pippo.core.controller.Controller; import ro.fortsoft.pippo.core.controller.Controller;
import ro.fortsoft.pippo.core.controller.ControllerHandler; import ro.fortsoft.pippo.core.controller.ControllerHandler;
import ro.fortsoft.pippo.core.util.StringUtils; import ro.fortsoft.pippo.core.util.StringUtils;
Expand Down Expand Up @@ -108,31 +108,22 @@ public List<Route> getRoutes(String requestMethod) {
return routes; return routes;
} }


protected void validateRoute(Route route) throws Exception { protected void validateRoute(Route route) {
// validate the request method // validate the request method
if (!existsRequestMethod(route.getRequestMethod())) { if (StringUtils.isNullOrEmpty(route.getRequestMethod())) {
throw new Exception("Invalid request method: " + route.getRequestMethod()); throw new PippoRuntimeException("Unspecified request method!");
} }


// validate the uri pattern // validate the uri pattern
String uriPattern = route.getUriPattern(); String uriPattern = route.getUriPattern();
if (uriPattern == null || uriPattern.isEmpty()) { if (StringUtils.isNullOrEmpty(uriPattern)) {
throw new Exception("The uri pattern cannot be null or empty"); throw new PippoRuntimeException("The uri pattern cannot be null or empty");
} }
} }


private boolean existsRequestMethod(String requestMethod) {
return HttpConstants.Method.GET.equals(requestMethod)
|| HttpConstants.Method.POST.equals(requestMethod)
|| HttpConstants.Method.PUT.equals(requestMethod)
|| HttpConstants.Method.HEAD.equals(requestMethod)
|| HttpConstants.Method.DELETE.equals(requestMethod)
|| HttpConstants.Method.PATCH.equals(requestMethod);
}

@Override @Override
public List<RouteMatch> findRoutes(String requestUri, String requestMethod) { public List<RouteMatch> findRoutes(String requestUri, String requestMethod) {
log.debug("Finding route for '{} {}'", requestUri, requestMethod); log.debug("Finding route matches for {} '{}'", requestMethod, requestUri);
List<PatternBinding> bindings = bindingsCache.get(requestMethod); List<PatternBinding> bindings = bindingsCache.get(requestMethod);
if (bindings == null) { if (bindings == null) {
return Collections.emptyList(); return Collections.emptyList();
Expand All @@ -150,8 +141,8 @@ public List<RouteMatch> findRoutes(String requestUri, String requestMethod) {
} }


@Override @Override
public void addRoute(Route route) throws Exception { public void addRoute(Route route) {
log.debug("Add route for '{} {}'", route.getRequestMethod(), route.getUriPattern()); log.debug("Add route for {} '{}'", route.getRequestMethod(), route.getUriPattern());
validateRoute(route); validateRoute(route);
routes.add(route); routes.add(route);


Expand Down
Expand Up @@ -70,10 +70,10 @@ public void testEmptyUriPatternRoute() throws Exception {
} }


@Test @Test
public void testInvalidMethodRequestRoute() throws Exception { public void testUnspecifiedMethodRequestRoute() throws Exception {
Route route = new Route("/.*", "GETT", new EmptyRouteHandler()); Route route = new Route("/.*", "", new EmptyRouteHandler());
thrown.expect(Exception.class); thrown.expect(Exception.class);
thrown.expectMessage("Invalid request method"); thrown.expectMessage("Unspecified request method");
router.addRoute(route); router.addRoute(route);
} }


Expand Down

0 comments on commit 6474885

Please sign in to comment.