Search in sources :

Example 16 with RequestContext

use of org.webpieces.ctx.api.RequestContext 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 17 with RequestContext

use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.

the class ProxyStreamHandle method createRedirect.

private XFuture<Void> createRedirect(HttpPort requestedPort, RouteId id, Map<String, Object> args, boolean isAjaxRedirect) {
    if (methodMeta == null) {
        throw new IllegalStateException("Somehow methodMeta is missing.  This method should only be called from filters and controllers");
    }
    RequestContext ctx = methodMeta.getCtx();
    RouterRequest request = ctx.getRequest();
    Method method = methodMeta.getLoadedController().getControllerMethod();
    UrlInfo urlInfo = reverseRoutes.routeToUrl(id, method, args, ctx, requestedPort);
    boolean isSecure = urlInfo.isSecure();
    int port = urlInfo.getPort();
    String path = urlInfo.getPath();
    RedirectResponse redirectResponse = new RedirectResponse(isAjaxRedirect, isSecure, request.domain, port, path);
    return sendRedirect(redirectResponse);
}
Also used : UrlInfo(org.webpieces.router.impl.UrlInfo) RedirectResponse(org.webpieces.router.impl.dto.RedirectResponse) RequestContext(org.webpieces.ctx.api.RequestContext) Method(java.lang.reflect.Method) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 18 with RequestContext

use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.

the class DScopedRouter method invokeRouteCatchNotFound.

/**
 * NOTE: We have to catch any exception from the method processNotFound so we can't catch and call internalServerError in this
 * method without nesting even more!!! UGH, more nesting sucks
 */
private RouterStreamRef invokeRouteCatchNotFound(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
    RouterStreamRef streamRef = super.invokeRoute(ctx, handler, subPath);
    XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
        if (t == null)
            return XFuture.completedFuture(r);
        if (t instanceof NotFoundException)
            return notFound((NotFoundException) t, ctx, handler);
        return futureUtil.failedFuture(t);
    }).thenCompose(Function.identity());
    return new RouterStreamRef("DScopedNotFoundCheck", writer, streamRef);
}
Also used : Logger(org.slf4j.Logger) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) LoggerFactory(org.slf4j.LoggerFactory) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) Function(java.util.function.Function) InternalErrorRouteFailedException(org.webpieces.router.api.exceptions.InternalErrorRouteFailedException) FutureHelper(org.webpieces.util.futures.FutureHelper) NotFoundException(org.webpieces.http.exception.NotFoundException) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) Http2ErrorCode(com.webpieces.http2.api.dto.lowlevel.lib.Http2ErrorCode) Map(java.util.Map) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterInfo(org.webpieces.router.impl.model.RouterInfo) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) SupressedExceptionLog(org.webpieces.logging.SupressedExceptionLog) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 19 with RequestContext

use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.

the class DScopedRouter method invokeRoute.

@Override
public RouterStreamRef invokeRoute(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
    RouterStreamRef streamRef = invokeRouteCatchNotFound(ctx, handler, subPath);
    XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
        if (t == null)
            return XFuture.completedFuture(r);
        return tryRenderWebAppErrorControllerResult(ctx, handler, t);
    }).thenCompose(Function.identity());
    XFuture<StreamWriter> proxyWriter = writer.thenApply(w -> createProxy(w, ctx, handler));
    return new RouterStreamRef("dScopedRouter", proxyWriter, streamRef);
}
Also used : Logger(org.slf4j.Logger) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) LoggerFactory(org.slf4j.LoggerFactory) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) Function(java.util.function.Function) InternalErrorRouteFailedException(org.webpieces.router.api.exceptions.InternalErrorRouteFailedException) FutureHelper(org.webpieces.util.futures.FutureHelper) NotFoundException(org.webpieces.http.exception.NotFoundException) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) Http2ErrorCode(com.webpieces.http2.api.dto.lowlevel.lib.Http2ErrorCode) Map(java.util.Map) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterInfo(org.webpieces.router.impl.model.RouterInfo) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) SupressedExceptionLog(org.webpieces.logging.SupressedExceptionLog) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 20 with RequestContext

use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.

the class ResponseProcessorNotFound 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.NOT_FOUND);
    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)

Aggregations

RequestContext (org.webpieces.ctx.api.RequestContext)25 RouterRequest (org.webpieces.ctx.api.RouterRequest)8 LoadedController (org.webpieces.router.impl.loader.LoadedController)7 XFuture (org.webpieces.util.futures.XFuture)6 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)5 List (java.util.List)5 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)5 Method (java.lang.reflect.Method)4 Map (java.util.Map)4 Function (java.util.function.Function)4 NotFoundException (org.webpieces.http.exception.NotFoundException)4 ArrayList (java.util.ArrayList)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 HttpMethod (org.webpieces.ctx.api.HttpMethod)3 RenderResponse (org.webpieces.router.impl.dto.RenderResponse)3 View (org.webpieces.router.impl.dto.View)3 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)3 FutureHelper (org.webpieces.util.futures.FutureHelper)3 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)2