Search in sources :

Example 6 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext in project ezyhttp by youngmonkeys.

the class ThymeleafViewContextBuilderTest method test.

@Test
public void test() {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewContextBuilder sut = new ThymeleafViewContextBuilder().templateResolver(resolver);
    // when
    ViewContext viewContext = sut.build();
    // then
    Asserts.assertNotNull(viewContext);
}
Also used : TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) Test(org.testng.annotations.Test)

Example 7 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ViewDialect(com.tvd12.ezyhttp.server.core.view.ViewDialect) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 8 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 9 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext 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);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Entry(java.util.Map.Entry) Redirect(com.tvd12.ezyhttp.server.core.view.Redirect) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) View(com.tvd12.ezyhttp.server.core.view.View) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Example 10 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method buildViewContextTemplateResolverNotNullIsNull.

@Test
public void buildViewContextTemplateResolverNotNullIsNull() {
    // given
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    ViewContextBuilder viewContextBuilder = mock(ViewContextBuilder.class);
    when(beanContext.getSingleton(ViewContextBuilder.class)).thenReturn(viewContextBuilder);
    TemplateResolver templateResolver = mock(TemplateResolver.class);
    when(beanContext.getSingleton(TemplateResolver.class)).thenReturn(templateResolver);
    when(viewContextBuilder.templateResolver(templateResolver)).thenReturn(viewContextBuilder);
    ViewContext viewContext = mock(ViewContext.class);
    when(viewContextBuilder.build()).thenReturn(viewContext);
    EzySingletonFactory singletonFactory = mock(EzySingletonFactory.class);
    when(beanContext.getSingletonFactory()).thenReturn(singletonFactory);
    ViewDialect viewDialect = mock(ViewDialect.class);
    List<ViewDialect> viewDialects = Collections.singletonList(viewDialect);
    when(beanContext.getSingletonsOf(ViewDialect.class)).thenReturn(viewDialects);
    when(viewContextBuilder.viewDialects(viewDialects)).thenReturn(viewContextBuilder);
    ViewDecorator viewDecorator = mock(ViewDecorator.class);
    List<ViewDecorator> viewDecorators = Collections.singletonList(viewDecorator);
    when(beanContext.getSingletonsOf(ViewDecorator.class)).thenReturn(viewDecorators);
    when(viewContextBuilder.viewDecorators(viewDecorators)).thenReturn(viewContextBuilder);
    MessageProvider messageProvider = mock(MessageProvider.class);
    List<MessageProvider> messageProviders = Collections.singletonList(messageProvider);
    when(beanContext.getSingletonsOf(MessageProvider.class)).thenReturn(messageProviders);
    when(viewContextBuilder.messageProviders(messageProviders)).thenReturn(viewContextBuilder);
    AbsentMessageResolver absentMessageResolver = mock(AbsentMessageResolver.class);
    when(beanContext.getSingleton(AbsentMessageResolver.class)).thenReturn(absentMessageResolver);
    when(viewContextBuilder.absentMessageResolver(absentMessageResolver)).thenReturn(viewContextBuilder);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    ViewContext actual = MethodInvoker.create().object(sut).method("buildViewContext").param(EzyBeanContext.class, beanContext).invoke(ViewContext.class);
    // then
    Asserts.assertEquals(viewContext, actual);
    verify(beanContext, times(1)).getSingleton(ViewContext.class);
    verify(beanContext, times(1)).getSingleton(ViewContextBuilder.class);
    verify(beanContext, times(1)).getSingleton(TemplateResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDialect.class);
    verify(beanContext, times(1)).getSingletonsOf(ViewDecorator.class);
    verify(beanContext, times(1)).getSingleton(AbsentMessageResolver.class);
    verify(beanContext, times(1)).getSingletonsOf(MessageProvider.class);
    verify(viewContextBuilder, times(1)).templateResolver(templateResolver);
    verify(viewContextBuilder, times(1)).viewDialects(viewDialects);
    verify(viewContextBuilder, times(1)).viewDecorators(viewDecorators);
    verify(viewContextBuilder, times(1)).messageProviders(messageProviders);
    verify(viewContextBuilder, times(1)).absentMessageResolver(absentMessageResolver);
    verify(viewContextBuilder, times(1)).build();
    verify(singletonFactory, times(1)).addSingleton(viewContext);
}
Also used : MessageProvider(com.tvd12.ezyhttp.server.core.view.MessageProvider) AbsentMessageResolver(com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) EzySingletonFactory(com.tvd12.ezyfox.bean.EzySingletonFactory) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewDialect(com.tvd12.ezyhttp.server.core.view.ViewDialect) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Aggregations

ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)10 Test (org.testng.annotations.Test)9 TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)7 View (com.tvd12.ezyhttp.server.core.view.View)5 ThymeleafViewContextBuilder (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 PrintWriter (java.io.PrintWriter)4 ServletContext (javax.servlet.ServletContext)4 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)3 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)3 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)3 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)2 AbsentMessageResolver (com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver)2 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)2 Cookie (javax.servlet.http.Cookie)2 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1