From 1f15b7e0741b7a57fb9bb02173abb4dc5ac0749a Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 15 Dec 2015 11:52:01 -0500 Subject: [PATCH] Add ResponseStatusException This change adds a ResponseStatusException to associate an exception with a status code at runtime. Along with that is an ResponseStatusExceptionHandler that handles ResponseStatusException by setting the response status. --- .../web/ResponseStatusException.java | 45 +++++++++++ .../ResponseStatusExceptionHandler.java | 43 ++++++++++ .../ResponseStatusExceptionHandlerTests.java | 78 +++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java create mode 100644 spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java create mode 100644 spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java diff --git a/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java b/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java new file mode 100644 index 00000000000..3aa9eac84f4 --- /dev/null +++ b/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java @@ -0,0 +1,45 @@ +/* + * 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; + +import org.springframework.core.NestedRuntimeException; +import org.springframework.http.HttpStatus; + +/** + * Exception wrapper to associate an exception with a status code at runtime. + * + * @author Rossen Stoyanchev + */ +public class ResponseStatusException extends NestedRuntimeException { + + private final HttpStatus httpStatus; + + + public ResponseStatusException(HttpStatus status) { + this(status, null); + } + + public ResponseStatusException(HttpStatus status, Throwable cause) { + super("Request processing failure with status code: " + status, cause); + this.httpStatus = status; + } + + + public HttpStatus getHttpStatus() { + return this.httpStatus; + } + +} diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java new file mode 100644 index 00000000000..828c9a52c52 --- /dev/null +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java @@ -0,0 +1,43 @@ +/* + * 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.reactivestreams.Publisher; +import reactor.Publishers; + +import org.springframework.http.server.reactive.HttpExceptionHandler; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.ResponseStatusException; + +/** + * Handle {@link ResponseStatusException} by setting the response status. + * + * @author Rossen Stoyanchev + */ +public class ResponseStatusExceptionHandler implements HttpExceptionHandler { + + + @Override + public Publisher handle(ServerHttpRequest request, ServerHttpResponse response, Throwable ex) { + if (ex instanceof ResponseStatusException) { + response.setStatusCode(((ResponseStatusException) ex).getHttpStatus()); + return response.writeHeaders(); + } + return Publishers.error(ex); + } + +} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java new file mode 100644 index 00000000000..f7162f5fa8c --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java @@ -0,0 +1,78 @@ +/* + * 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.net.URI; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.junit.Before; +import org.junit.Test; +import org.reactivestreams.Publisher; +import reactor.rx.Streams; +import reactor.rx.action.Signal; + +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.MockServerHttpRequest; +import org.springframework.http.server.reactive.MockServerHttpResponse; +import org.springframework.web.ResponseStatusException; + +import static junit.framework.TestCase.assertSame; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Rossen Stoyanchev + */ +public class ResponseStatusExceptionHandlerTests { + + private ResponseStatusExceptionHandler handler; + + private MockServerHttpRequest request; + + private MockServerHttpResponse response; + + + @Before + public void setUp() throws Exception { + this.handler = new ResponseStatusExceptionHandler(); + this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path")); + this.response = new MockServerHttpResponse(); + } + + + @Test + public void handleException() throws Exception { + Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST); + Publisher publisher = this.handler.handle(this.request, this.response, ex); + + Streams.wrap(publisher).toList().await(5, TimeUnit.SECONDS); + assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatus()); + } + + @Test + public void unresolvedException() throws Exception { + Throwable ex = new IllegalStateException(); + Publisher publisher = this.handler.handle(this.request, this.response, ex); + + List> signals = Streams.wrap(publisher).materialize().toList().await(5, TimeUnit.SECONDS); + assertEquals(1, signals.size()); + assertTrue(signals.get(0).hasError()); + assertSame(ex, signals.get(0).getThrowable()); + } + +}