public ReactorServerHttpResponse(HttpServerResponse response, DataBufferFactory bufferFactory) { super(bufferFactory, new HttpHeaders(new NettyHeadersAdapter(response.responseHeaders()))); Assert.notNull(response, "HttpServerResponse must not be null"); this.response = response; }
public ReactorServerHttpResponse(HttpServerResponse response, DataBufferFactory bufferFactory) { super(bufferFactory, new HttpHeaders(new NettyHeadersAdapter(response.responseHeaders()))); Assert.notNull(response, "HttpServerResponse must not be null"); this.response = response; }
private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) { if (ObjectUtils.isEmpty(mimeTypes)) { return ALWAYS_COMPRESS; } return (request, response) -> { String contentType = response.responseHeaders() .get(HttpHeaderNames.CONTENT_TYPE); if (StringUtils.isEmpty(contentType)) { return false; } MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType); return Arrays.stream(mimeTypes).map(MimeTypeUtils::parseMimeType) .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); }; }
@Override protected void applyHeaders() { getHeaders().forEach((headerName, headerValues) -> { for (String value : headerValues) { this.response.responseHeaders().add(headerName, value); } }); }
public ReactorServerHttpResponse(HttpServerResponse response, DataBufferFactory bufferFactory) { super(bufferFactory, new HttpHeaders(new NettyHeadersAdapter(response.responseHeaders()))); Assert.notNull(response, "HttpServerResponse must not be null"); this.response = response; }
@Nullable static BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate(@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate, int minResponseSize) { if (minResponseSize <= 0) { if (compressionPredicate != null) { return compressionPredicate; } else { return null; } } BiPredicate<HttpServerRequest, HttpServerResponse> lengthPredicate = (req, res) -> { String length = res.responseHeaders() .get(HttpHeaderNames.CONTENT_LENGTH); if (length == null) { return true; } try { return Long.parseLong(length) >= minResponseSize; } catch (NumberFormatException nfe) { return true; } }; if (compressionPredicate != null) { lengthPredicate = lengthPredicate.and(compressionPredicate); } return lengthPredicate; }
@Nullable static BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate(@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate, int minResponseSize) { if (minResponseSize <= 0) { if (compressionPredicate != null) { return compressionPredicate; } else { return null; } } BiPredicate<HttpServerRequest, HttpServerResponse> lengthPredicate = (req, res) -> { String length = res.responseHeaders() .get(HttpHeaderNames.CONTENT_LENGTH); if (length == null) { return true; } try { return Long.parseLong(length) >= minResponseSize; } catch (NumberFormatException nfe) { return true; } }; if (compressionPredicate != null) { lengthPredicate = lengthPredicate.and(compressionPredicate); } return lengthPredicate; }
@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")); } }
@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")); } }
.route(req -> req.uri().startsWith("/3"), (req, res) -> { res.responseHeaders().set("Content-Length", 2); return res.sendString(Mono.just("OK")); }) .route(req -> req.uri().startsWith("/6"), (req, res) -> { res.responseHeaders().set("Content-Length", 2); return res.sendHeaders(); }) .route(req -> req.uri().startsWith("/9"), (req, res) -> { res.responseHeaders().set("Content-Length", 2); return res.send(); }))