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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations