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);
}
}
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();
}
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();
}
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();
}
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);
}
Aggregations