Add basic support for @RequestMapping

Just enough for a test with an @ResponseBody method that accepts an
@RequestParam String arg and returning Publisher<String> or String.

See RequestMappingIntegrationTests.
This commit is contained in:
Rossen Stoyanchev
2015-08-25 12:35:43 -04:00
parent 9df3dd4495
commit 202825554c
17 changed files with 703 additions and 150 deletions

View File

@@ -26,7 +26,10 @@ import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Arjen Poutsma

View File

@@ -19,7 +19,9 @@ package org.springframework.reactive.util;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Arjen Poutsma

View File

@@ -1,118 +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.reactive.web.dispatch;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.server.HttpServer;
import org.reactivestreams.Publisher;
import reactor.rx.Streams;
import org.springframework.http.MediaType;
import org.springframework.reactive.web.http.ServerHttpRequest;
import org.springframework.reactive.web.http.ServerHttpResponse;
import org.springframework.reactive.web.http.rxnetty.RequestHandlerAdapter;
import org.springframework.web.context.support.StaticWebApplicationContext;
/**
* @author Rossen Stoyanchev
*/
public class DispatcherApp {
public static void main(String[] args) {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class);
wac.registerSingleton("handlerAdapter", PlainTextHandlerAdapter.class);
wac.registerSingleton("resultHandler", PlainTextResultHandler.class);
wac.refresh();
SimpleUrlHandlerMapping handlerMapping = wac.getBean(SimpleUrlHandlerMapping.class);
handlerMapping.addHandler("/text", new HelloWorldTextHandler());
DispatcherHandler dispatcherHandler = new DispatcherHandler();
dispatcherHandler.initStrategies(wac);
RequestHandlerAdapter requestHandler = new RequestHandlerAdapter(dispatcherHandler);
HttpServer<ByteBuf, ByteBuf> server = HttpServer.newServer(8080);
server.start(requestHandler::handle);
server.awaitShutdown();
}
private static class SimpleUrlHandlerMapping implements HandlerMapping {
private final Map<String, Object> handlerMap = new HashMap<>();
public void addHandler(String path, Object handler) {
this.handlerMap.put(path, handler);
}
@Override
public Object getHandler(ServerHttpRequest request) {
return this.handlerMap.get(request.getURI().getPath());
}
}
private interface PlainTextHandler {
Publisher<String> handle(ServerHttpRequest request, ServerHttpResponse response);
}
private static class HelloWorldTextHandler implements PlainTextHandler {
@Override
public Publisher<String> handle(ServerHttpRequest request, ServerHttpResponse response) {
return Streams.just("Hello world.");
}
}
private static class PlainTextHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return PlainTextHandler.class.isAssignableFrom(handler.getClass());
}
@Override
public Publisher<HandlerResult> handle(ServerHttpRequest request, ServerHttpResponse response, Object handler) {
Publisher<String> publisher = ((PlainTextHandler) handler).handle(request, response);
return Streams.wrap(publisher).map(HandlerResult::new);
}
}
private static class PlainTextResultHandler implements HandlerResultHandler {
@Override
public boolean supports(HandlerResult result) {
Object value = result.getReturnValue();
return (value != null && String.class.equals(value.getClass()));
}
@Override
public Publisher<Void> handleResult(ServerHttpRequest request, ServerHttpResponse response, HandlerResult result) {
response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
byte[] bytes = ((String) result.getReturnValue()).getBytes(Charset.forName("UTF-8"));
return response.writeWith(Streams.just(bytes));
}
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch;
package org.springframework.reactive.web.dispatch.handler;
import java.net.URI;
import java.nio.charset.Charset;
@@ -26,8 +26,7 @@ import reactor.rx.Streams;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.reactive.web.dispatch.handler.HttpHandlerAdapter;
import org.springframework.reactive.web.dispatch.handler.SimpleUrlHandlerMapping;
import org.springframework.reactive.web.dispatch.DispatcherHandler;
import org.springframework.reactive.web.http.AbstractHttpHandlerIntegrationTests;
import org.springframework.reactive.web.http.HttpHandler;
import org.springframework.reactive.web.http.ServerHttpRequest;
@@ -43,7 +42,7 @@ import static org.junit.Assert.assertArrayEquals;
*/
public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
private static final Charset CHARSET = Charset.forName("UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
@Override
@@ -55,7 +54,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
wac.refresh();
DispatcherHandler dispatcherHandler = new DispatcherHandler();
dispatcherHandler.initStrategies(wac);
dispatcherHandler.setApplicationContext(wac);
return dispatcherHandler;
}
@@ -68,7 +67,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
RequestEntity<Void> request = RequestEntity.get(url).build();
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
assertArrayEquals("foo".getBytes(CHARSET), response.getBody());
assertArrayEquals("foo".getBytes(UTF_8), response.getBody());
}
@Test
@@ -80,7 +79,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
RequestEntity<Void> request = RequestEntity.get(url).build();
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
assertArrayEquals("bar".getBytes(CHARSET), response.getBody());
assertArrayEquals("bar".getBytes(UTF_8), response.getBody());
}
@@ -98,7 +97,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
@Override
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
return response.writeWith(Streams.just("foo".getBytes(CHARSET)));
return response.writeWith(Streams.just("foo".getBytes(UTF_8)));
}
}
@@ -106,7 +105,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
@Override
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
return response.writeWith(Streams.just("bar".getBytes(CHARSET)));
return response.writeWith(Streams.just("bar".getBytes(UTF_8)));
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.reactive.web.dispatch.method.annotation;
import java.net.URI;
import java.nio.charset.Charset;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.rx.Streams;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.reactive.web.dispatch.DispatcherHandler;
import org.springframework.reactive.web.http.AbstractHttpHandlerIntegrationTests;
import org.springframework.reactive.web.http.HttpHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.support.StaticWebApplicationContext;
import static org.junit.Assert.assertArrayEquals;
/**
* @author Rossen Stoyanchev
*/
public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
private static final Charset UTF_8 = Charset.forName("UTF-8");
@Override
protected HttpHandler createHttpHandler() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("handlerMapping", RequestMappingHandlerMapping.class);
wac.registerSingleton("handlerAdapter", RequestMappingHandlerAdapter.class);
wac.registerSingleton("responseBodyResultHandler", ResponseBodyResultHandler.class);
wac.registerSingleton("controller", TestController.class);
wac.refresh();
DispatcherHandler dispatcherHandler = new DispatcherHandler();
dispatcherHandler.setApplicationContext(wac);
return dispatcherHandler;
}
@Test
public void helloWithQueryParam() throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/param?name=George");
RequestEntity<Void> request = RequestEntity.get(url).build();
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
assertArrayEquals("Hello George!".getBytes(UTF_8), response.getBody());
}
@Controller
@SuppressWarnings("unused")
private static class TestController {
@RequestMapping("/param")
@ResponseBody
public Publisher<String> handleWithParam(@RequestParam String name) {
return Streams.just("Hello ", name, "!");
}
}
}