HttpFileRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> { @Override protected void decode(final ChannelHandlerContext context, final FullHttpRequest request, final List<Object> out) { HttpFileRequest fileRequest = null; if (request.getMethod().equals(GET)) { QueryStringDecoder requestDecoder = new QueryStringDecoder(request.getUri()); if (requestDecoder.path().startsWith(HTTP_CONTENT_REQUEST_PATH.toString()) && requestDecoder.parameters().get(HTTP_CONTENT_ID.toString()) != null) { Optional<MediaNode> node = mediaManager.getNode(requestDecoder.parameters().get(HTTP_CONTENT_ID.toString()).get(0)); if (node.isPresent() && node.get() instanceof ContentNode) { ContentNode contentNode = (ContentNode) node.get(); fileRequest = new HttpFileRequest(request, new File(contentNode.getPath()), contentNode.getMimeType(), false); } } else { String requestedFileName = getRequestedFileName(requestDecoder); MimeType mimeType = mimeTypeManager.getMimeType(requestedFileName); if (mimeType != null) { fileRequest = new HttpFileRequest(request, new File(uiDirectory, requestedFileName), mimeType, true); } } } if (fileRequest != null) { out.add(fileRequest); } else { out.add(request.retain()); } } @Inject  HttpFileRequestDecoder(final MediaManager mediaManager, final MimeTypeManager mimeTypeManager, @Named("uiDirectory") final String uiDirectory);   }
@Test public void testDecodeUiFile() throws Exception { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/index.html").atLeastOnce(); expect(mimeTypeManager.getMimeType("/index.html")).andReturn(MimeType.valueOf("text/html")).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0).getClass(), HttpFileRequest.class); HttpFileRequest fileRequest = (HttpFileRequest) out.get(0); assertNotNull(fileRequest.getFile()); assertNotNull(fileRequest.getMimeType()); assertNotNull(fileRequest.getHttpMessage()); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodeUiFileEmpty() throws Exception { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/").atLeastOnce(); expect(mimeTypeManager.getMimeType("/index.html")).andReturn(MimeType.valueOf("text/html")).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0).getClass(), HttpFileRequest.class); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodeUiFileNoMimeType() throws Exception { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/index.html1").atLeastOnce(); expect(request.retain()).andReturn(request).atLeastOnce(); expect(mimeTypeManager.getMimeType("/index.html1")).andReturn(null).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0), request); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodePostMessage() throws Exception { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(POST).atLeastOnce(); expect(request.retain()).andReturn(request).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0), request); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodeContentFile() { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/content?id=1234").atLeastOnce(); ContentNode contentNode = new ContentNode("id", "parentId", "name", new File("file"), MimeType.valueOf("video/x-msvideo")); expect(mediaManager.getNode("1234")).andReturn(Optional.of(contentNode)).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0).getClass(), HttpFileRequest.class); HttpFileRequest fileRequest = (HttpFileRequest) out.get(0); assertNotNull(fileRequest.getFile()); assertNotNull(fileRequest.getMimeType()); assertNotNull(fileRequest.getHttpMessage()); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodeContentFileEmptyContentId() { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/content?id=").atLeastOnce(); expect(request.retain()).andReturn(request).atLeastOnce(); expect(mediaManager.getNode("")).andReturn(Optional.empty()).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0), request); verify(context, request, mediaManager, mimeTypeManager); }
@Test public void testDecodeContentFileNullContentId() { FullHttpRequest request = createMock(FullHttpRequest.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); MediaManager mediaManager = createMock(MediaManager.class); MimeTypeManager mimeTypeManager = createMock(MimeTypeManager.class); List<Object> out = new ArrayList<>(); expect(request.getMethod()).andReturn(GET).atLeastOnce(); expect(request.getUri()).andReturn("/content").atLeastOnce(); expect(request.retain()).andReturn(request).atLeastOnce(); expect(mimeTypeManager.getMimeType("/content")).andReturn(null).atLeastOnce(); replay(context, request, mediaManager, mimeTypeManager); HttpFileRequestDecoder decoder = new HttpFileRequestDecoder(mediaManager, mimeTypeManager, System.getProperty("java.io.tmpdir")); decoder.decode(context, request, out); assertEquals(out.size(), 1); assertEquals(out.get(0), request); verify(context, request, mediaManager, mimeTypeManager); }