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
This commit is contained in:
Dave Syer
2017-01-06 12:40:46 +00:00
parent 78d71651da
commit 4ad01be090
8 changed files with 228 additions and 10 deletions

View File

@@ -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<String> function(@PathVariable String name,
@RequestBody Flux<String> body) {
Function<Object, Object> function;
@@ -57,14 +60,14 @@ public class FunctionController {
}
@SuppressWarnings("unchecked")
Flux<String> result = (Flux<String>) function.apply(body);
return result;
return debug ? result.log() : result;
}
@GetMapping("/{name}")
@GetMapping(path = "/{name}")
public Flux<String> supplier(@PathVariable String name) {
@SuppressWarnings("unchecked")
Flux<String> result = (Flux<String>) functions.lookupSupplier(name).get();
return result;
return debug ? result.log() : result;
}
}

View File

@@ -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<HttpMessageReader<?>> 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<String> decode(Publisher<DataBuffer> inputStream,
ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Flux<DataBuffer> inputFlux = Flux.from(inputStream);
inputFlux = Flux.from(inputStream).flatMap(SseDecoder::splitOnNewline);
return inputFlux.map(buffer -> decodeDataBuffer(buffer, mimeType));
}
private static Flux<DataBuffer> splitOnNewline(DataBuffer dataBuffer) {
List<DataBuffer> 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;
}
}
}