Search in sources :

Example 1 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilder method addResourceRequestHandlers.

protected void addResourceRequestHandlers(EzyBeanContext beanContext) {
    ResourceResolver resourceResolver = getResourceResolver(beanContext);
    if (resourceResolver == null) {
        return;
    }
    ResourceDownloadManager downloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    Map<String, Resource> resources = resourceResolver.getResources();
    for (String resourceURI : resources.keySet()) {
        Resource resource = resources.get(resourceURI);
        RequestURI requestURI = new RequestURI(HttpMethod.GET, resourceURI, false, true, true, resource.getFullPath());
        RequestHandler requestHandler = new ResourceRequestHandler(resource.getPath(), resource.getUri(), resource.getExtension(), downloadManager);
        requestHandlerManager.addHandler(requestURI, requestHandler);
    }
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) Resource(com.tvd12.ezyhttp.server.core.resources.Resource) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI)

Example 2 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager in project ezyhttp by youngmonkeys.

the class ResourceRequestHandlerTest method handleWithDrainExceptionTest.

@Test
public void handleWithDrainExceptionTest() throws Exception {
    // given
    String resourcePath = "static/index.html";
    String resourceURI = "/index.html";
    String resourceExtension = "html";
    ResourceDownloadManager downloadManager = new ResourceDownloadManager();
    ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, new EzyAnywayInputStreamLoader(), downloadManager);
    RequestArguments arguments = mock(RequestArguments.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(arguments.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    doThrow(IOException.class).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
    when(asyncContext.getResponse()).thenReturn(response);
    // when
    sut.handle(arguments);
    // then
    Asserts.assertEquals(HttpMethod.GET, sut.getMethod());
    Asserts.assertEquals("/index.html", sut.getRequestURI());
    Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getResponseContentType());
    Thread.sleep(300);
    downloadManager.stop();
    verify(arguments, times(1)).getAsyncContext();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : EzyAnywayInputStreamLoader(com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Example 3 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager in project ezyhttp by youngmonkeys.

the class ApplicationContextBuilderTest method test.

@Test
public void test() {
    // given
    EzyBeanContext internalBeanContext = EzyBeanContext.builder().addSingleton(new InternalSingleton1()).build();
    Properties properties = new Properties();
    properties.put("b", 2);
    ApplicationContext applicationContext = new ApplicationContextBuilder().scan("com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller").scan(Arrays.asList("com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service")).addComponentClasses(Collections.singletonList(SourceService.class)).addPropertiesSources(Collections.singletonList("application3.yaml")).addPropertiesSource("application-enable.yaml").addProperty("a", "1").addProperties(properties).addProperties(Collections.singletonMap("c", "3")).beanContext(internalBeanContext).addSingleton(new InternalSingleton2()).addSingleton(Collections.singletonMap("internalSingleton3", new InternalSingleton3())).addSingleton(mock(AbsentMessageResolver.class)).addComponentClass(InternalSingleton1.class).addPropertiesSources().build();
    EzyBeanContext beanContext = applicationContext.getBeanContext();
    // when
    int actualOneProp = beanContext.getProperty("one", int.class);
    boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
    Boolean resourceEnable = beanContext.getProperty("resources.enable", boolean.class);
    Boolean resourceUploadEnable = beanContext.getProperty("resources.upload.enable", boolean.class);
    UserService userService = beanContext.getSingleton(UserService.class);
    UserService0 userService0 = beanContext.getSingleton(UserService0.class);
    ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
    ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
    ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    Set<String> packagesToScan = beanContext.getPackagesToScan();
    EventService eventService = beanContext.getSingleton(EventService.class);
    SourceService sourceService = beanContext.getSingleton(SourceService.class);
    String helloValue = beanContext.getProperty("hello", String.class);
    InternalSingleton1 internalSingleton1 = beanContext.getBeanCast(InternalSingleton1.class);
    InternalSingleton2 internalSingleton2 = beanContext.getBeanCast(InternalSingleton2.class);
    InternalSingleton3 internalSingleton3 = beanContext.getBeanCast(InternalSingleton3.class);
    // then
    Asserts.assertEquals(1, actualOneProp);
    Asserts.assertTrue(managementEnable);
    Asserts.assertTrue(resourceEnable);
    Asserts.assertTrue(resourceUploadEnable);
    Asserts.assertNotNull(userService);
    Asserts.assertNotNull(userService0);
    Asserts.assertNotNull(viewContextBuilder);
    Asserts.assertNotNull(resourceDownloadManager);
    Asserts.assertEquals(4, resourceResolver.getResources().size());
    Asserts.assertNotNull(eventService);
    Asserts.assertNotNull(sourceService);
    Asserts.assertNotNull(helloValue);
    Asserts.assertEquals("1", beanContext.getProperty("a", String.class));
    Asserts.assertEquals(2, beanContext.getProperty("b", int.class));
    Asserts.assertEquals("3", beanContext.getProperty("c", String.class));
    Asserts.assertEquals("3", applicationContext.getProperty("c", String.class));
    Asserts.assertEquals(packagesToScan, Sets.newHashSet("com.tvd12.ezyhttp.server", "com.tvd12.ezyhttp.server.core.test", "com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller", "com.tvd12.ezyhttp.server.core.test.event", "com.tvd12.ezyhttp.server.core.test.api", "com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service"));
    Asserts.assertNotNull(internalSingleton1);
    Asserts.assertNotNull(internalSingleton2);
    Asserts.assertNotNull(internalSingleton3);
    ComponentManager componentManager = ComponentManager.getInstance();
    Asserts.assertEquals(componentManager.getAsyncDefaultTimeout(), 10000);
    System.out.println(applicationContext);
    applicationContext.destroy();
}
Also used : EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) UserService(com.tvd12.ezyhttp.server.core.test.service.UserService) EventService(com.tvd12.ezyhttp.server.core.test.event.EventService) Properties(java.util.Properties) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ApplicationContext(com.tvd12.ezyhttp.server.core.ApplicationContext) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) UserService0(com.tvd12.ezyhttp.server.core.test.service.UserService0) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) SourceService(com.tvd12.ezyhttp.server.core.test.event.SourceService) Test(org.testng.annotations.Test)

