Search in sources :

Example 1 with View

use of org.webpieces.router.impl.dto.View 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 2 with View

use of org.webpieces.router.impl.dto.View 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 3 with View

use of org.webpieces.router.impl.dto.View in project webpieces by deanhiller.

the class ProxyStreamHandle method sendRenderHtml.

public XFuture<Void> sendRenderHtml(RenderResponse resp) {
    Http2Request request = originalHttp2Request;
    if (log.isInfoEnabled())
        log.info("About to send render html response for request=" + request + " controller=" + resp.view.getControllerName() + "." + resp.view.getMethodName());
    View view = resp.view;
    String packageStr = view.getPackageName();
    // For this type of View, the template is the name of the method..
    String templateClassName = view.getRelativeOrAbsolutePath();
    int lastIndexOf = templateClassName.lastIndexOf(".");
    String extension = null;
    if (lastIndexOf > 0) {
        extension = templateClassName.substring(lastIndexOf + 1);
    }
    String templatePath = templateClassName;
    if (!templatePath.startsWith("/")) {
        // relative path so need to form absolute path...
        if (lastIndexOf > 0) {
            templateClassName = templateClassName.substring(0, lastIndexOf);
        }
        templatePath = getTemplatePath(packageStr, templateClassName, extension);
    }
    // TODO: stream this out with chunked response instead??....
    StringWriter out = new StringWriter();
    try {
        templatingService.loadAndRunTemplate(templatePath, out, resp.pageArgs);
    } catch (MissingPropException e) {
        Set<String> keys = resp.pageArgs.keySet();
        throw new ControllerPageArgsException("Controller.method=" + view.getControllerName() + "." + view.getMethodName() + " did\nnot" + " return enough arguments for the template =" + templatePath + ".  specifically, the method\nreturned these" + " arguments=" + keys + "  There is a chance in your html you forgot the '' around a variable name\n" + "such as #{set 'key'}# but you put #{set key}# which is 'usually' not the correct way\n" + "The missing properties are as follows....\n" + e.getMessage(), e);
    }
    String content = out.toString();
    StatusCode statusCode;
    switch(resp.routeType) {
        case HTML:
            statusCode = StatusCode.HTTP_200_OK;
            break;
        case NOT_FOUND:
            statusCode = StatusCode.HTTP_404_NOT_FOUND;
            break;
        case INTERNAL_SERVER_ERROR:
            statusCode = StatusCode.HTTP_500_INTERNAL_SERVER_ERROR;
            break;
        default:
            throw new IllegalStateException("did add case for state=" + resp.routeType);
    }
    // The real mime type is looked up based on extension so htm or html results in text/html
    if (extension == null) {
        extension = "txt";
    }
    String finalExt = extension;
    return futureUtil.catchBlockWrap(() -> createResponseAndSend(request, statusCode, content, finalExt, "text/plain"), (t) -> convert(t));
}
Also used : Set(java.util.Set) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StringWriter(java.io.StringWriter) ControllerPageArgsException(org.webpieces.router.api.exceptions.ControllerPageArgsException) View(org.webpieces.router.impl.dto.View) StatusCode(org.webpieces.http.StatusCode) MissingPropException(org.webpieces.ctx.api.MissingPropException)

Example 4 with View

use of org.webpieces.router.impl.dto.View 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

View (org.webpieces.router.impl.dto.View)4 RequestContext (org.webpieces.ctx.api.RequestContext)3 RenderResponse (org.webpieces.router.impl.dto.RenderResponse)3 LoadedController (org.webpieces.router.impl.loader.LoadedController)3 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 MissingPropException (org.webpieces.ctx.api.MissingPropException)1 RouterRequest (org.webpieces.ctx.api.RouterRequest)1 StatusCode (org.webpieces.http.StatusCode)1 ControllerPageArgsException (org.webpieces.router.api.exceptions.ControllerPageArgsException)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1