/** * Verifies that a route is NOT handled. * * For instance you would call this route to make sure _test routes are not handled * in production mode. */ public void isNotHandledByRoutesInRouter() { assertTrue(router.getRouteFor(httpMethod, url) == null); }
public void initRoutes() throws Exception { String applicationRoutesClassName = ninjaBaseDirectoryResolver.resolveApplicationClassName(ROUTES_CONVENTION_LOCATION); if (SwissKnife.doesClassExist(applicationRoutesClassName, this)) { Class<?> clazz = Class.forName(applicationRoutesClassName); ApplicationRoutes applicationRoutes = (ApplicationRoutes) injector .getInstance(clazz); Router router = this.injector.getInstance(Router.class); applicationRoutes.init(router); router.compileRoutes(); } }
private Builder builder(Class<?> controllerClass, String methodName) { Optional<Route> route = this.router.getRouteForControllerClassAndMethod( controllerClass, methodName); if (route.isPresent()) { return new Builder(this.ninjaProperties.getContextPath(), route.get()); } throw new IllegalArgumentException("Reverse route not found for " + controllerClass.getCanonicalName() + "." + methodName); }
String reverseRoute = router.getReverseRoute( clazz, strings.get(1),
/** * Takes all methods and registers them at the controller using the path: Class:@Path + Method:@Path. * If no @Path Annotation is present at the method just the Class:@Path is used. */ private void registerMethods() { // register routes for all the methods for (Method method : methods) { final Class<?> controllerClass = method.getDeclaringClass(); final Path methodPath = method.getAnnotation(Path.class); final Set<String> controllerPaths = controllers .get(controllerClass); String[] paths = {"/"}; if (methodPath != null) { paths = methodPath.value(); } for (String controllerPath : controllerPaths) { for (String methodPathSpec : paths) { final String httpMethod = getHttpMethod(method); final String fullPath = controllerPath + methodPathSpec; final String methodName = method.getName(); router.METHOD(httpMethod).route(fullPath) .with(controllerClass, methodName); } } } }
/** * The worker method used internally. Checks if that route is active. * * @param router The router to ask. * @param httpMethod The method to use (GET, POST...) * @param url The url to use ("/_test" or "/profile/myId" ...) * @param controllerClass The controller class supposed to handle the url. * @param controllerMethodName The controller method supposed to handle the url * @return true if handled by given configuration / false if not. */ private static boolean isHttpMethodAndUrlMatchedByControllerClassAndControllerMethod( Router router, String httpMethod, String url, Class controllerClass, String controllerMethodName) { if (!router.getRouteFor(httpMethod, url).getControllerClass().equals(controllerClass)) { return false; } if (!router.getRouteFor(httpMethod, url).getControllerMethod().getName().equals( controllerMethodName)) { return false; } return true; }
@Override @Timed public void onRouteRequest(Context.Impl context) { activeRequests.inc(); String httpMethod = context.getMethod(); Route route = router.getRouteFor(httpMethod, context.getRequestPath()); context.setRoute(route); if (route != null) { allRequestsMeter.mark(); try { Result result = route.getFilterChain().next(context); resultHandler.handleResult(result, context); } catch (Exception exception) { if (exception instanceof BadRequestException) { badRequests.mark(); } else { internalServerErrors.mark(); } Result result = onException(context, exception); renderErrorResultAndCatchAndLogExceptions(result, context); } } else { // throw a 404 "not found" because we did not find the route routesNotFound.mark(); Result result = getNotFoundResult(context); renderErrorResultAndCatchAndLogExceptions(result, context); } activeRequests.dec(); }
Route route = router.getRouteFor(httpMethod, context.getRequestPath());