Refactor package structure for web

The web related code is now under org.springframework.web.reactive.
This is parallel to org.springframework.web (the top-level package of
spring-webmvc).
This commit is contained in:
Rossen Stoyanchev
2015-11-13 17:49:31 -05:00
parent 54ce20a5e0
commit 81867fa423
55 changed files with 113 additions and 100 deletions

View File

@@ -14,19 +14,21 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.reactive.web.http.reactor.ReactorHttpServer;
import org.springframework.reactive.web.http.rxnetty.RxNettyHttpServer;
import org.springframework.reactive.web.http.servlet.JettyHttpServer;
import org.springframework.reactive.web.http.servlet.TomcatHttpServer;
import org.springframework.reactive.web.http.undertow.UndertowHttpServer;
import org.springframework.web.reactive.HttpServer;
import org.springframework.web.reactive.ReactorHttpServer;
import org.springframework.web.reactive.RxNettyHttpServer;
import org.springframework.web.reactive.JettyHttpServer;
import org.springframework.web.reactive.TomcatHttpServer;
import org.springframework.web.reactive.UndertowHttpServer;
import org.springframework.util.SocketUtils;
import org.springframework.web.reactive.HttpHandler;
@RunWith(Parameterized.class)

View File

@@ -0,0 +1,33 @@
/*
* 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.web.reactive;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.Lifecycle;
import org.springframework.web.reactive.HttpHandler;
/**
* @author Rossen Stoyanchev
*/
public interface HttpServer extends InitializingBean, Lifecycle {
void setPort(int port);
void setHandler(HttpHandler handler);
}

View File

