Search in sources :

Example 11 with RequestInterceptor

use of com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doPutWithRedirectTest.

@Test
public void doPutWithRedirectTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/put-with-redirect-attributes";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.PUT.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
    when(request.getParameter("param")).thenReturn("ParameterValue");
    when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
    when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
    when(request.getHeader("header")).thenReturn("HeaderValue");
    when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    PutWithRedirectAttributesRequestHandler requestHandler = new PutWithRedirectAttributesRequestHandler();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.PUT, requestURI, false), requestHandler);
    RequestInterceptor interceptor = mock(RequestInterceptor.class);
    when(interceptor.preHandle(any(), any())).thenReturn(true);
    componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).sendRedirect("/home");
    verify(interceptor, times(1)).preHandle(any(), any());
    verify(interceptor, times(1)).postHandle(any(), any());
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) ServletOutputStream(javax.servlet.ServletOutputStream) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) Test(org.testng.annotations.Test)

Example 12 with RequestInterceptor

use of com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilder method registerComponents.

protected void registerComponents(EzyBeanContext beanContext) {
    Set controllers = new HashSet<>();
    controllers.addAll(beanContext.getSingletons(Controller.class));
    controllers.addAll(beanContext.getSingletonsOf(IRequestController.class));
    controllerManager.addControllers(controllers);
    List exceptionHandlers = beanContext.getSingletons(ExceptionHandler.class);
    exceptionHandlerManager.addExceptionHandlers(exceptionHandlers);
    List<RequestInterceptor> requestInterceptors = beanContext.getSingletons(Interceptor.class);
    requestInterceptors.sort(InterceptorAnnotations.comparator());
    interceptorManager.addRequestInterceptors(requestInterceptors);
    List bodyConverters = beanContext.getSingletons(BodyConvert.class);
    dataConverters.addBodyConverters(bodyConverters);
    List stringConverters = beanContext.getSingletons(StringConvert.class);
    List uncaughtErrorHandlers = beanContext.getSingletonsOf(UnhandledErrorHandler.class);
    List requestResponseWathcers = beanContext.getSingletonsOf(RequestResponseWatcher.class);
    dataConverters.setStringConverters(stringConverters);
    componentManager.setViewContext(buildViewContext(beanContext));
    componentManager.setServerPort(getServerPort(beanContext));
    componentManager.setExposeManagementURIs(isExposeManagementURIs(beanContext));
    componentManager.setManagementPort(getManagementPort(beanContext));
    componentManager.setAsyncDefaultTimeout(getAsyncDefaultTimeout(beanContext));
    componentManager.setUnhandledErrorHandler(uncaughtErrorHandlers);
    componentManager.addRequestResponseWatchers(requestResponseWathcers);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IRequestController(com.tvd12.ezyhttp.server.core.handler.IRequestController) List(java.util.List) ArrayList(java.util.ArrayList) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) Controller(com.tvd12.ezyhttp.server.core.annotation.Controller) IRequestController(com.tvd12.ezyhttp.server.core.handler.IRequestController) HashSet(java.util.HashSet)

Aggregations

RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)12 Test (org.testng.annotations.Test)11 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)10 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)10 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)10 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)10 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)10 Cookie (javax.servlet.http.Cookie)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 ToString (lombok.ToString)10 ServletOutputStream (javax.servlet.ServletOutputStream)9 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)3 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)3 RequestResponseWatcher (com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher)2 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)1 Controller (com.tvd12.ezyhttp.server.core.annotation.Controller)1 IRequestController (com.tvd12.ezyhttp.server.core.handler.IRequestController)1 RequestURIManager (com.tvd12.ezyhttp.server.core.manager.RequestURIManager)1 RequestURIMeta (com.tvd12.ezyhttp.server.core.request.RequestURIMeta)1