Search in sources :

Example 1 with ResourceRequestHandler

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

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

use of com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler in project ezyhttp by youngmonkeys.

the class ResourceRequestHandlerTest method handleWithDrainExceptionWhenCallTest.

@SuppressWarnings("unchecked")
@Test
public void handleWithDrainExceptionWhenCallTest() throws Exception {
    // given
    String resourcePath = "static/index.html";
    String resourceURI = "/index.html";
    String resourceExtension = "html";
    ResourceDownloadManager downloadManager = mock(ResourceDownloadManager.class);
    doThrow(IllegalStateException.class).when(downloadManager).drainAsync(any(InputStream.class), any(OutputStream.class), any(EzyResultCallback.class));
    int timeout = RandomUtil.randomSmallInt() + 1;
    ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, downloadManager, timeout);
    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
    verify(arguments, times(1)).getAsyncContext();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(asyncContext, times(1)).setTimeout(timeout);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : EzyResultCallback(com.tvd12.ezyfox.concurrent.callback.EzyResultCallback) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) 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 4 with ResourceRequestHandler

use of com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler in project ezyhttp by youngmonkeys.

the class ResourceRequestHandlerTest method handleAsyncTest.

@Test
public void handleAsyncTest() 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, 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);
    when(asyncContext.getResponse()).thenReturn(response);
    // when
    sut.handle(arguments);
    // then
    Asserts.assertTrue(sut.isAsync());
    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.OK);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : 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 5 with ResourceRequestHandler

use of com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler in project ezyhttp by youngmonkeys.

the class FileUploadService method download.

public void download(RequestArguments requestArguments, String file) throws Exception {
    ResourceRequestHandler handler = new ResourceRequestHandler("files/" + file, "files/" + file, EzyFileUtil.getFileExtension(file), resourceDownloadManager);
    handler.handle(requestArguments);
}
Also used : ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)

Aggregations

ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)5 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)4 RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)3 AsyncContext (javax.servlet.AsyncContext)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.testng.annotations.Test)3 EzyResultCallback (com.tvd12.ezyfox.concurrent.callback.EzyResultCallback)1 EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)1 Resource (com.tvd12.ezyhttp.server.core.resources.Resource)1 ResourceResolver (com.tvd12.ezyhttp.server.core.resources.ResourceResolver)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1