@Override public Mono<Void> writeWith(Path file, long position, long count) { return doCommit(() -> this.response.sendFile(file, position, count).then()); }
@Override public Mono<Void> writeWith(Path file, long position, long count) { return doCommit(() -> this.response.sendFile(file, position, count).then()); }
@Override public HttpServerRoutes directory(String uri, Path directory, Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(directory, "directory"); return route(HttpPredicate.prefix(uri), (req, resp) -> { String prefix = URI.create(req.uri()) .getPath() .replaceFirst(uri, ""); if(prefix.charAt(0) == '/'){ prefix = prefix.substring(1); } Path p = directory.resolve(prefix); if (Files.isReadable(p)) { if (interceptor != null) { return interceptor.apply(resp) .sendFile(p); } return resp.sendFile(p); } return resp.sendNotFound(); }); }
/** * Listen for HTTP GET on the passed path to be used as a routing condition. The * file on the provided {@link Path} is served. * <p> * Additional regex matching is available e.g. * "/test/{param}". Params are resolved using {@link HttpServerRequest#param(CharSequence)} * * @param uri The {@link HttpPredicate} to use to trigger the route, pattern matching * and capture are supported * @param path the Path to the file to serve * @param interceptor a channel pre-intercepting handler e.g. for content type header * * @return this {@link HttpServerRoutes} */ default HttpServerRoutes file(Predicate<HttpServerRequest> uri, Path path, @Nullable Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(path, "path"); return route(uri, (req, resp) -> { if (!Files.isReadable(path)) { return resp.send(ByteBufFlux.fromPath(path)); } if (interceptor != null) { return interceptor.apply(resp) .sendFile(path); } return resp.sendFile(path); }); }
@Override public HttpServerRoutes directory(String uri, Path directory, Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(directory, "directory"); return route(HttpPredicate.prefix(uri), (req, resp) -> { String prefix = URI.create(req.uri()) .getPath() .replaceFirst(uri, ""); if(prefix.charAt(0) == '/'){ prefix = prefix.substring(1); } Path p = directory.resolve(prefix); if (Files.isReadable(p)) { if (interceptor != null) { return interceptor.apply(resp) .sendFile(p); } return resp.sendFile(p); } return resp.sendNotFound(); }); }
@Override public Mono<Void> writeWith(File file, long position, long count) { return doCommit(() -> this.response.sendFile(file.toPath(), position, count).then()); }
/** * Listen for HTTP GET on the passed path to be used as a routing condition. The * file on the provided {@link Path} is served. * <p> * Additional regex matching is available e.g. * "/test/{param}". Params are resolved using {@link HttpServerRequest#param(CharSequence)} * * @param uri The {@link HttpPredicate} to use to trigger the route, pattern matching * and capture are supported * @param path the Path to the file to serve * @param interceptor a channel pre-intercepting handler e.g. for content type header * * @return this {@link HttpServerRoutes} */ default HttpServerRoutes file(Predicate<HttpServerRequest> uri, Path path, @Nullable Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(path, "path"); return route(uri, (req, resp) -> { if (!Files.isReadable(path)) { return resp.send(ByteBufFlux.fromPath(path)); } if (interceptor != null) { return interceptor.apply(resp) .sendFile(path); } return resp.sendFile(path); }); }
@Override public Mono<Void> writeWith(Path file, long position, long count) { return doCommit(() -> this.response.sendFile(file, position, count).then()); }
@Test public void sendZipFileDefault() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.sendFile(fromZipFile, 0, fileSize)); } }
@Test public void sendZipFileCompressionPredicate_1() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.sendFile(fromZipFile, 0, fileSize), true, -1, (req, res) -> true); } }
@Test public void sendZipFileCompressionSize_3() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader(HttpHeaderNames.CONTENT_LENGTH, "1245") .sendFile(fromZipFile, 0, fileSize), true, 512, null); } }
@Test public void sendZipFileCompressionOn() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.compression(true).sendFile(fromZipFile, 0, fileSize), true, -1, (req, res) -> false); } }
@Test public void sendZipFileCompressionSize_1() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader(HttpHeaderNames.CONTENT_LENGTH, "1245") .sendFile(fromZipFile, 0, fileSize), true, 2048, null); } }
@Test public void sendZipFileCompressionSize_2() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader(HttpHeaderNames.CONTENT_LENGTH, "1245") .sendFile(fromZipFile, 0, fileSize), true, 2048, (req, res) -> true); } }
@Test public void sendZipFileCompressionSize_4() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader(HttpHeaderNames.CONTENT_LENGTH, "1245") .sendFile(fromZipFile, 0, fileSize), true, 512, (req, res) -> false); } }
@Test public void sendZipFileCompressionPredicate_2() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader("test", "test").sendFile(fromZipFile, 0, fileSize), true, -1, (req, res) -> res.responseHeaders().contains("test")); } }
@Test public void sendZipFileCompressionPredicate_3() throws IOException { Path path = Files.createTempFile(null, ".zip"); Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING); path.toFile().deleteOnExit(); try (FileSystem zipFs = FileSystems.newFileSystem(path, null)) { Path fromZipFile = zipFs.getPath("/largeFile.txt"); long fileSize = Files.size(fromZipFile); assertSendFile(out -> out.addHeader("test", "test").sendFile(fromZipFile, 0, fileSize), true, -1, (req, res) -> !res.responseHeaders().contains("test")); } }