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