@@ -0,0 +1,48 @@
/*
* 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.web.reactive;
import org.springframework.web.reactive.HttpHandler;
/**
* @author Rossen Stoyanchev
*/
public class HttpServerSupport {
private int port = -1;
private HttpHandler httpHandler;
public void setPort(int port) {
this.port = port;
}
public int getPort() {
return this.port;
}
public void setHandler(HttpHandler handler) {
this.httpHandler = handler;
}
public HttpHandler getHttpHandler() {
return this.httpHandler;
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.web.reactive;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
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.web.reactive.server.servlet.HttpHandlerServlet;
/**
* @author Rossen Stoyanchev
*/
public class JettyHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private Server jettyServer;
private boolean running;
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
if (getPort() == -1) {
setPort(SocketUtils.findAvailableTcpPort(8080));
}
this.jettyServer = new Server();
Assert.notNull(getHttpHandler());
HttpHandlerServlet servlet = new HttpHandlerServlet();
servlet.setHandler(getHttpHandler());
ServletHolder servletHolder = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(this.jettyServer, "", false, false);
contextHandler.addServlet(servletHolder, "/");
ServerConnector connector = new ServerConnector(this.jettyServer);
connector.setPort(getPort());
this.jettyServer.addConnector(connector);
}
@Override
public void start() {
if (!this.running) {
try {
this.running = true;
this.jettyServer.start();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
try {
this.running = false;
jettyServer.stop();
jettyServer.destroy();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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.web.reactive;
import reactor.io.buffer.Buffer;
import reactor.io.net.ReactiveNet;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.reactive.server.reactor.RequestHandlerAdapter;
/**
* @author Stephane Maldini
*/
public class ReactorHttpServer extends HttpServerSupport
implements InitializingBean, HttpServer {
private RequestHandlerAdapter reactorHandler;
private reactor.io.net.http.HttpServer<Buffer, Buffer> reactorServer;
private boolean running;
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
this.reactorHandler = new RequestHandlerAdapter(getHttpHandler());
this.reactorServer = (getPort() != -1 ? ReactiveNet.httpServer(getPort()) :
ReactiveNet.httpServer());
}
@Override
public void start() {
if (!this.running) {
try {
this.reactorServer.startAndAwait(reactorHandler);
this.running = true;
}
catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
this.reactorServer.shutdown();
this.running = false;
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.web.reactive;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.reactive.server.rxnetty.RequestHandlerAdapter;
/**
* @author Rossen Stoyanchev
*/
public class RxNettyHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private RequestHandlerAdapter rxNettyHandler;
private io.reactivex.netty.protocol.http.server.HttpServer<ByteBuf, ByteBuf> rxNettyServer;
private boolean running;
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
this.rxNettyHandler = new RequestHandlerAdapter(getHttpHandler());
this.rxNettyServer = (getPort() != -1 ?
io.reactivex.netty.protocol.http.server.HttpServer.newServer(getPort()) :
io.reactivex.netty.protocol.http.server.HttpServer.newServer());
}
@Override
public void start() {
if (!this.running) {
this.running = true;
this.rxNettyServer.start(this.rxNettyHandler);
}
}
@Override
public void stop() {
if (this.running) {
this.running = false;
this.rxNettyServer.shutdown();
}
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.web.reactive;
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
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.web.reactive.server.servlet.HttpHandlerServlet;
/**
* @author Rossen Stoyanchev
*/
public class TomcatHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private Tomcat tomcatServer;
private boolean running;
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
if (getPort() == -1) {
setPort(SocketUtils.findAvailableTcpPort(8080));
}
this.tomcatServer = new Tomcat();
this.tomcatServer.setPort(getPort());
Assert.notNull(getHttpHandler());
HttpHandlerServlet servlet = new HttpHandlerServlet();
servlet.setHandler(getHttpHandler());
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootContext = tomcatServer.addContext("", base.getAbsolutePath());
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
rootContext.addServletMapping("/", "httpHandlerServlet");
}
@Override
public void start() {
if (!this.running) {
try {
this.running = true;
this.tomcatServer.start();
}
catch (LifecycleException ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
try {
this.running = false;
this.tomcatServer.stop();
this.tomcatServer.destroy();
}
catch (LifecycleException ex) {
throw new IllegalStateException(ex);
}
}
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.web.reactive;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.reactive.server.undertow.UndertowHttpHandlerAdapter;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
/**
* @author Marek Hawrylczak
*/
public class UndertowHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private Undertow server;
private boolean running;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
HttpHandler handler = new UndertowHttpHandlerAdapter(getHttpHandler());
int port = (getPort() != -1 ? getPort() : 8080);
this.server = Undertow.builder().addHttpListener(port, "localhost")
.setHandler(handler).build();
}
@Override
public void start() {
if (!this.running) {
this.server.start();
this.running = true;
}
}
@Override
public void stop() {
if (this.running) {
this.server.stop();
this.running = false;
}
}
@Override
public boolean isRunning() {
return this.running;
}
}

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import org.reactivestreams.Publisher;
import org.springframework.http.server.ReactiveServerHttpRequest;
import org.springframework.http.server.ReactiveServerHttpResponse;
import org.springframework.web.reactive.HttpHandler;
/**
* @author Arjen Poutsma

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import java.net.URI;
import java.util.Random;
@@ -24,6 +24,7 @@ import org.junit.Test;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.AbstractHttpHandlerIntegrationTests;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import java.nio.ByteBuffer;
import java.util.Random;
@@ -29,6 +29,7 @@ import reactor.rx.Streams;
import org.springframework.http.server.ReactiveServerHttpRequest;
import org.springframework.http.server.ReactiveServerHttpResponse;
import org.springframework.web.reactive.HttpHandler;
import static org.junit.Assert.assertEquals;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import java.net.URI;
import java.util.Random;
@@ -24,6 +24,7 @@ import org.junit.Test;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.AbstractHttpHandlerIntegrationTests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch;
package org.springframework.web.reactive.handler;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -23,6 +23,8 @@ import org.reactivestreams.Publisher;
import org.springframework.core.ResolvableType;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.handler.SimpleHandlerResultHandler;
/**
* @author Sebastien Deleuze

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch.handler;
package org.springframework.web.reactive.handler;
import java.net.URI;
import java.nio.charset.Charset;
@@ -31,10 +31,9 @@ import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ReactiveServerHttpRequest;
import org.springframework.http.server.ReactiveServerHttpResponse;
import org.springframework.reactive.web.dispatch.DispatcherHandler;
import org.springframework.reactive.web.dispatch.SimpleHandlerResultHandler;
import org.springframework.reactive.web.http.AbstractHttpHandlerIntegrationTests;
import org.springframework.reactive.web.http.HttpHandler;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.AbstractHttpHandlerIntegrationTests;
import org.springframework.web.reactive.HttpHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.support.StaticWebApplicationContext;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
@@ -31,6 +31,7 @@ import org.springframework.http.server.ReactiveServerHttpRequest;
import org.springframework.http.server.ReactiveServerHttpResponse;
import org.springframework.reactive.io.BufferOutputStream;
import org.springframework.reactive.io.ByteBufferPublisherInputStream;
import org.springframework.web.reactive.HttpHandler;
import static org.junit.Assert.fail;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http;
package org.springframework.web.reactive.handler;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
@@ -24,6 +24,8 @@ import org.junit.Test;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.AbstractHttpHandlerIntegrationTests;
import org.springframework.web.reactive.HttpHandler;
/**
* @author Arjen Poutsma

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch.method.annotation;
package org.springframework.web.reactive.method.annotation;
import java.net.URI;
import java.net.URISyntaxException;
@@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.method.annotation.RequestMappingHandlerMapping;
import static org.junit.Assert.assertEquals;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch.method.annotation;
package org.springframework.web.reactive.method.annotation;
import java.net.URI;
@@ -51,10 +51,10 @@ import org.springframework.reactive.codec.encoder.ByteBufferEncoder;
import org.springframework.reactive.codec.encoder.JacksonJsonEncoder;
import org.springframework.reactive.codec.encoder.JsonObjectEncoder;
import org.springframework.reactive.codec.encoder.StringEncoder;
import org.springframework.reactive.web.dispatch.DispatcherHandler;
import org.springframework.reactive.web.dispatch.SimpleHandlerResultHandler;
import org.springframework.reactive.web.http.AbstractHttpHandlerIntegrationTests;
import org.springframework.reactive.web.http.HttpHandler;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.handler.SimpleHandlerResultHandler;
import org.springframework.web.reactive.AbstractHttpHandlerIntegrationTests;
import org.springframework.web.reactive.HttpHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch.method.annotation;
package org.springframework.web.reactive.method.annotation;
import java.util.Collections;
@@ -24,9 +24,10 @@ import org.reactivestreams.Publisher;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.reactive.codec.encoder.StringEncoder;
import org.springframework.reactive.web.dispatch.HandlerResult;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.method.annotation.ResponseBodyResultHandler;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

View File

@@ -14,13 +14,15 @@
* limitations under the License.
*/
package org.springframework.reactive.web.http.servlet;
package org.springframework.web.reactive.server.servlet;
import javax.servlet.AsyncContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.reactive.server.servlet.AsyncContextSynchronizer;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;