Skip to content

Commit

Permalink
Fix path used by the health check valve
Browse files Browse the repository at this point in the history
When not associated with a Context, it should use the full URI.
  • Loading branch information
rmaucher committed Sep 1, 2020
1 parent 0f1e263 commit 9609da1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 21 additions & 2 deletions java/org/apache/catalina/valves/HealthCheckValve.java
Expand Up @@ -20,6 +20,8 @@

import jakarta.servlet.ServletException;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.tomcat.util.buf.MessageBytes;
Expand All @@ -35,8 +37,14 @@ public class HealthCheckValve extends ValveBase {
" \"status\": \"UP\",\n" +
" \"checks\": []\n" +
"}";

private String path = "/health";

/**
* Will be set to true if the valve is associated with a context.
*/
protected boolean context = false;

public HealthCheckValve() {
super(true);
}
Expand All @@ -49,11 +57,22 @@ public final void setPath(String path) {
this.path = path;
}

@Override
protected synchronized void startInternal() throws LifecycleException {
super.startInternal();
if (getContainer() instanceof Context) {
context = true;
} else {
context = false;
}
}

@Override
public void invoke(Request request, Response response)
throws IOException, ServletException {
MessageBytes requestPathMB = request.getRequestPathMB();
if (requestPathMB.equals(path)) {
MessageBytes urlMB =
context ? request.getRequestPathMB() : request.getDecodedRequestURIMB();
if (urlMB.equals(path)) {
response.setContentType("application/json");
response.getOutputStream().print(UP);
} else {
Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Expand Up @@ -101,6 +101,10 @@
Use the correct method to calculate session idle time in
<code>PersistentValve</code>. (kfujino)
</fix>
<fix>
Fix path used by the health check valve when it is not associated with
a <code>Context</code>. (remm)
</fix>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit 9609da1

Please sign in to comment.