HttpStaticResourcesProcessor implements Processor { @Override public void process(HttpRequest request, HttpResponse response) throws Exception { String path = request.getPath().replaceFirst("/", ""); String suffix = path.substring(path.lastIndexOf(".") + 1); File jarFile = context.getJarFile(); byte[] bytes = FileUtils.readFileFromJar(jarFile, path); ContentTypeMapper mapper = context.getContentTypeMapper(); String contentType = mapper.get(suffix); if (contentType != null) { response.addHeader("Content-Type", contentType); } OutputStream outputStream = response.getResponseOutputStream(); outputStream.write(bytes); } HttpStaticResourcesProcessor(ServletContext context); @Override void process(HttpRequest request, HttpResponse response);  }
@Test public void testStaticResourcesProcessorCanHandle() throws Exception { ServletContext servletContext = ServletContext.contextWithMapperAndClassLoader(null,null,"sample.jar"); Processor processor = new HttpStaticResourcesProcessor(servletContext); HttpResponse response = new HttpResponse(ByteBuffer.allocate(1024)); Map<String, String> headers = new HashMap<>(); headers.put("Sec-Fetch-Dest", "image"); headers.put("Connection", "Keep-Alive"); headers.put("User-Agent", "Apache-HttpClient/4.5.3 (Java/1.8.0_232)"); headers.put("Accept-Encoding", "gzip,deflate"); HttpRequest request = HttpRequest.newBuilder() .setMethod("GET") .setHttpVersion("HTTP/1.1") .setHeaders(headers) .setPath("/favicon.ico") .build(); processor.process(request,response); byte[] actual = response.getBodyBytes(); byte[] except = FileUtils.readBytes(new File("core/src/test/resources/favicon.ico")); assertArrayEquals(except, actual); }
@Test public void testStaticResourcesProcessJsFile() throws Exception { ServletContext servletContext = ServletContext.contextWithMapperAndClassLoader(null,null,"sample.jar"); Processor processor = new HttpStaticResourcesProcessor(servletContext); HttpResponse response = new HttpResponse(ByteBuffer.allocate(1024)); Map<String, String> headers = new HashMap<>(); headers.put("Sec-Fetch-Dest", "image"); headers.put("Connection", "Keep-Alive"); headers.put("User-Agent", "Apache-HttpClient/4.5.3 (Java/1.8.0_232)"); headers.put("Accept-Encoding", "gzip,deflate"); HttpRequest request = HttpRequest.newBuilder() .setMethod("GET") .setHttpVersion("HTTP/1.1") .setHeaders(headers) .setPath("/scripts/jquery.min.js") .build(); processor.process(request,response); byte[] actual = response.getBodyBytes(); assertEquals(92554, actual.length); }