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