/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router type * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); return routes.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router type * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); return routes.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); return router.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); return router.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
public static Result routeAndCall(Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); if(routes.routes().isDefinedAt(request._underlyingRequest())) { return invokeHandler(routes.routes().apply(request._underlyingRequest()), request, timeout); } else { return null; } } catch(RuntimeException e) { throw e; } catch(Throwable t) { throw new RuntimeException(t); } }
@SuppressWarnings(value = "unchecked") private static Result invokeHandler(play.api.Application app, play.api.mvc.Handler handler, Request requestBuilder, long timeout) { if (handler instanceof play.api.mvc.Action) { play.api.mvc.Action action = (play.api.mvc.Action) handler; return wrapScalaResult(action.apply(requestBuilder.asScala()), timeout); } else if (handler instanceof JavaHandler) { final play.api.inject.Injector injector = app.injector(); final JavaHandlerComponents handlerComponents = injector.instanceOf(JavaHandlerComponents.class); return invokeHandler( app, ((JavaHandler) handler).withComponents(handlerComponents), requestBuilder, timeout ); } else { throw new RuntimeException("This is not a JavaAction and can't be invoked this way."); } }
@SuppressWarnings(value = "unchecked") private static Result invokeHandler(play.api.mvc.Handler handler, Request requestBuilder, long timeout) { if (handler instanceof play.api.mvc.Action) { play.api.mvc.Action action = (play.api.mvc.Action) handler; return wrapScalaResult(action.apply(requestBuilder._underlyingRequest()), timeout); } else if (handler instanceof JavaHandler) { return invokeHandler( ((JavaHandler) handler).withComponents(Play.application().injector().instanceOf(JavaHandlerComponents.class)), requestBuilder, timeout ); } else { throw new RuntimeException("This is not a JavaAction and can't be invoked this way."); } }
public static Result routeAndCall(Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); if(router.routes().isDefinedAt(request._underlyingRequest())) { return invokeHandler(router.routes().apply(request._underlyingRequest()), request, timeout); } else { return null; } } catch(RuntimeException e) { throw e; } catch(Throwable t) { throw new RuntimeException(t); } }
@SuppressWarnings(value = "unchecked") private static Result invokeHandler(play.api.Application app, play.api.mvc.Handler handler, Request requestBuilder, long timeout) { if (handler instanceof play.api.mvc.Action) { play.api.mvc.Action action = (play.api.mvc.Action) handler; return wrapScalaResult(action.apply(requestBuilder.asScala()), timeout); } else if (handler instanceof JavaHandler) { final play.api.inject.Injector injector = app.injector(); final JavaHandlerComponents handlerComponents = injector.instanceOf(JavaHandlerComponents.class); return invokeHandler( app, ((JavaHandler) handler).withComponents(handlerComponents), requestBuilder, timeout ); } else { throw new RuntimeException("This is not a JavaAction and can't be invoked this way."); } }
/** * Call an action method while decorating it with the right @With interceptors. */ public static Result callAction(HandlerRef actionReference, FakeRequest fakeRequest) { play.api.mvc.HandlerRef handlerRef = (play.api.mvc.HandlerRef)actionReference; return invokeHandler(handlerRef.handler(), fakeRequest); }
/** * Use the Router to determine the Action to call for this request and executes it. * @deprecated * @see #route instead */ public static Result routeAndCall(Class<? extends play.core.Router.Routes> router, FakeRequest fakeRequest) { try { play.core.Router.Routes routes = (play.core.Router.Routes)router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); if(routes.routes().isDefinedAt(fakeRequest.getWrappedRequest())) { return invokeHandler(routes.routes().apply(fakeRequest.getWrappedRequest()), fakeRequest); } else { return null; } } catch(RuntimeException e) { throw e; } catch(Throwable t) { throw new RuntimeException(t); } }