Search in sources :

Example 6 with LoadedController

use of org.webpieces.router.impl.loader.LoadedController in project webpieces by deanhiller.

the class ServiceInvoker method invokeSvc.

public XFuture<Void> invokeSvc(MethodMeta meta, String i18nBundleName, Endpoint service, Processor processor, ProxyStreamHandle handle) {
    if (processor == null || meta == null || service == null || handle == null)
        throw new IllegalArgumentException("nothing can be null into this metehod");
    handle.initJustBeforeInvoke(reverseRoutes, meta);
    RequestContext requestCtx = meta.getCtx();
    LoadedController loadedController = meta.getLoadedController();
    Messages messages = new Messages(i18nBundleName, "webpieces");
    requestCtx.setMessages(messages);
    XFuture<Action> response = futureUtil.catchBlockWrap(() -> invokeService(service, meta), (t) -> convert(loadedController, t));
    return response.thenCompose(resp -> continueProcessing(handle, meta, resp, processor));
}
Also used : Action(org.webpieces.router.api.controller.actions.Action) Messages(org.webpieces.ctx.api.Messages) LoadedController(org.webpieces.router.impl.loader.LoadedController) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 7 with LoadedController

use of org.webpieces.router.impl.loader.LoadedController in project webpieces by deanhiller.

the class StreamProxy method openStream.

@Override
public RouterStreamRef openStream(MethodMeta meta, ProxyStreamHandle handle) {
    RequestContext requestCtx = meta.getCtx();
    LoadedController loadedController = meta.getLoadedController();
    Object instance = loadedController.getControllerInstance();
    Method controllerMethod = loadedController.getControllerMethod();
    Parameter[] parameters = loadedController.getParameters();
    if (parameters.length != 1)
        throw new IllegalArgumentException("Your method='" + controllerMethod + "' MUST one parameter and does not.  It needs to take a RouterStreamHandler");
    else if (!ResponseStreamHandle.class.equals(parameters[0].getType()))
        throw new IllegalArgumentException("The single parameter must be RouterStreamHandle and was not for this method='" + controllerMethod + "'");
    else if (!StreamRef.class.equals(controllerMethod.getReturnType()))
        throw new IllegalArgumentException("The return value must be a subclass of StreamRef and was not for this method='" + controllerMethod + "'");
    StreamRef streamRef = invokeStream(meta, controllerMethod, instance, requestCtx, handle);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    XFuture<StreamWriter> newFuture = futureUtil.catchBlockWrap(() -> writer, (t) -> convert(loadedController, t));
    Function<CancelReason, XFuture<Void>> cancelFunc = (reason) -> streamRef.cancel(reason);
    return new RouterStreamRef("streamProxy", newFuture, cancelFunc);
}
Also used : LoadedController(org.webpieces.router.impl.loader.LoadedController) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Function(java.util.function.Function) FutureHelper(org.webpieces.util.futures.FutureHelper) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) ControllerException(org.webpieces.router.api.exceptions.ControllerException) MethodMeta(org.webpieces.router.api.routes.MethodMeta) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Parameter(java.lang.reflect.Parameter) ServiceInvoker(org.webpieces.router.impl.routeinvoker.ServiceInvoker) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) StreamService(org.webpieces.router.api.streams.StreamService) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) LoadedController(org.webpieces.router.impl.loader.LoadedController) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) StreamRef(com.webpieces.http2.api.streaming.StreamRef) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Parameter(java.lang.reflect.Parameter) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 8 with LoadedController

use of org.webpieces.router.impl.loader.LoadedController in project webpieces by deanhiller.

the class ResponseProcessorAppError method createRenderResponse.

public XFuture<Void> createRenderResponse(MethodMeta meta, RenderImpl controllerResponse, ProxyStreamHandle handle) {
    LoadedController loadedController = meta.getLoadedController();
    String controllerName = loadedController.getControllerInstance().getClass().getName();
    String methodName = loadedController.getControllerMethod().getName();
    String relativeOrAbsolutePath = controllerResponse.getRelativeOrAbsolutePath();
    if (relativeOrAbsolutePath == null) {
        relativeOrAbsolutePath = methodName + ".html";
    }
    Map<String, Object> pageArgs = controllerResponse.getPageArgs();
    RequestContext ctx = meta.getCtx();
    // Add context as a page arg:
    pageArgs.put("_context", ctx);
    pageArgs.put("_session", ctx.getSession());
    pageArgs.put("_flash", ctx.getFlash());
    pageArgs.put("_appContext", ctx.getApplicationContext());
    View view = new View(controllerName, methodName, relativeOrAbsolutePath);
    RenderResponse resp = new RenderResponse(view, pageArgs, RouteType.INTERNAL_SERVER_ERROR);
    return handle.sendRenderHtml(resp);
}
Also used : LoadedController(org.webpieces.router.impl.loader.LoadedController) RequestContext(org.webpieces.ctx.api.RequestContext) RenderResponse(org.webpieces.router.impl.dto.RenderResponse) View(org.webpieces.router.impl.dto.View)

Example 9 with LoadedController

use of org.webpieces.router.impl.loader.LoadedController in project webpieces by deanhiller.

the class ResponseProcessorHtml method createRenderResponse.

