RenderTemplateMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) { HttpResponse response = castToHttpResponse(chain.next(request)); if (TemplatedHttpResponse.class.isInstance(response)) { TemplatedHttpResponse tres = TemplatedHttpResponse.class.cast(response); if (exports.contains(REQUEST)) { tres.getContext().put(exports.getExportName(REQUEST), request); } if (exports.contains(PARAMS)) { tres.getContext().put(exports.getExportName(PARAMS), request.getParams()); } if (exports.contains(USER_PRINCIPAL)) { Stream.of(request) .filter(Objects::nonNull) .map(PrincipalAvailable.class::cast) .findAny() .ifPresent(principal -> tres.getContext() .put(exports.getExportName(USER_PRINCIPAL), principal.getPrincipal())); tres.getContext().put("hasPermission", templateEngine.createFunction(HAS_PERMISSION)); tres.getContext().put("hasAnyPermissions", templateEngine.createFunction(HAS_ANY_PERMISSIONS)); tres.getContext().put("hasAllPermissions", templateEngine.createFunction(HAS_ALL_PERMISSIONS)); } if (exports.contains(SESSION)) { tres.getContext().put(exports.getExportName(SESSION), request.getSession()); } if (exports.contains(CONVERSATION)) { Conversation conversation = request.getConversation(); if (conversation != null && !request.getConversation().isTransient()) { String token = conversation.getId() + "$" + hmacEncoder.encodeToHex(conversation.getId() + "$" + conversation.getTimeout()) + "$" + conversation.getTimeout(); tres.getContext().put("conversationToken", token); } tres.getContext().put(exports.getExportName(CONVERSATION), conversation); } if (exports.contains(CONVERSATION_STATE)) { ConversationState conversationState = request.getConversationState(); tres.getContext().put(exports.getExportName(CONVERSATION_STATE), conversationState); } userFunctions.forEach((key, value) -> tres.getContext().put(key, templateEngine.createFunction(value))); render(tres); } return response; }  @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain); void setUserFunctions(Map<String, Function<List, Object>> userFunctions);  }
@Test void handle() { final HttpRequest request = new DefaultHttpRequest(); Map<String, SystemComponent> components = new HashMap<>(); TemplateEngine templateEngine = new TemplateEngine() { @Override protected ComponentLifecycle lifecycle() { return null; } @Override public HttpResponse render(String name, Object... keyOrVals) { final TemplatedHttpResponse response = TemplatedHttpResponse.create("template1"); response.setBody(new LazyRenderInputStream(() -> new ByteArrayInputStream("hello".getBytes()))); return response; } @Override public Object createFunction(Function func) { return null; } }; components.put("templateEngine", templateEngine); ComponentInjector injector = new ComponentInjector(components); injector.inject(sut); final HttpResponse res = sut.handle(request, new DefaultMiddlewareChain(Predicates.ANY, "null", (Middleware<HttpRequest, HttpResponse, HttpRequest, HttpResponse>) (request1, chain) -> templateEngine.render("template1"))); assertThat(res.getBodyAsString()).isEqualTo("hello"); }