Skip to content

Commit

Permalink
Use stream or writer for top level exception to response (#3970)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjantijms authored Sep 16, 2024
1 parent 7b2d3a6 commit be5a201
Showing 1 changed file with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@
*/
package cloud.piranha.core.impl;

import cloud.piranha.core.api.CurrentRequestHolder;
import static cloud.piranha.core.api.CurrentRequestHolder.CURRENT_REQUEST_ATTRIBUTE;
import cloud.piranha.core.api.FilterEnvironment;
import cloud.piranha.core.api.ServletEnvironment;
import cloud.piranha.core.api.WebApplicationRequest;
import static cloud.piranha.core.impl.DefaultWebApplicationRequest.unwrap;
import static jakarta.servlet.AsyncContext.ASYNC_CONTEXT_PATH;
import static jakarta.servlet.AsyncContext.ASYNC_PATH_INFO;
import static jakarta.servlet.AsyncContext.ASYNC_QUERY_STRING;
import static jakarta.servlet.AsyncContext.ASYNC_REQUEST_URI;
import static jakarta.servlet.AsyncContext.ASYNC_SERVLET_PATH;
import static jakarta.servlet.DispatcherType.ASYNC;
import static jakarta.servlet.DispatcherType.ERROR;
import static jakarta.servlet.DispatcherType.FORWARD;
import static jakarta.servlet.DispatcherType.INCLUDE;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
Expand All @@ -51,10 +36,10 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -63,6 +48,25 @@
import java.util.Map;
import java.util.function.Predicate;

import cloud.piranha.core.api.CurrentRequestHolder;
import cloud.piranha.core.api.FilterEnvironment;
import cloud.piranha.core.api.ServletEnvironment;
import cloud.piranha.core.api.WebApplicationRequest;

import static cloud.piranha.core.api.CurrentRequestHolder.CURRENT_REQUEST_ATTRIBUTE;
import static cloud.piranha.core.impl.DefaultWebApplicationRequest.unwrap;
import static jakarta.servlet.AsyncContext.ASYNC_CONTEXT_PATH;
import static jakarta.servlet.AsyncContext.ASYNC_PATH_INFO;
import static jakarta.servlet.AsyncContext.ASYNC_QUERY_STRING;
import static jakarta.servlet.AsyncContext.ASYNC_REQUEST_URI;
import static jakarta.servlet.AsyncContext.ASYNC_SERVLET_PATH;
import static jakarta.servlet.DispatcherType.ASYNC;
import static jakarta.servlet.DispatcherType.ERROR;
import static jakarta.servlet.DispatcherType.FORWARD;
import static jakarta.servlet.DispatcherType.INCLUDE;
import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;

/**
* The default ServletRequestDispatcher.
*
Expand Down Expand Up @@ -170,10 +174,10 @@ public void request(DefaultWebApplicationRequest webappRequest, DefaultWebApplic
}

/*
* REFACTOR - We used a response header to signal that we are not
* REFACTOR - We used a response header to signal that we are not
* listening to add/setHeader. In the block below we remove the header
* and reset the buffer as we need the code below do its work. However
* we really need to refactor this and move the code below to the
* we really need to refactor this and move the code below to the
* DefaultWebApplicationResponse, because it should be handled in the
* sendError call.
*/
Expand All @@ -199,7 +203,11 @@ public void request(DefaultWebApplicationRequest webappRequest, DefaultWebApplic
rethrow(e);
}
} else if (exception != null) {
exception.printStackTrace(webappResponse.getWriter());
if (webappResponse.gotWriter) {
exception.printStackTrace(webappResponse.getWriter());
} else {
exception.printStackTrace(new PrintStream(webappResponse.getOutputStream()));
}
webappResponse.flushBuffer();
rethrow(exception);
} else if (webappRequest.getAttribute(ERROR_MESSAGE) != null) {
Expand Down Expand Up @@ -279,7 +287,7 @@ public void include(ServletRequest servletRequest, ServletResponse servletRespon
request.setDispatcherType(INCLUDE);
request.setServletPath(path == null ? "/" + servletEnvironment.getServletName() : getServletPath(path));
request.setPathInfo(null);

if (path != null) {
request.setQueryString(getQueryString(path));
}
Expand Down

0 comments on commit be5a201

Please sign in to comment.