public XFuture<Void> createRenderResponse(MethodMeta meta, RenderImpl controllerResponse, ProxyStreamHandle handle) {
    RequestContext ctx = meta.getCtx();
    RouterRequest request = ctx.getRequest();
    LoadedController loadedController = meta.getLoadedController();
    Method method = loadedController.getControllerMethod();
    // not broken)
    if (HttpMethod.POST == request.method) {
        throw new IllegalReturnValueException("Controller method='" + method + "' MUST follow the PRG " + "pattern(https://en.wikipedia.org/wiki/Post/Redirect/Get) so " + "users don't have a poor experience using your website with the browser back button.  " + "This means on a POST request, you cannot return RenderHtml object and must return Redirects");
    }
    String controllerName = loadedController.getControllerInstance().getClass().getName();
    String methodName = loadedController.getControllerMethod().getName();
    String relativeOrAbsolutePath = controllerResponse.getRelativeOrAbsolutePath();
    if (relativeOrAbsolutePath == null) {
        relativeOrAbsolutePath = methodName + ".html";
    }
    Map<String, Object> pageArgs = controllerResponse.getPageArgs();
    // Add context as a page arg:
    pageArgs.put("_context", ctx);
    pageArgs.put("_session", ctx.getSession());
    pageArgs.put("_flash", ctx.getFlash());
    pageArgs.put("_appContext", ctx.getApplicationContext());
    View view = new View(controllerName, methodName, relativeOrAbsolutePath);
    RenderResponse resp = new RenderResponse(view, pageArgs, RouteType.HTML);
    return handle.sendRenderHtml(resp);
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) LoadedController(org.webpieces.router.impl.loader.LoadedController) RequestContext(org.webpieces.ctx.api.RequestContext) HttpMethod(org.webpieces.ctx.api.HttpMethod) Method(java.lang.reflect.Method) RenderResponse(org.webpieces.router.impl.dto.RenderResponse) View(org.webpieces.router.impl.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 10 with LoadedController

use of org.webpieces.router.impl.loader.LoadedController in project webpieces by deanhiller.

the class ContentTypeBuilderImpl method addRoute.

@Override
public void addRoute(String path, String controllerMethod) {
    if (!controllerMethod.contains("."))
        throw new IllegalArgumentException("controllerMethod must contain a . for the form Controller.method");
    UrlPath p = new UrlPath(routerInfo, path);
    RouteModuleInfo moduleInfo = CurrentRoutes.get();
    RouteInfo routeInfo = new RouteInfo(moduleInfo, controllerMethod);
    // MUST DO loadControllerIntoMeta HERE so stack trace has customer's line in it so he knows EXACTLY what
    // he did wrong when reading the exception!!
    BinderAndLoader container = holder.getFinder().loadContentController(resettingLogic.getInjector(), routeInfo);
    MatchInfo matchInfo = createMatchInfo(p, Port.HTTPS, HttpMethod.POST, holder.getUrlEncoding());
    LoadedController loadedController = container.getMetaAndController().getLoadedController();
    FContentRouter router = new FContentRouter(holder.getRouteInvoker2(), loadedController, moduleInfo, matchInfo, container.getBinder());
    SvcProxyForContent svc = new SvcProxyForContent(holder.getSvcProxyLogic(), futureUtil);
    RouterAndInfo routerAndInfo = new RouterAndInfo(router, routeInfo, container.getMetaAndController(), svc);
    newDynamicRoutes.add(routerAndInfo);
    // There is no routeId...
    // if(routeId != null) //if there is a routeId, then add the reverse mapping
    // resettingLogic.getReverseRoutes().addRoute(routeId, router);
    log.info("scope:'" + routerInfo + "' added content route=" + matchInfo + " method=" + routeInfo.getControllerMethodString());
}
Also used : BinderAndLoader(org.webpieces.router.impl.loader.BinderAndLoader) LoadedController(org.webpieces.router.impl.loader.LoadedController) UrlPath(org.webpieces.router.impl.UrlPath) MatchInfo(org.webpieces.router.impl.routers.MatchInfo) SvcProxyForContent(org.webpieces.router.impl.services.SvcProxyForContent) FContentRouter(org.webpieces.router.impl.routers.FContentRouter) RouteModuleInfo(org.webpieces.router.impl.model.RouteModuleInfo)

Aggregations

LoadedController (org.webpieces.router.impl.loader.LoadedController)12 RequestContext (org.webpieces.ctx.api.RequestContext)7 RouteModuleInfo (org.webpieces.router.impl.model.RouteModuleInfo)5 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)4 MethodMeta (org.webpieces.router.api.routes.MethodMeta)3 UrlPath (org.webpieces.router.impl.UrlPath)3 RenderResponse (org.webpieces.router.impl.dto.RenderResponse)3 View (org.webpieces.router.impl.dto.View)3 MatchInfo (org.webpieces.router.impl.routers.MatchInfo)3 Injector (com.google.inject.Injector)2 Method (java.lang.reflect.Method)2 Messages (org.webpieces.ctx.api.Messages)2 WebInjector (org.webpieces.router.impl.WebInjector)2 BinderAndLoader (org.webpieces.router.impl.loader.BinderAndLoader)2 RouteInfo (org.webpieces.router.impl.routebldr.RouteInfo)2 InvokeInfo (org.webpieces.router.impl.routeinvoker.InvokeInfo)2 Endpoint (org.webpieces.router.impl.routers.Endpoint)2 FContentRouter (org.webpieces.router.impl.routers.FContentRouter)2 SvcProxyFixedRoutes (org.webpieces.router.impl.services.SvcProxyFixedRoutes)2 SvcProxyForContent (org.webpieces.router.impl.services.SvcProxyForContent)2