Minor improvement proposed for ServletRedirectResult sendRedirect()

Supply log warning when an IOException or IllegalStateException occurs to
better allow developers to track the failed redirect location and status
details.  The exceptions are re-thrown to ensure existing flow-control
behaviour is preserved.

When getWriter() is called, utilize a finally block to ensure close is
called.
This commit is contained in:
JCgH4164838Gh792C124B5
2019-09-17 22:22:41 -04:00
parent 632f19eab3
commit 5b4b554d11
@@ -250,13 +250,24 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec
* @throws IOException in case of IO errors
*/
protected void sendRedirect(HttpServletResponse response, String finalLocation) throws IOException {
if (SC_FOUND == statusCode) {
response.sendRedirect(finalLocation);
} else {
response.setStatus(statusCode);
response.setHeader("Location", finalLocation);
response.getWriter().write(finalLocation);
response.getWriter().close();
try {
if (SC_FOUND == statusCode) {
response.sendRedirect(finalLocation);
} else {
response.setStatus(statusCode);
response.setHeader("Location", finalLocation);
try {
response.getWriter().write(finalLocation);
} finally {
response.getWriter().close();
}
}
} catch (IOException ioe) {
LOG.warn("Unable to redirect to: {}, code: {}! (IOException): {}", finalLocation, statusCode, ioe.toString());
throw ioe; // Re-throw required to preserve existing default behaviour
} catch (IllegalStateException ise) {
LOG.warn("Unable to redirect to: {}, code: {}! isCommited: {}. (IllegalStateException): {}", finalLocation, statusCode, response.isCommitted(), ise.toString());
throw ise; // Re-throw required to preserve existing default behaviour
}
}