HttpFileRequestHandler extends SimpleChannelInboundHandler<HttpFileRequest> { @Override protected void channelRead0(final ChannelHandlerContext context, final HttpFileRequest request) throws HttpFileRequestException, IOException { File file = request.getFile(); if (!isValidFile(file)) { throw new HttpFileRequestException(file.getPath(), NOT_FOUND); } RandomAccessFile randomFile = new RandomAccessFile(file, "r"); long fileLength = randomFile.length(); long startOffset = getStartOffset(request.getHttpMessage()); HttpResponse response = buildHttpResponse(startOffset, fileLength); addContentHeaders(response, fileLength - startOffset, request); addDateAndCacheHeaders(response, request); boolean keepAlive = addKeepAliveHeader(response, request); context.write(response); context.write(new ChunkedFile(randomFile, startOffset, fileLength - startOffset, CHUNK_SIZE)); ChannelFuture lastContentFuture = context.writeAndFlush(EMPTY_LAST_CONTENT); if (!keepAlive) { lastContentFuture.addListener(CLOSE); } } @Inject  HttpFileRequestHandler(final ConfigurationManager configurationManager); @Override void exceptionCaught(final ChannelHandlerContext context, final Throwable cause);  }
@Test public void testFileRequestHandler() throws Exception { File indexHtml = File.createTempFile("index", ".html"); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce(); expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), false); replay(context, httpRequest, channel, configurationManager); HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); verify(context, httpRequest, channel, configurationManager); }
@Test public void testFileRequestHandlerNoCache() throws Exception { File indexHtml = File.createTempFile("index", ".html"); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(0); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce(); expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), true); replay(context, httpRequest, channel, configurationManager); HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); verify(context, httpRequest, channel, configurationManager); }
@Test(expected = HttpFileRequestException.class) public void testFileRequestHandlerInvalidFile() throws Exception { HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); HttpFileRequest request = new HttpFileRequest(httpRequest, new File("invalidFile"), MimeType.valueOf("text/html"), false); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); replay(context, httpRequest, configurationManager); try { HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); } finally { verify(context, httpRequest, configurationManager); } }
@Test public void testFileRequestHandlerWithOffset() throws Exception { File indexHtml = File.createTempFile("index", ".html"); FileWriter fw = new FileWriter(indexHtml); BufferedWriter bw = new BufferedWriter(fw); bw.write("some content in index.html"); bw.close(); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); headers.add(RANGE, "bytes=5-"); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce(); expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), false); replay(context, httpRequest, channel, configurationManager); HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); verify(context, httpRequest, channel, configurationManager); }
@Test(expected = HttpFileRequestException.class) public void testFileRequestHandlerWithEmptyOffset() throws Exception { File indexHtml = File.createTempFile("index", ".html"); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); headers.add(RANGE, ""); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), true); replay(context, httpRequest, configurationManager); try { HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); } finally { verify(context, httpRequest, configurationManager); } }
@Test(expected = HttpFileRequestException.class) public void testFileRequestHandlerWithBadOffset() throws Exception { File indexHtml = File.createTempFile("index", ".html"); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); headers.add(RANGE, "bytes=5-"); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), true); replay(context, httpRequest, configurationManager); try { HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); } finally { verify(context, httpRequest, configurationManager); } }
@Test public void testFileRequestHandlerWithoutKeepAlive() throws Exception { File indexHtml = File.createTempFile("index", ".html"); indexHtml.deleteOnExit(); HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HOST, "localhost"); headers.add(CONNECTION, CLOSE); ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); FullHttpRequest httpRequest = createMock(FullHttpRequest.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(httpRequest.headers()).andReturn(headers).atLeastOnce(); expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()), MimeType.valueOf("text/html"), true); replay(context, httpRequest, channel, configurationManager); HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationManager); handler.channelRead0(context, request); verify(context, httpRequest, channel, configurationManager); }