Search in sources :

Example 1 with ComponentManager

use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilder method createBeanContext.

protected EzyBeanContext createBeanContext() {
    if (packageToScans.isEmpty()) {
        throw new IllegalStateException("must scan at least one package");
    }
    Set<String> allPackageToScans = new HashSet<>();
    allPackageToScans.add(DEFAULT_PACKAGE_TO_SCAN);
    allPackageToScans.addAll(packageToScans);
    EzyReflection reflection = EzyPackages.scanPackages(allPackageToScans);
    addComponentClassesFromReflection(reflection);
    allPackageToScans.addAll(packageToScans);
    allPackageToScans.addAll(getPackagesToScanFromProviders(reflection));
    reflection = EzyPackages.scanPackages(allPackageToScans);
    Set controllerClasses = reflection.getAnnotatedClasses(Controller.class);
    Set interceptorClases = reflection.getAnnotatedClasses(Interceptor.class);
    Set exceptionHandlerClasses = reflection.getAnnotatedClasses(ExceptionHandler.class);
    Set bodyConverterClasses = reflection.getAnnotatedClasses(BodyConvert.class);
    Set stringConverterClasses = reflection.getAnnotatedClasses(StringConvert.class);
    Set bootstrapClasses = reflection.getAnnotatedClasses(ApplicationBootstrap.class);
    Map<String, Class> serviceClasses = getServiceClasses(reflection);
    EzyPropertiesMap propertiesMap = getPropertiesMap(reflection);
    EzyBeanContext beanContext = newBeanContextBuilder().scan(allPackageToScans).addSingletonClasses(componentClasses).addSingletonClasses(serviceClasses).addSingletonClasses(controllerClasses).addSingletonClasses(interceptorClases).addSingletonClasses(exceptionHandlerClasses).addSingletonClasses(bodyConverterClasses).addSingletonClasses(stringConverterClasses).addSingletonClasses(bootstrapClasses).propertiesMap(propertiesMap).addSingleton("systemObjectMapper", objectMapper).addSingleton("componentManager", componentManager).addSingleton("requestHandlerManager", requestHandlerManager).addSingleton("featureURIManager", requestHandlerManager.getFeatureURIManager()).addSingleton("requestURIManager", requestHandlerManager.getRequestURIManager()).addAllClasses(EzyPackages.scanPackage(DEFAULT_PACKAGE_TO_SCAN)).build();
    setComponentProperties(beanContext);
    registerComponents(beanContext);
    addRequestHandlers(beanContext);
    addResourceRequestHandlers(beanContext);
    addExceptionHandlers();
    return beanContext;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) EzyReflection(com.tvd12.ezyfox.reflect.EzyReflection) EzyPropertiesMap(com.tvd12.ezyfox.bean.EzyPropertiesMap) HashSet(java.util.HashSet)

Example 2 with ComponentManager

use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetResponseContentTypeNull.

@Test
public void doGetResponseContentTypeNull() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.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") });
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(request.startAsync(request, response)).thenReturn(asyncContext);
    when(request.isAsyncStarted()).thenReturn(true);
    EzyWrap<AsyncListener> asyncListener = new EzyWrap<>();
    doAnswer(it -> {
        asyncListener.setValue(it.getArgumentAt(0, AsyncListener.class));
        return null;
    }).when(asyncContext).addListener(any(AsyncListener.class));
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandlerContentTypeNull requestHandler = new GetRequestHandlerContentTypeNull();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
    // when
    sut.service(request, response);
    asyncListener.getValue().onComplete(new AsyncEvent(asyncContext));
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).addListener(any(AsyncListener.class));
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) ToString(lombok.ToString) AsyncEvent(javax.servlet.AsyncEvent) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncListener(javax.servlet.AsyncListener) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Example 3 with ComponentManager

use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.

the class BlockingServletTest method requestHandlerNullAndHasErrorHandler.

@Test
public void requestHandlerNullAndHasErrorHandler() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
    ResponseEntity responseEntity = ResponseEntity.ok();
    when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(responseEntity);
    componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get-handler-null";
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    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)).setStatus(StatusCodes.OK);
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) ServletOutputStream(javax.servlet.ServletOutputStream) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) Test(org.testng.annotations.Test)

Example 4 with ComponentManager

use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.

the class BlockingServletTest method requestHandlerEmpty.

@Test
public void requestHandlerEmpty() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandler requestHandler = new GetRequestHandler();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.METHOD_NOT_ALLOWED);
    verify(outputStream, times(1)).write("method GET not allowed".getBytes());
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) Test(org.testng.annotations.Test)

Example 5 with ComponentManager

use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.

the class BlockingServletTest method requestHandlerEmptyWithErrorHandlerButDataNull.

@Test
public void requestHandlerEmptyWithErrorHandlerButDataNull() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    UnhandledErrorHandler unhandledErrorHandler = mock(UnhandledErrorHandler.class);
    when(unhandledErrorHandler.handleError(HttpMethod.GET, request, response, HttpServletResponse.SC_NOT_FOUND, null)).thenReturn(null);
    componentManager.setUnhandledErrorHandler(Collections.singletonList(unhandledErrorHandler));
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandler requestHandler = new GetRequestHandler();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.POST, requestURI, false), requestHandler);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.METHOD_NOT_ALLOWED);
    verify(outputStream, times(1)).write("method GET not allowed".getBytes());
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ToString(lombok.ToString) Test(org.testng.annotations.Test)

Aggregations

ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)31 Test (org.testng.annotations.Test)31 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 ToString (lombok.ToString)29 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)27 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)24 ServletOutputStream (javax.servlet.ServletOutputStream)24 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 Cookie (javax.servlet.http.Cookie)22 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)10 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)4 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)4 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)4 ExceptionHandlerManager (com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager)4 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)3 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)2 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)2