use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class ThymeleafViewContextTest method renderWithDialectButNotViewDialect.
@Test
public void renderWithDialectButNotViewDialect() throws Exception {
// given
TemplateResolver resolver = TemplateResolver.builder().build();
ViewDialect viewDialect = mock(ViewDialect.class);
ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).viewDialects(Collections.singletonList(viewDialect)).build();
ServletContext servletContext = mock(ServletContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
PrintWriter writer = mock(PrintWriter.class);
when(response.getWriter()).thenReturn(writer);
View view = View.builder().template("index.html").build();
// when
viewContext.render(servletContext, request, response, view);
// then
Asserts.assertNotNull(viewContext);
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class ThymeleafViewContextTest method renderWithViewDialect.
@Test
public void renderWithViewDialect() throws Exception {
// given
TemplateResolver resolver = TemplateResolver.builder().build();
ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).viewDialects(Collections.singletonList(new VewHelloDialect())).build();
ServletContext servletContext = mock(ServletContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
PrintWriter writer = mock(PrintWriter.class);
when(response.getWriter()).thenReturn(writer);
View view = View.builder().template("index.html").build();
// when
viewContext.render(servletContext, request, response, view);
// then
Asserts.assertNotNull(viewContext);
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class BlockingServlet method handleResponseData.
protected void handleResponseData(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception {
Object body = data;
if (data instanceof ResponseEntity) {
ResponseEntity entity = (ResponseEntity) body;
body = entity.getBody();
response.setStatus(entity.getStatus());
MultiValueMap headers = entity.getHeaders();
if (headers != null) {
Map<String, String> encodedHeaders = headers.toMap();
for (Entry<String, String> entry : encodedHeaders.entrySet()) {
response.addHeader(entry.getKey(), entry.getValue());
}
}
} else if (data instanceof Redirect) {
Redirect redirect = (Redirect) data;
for (Cookie cookie : redirect.getCookies()) {
response.addCookie(cookie);
}
for (Entry<String, String> e : redirect.getHeaders().entrySet()) {
response.addHeader(e.getKey(), e.getValue());
}
Map<String, Object> attributes = redirect.getAttributes();
if (attributes != null) {
String attributesValue = objectMapper.writeValueAsString(attributes);
Cookie attributesCookie = new Cookie(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_NAME, EzyBase64.encodeUtf(attributesValue));
attributesCookie.setMaxAge(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_MAX_AGE);
response.addCookie(attributesCookie);
}
response.sendRedirect(redirect.getUri() + redirect.getQueryString());
return;
} else if (data instanceof View) {
if (viewContext == null) {
throw new IllegalStateException("viewContext is null, " + "you must add ezyhttp-server-thymeleaf to your dependencies" + " or create viewContext by yourself");
}
View view = (View) data;
for (Cookie cookie : view.getCookies()) {
response.addCookie(cookie);
}
for (Entry<String, String> e : view.getHeaders().entrySet()) {
response.addHeader(e.getKey(), e.getValue());
}
response.setContentType(view.getContentType());
viewContext.render(getServletContext(), request, response, view);
return;
} else {
response.setStatus(HttpServletResponse.SC_OK);
}
if (body != null) {
responseBody(response, body);
}
}
Aggregations