diff --git a/http-poja-apache/src/main/java/io/micronaut/http/poja/apache/ApacheServletHttpRequest.java b/http-poja-apache/src/main/java/io/micronaut/http/poja/apache/ApacheServletHttpRequest.java index bc6091da6..e82bfb1cd 100644 --- a/http-poja-apache/src/main/java/io/micronaut/http/poja/apache/ApacheServletHttpRequest.java +++ b/http-poja-apache/src/main/java/io/micronaut/http/poja/apache/ApacheServletHttpRequest.java @@ -31,6 +31,7 @@ import io.micronaut.http.cookie.Cookies; import io.micronaut.http.poja.PojaHttpRequest; import io.micronaut.http.poja.apache.exception.ApacheServletBadRequestException; +import io.micronaut.http.poja.exception.NoPojaRequestException; import io.micronaut.http.poja.util.MultiValueHeaders; import io.micronaut.http.poja.util.MultiValuesQueryParameters; import io.micronaut.http.simple.cookies.SimpleCookies; @@ -110,6 +111,9 @@ public ApacheServletHttpRequest( } catch (HttpException | IOException e) { throw new ApacheServletBadRequestException("HTTP request could not be parsed", e); } + if (request == null) { + throw new NoPojaRequestException(); + } method = HttpMethod.parse(request.getMethod()); try { diff --git a/http-poja-common/src/main/java/io/micronaut/http/poja/PojaHttpServerlessApplication.java b/http-poja-common/src/main/java/io/micronaut/http/poja/PojaHttpServerlessApplication.java index e7461253e..7988a2e0a 100644 --- a/http-poja-common/src/main/java/io/micronaut/http/poja/PojaHttpServerlessApplication.java +++ b/http-poja-common/src/main/java/io/micronaut/http/poja/PojaHttpServerlessApplication.java @@ -17,6 +17,7 @@ import io.micronaut.context.ApplicationContext; import io.micronaut.core.annotation.NonNull; +import io.micronaut.http.poja.exception.NoPojaRequestException; import io.micronaut.runtime.ApplicationConfiguration; import io.micronaut.runtime.EmbeddedApplication; import io.micronaut.servlet.http.ServletExchange; @@ -90,6 +91,9 @@ protected ServletExchange createExchange(Object request, Object respon runIndefinitely(servletHttpHandler, input, output); } catch (IOException e) { throw new RuntimeException(e); + } catch (NoPojaRequestException e) { + this.stop(); + Thread.currentThread().interrupt(); } return this; } diff --git a/http-poja-common/src/main/java/io/micronaut/http/poja/exception/NoPojaRequestException.java b/http-poja-common/src/main/java/io/micronaut/http/poja/exception/NoPojaRequestException.java new file mode 100644 index 000000000..b4b3f5775 --- /dev/null +++ b/http-poja-common/src/main/java/io/micronaut/http/poja/exception/NoPojaRequestException.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017-2024 original 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 + * + * https://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 io.micronaut.http.poja.exception; + +/** + * An exception to be thrown when no additional requests can be parsed. + * This can happen if the input stream is closed when waiting for a new request. + * This is not an error condition, therefore application can simply stop. + */ +public class NoPojaRequestException extends RuntimeException { + + /** + * Constructor for the exception. + */ + public NoPojaRequestException() { + super("No new request was found in the input stream"); + } + +} diff --git a/test-sample-poja/README.md b/test-sample-poja/README.md index 9d57e1c32..7d08ba1bb 100644 --- a/test-sample-poja/README.md +++ b/test-sample-poja/README.md @@ -37,3 +37,8 @@ Hello, Micronaut Without Netty! ``` +Alternatively redirect the input from a file: +```shell +gradle :micronaut-test-sample-poja:run --console=plain