Spring-consistent naming in server.http package
This change also removes reactor-stream variants of the request and response since the request and response aren't used directly by application code and get passed through reactor.Publishers anyway.
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
package org.springframework.http.server.reactor;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.convert.DependencyUtils;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.io.net.ReactiveChannelHandler;
|
||||
import reactor.io.net.http.HttpChannel;
|
||||
@@ -27,30 +26,22 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
public class ReactorHttpHandlerAdapter
|
||||
public class HttpHandlerChannelHandler
|
||||
implements ReactiveChannelHandler<Buffer, Buffer, HttpChannel<Buffer, Buffer>> {
|
||||
|
||||
private final ReactiveHttpHandler httpHandler;
|
||||
|
||||
|
||||
public ReactorHttpHandlerAdapter(ReactiveHttpHandler httpHandler) {
|
||||
public HttpHandlerChannelHandler(ReactiveHttpHandler httpHandler) {
|
||||
Assert.notNull(httpHandler, "'httpHandler' is required.");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> apply(HttpChannel<Buffer, Buffer> channel) {
|
||||
final PublisherReactorServerHttpRequest adaptedRequest;
|
||||
final PublisherReactorServerHttpResponse adaptedResponse;
|
||||
|
||||
if(DependencyUtils.hasReactorStream()){
|
||||
adaptedRequest = new ReactorServerHttpRequest(channel);
|
||||
adaptedResponse = new ReactorServerHttpResponse(channel);
|
||||
}
|
||||
else{
|
||||
adaptedRequest = new PublisherReactorServerHttpRequest(channel);
|
||||
adaptedResponse = new PublisherReactorServerHttpResponse(channel);
|
||||
}
|
||||
ReactorServerHttpRequest adaptedRequest = new ReactorServerHttpRequest(channel);
|
||||
ReactorServerHttpResponse adaptedResponse = new ReactorServerHttpResponse(channel);
|
||||
return this.httpHandler.handle(adaptedRequest, adaptedResponse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.reactor;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.Publishers;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.io.net.http.HttpChannel;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.ReactiveServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
public class PublisherReactorServerHttpRequest implements ReactiveServerHttpRequest {
|
||||
|
||||
private final HttpChannel<Buffer, ?> channel;
|
||||
|
||||
private HttpHeaders headers;
|
||||
|
||||
|
||||
public PublisherReactorServerHttpRequest(HttpChannel<Buffer, ?> request) {
|
||||
Assert.notNull("'request', request must not be null.");
|
||||
this.channel = request;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
for (String name : this.channel.headers().names()) {
|
||||
for (String value : this.channel.headers().getAll(name)) {
|
||||
this.headers.add(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return HttpMethod.valueOf(this.channel.method().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
try {
|
||||
return new URI(this.channel.uri());
|
||||
} catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Could not get URI: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<ByteBuffer> getBody() {
|
||||
return Publishers.map(channel.input(), Buffer::byteBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.reactor;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.Publishers;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.io.net.http.HttpChannel;
|
||||
import reactor.io.net.http.model.Status;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.ReactiveServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
public class PublisherReactorServerHttpResponse implements ReactiveServerHttpResponse {
|
||||
|
||||
private final HttpChannel<?, Buffer> channel;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
private boolean headersWritten = false;
|
||||
|
||||
|
||||
public PublisherReactorServerHttpResponse(HttpChannel<?, Buffer> response) {
|
||||
Assert.notNull("'response', response must not be null.");
|
||||
this.channel = response;
|
||||
this.headers = new HttpHeaders();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setStatusCode(HttpStatus status) {
|
||||
this.channel.responseStatus(Status.valueOf(status.value()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> writeHeaders() {
|
||||
if (this.headersWritten) {
|
||||
return Publishers.empty();
|
||||
}
|
||||
applyHeaders();
|
||||
return this.channel.writeHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> setBody(Publisher<ByteBuffer> contentPublisher) {
|
||||
applyHeaders();
|
||||
return this.channel.writeWith(Publishers.map(contentPublisher, Buffer::new));
|
||||
}
|
||||
|
||||
private void applyHeaders() {
|
||||
if (!this.headersWritten) {
|
||||
for (String name : this.headers.keySet()) {
|
||||
for (String value : this.headers.get(name)) {
|
||||
this.channel.responseHeaders().add(name, value);
|
||||
}
|
||||
}
|
||||
this.headersWritten = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,28 +13,69 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.server.reactor;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.Publishers;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.io.net.http.HttpChannel;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.ReactiveServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
public class ReactorServerHttpRequest extends PublisherReactorServerHttpRequest {
|
||||
public class ReactorServerHttpRequest implements ReactiveServerHttpRequest {
|
||||
|
||||
private final HttpChannel<Buffer, ?> channel;
|
||||
|
||||
private HttpHeaders headers;
|
||||
|
||||
|
||||
public ReactorServerHttpRequest(HttpChannel<Buffer, ?> request) {
|
||||
super(request);
|
||||
Assert.notNull("'request', request must not be null.");
|
||||
this.channel = request;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
for (String name : this.channel.headers().names()) {
|
||||
for (String value : this.channel.headers().getAll(name)) {
|
||||
this.headers.add(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<ByteBuffer> getBody() {
|
||||
return Streams.wrap(super.getBody());
|
||||
public HttpMethod getMethod() {
|
||||
return HttpMethod.valueOf(this.channel.method().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
try {
|
||||
return new URI(this.channel.uri());
|
||||
} catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Could not get URI: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<ByteBuffer> getBody() {
|
||||
return Publishers.map(this.channel.input(), Buffer::byteBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,33 +13,73 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.server.reactor;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.Publishers;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.io.net.http.HttpChannel;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.Streams;
|
||||
import reactor.io.net.http.model.Status;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.ReactiveServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
*/
|
||||
public class ReactorServerHttpResponse extends PublisherReactorServerHttpResponse {
|
||||
public class ReactorServerHttpResponse implements ReactiveServerHttpResponse {
|
||||
|
||||
private final HttpChannel<?, Buffer> channel;
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
private boolean headersWritten = false;
|
||||
|
||||
|
||||
public ReactorServerHttpResponse(HttpChannel<?, Buffer> response) {
|
||||
super(response);
|
||||
Assert.notNull("'response', response must not be null.");
|
||||
this.channel = response;
|
||||
this.headers = new HttpHeaders();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setStatusCode(HttpStatus status) {
|
||||
this.channel.responseStatus(Status.valueOf(status.value()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<Void> writeHeaders() {
|
||||
return Streams.wrap(super.writeHeaders());
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<Void> setBody(Publisher<ByteBuffer> contentPublisher) {
|
||||
return Streams.wrap(super.setBody(contentPublisher));
|
||||
public Publisher<Void> writeHeaders() {
|
||||
if (this.headersWritten) {
|
||||
return Publishers.empty();
|
||||
}
|
||||
applyHeaders();
|
||||
return this.channel.writeHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> setBody(Publisher<ByteBuffer> contentPublisher) {
|
||||
applyHeaders();
|
||||
return this.channel.writeWith(Publishers.map(contentPublisher, Buffer::new));
|
||||
}
|
||||
|
||||
private void applyHeaders() {
|
||||
if (!this.headersWritten) {
|
||||
for (String name : this.headers.keySet()) {
|
||||
for (String value : this.headers.get(name)) {
|
||||
this.channel.responseHeaders().add(name, value);
|
||||
}
|
||||
}
|
||||
this.headersWritten = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,12 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RxNettyHttpHandlerAdapter implements RequestHandler<ByteBuf, ByteBuf> {
|
||||
public class HttpHandlerRequestHandler implements RequestHandler<ByteBuf, ByteBuf> {
|
||||
|
||||
private final ReactiveHttpHandler httpHandler;
|
||||
|
||||
|
||||
public RxNettyHttpHandlerAdapter(ReactiveHttpHandler httpHandler) {
|
||||
public HttpHandlerRequestHandler(ReactiveHttpHandler httpHandler) {
|
||||
Assert.notNull(httpHandler, "'httpHandler' is required.");
|
||||
this.httpHandler = httpHandler;
|
||||
}
|
||||
@@ -37,11 +37,11 @@ import org.springframework.http.server.ReactiveHttpHandler;
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
@WebServlet(asyncSupported = true)
|
||||
public class Servlet31HttpHandlerAdapter extends HttpServlet {
|
||||
public class HttpHandlerServlet extends HttpServlet {
|
||||
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
private static Log logger = LogFactory.getLog(Servlet31HttpHandlerAdapter.class);
|
||||
private static Log logger = LogFactory.getLog(HttpHandlerServlet.class);
|
||||
|
||||
|
||||
private ReactiveHttpHandler handler;
|
||||
@@ -24,7 +24,7 @@ import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.http.server.servlet31.Servlet31HttpHandlerAdapter;
|
||||
import org.springframework.http.server.servlet31.HttpHandlerServlet;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -51,7 +51,7 @@ public class JettyHttpServer extends HttpServerSupport implements InitializingBe
|
||||
this.jettyServer = new Server();
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
Servlet31HttpHandlerAdapter servlet = new Servlet31HttpHandlerAdapter();
|
||||
HttpHandlerServlet servlet = new HttpHandlerServlet();
|
||||
servlet.setHandler(getHttpHandler());
|
||||
ServletHolder servletHolder = new ServletHolder(servlet);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import reactor.io.net.ReactiveNet;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.http.server.reactor.ReactorHttpHandlerAdapter;
|
||||
import org.springframework.http.server.reactor.HttpHandlerChannelHandler;
|
||||
|
||||
/**
|
||||
* @author Stephane Maldini
|
||||
@@ -29,7 +29,7 @@ import org.springframework.http.server.reactor.ReactorHttpHandlerAdapter;
|
||||
public class ReactorHttpServer extends HttpServerSupport
|
||||
implements InitializingBean, HttpServer {
|
||||
|
||||
private ReactorHttpHandlerAdapter reactorHandler;
|
||||
private HttpHandlerChannelHandler reactorHandler;
|
||||
|
||||
private reactor.io.net.http.HttpServer<Buffer, Buffer> reactorServer;
|
||||
|
||||
@@ -44,7 +44,7 @@ public class ReactorHttpServer extends HttpServerSupport
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler());
|
||||
this.reactorHandler = new HttpHandlerChannelHandler(getHttpHandler());
|
||||
|
||||
this.reactorServer = (getPort() != -1 ? ReactiveNet.httpServer(getPort()) :
|
||||
ReactiveNet.httpServer());
|
||||
|
||||
@@ -20,7 +20,7 @@ import io.netty.buffer.ByteBuf;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.http.server.rxnetty.RxNettyHttpHandlerAdapter;
|
||||
import org.springframework.http.server.rxnetty.HttpHandlerRequestHandler;
|
||||
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ import org.springframework.http.server.rxnetty.RxNettyHttpHandlerAdapter;
|
||||
*/
|
||||
public class RxNettyHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
|
||||
|
||||
private RxNettyHttpHandlerAdapter rxNettyHandler;
|
||||
private HttpHandlerRequestHandler rxNettyHandler;
|
||||
|
||||
private io.reactivex.netty.protocol.http.server.HttpServer<ByteBuf, ByteBuf> rxNettyServer;
|
||||
|
||||
@@ -45,7 +45,7 @@ public class RxNettyHttpServer extends HttpServerSupport implements Initializing
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler());
|
||||
this.rxNettyHandler = new HttpHandlerRequestHandler(getHttpHandler());
|
||||
|
||||
this.rxNettyServer = (getPort() != -1 ?
|
||||
io.reactivex.netty.protocol.http.server.HttpServer.newServer(getPort()) :
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.http.server.servlet31.Servlet31HttpHandlerAdapter;
|
||||
import org.springframework.http.server.servlet31.HttpHandlerServlet;
|
||||
|
||||
|
||||
/**
|
||||
@@ -54,7 +54,7 @@ public class TomcatHttpServer extends HttpServerSupport implements InitializingB
|
||||
this.tomcatServer.setPort(getPort());
|
||||
|
||||
Assert.notNull(getHttpHandler());
|
||||
Servlet31HttpHandlerAdapter servlet = new Servlet31HttpHandlerAdapter();
|
||||
HttpHandlerServlet servlet = new HttpHandlerServlet();
|
||||
servlet.setHandler(getHttpHandler());
|
||||
|
||||
File base = new File(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.http.server.support;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.http.server.undertow.UndertowHttpHandlerAdapter;
|
||||
import org.springframework.http.server.undertow.HttpHandlerHttpHandler;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.server.HttpHandler;
|
||||
@@ -36,7 +36,7 @@ public class UndertowHttpServer extends HttpServerSupport implements Initializin
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(getHttpHandler());
|
||||
HttpHandler handler = new UndertowHttpHandlerAdapter(getHttpHandler());
|
||||
HttpHandler handler = new HttpHandlerHttpHandler(getHttpHandler());
|
||||
int port = (getPort() != -1 ? getPort() : 8080);
|
||||
this.server = Undertow.builder().addHttpListener(port, "localhost")
|
||||
.setHandler(handler).build();
|
||||
|
||||
@@ -32,15 +32,15 @@ import org.reactivestreams.Subscription;
|
||||
* @author Marek Hawrylczak
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandler {
|
||||
public class HttpHandlerHttpHandler implements io.undertow.server.HttpHandler {
|
||||
|
||||
private static Log logger = LogFactory.getLog(UndertowHttpHandlerAdapter.class);
|
||||
private static Log logger = LogFactory.getLog(HttpHandlerHttpHandler.class);
|
||||
|
||||
|
||||
private final ReactiveHttpHandler delegate;
|
||||
|
||||
|
||||
public UndertowHttpHandlerAdapter(ReactiveHttpHandler delegate) {
|
||||
public HttpHandlerHttpHandler(ReactiveHttpHandler delegate) {
|
||||
Assert.notNull(delegate, "'delegate' is required.");
|
||||
this.delegate = delegate;
|
||||
}
|
||||
Reference in New Issue
Block a user