Search in sources :

Example 91 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.

the class ThymeleafViewContext method render.

@Override
public void render(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, View view) throws IOException {
    for (ViewDecorator viewDecorator : viewDecorators) {
        viewDecorator.decorate(request, view);
    }
    WebContext ctx = new WebContext(request, response, servletContext, view.getLocale());
    ctx.setVariables(view.getVariables());
    templateEngine.process(view.getTemplate(), ctx, response.getWriter());
}
Also used : WebContext(org.thymeleaf.context.WebContext) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator)

Example 92 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.

the class ThymeleafViewContextTest method test.

@Test
public void test() throws Exception {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).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 93 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyhttp by youngmonkeys.

the class ThymeleafViewContextTest method renderWithViewDecorator.

@Test
public void renderWithViewDecorator() throws Exception {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewDecorator viewDecorator = mock(ViewDecorator.class);
    ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).viewDecorators(Collections.singletonList(viewDecorator)).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);
    verify(viewDecorator, times(1)).decorate(request, view);
}
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) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 94 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-examples by tvd12.

the class BookController method addBook.

@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
    Book existedBook = bookRepository.findByNameAndAuthorId(request.getBookName(), request.getAuthorId());
    if (existedBook != null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
    }
    Author author = authorRepository.findById(request.getAuthorId());
    if (author == null) {
        throw new HttpBadRequestException("author: " + request.getAuthorId() + " not found");
    }
    Category category = categoryRepository.findById(request.getCategoryId());
    if (category == null) {
        throw new HttpBadRequestException("category: " + request.getCategoryId() + " not found");
    }
    val bookId = maxIdRepository.incrementAndGet("book");
    val book = requestToEntityConverter.toBookEntity(request, bookId);
    bookRepository.save(book);
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : lombok.val(lombok.val) Category(com.tvd12.ezydata.example.mongo.entity.Category) Book(com.tvd12.ezydata.example.mongo.entity.Book) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Author(com.tvd12.ezydata.example.mongo.entity.Author)

Example 95 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-examples by tvd12.

the class CategoryController method addCategory.

@DoPost("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
    Category existedCategory = categoryRepository.findByName(request.getCategoryName());
    if (existedCategory != null) {
        throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
    }
    Category category = new Category(maxIdRepository.incrementAndGet("category"), request.getCategoryName());
    categoryRepository.save(category);
    return category;
}
Also used : Category(com.tvd12.ezydata.example.mongo.entity.Category) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Aggregations

Test (org.testng.annotations.Test)128 HttpServletResponse (javax.servlet.http.HttpServletResponse)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)37 BeforeTest (org.testng.annotations.BeforeTest)36 BaseTest (com.tvd12.test.base.BaseTest)31 EzyArray (com.tvd12.ezyfox.entity.EzyArray)30 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 ToString (lombok.ToString)29 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)27 ServletOutputStream (javax.servlet.ServletOutputStream)25 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)24 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)24 Cookie (javax.servlet.http.Cookie)24 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)22 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)21 HttpClient (com.tvd12.ezyhttp.client.HttpClient)21 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)21