Browse Source
This commit introduces support for zero-copy file transfers in the HTTP response, through the ZeroCopyHttpOutputMessage subinterface of ReactiveHttpOutputMessage.pull/1111/head
8 changed files with 225 additions and 7 deletions
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
/* |
||||
* Copyright 2002-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http; |
||||
|
||||
import java.io.File; |
||||
|
||||
import reactor.core.publisher.Mono; |
||||
|
||||
/** |
||||
* Sub-interface of {@code ReactiveOutputMessage} that has support for "zero-copy" |
||||
* file transfers. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @see <a href="https://en.wikipedia.org/wiki/Zero-copy">Zero-copy</a> |
||||
*/ |
||||
public interface ZeroCopyHttpOutputMessage extends ReactiveHttpOutputMessage { |
||||
|
||||
/** |
||||
* Set the body of the message to the given {@link File} which will be |
||||
* used to write to the underlying HTTP layer. |
||||
* @param file the file to transfer |
||||
* @param position the position within the file from which the transfer is to begin |
||||
* @param count the number of bytes to be transferred |
||||
* @return a publisher that indicates completion or error. |
||||
*/ |
||||
Mono<Void> setBody(File file, long position, long count); |
||||
|
||||
} |
||||
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
/* |
||||
* Copyright 2002-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.server.reactive; |
||||
|
||||
import java.io.File; |
||||
import java.net.URI; |
||||
|
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.core.io.ClassPathResource; |
||||
import org.springframework.core.io.Resource; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.RequestEntity; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.http.ZeroCopyHttpOutputMessage; |
||||
import org.springframework.http.server.reactive.boot.ReactorHttpServer; |
||||
import org.springframework.http.server.reactive.boot.UndertowHttpServer; |
||||
import org.springframework.web.client.RestTemplate; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.junit.Assume.assumeTrue; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
public class ZeroCopyIntegrationTests extends AbstractHttpHandlerIntegrationTests { |
||||
|
||||
private final ZeroCopyHandler handler = new ZeroCopyHandler(); |
||||
|
||||
@Override |
||||
protected HttpHandler createHttpHandler() { |
||||
return handler; |
||||
} |
||||
|
||||
@Test |
||||
public void zeroCopy() throws Exception { |
||||
// Zero-copy only does not support servlet
|
||||
assumeTrue(server instanceof ReactorHttpServer || |
||||
server instanceof UndertowHttpServer); |
||||
|
||||
RestTemplate restTemplate = new RestTemplate(); |
||||
|
||||
RequestEntity request = |
||||
RequestEntity.get(new URI("http://localhost:" + port)).build(); |
||||
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); |
||||
|
||||
Resource logo = |
||||
new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); |
||||
|
||||
assertTrue(response.hasBody()); |
||||
assertEquals(logo.contentLength(), response.getHeaders().getContentLength()); |
||||
assertEquals(logo.contentLength(), response.getBody().length); |
||||
assertEquals(MediaType.IMAGE_PNG, response.getHeaders().getContentType()); |
||||
|
||||
} |
||||
|
||||
private static class ZeroCopyHandler implements HttpHandler { |
||||
|
||||
@Override |
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { |
||||
try { |
||||
ZeroCopyHttpOutputMessage zeroCopyResponse = |
||||
(ZeroCopyHttpOutputMessage) response; |
||||
|
||||
Resource logo = new ClassPathResource("spring.png", |
||||
ZeroCopyIntegrationTests.class); |
||||
File logoFile = logo.getFile(); |
||||
zeroCopyResponse.getHeaders().setContentType(MediaType.IMAGE_PNG); |
||||
zeroCopyResponse.getHeaders().setContentLength(logoFile.length()); |
||||
return zeroCopyResponse.setBody(logoFile, 0, logoFile.length()); |
||||
|
||||
} |
||||
catch (Throwable ex) { |
||||
return Mono.error(ex); |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
|
After Width: | Height: | Size: 951 B |
Loading…
Reference in new issue