use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.
the class BodyInsertersTests method ofServerSentEventFlux.
@Test
public void ofServerSentEventFlux() throws Exception {
ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
Flux<ServerSentEvent<String>> body = Flux.just(event);
BodyInserter<Flux<ServerSentEvent<String>>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.
the class AbstractMessageReaderArgumentResolver method readBody.
protected Mono<Object> readBody(MethodParameter bodyParameter, boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {
ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve());
ResolvableType elementType = (adapter != null ? bodyType.getGeneric(0) : bodyType);
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
MediaType mediaType = request.getHeaders().getContentType();
if (mediaType == null) {
mediaType = MediaType.APPLICATION_OCTET_STREAM;
}
for (HttpMessageReader<?> reader : getMessageReaders()) {
if (reader.canRead(elementType, mediaType)) {
Map<String, Object> readHints = Collections.emptyMap();
if (adapter != null && adapter.isMultiValue()) {
Flux<?> flux;
if (reader instanceof ServerHttpMessageReader) {
ServerHttpMessageReader<?> serverReader = ((ServerHttpMessageReader<?>) reader);
flux = serverReader.read(bodyType, elementType, request, response, readHints);
} else {
flux = reader.read(elementType, request, readHints);
}
flux = flux.onErrorResumeWith(ex -> Flux.error(getReadError(bodyParameter, ex)));
if (isBodyRequired || !adapter.supportsEmpty()) {
flux = flux.switchIfEmpty(Flux.error(getRequiredBodyError(bodyParameter)));
}
Object[] hints = extractValidationHints(bodyParameter);
if (hints != null) {
flux = flux.doOnNext(target -> validate(target, hints, bodyParameter, bindingContext, exchange));
}
return Mono.just(adapter.fromPublisher(flux));
} else {
Mono<?> mono;
if (reader instanceof ServerHttpMessageReader) {
ServerHttpMessageReader<?> serverReader = (ServerHttpMessageReader<?>) reader;
mono = serverReader.readMono(bodyType, elementType, request, response, readHints);
} else {
mono = reader.readMono(elementType, request, readHints);
}
mono = mono.otherwise(ex -> Mono.error(getReadError(bodyParameter, ex)));
if (isBodyRequired || (adapter != null && !adapter.supportsEmpty())) {
mono = mono.otherwiseIfEmpty(Mono.error(getRequiredBodyError(bodyParameter)));
}
Object[] hints = extractValidationHints(bodyParameter);
if (hints != null) {
mono = mono.doOnNext(target -> validate(target, hints, bodyParameter, bindingContext, exchange));
}
if (adapter != null) {
return Mono.just(adapter.fromPublisher(mono));
} else {
return Mono.from(mono);
}
}
}
}
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes));
}
use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
HttpServletRequest servletRequest = getHttpServletRequest(request);
HttpServletResponse servletResponse = getHttpServletResponse(response);
JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(handler, session -> {
HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
DataBufferFactory factory = response.bufferFactory();
return new JettyWebSocketSession(session, info, factory);
});
startLazily(servletRequest);
boolean isUpgrade = this.factory.isUpgradeRequest(servletRequest, servletResponse);
Assert.isTrue(isUpgrade, "Not a WebSocket handshake");
try {
adapterHolder.set(new WebSocketHandlerContainer(adapter, subProtocol));
this.factory.acceptWebSocket(servletRequest, servletResponse);
} catch (IOException ex) {
return Mono.error(ex);
} finally {
adapterHolder.remove();
}
return Mono.empty();
}
use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.
the class HttpMessageWriterView method applyMessageWriter.
@SuppressWarnings("unchecked")
private <T> Mono<Void> applyMessageWriter(Object value, MediaType contentType, ServerWebExchange exchange) {
if (value == null) {
return Mono.empty();
}
Publisher<? extends T> stream = Mono.just((T) value);
ResolvableType type = ResolvableType.forClass(value.getClass());
ServerHttpResponse response = exchange.getResponse();
return ((HttpMessageWriter<T>) getMessageWriter()).write(stream, type, contentType, response, Collections.emptyMap());
}
use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.
the class ScriptTemplateView method renderInternal.
@Override
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange) {
return Mono.defer(() -> {
ServerHttpResponse response = exchange.getResponse();
try {
ScriptEngine engine = getEngine();
Invocable invocable = (Invocable) engine;
String url = getUrl();
String template = getTemplate(url);
Function<String, String> templateLoader = path -> {
try {
return getTemplate(path);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
};
RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
Object html;
if (this.renderObject != null) {
Object thiz = engine.eval(this.renderObject);
html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
} else {
html = invocable.invokeFunction(this.renderFunction, template, model, context);
}
byte[] bytes = String.valueOf(html).getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().allocateBuffer(bytes.length).write(bytes);
return response.writeWith(Mono.just(buffer));
} catch (ScriptException ex) {
throw new IllegalStateException("Failed to render script template", new StandardScriptEvalException(ex));
} catch (Exception ex) {
throw new IllegalStateException("Failed to render script template", ex);
}
});
}
Aggregations