Skip to content

Commit

Permalink
Stop the POJA application if input stream is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
andriy-dmytruk committed Oct 17, 2024
1 parent 9318fae commit 5736bc2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,9 @@ protected ServletExchange<REQ, RES> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}
5 changes: 5 additions & 0 deletions test-sample-poja/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ Hello, Micronaut Without Netty!

```

Alternatively redirect the input from a file:
```shell
gradle :micronaut-test-sample-poja:run --console=plain <test-sample-poja/test-request.txt
```

1 change: 1 addition & 0 deletions test-sample-poja/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ graalvmNative {
binaries.all {
buildArgs.add("--gc=serial")
buildArgs.add("--install-exit-handlers")
buildArgs.add("-Ob")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.http.poja.sample;

import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.Micronaut;

/**
Expand All @@ -26,7 +27,8 @@
public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
ApplicationContext context = Micronaut.run(Application.class, args);
context.stop();
}

}
Expand Down
3 changes: 3 additions & 0 deletions test-sample-poja/test-request.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET / HTTP/1.1
Content-Length: 0

0 comments on commit 5736bc2

Please sign in to comment.