Example 4 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager in project ezyhttp by youngmonkeys.

the class EzyHttpApplicationTest method startWith2Params.

@Test
public void startWith2Params() throws Exception {
    // given
    EzyHttpApplication sut = EzyHttpApplication.start(EzyHttpApplicationTest.class, getClass());
    ApplicationContext applicationContext = sut.getApplicationContext();
    EzyBeanContext beanContext = applicationContext.getBeanContext();
    // when
    int actualOneProp = beanContext.getProperty("one", int.class);
    boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
    UserService userService = applicationContext.getSingleton(UserService.class);
    ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
    ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
    ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    List<Object> bootstraps = applicationContext.getSingletons(ApplicationBootstrap.class);
    // then
    Asserts.assertEquals(1, actualOneProp);
    Asserts.assertTrue(managementEnable);
    Asserts.assertNotNull(userService);
    Asserts.assertNotNull(viewContextBuilder);
    Asserts.assertNotNull(resourceDownloadManager);
    Asserts.assertEquals(4, resourceResolver.getResources().size());
    Asserts.assertFalse(bootstraps.isEmpty());
    sut.stop();
}
Also used : ApplicationContext(com.tvd12.ezyhttp.server.core.ApplicationContext) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) EzyHttpApplication(com.tvd12.ezyhttp.server.core.EzyHttpApplication) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) UserService(com.tvd12.ezyhttp.server.core.test.service.UserService) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) Test(org.testng.annotations.Test)

Example 5 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager in project ezyhttp by youngmonkeys.

the class ResourceDownloadManagerTest method drainFailedDueToOutputStreamDueToError.

@Test
public void drainFailedDueToOutputStreamDueToError() throws Exception {
    // given
    info("start drainFailedDueToOutputStreamDueToError");
    ResourceDownloadManager sut = new ResourceDownloadManager();
    BlockingQueue<Object> queue = FieldUtil.getFieldValue(sut, "queue");
    queue.offer(new Object());
    InputStream inputStream = mock(InputStream.class);
    OutputStream outputStream = mock(OutputStream.class);
    // when
    sut.drain(inputStream, outputStream);
    // then
    sut.stop();
    info("finish drainFailedDueToOutputStreamDueToError");
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)18 Test (org.testng.annotations.Test)17 BaseTest (com.tvd12.test.base.BaseTest)10 ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)5 ResourceResolver (com.tvd12.ezyhttp.server.core.resources.ResourceResolver)5 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)4 ApplicationContext (com.tvd12.ezyhttp.server.core.ApplicationContext)4 RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)3 UserService (com.tvd12.ezyhttp.server.core.test.service.UserService)3 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 AsyncContext (javax.servlet.AsyncContext)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)2 EzyHttpApplication (com.tvd12.ezyhttp.server.core.EzyHttpApplication)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EzyFutureMap (com.tvd12.ezyfox.concurrent.EzyFutureMap)1 EzyResultCallback (com.tvd12.ezyfox.concurrent.callback.EzyResultCallback)1 EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1