Search in sources :

Example 11 with RequestContext

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

the class BeansController method postArray.

public Redirect postArray(UserDto user, List<UserDto> users) {
    lib1.validateUser(user);
    Current.validation().addError("user.accounts[0].addresses[0].street", "This is too ugly a street name");
    if (Current.validation().hasErrors()) {
        RequestContext ctx = Current.getContext();
        return Actions.redirectFlashAllSecure(BeansRouteId.ARRAY_FORM_ROUTE, ctx, "password");
    }
    return Actions.redirect(BeansRouteId.ARRAY_FORM_ROUTE);
}
Also used : RequestContext(org.webpieces.ctx.api.RequestContext)

Example 12 with RequestContext

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

the class BeansController method postUser.

public Redirect postUser(UserDto user, String password) {
    // Validate any other stuff you need here adding errors and such
    lib1.validateUser(user);
    RequestContext ctx = Current.getContext();
    if (Current.validation().hasErrors()) {
        Current.flash().setMessage("Invalid values below");
        return Actions.redirectFlashAllSecure(BeansRouteId.USER_FORM_ROUTE, ctx, "password");
    }
    lib.saveUser(user);
    return Actions.redirect(BeansRouteId.LIST_USERS_ROUTE);
}
Also used : RequestContext(org.webpieces.ctx.api.RequestContext)

Example 13 with RequestContext

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

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

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

the class ResponseProcessorHtml method createRawRedirect.

public XFuture<Void> createRawRedirect(MethodMeta meta, RawRedirect controllerResponse, ProxyStreamHandle handle) {
    RequestContext ctx = meta.getCtx();
    String url = controllerResponse.getUrl();
    if (url.startsWith("http")) {
        return handle.sendRedirect(new RedirectResponse(url));
    }
    RouterRequest request = ctx.getRequest();
    RedirectResponse redirectResponse = new RedirectResponse(false, request.isHttps, request.domain, request.port, url);
    return handle.sendRedirect(redirectResponse);
}
Also used : RedirectResponse(org.webpieces.router.impl.dto.RedirectResponse) RequestContext(org.webpieces.ctx.api.RequestContext) RouterRequest(org.webpieces.ctx.api.RouterRequest)

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