Search in sources :

Example 1 with RequestURIManager

use of com.tvd12.ezyhttp.server.core.manager.RequestURIManager 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 RequestURIManager

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

the class BlockingServletTest method doGetSecureTest.

@Test
public void doGetSecureTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    RequestResponseWatcher watcher = mock(RequestResponseWatcher.class);
    componentManager.addRequestResponseWatchers(Collections.singletonList(watcher));
    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") });
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandler requestHandler = new GetRequestHandler();
    RequestURIMeta uriMeta = RequestURIMeta.builder().management(false).authenticated(true).api(true).build();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, uriMeta), 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
    RequestURIManager requestURIManager = requestHandlerManager.getRequestURIManager();
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get"));
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get/"));
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).getContentType();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.OK);
    DataConverters dataConverters = componentManager.getDataConverters();
    BodySerializer bodySerializer = dataConverters.getBodySerializer(ContentTypes.APPLICATION_JSON);
    ExResponse responseData = new ExResponse("Hello ParameterValue, HeaderValue, CookieValue");
    verify(outputStream, times(1)).write(bodySerializer.serialize(responseData));
    verify(interceptor, times(1)).preHandle(any(), any());
    verify(interceptor, times(1)).postHandle(any(), any());
    verify(watcher, times(1)).watchRequest(HttpMethod.GET, request);
    verify(watcher, times(1)).watchResponse(HttpMethod.GET, request, response);
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) RequestURIManager(com.tvd12.ezyhttp.server.core.manager.RequestURIManager) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) BodySerializer(com.tvd12.ezyhttp.core.codec.BodySerializer) RequestResponseWatcher(com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher) Test(org.testng.annotations.Test)

Example 3 with RequestURIManager

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

the class RequestURIManagerTest method test.

@Test
public void test() {
    // given
    RequestURIManager sut = new RequestURIManager();
    sut.addHandledURI(HttpMethod.GET, "a");
    sut.addHandledURI(HttpMethod.GET, "a1");
    sut.addAuthenticatedURI(HttpMethod.POST, "b");
    sut.addAuthenticatedURI(HttpMethod.POST, "b1");
    sut.addApiURI(HttpMethod.PUT, "c");
    sut.addApiURI(HttpMethod.PUT, "c1");
    sut.addPaymentURI(HttpMethod.DELETE, "d");
    sut.addManagementURI(HttpMethod.DELETE, "e");
    // when
    // then
    Asserts.assertTrue(sut.containsHandledURI(HttpMethod.GET, "a"));
    Asserts.assertFalse(sut.containsHandledURI(HttpMethod.GET, "I don't know"));
    Asserts.assertFalse(sut.containsHandledURI(HttpMethod.PATCH, "I don't know"));
    Asserts.assertEquals(sut.getHandledURIs(HttpMethod.GET), Sets.newHashSet("a", "a1"), false);
    Asserts.assertEmpty(sut.getHandledURIs(HttpMethod.PATCH));
    Assert.assertTrue(sut.isAuthenticatedURI(HttpMethod.POST, "b"));
    Asserts.assertFalse(sut.isAuthenticatedURI(HttpMethod.POST, "I don't know"));
    Asserts.assertFalse(sut.isAuthenticatedURI(HttpMethod.PATCH, "I don't know"));
    Asserts.assertEquals(sut.getAuthenticatedURIs(HttpMethod.POST), Sets.newHashSet("b", "b1"), false);
    Asserts.assertEmpty(sut.getAuthenticatedURIs(HttpMethod.PATCH));
    Assert.assertTrue(sut.isApiURI(HttpMethod.PUT, "c"));
    Asserts.assertFalse(sut.isApiURI(HttpMethod.PUT, "I don't know"));
    Asserts.assertFalse(sut.isApiURI(HttpMethod.PATCH, "I don't know"));
    Asserts.assertEquals(sut.getApiURIs(HttpMethod.PUT), Arrays.asList("c", "c1"), false);
    Asserts.assertEmpty(sut.getApiURIs(HttpMethod.PATCH));
    Assert.assertTrue(sut.isPaymentURI(HttpMethod.DELETE, "d"));
    Asserts.assertFalse(sut.isPaymentURI(HttpMethod.DELETE, "I don't know"));
    Asserts.assertFalse(sut.isPaymentURI(HttpMethod.PATCH, "I don't know"));
    Asserts.assertEquals(sut.getPaymentURIs(HttpMethod.DELETE), Collections.singletonList("d"), false);
    Asserts.assertEmpty(sut.getPaymentURIs(HttpMethod.PATCH));
    Assert.assertTrue(sut.isManagementURI(HttpMethod.DELETE, "e"));
    Asserts.assertFalse(sut.isManagementURI(HttpMethod.DELETE, "I don't know"));
    Asserts.assertFalse(sut.isManagementURI(HttpMethod.PATCH, "I don't know"));
    Asserts.assertEquals(sut.getManagementURIs(HttpMethod.DELETE), Collections.singletonList("e"), false);
    Asserts.assertEmpty(sut.getManagementURIs(HttpMethod.PATCH));
}
Also used : RequestURIManager(com.tvd12.ezyhttp.server.core.manager.RequestURIManager) Test(org.testng.annotations.Test)

Aggregations

RequestURIManager (com.tvd12.ezyhttp.server.core.manager.RequestURIManager)2 Test (org.testng.annotations.Test)2 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)1 EzyPropertiesMap (com.tvd12.ezyfox.bean.EzyPropertiesMap)1 EzyReflection (com.tvd12.ezyfox.reflect.EzyReflection)1 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)1 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1 RequestResponseWatcher (com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher)1 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)1 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)1 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)1 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)1 RequestURIMeta (com.tvd12.ezyhttp.server.core.request.RequestURIMeta)1 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1