StaticFileServer implements StaticFileHandler { @Override public boolean serveStaticResource(HttpServletRequest request, HttpServletResponse response) throws IOException { String filenameWithPath = getRequestFilename(request); if (!isPathSafe(filenameWithPath)) { getLogger().info("Blocked attempt to access file: {}", filenameWithPath); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } URL resourceUrl = null; if (isAllowedVAADINBuildUrl(filenameWithPath)) { resourceUrl = servletService.getClassLoader() .getResource("META-INF" + filenameWithPath); } if (resourceUrl == null) { resourceUrl = servletService.getStaticResource(filenameWithPath); } if (resourceUrl == null && shouldFixIncorrectWebjarPaths() && isIncorrectWebjarPath(filenameWithPath)) { resourceUrl = servletService.getStaticResource( fixIncorrectWebjarPath(filenameWithPath)); } if (resourceUrl == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return true; } writeCacheHeaders(filenameWithPath, response); long timestamp = writeModificationTimestamp(resourceUrl, request, response); if (browserHasNewestVersion(request, timestamp)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } responseWriter.writeResponseContents(filenameWithPath, resourceUrl, request, response); return true; } StaticFileServer(VaadinServletService servletService); @Override boolean isStaticResourceRequest(HttpServletRequest request); @Override boolean serveStaticResource(HttpServletRequest request,
            HttpServletResponse response);  }
@Test public void serveNonExistingStaticResource() throws IOException { setupRequestURI("", "", "/nonexisting/file.js"); fileServer.serveStaticResource(request, response); Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, responseCode.get()); }
@Test public void serveStaticResource() throws IOException { setupRequestURI("", "/some", "/file.js"); byte[] fileData = "function() {eval('foo');};" .getBytes(StandardCharsets.UTF_8); Mockito.when(servletService.getStaticResource("/some/file.js")) .thenReturn(createFileURLWithDataAndLength("/some/file.js", fileData)); CapturingServletOutputStream out = new CapturingServletOutputStream(); Mockito.when(response.getOutputStream()).thenReturn(out); Assert.assertTrue(fileServer.serveStaticResource(request, response)); Assert.assertArrayEquals(fileData, out.getOutput()); }
@Test public void customStaticBuildResource_isServed() throws IOException { String pathInfo = "/VAADIN/build/my-text.txt"; setupRequestURI("", "", pathInfo); byte[] fileData = "function() {eval('foo');};" .getBytes(StandardCharsets.UTF_8); ClassLoader mockLoader = Mockito.mock(ClassLoader.class); Mockito.when(servletService.getClassLoader()).thenReturn(mockLoader); Mockito.when(mockLoader.getResource("META-INF" + pathInfo)).thenReturn( createFileURLWithDataAndLength("META-INF" + pathInfo, fileData)); mockStatsBundles(mockLoader); mockConfigurationPolyfills(); CapturingServletOutputStream out = new CapturingServletOutputStream(); Mockito.when(response.getOutputStream()).thenReturn(out); Assert.assertTrue(fileServer.serveStaticResource(request, response)); Assert.assertArrayEquals(fileData, out.getOutput()); }
@Test public void nonexistingStaticBuildResource_returnsNotFound() throws IOException { String pathInfo = "/VAADIN/build/my-text.txt"; setupRequestURI("", "", pathInfo); byte[] fileData = "function() {eval('foo');};" .getBytes(StandardCharsets.UTF_8); ClassLoader mockLoader = Mockito.mock(ClassLoader.class); Mockito.when(servletService.getClassLoader()).thenReturn(mockLoader); mockStatsBundles(mockLoader); mockConfigurationPolyfills(); CapturingServletOutputStream out = new CapturingServletOutputStream(); Mockito.when(response.getOutputStream()).thenReturn(out); Assert.assertTrue(fileServer.serveStaticResource(request, response)); Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, responseCode.get()); }
@Test public void customStatsJson_isServedFromServlet() throws IOException { String pathInfo = "/VAADIN/build/stats.json"; setupRequestURI("", "/servlet", pathInfo); byte[] fileData = "function() {eval('foo');};" .getBytes(StandardCharsets.UTF_8); ClassLoader mockLoader = Mockito.mock(ClassLoader.class); Mockito.when(servletService.getClassLoader()).thenReturn(mockLoader); mockStatsBundles(mockLoader); mockConfigurationPolyfills(); Mockito.when(servletService.getStaticResource(pathInfo)) .thenReturn(createFileURLWithDataAndLength(pathInfo, fileData)); CapturingServletOutputStream out = new CapturingServletOutputStream(); Mockito.when(response.getOutputStream()).thenReturn(out); Assert.assertTrue(fileServer.serveStaticResource(request, response)); Assert.assertArrayEquals(fileData, out.getOutput()); }
@Test public void serveStaticResourceBrowserHasLatest() throws IOException { long browserLatest = 123L; long fileModified = 123L; setupRequestURI("", "/some", "/file.js"); Mockito.when(request.getDateHeader("If-Modified-Since")) .thenReturn(browserLatest); byte[] fileData = "function() {eval('foo');};" .getBytes(StandardCharsets.UTF_8); Mockito.when(servletService.getStaticResource("/some/file.js")) .thenReturn(createFileURLWithDataAndLength("/some/file.js", fileData, fileModified)); CapturingServletOutputStream out = new CapturingServletOutputStream(); Mockito.when(response.getOutputStream()).thenReturn(out); Assert.assertTrue(fileServer.serveStaticResource(request, response)); Assert.assertEquals(0, out.getOutput().length); Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, responseCode.get()); }