Search in sources :

Example 1 with RenderResponse

use of org.webpieces.router.api.dto.RenderResponse 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 RenderResponse

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

the class ErrorTest method testNoMethod.

@Test
public void testNoMethod() {
    log.info("starting");
    String moduleFileContents = NoMethodRouterModules.class.getName();
    RouterService server = ErrorCommonTest.createServer(false, moduleFileContents);
    //this should definitely not throw since we lazy load everything in dev...
    server.start();
    RouterRequest req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockResponseStream mockResponseStream = new MockResponseStream();
    server.incomingCompleteRequest(req, mockResponseStream);
    List<RenderResponse> renders = mockResponseStream.getSendRenderHtmlList();
    Assert.assertEquals(1, renders.size());
    RenderResponse renderResponse = renders.get(0);
    Assert.assertEquals(RouteType.INTERNAL_SERVER_ERROR, renderResponse.routeType);
}
Also used : RouterService(org.webpieces.router.api.RouterService) RenderResponse(org.webpieces.router.api.dto.RenderResponse) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) Test(org.junit.Test) ErrorCommonTest(org.webpieces.router.api.error.ErrorCommonTest)

Aggregations

RouterRequest (org.webpieces.ctx.api.RouterRequest)2 RenderResponse (org.webpieces.router.api.dto.RenderResponse)2 Method (java.lang.reflect.Method)1 Test (org.junit.Test)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 RouterService (org.webpieces.router.api.RouterService)1 View (org.webpieces.router.api.dto.View)1 ErrorCommonTest (org.webpieces.router.api.error.ErrorCommonTest)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1 MockResponseStream (org.webpieces.router.api.mocks.MockResponseStream)1