From 4ad01be0909dec8fc9f24d46a7622cf9270bcc62 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 6 Jan 2017 12:40:46 +0000 Subject: [PATCH] Add support for server-side events and tests User can POST to web endpoint in SSE style, i.e: HTTP/1.1 Content-Type: text/event-stream data:foo data:bar Will be converted to a Flux with values foo and bar --- spring-cloud-function-deployer/pom.xml | 5 + .../function/deployer/ApplicationRunner.java | 21 +++- ...actingFunctionCatalogIntegrationTests.java | 3 +- .../function/stream/StreamConfiguration.java | 1 + spring-cloud-function-web/pom.xml | 10 ++ .../function/web/FunctionController.java | 13 ++- .../cloud/function/web/RestApplication.java | 83 +++++++++++++- .../function/web/RestApplicationTests.java | 102 ++++++++++++++++++ 8 files changed, 228 insertions(+), 10 deletions(-) create mode 100644 spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/RestApplicationTests.java diff --git a/spring-cloud-function-deployer/pom.xml b/spring-cloud-function-deployer/pom.xml index 044618b53..dccb0937b 100644 --- a/spring-cloud-function-deployer/pom.xml +++ b/spring-cloud-function-deployer/pom.xml @@ -30,6 +30,11 @@ spring-cloud-function-web ${project.version} + + org.springframework.cloud + spring-cloud-function-stream + ${project.version} + org.springframework.boot spring-boot-configuration-processor diff --git a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/ApplicationRunner.java b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/ApplicationRunner.java index 4180c6e31..0eaef5e5a 100644 --- a/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/ApplicationRunner.java +++ b/spring-cloud-function-deployer/src/main/java/org/springframework/cloud/function/deployer/ApplicationRunner.java @@ -45,6 +45,8 @@ import org.springframework.boot.loader.thin.ArchiveUtils; import org.springframework.cloud.deployer.thin.ContextRunner; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.SpringVersion; +import org.springframework.messaging.Message; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -60,6 +62,8 @@ public class ApplicationRunner implements CommandLineRunner { private static final String DEFAULT_REACTOR_VERSION = "3.0.4.RELEASE"; + private static final String DEFAULT_SPRING_VERSION = SpringVersion.getVersion(); + private static Log logger = LogFactory.getLog(ApplicationRunner.class); public static void main(String[] args) { @@ -125,14 +129,18 @@ public class ApplicationRunner implements CommandLineRunner { for (URL url : urls) { child.add(url); } - String reactor = getReactorCoordinates(); DependencyResolutionContext context = new DependencyResolutionContext(); AetherEngine engine = AetherEngine.create( RepositoryConfigurationFactory.createDefaultRepositoryConfiguration(), context); + String reactor = getReactorCoordinates(); + // spring-core is OK, spring-context is not, spring-messaging depends on + // spring-context (so it is not OK) + String spring = getSpringCoordinates(); try { - List resolved = engine.resolve(Arrays - .asList(new Dependency(new DefaultArtifact(reactor), "runtime"))); + List resolved = engine.resolve( + Arrays.asList(new Dependency(new DefaultArtifact(reactor), "runtime"), + new Dependency(new DefaultArtifact(spring), "runtime"))); for (File archive : resolved) { try { URL url = archive.toURI().toURL(); @@ -155,6 +163,13 @@ public class ApplicationRunner implements CommandLineRunner { return new URLClassLoader(child.toArray(new URL[0]), base); } + private String getSpringCoordinates() { + Package pkg = Message.class.getPackage(); + String version = null; + version = (pkg != null ? pkg.getImplementationVersion() : DEFAULT_SPRING_VERSION); + return "org.springframework:spring-core:" + version; + } + private String getReactorCoordinates() { Package pkg = Flux.class.getPackage(); String version = null; diff --git a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionExtractingFunctionCatalogIntegrationTests.java b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionExtractingFunctionCatalogIntegrationTests.java index fa466870a..d906f4f50 100644 --- a/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionExtractingFunctionCatalogIntegrationTests.java +++ b/spring-cloud-function-deployer/src/test/java/org/springframework/cloud/function/deployer/FunctionExtractingFunctionCatalogIntegrationTests.java @@ -38,7 +38,8 @@ public class FunctionExtractingFunctionCatalogIntegrationTests { public static void open() { port = SocketUtils.findAvailableTcpPort(); // System.setProperty("debug", "true"); - context = new ApplicationRunner().start("--server.port=" + port); + context = new ApplicationRunner().start("--server.port=" + port, + "--spring.cloud.stream.enabled=false"); } @AfterClass diff --git a/spring-cloud-function-stream/src/main/java/org/springframework/cloud/function/stream/StreamConfiguration.java b/spring-cloud-function-stream/src/main/java/org/springframework/cloud/function/stream/StreamConfiguration.java index bf1486bb9..eaf70789c 100644 --- a/spring-cloud-function-stream/src/main/java/org/springframework/cloud/function/stream/StreamConfiguration.java +++ b/spring-cloud-function-stream/src/main/java/org/springframework/cloud/function/stream/StreamConfiguration.java @@ -38,6 +38,7 @@ import reactor.core.publisher.Flux; @EnableBinding(Processor.class) @EnableConfigurationProperties(FunctionConfigurationProperties.class) @ConditionalOnClass({ Binder.class, AbstractFunctionInvoker.class }) +@ConditionalOnProperty(name = "spring.cloud.stream.enabled", havingValue = "true", matchIfMissing = true) public class StreamConfiguration { @Autowired diff --git a/spring-cloud-function-web/pom.xml b/spring-cloud-function-web/pom.xml index a31e42065..c68184059 100644 --- a/spring-cloud-function-web/pom.xml +++ b/spring-cloud-function-web/pom.xml @@ -28,6 +28,16 @@ spring-cloud-function-core ${project.version} + + org.springframework.cloud + spring-cloud-function-context + ${project.version} + test + + + org.springframework.boot + spring-boot-starter-test + diff --git a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/FunctionController.java b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/FunctionController.java index 2545b7955..1158fd05f 100644 --- a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/FunctionController.java +++ b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/FunctionController.java @@ -18,10 +18,10 @@ package org.springframework.cloud.function.web; import java.util.function.Function; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.embedded.ReactiveServerProperties; import org.springframework.cloud.function.registry.FunctionCatalog; -import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -38,6 +38,9 @@ import reactor.core.publisher.Flux; @ConditionalOnClass({ RestController.class, ReactiveServerProperties.class }) public class FunctionController { + @Value("${debug:${DEBUG:false}}") + private boolean debug = false; + private final FunctionCatalog functions; @Autowired @@ -45,7 +48,7 @@ public class FunctionController { this.functions = catalog; } - @PostMapping(path = "/{name}", consumes = MediaType.TEXT_PLAIN_VALUE) + @PostMapping(path = "/{name}") public Flux function(@PathVariable String name, @RequestBody Flux body) { Function function; @@ -57,14 +60,14 @@ public class FunctionController { } @SuppressWarnings("unchecked") Flux result = (Flux) function.apply(body); - return result; + return debug ? result.log() : result; } - @GetMapping("/{name}") + @GetMapping(path = "/{name}") public Flux supplier(@PathVariable String name) { @SuppressWarnings("unchecked") Flux result = (Flux) functions.lookupSupplier(name).get(); - return result; + return debug ? result.log() : result; } } diff --git a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RestApplication.java b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RestApplication.java index 3ccfab4af..ef9279a66 100644 --- a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RestApplication.java +++ b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RestApplication.java @@ -16,16 +16,97 @@ package org.springframework.cloud.function.web; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.IntPredicate; + +import org.reactivestreams.Publisher; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.core.ResolvableType; +import org.springframework.core.codec.StringDecoder; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferUtils; +import org.springframework.http.MediaType; +import org.springframework.http.codec.DecoderHttpMessageReader; +import org.springframework.http.codec.HttpMessageReader; +import org.springframework.util.MimeType; +import org.springframework.web.reactive.config.WebReactiveConfigurer; + +import reactor.core.publisher.Flux; /** * @author Mark Fisher */ @SpringBootApplication -public class RestApplication { +public class RestApplication implements WebReactiveConfigurer { + + @Override + public void extendMessageReaders(List> readers) { + readers.add(0, new DecoderHttpMessageReader<>(new SseDecoder())); + } public static void main(String[] args) { SpringApplication.run(RestApplication.class, args); } } + +class SseDecoder extends StringDecoder { + + private static final IntPredicate NEWLINE_DELIMITER = b -> b == '\n' || b == '\r'; + + @Override + public boolean canDecode(ResolvableType elementType, MimeType mimeType) { + return super.canDecode(elementType, mimeType) + && MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mimeType); + } + + @Override + public Flux decode(Publisher inputStream, + ResolvableType elementType, MimeType mimeType, Map hints) { + + Flux inputFlux = Flux.from(inputStream); + inputFlux = Flux.from(inputStream).flatMap(SseDecoder::splitOnNewline); + return inputFlux.map(buffer -> decodeDataBuffer(buffer, mimeType)); + } + + private static Flux splitOnNewline(DataBuffer dataBuffer) { + List results = new ArrayList<>(); + int startIdx = 0; + int endIdx; + final int limit = dataBuffer.readableByteCount(); + do { + endIdx = dataBuffer.indexOf(NEWLINE_DELIMITER, startIdx); + endIdx = dataBuffer.indexOf(NEWLINE_DELIMITER, endIdx + 1); + int length = (endIdx != -1 ? endIdx - startIdx + 1 : limit - startIdx) - 7; + if (length > 0) { + DataBuffer token = dataBuffer.slice(startIdx + 5, length); + results.add(DataBufferUtils.retain(token)); + } + startIdx = endIdx + 1; + } + while (startIdx < limit && endIdx != -1); + DataBufferUtils.release(dataBuffer); + return Flux.fromIterable(results); + } + + private String decodeDataBuffer(DataBuffer dataBuffer, MimeType mimeType) { + Charset charset = getCharset(mimeType); + CharBuffer charBuffer = charset.decode(dataBuffer.asByteBuffer()); + DataBufferUtils.release(dataBuffer); + return charBuffer.toString(); + } + + private Charset getCharset(MimeType mimeType) { + if (mimeType != null && mimeType.getCharset() != null) { + return mimeType.getCharset(); + } + else { + return DEFAULT_CHARSET; + } + } +} \ No newline at end of file diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/RestApplicationTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/RestApplicationTests.java new file mode 100644 index 000000000..d0c83db43 --- /dev/null +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/RestApplicationTests.java @@ -0,0 +1,102 @@ +/* + * Copyright 2016-2017 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.cloud.function.web; + +import java.net.URI; +import java.util.function.Function; +import java.util.function.Supplier; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.StringUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +import reactor.core.publisher.Flux; + +/** + * @author Dave Syer + * + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class RestApplicationTests { + + @LocalServerPort + private int port; + private TestRestTemplate rest = new TestRestTemplate(); + + @Test + public void wordsSSE() throws Exception { + assertThat( + rest.exchange( + RequestEntity.get(new URI("http://localhost:" + port + "/words")) + .accept(MediaType.TEXT_EVENT_STREAM).build(), + String.class).getBody()).isEqualTo(sse("foo", "bar")); + } + + @Test + public void words() throws Exception { + assertThat(rest.exchange( + RequestEntity.get(new URI("http://localhost:" + port + "/words")).build(), + String.class).getBody()).isEqualTo("foobar"); + } + + @Test + public void uppercase() { + assertThat(rest.postForObject("http://localhost:" + port + "/uppercase", + "foo\nbar", String.class)).isEqualTo("[FOO][BAR]"); + } + + @Test + public void uppercaseSSE() throws Exception { + assertThat(rest.exchange( + RequestEntity.post(new URI("http://localhost:" + port + "/uppercase")) + .accept(MediaType.TEXT_EVENT_STREAM) + .contentType(MediaType.TEXT_EVENT_STREAM).body(sse("foo", "bar")), + String.class).getBody()).isEqualTo(sse("[FOO]", "[BAR]")); + } + + private String sse(String... values) { + return "data:" + StringUtils.arrayToDelimitedString(values, "\n\ndata:") + "\n\n"; + } + + @SpringBootApplication + public static class TestConfiguration { + + @Bean + public Function, Flux> uppercase() { + return flux -> flux.map(value -> "[" + value.trim().toUpperCase() + "]"); + } + + @Bean + public Supplier> words() { + return () -> Flux.fromArray(new String[] { "foo", "bar" }); + } + + } + +}