Search in sources :

Example 1 with View

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

the class ResponseProcessor method createRenderResponse.

public CompletableFuture<Void> createRenderResponse(RenderImpl controllerResponse) {
    if (responseSent)
        throw new IllegalStateException("You already sent a response.  do not call Actions.redirect or Actions.render more than once");
    responseSent = true;
    RouterRequest request = ctx.getRequest();
    Method method = matchedMeta.getMethod();
    //not broken)
    if (matchedMeta.getRoute().getRouteType() == RouteType.HTML && 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 = matchedMeta.getControllerInstance().getClass().getName();
    String methodName = matchedMeta.getMethod().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());
    View view = new View(controllerName, methodName, relativeOrAbsolutePath);
    RenderResponse resp = new RenderResponse(view, pageArgs, matchedMeta.getRoute().getRouteType());
    return wrapFunctionInContext(() -> responseCb.sendRenderHtml(resp));
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) Method(java.lang.reflect.Method) HttpMethod(org.webpieces.ctx.api.HttpMethod) RenderResponse(org.webpieces.router.api.dto.RenderResponse) View(org.webpieces.router.api.dto.View) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 2 with View

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

the class ProxyResponse method sendRenderHtml.

@Override
public CompletableFuture<Void> sendRenderHtml(RenderResponse resp) {
    log.info(() -> "Sending render html response. req=" + request);
    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 (MissingPropertyException 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 = StatusCode.HTTP_200_OK;
    switch(resp.routeType) {
        case HTML:
            statusCode = StatusCode.HTTP_200_OK;
            break;
        case NOT_FOUND:
            statusCode = StatusCode.HTTP_404_NOTFOUND;
            break;
        case INTERNAL_SERVER_ERROR:
            statusCode = StatusCode.HTTP_500_INTERNAL_SVR_ERROR;
            break;
        default:
            throw new IllegalStateException("did add case for state=" + resp.routeType);
    }
    //NOTE: These are ALL String templates, so default the mimeType to text/plain
    if (extension == null) {
        extension = "txt";
    }
    return createResponseAndSend(statusCode, content, extension, "text/plain");
}
Also used : Set(java.util.Set) StringWriter(java.io.StringWriter) MissingPropertyException(groovy.lang.MissingPropertyException) View(org.webpieces.router.api.dto.View) StatusCode(com.webpieces.http2parser.api.dto.StatusCode)

Aggregations

View (org.webpieces.router.api.dto.View)2 StatusCode (com.webpieces.http2parser.api.dto.StatusCode)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 RouterRequest (org.webpieces.ctx.api.RouterRequest)1 RenderResponse (org.webpieces.router.api.dto.RenderResponse)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1