Skip to content

Commit

Permalink
Wrap call of deprecated HttpServletResponse#setStatus into try catch
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored and senivam committed Jul 25, 2024
1 parent 73a5fe7 commit a4f455e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,33 @@ private void setResponseForInvalidUri(final HttpServletResponse response, final
final Response.Status badRequest = Response.Status.BAD_REQUEST;
if (webComponent.configSetStatusOverSendError) {
response.reset();
//noinspection deprecation
response.setStatus(badRequest.getStatusCode(), badRequest.getReasonPhrase());
setStatus(response, badRequest.getStatusCode(), badRequest.getReasonPhrase());
} else {
response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase());
}
}

/**
* <p>
* Set status and reason-phrase if the API still contains the method. Otherwise, only a status is sent.
* </p>
* <p>
* It can happen the Servlet 6 API is used and the method is not there any longer. A proprietary API can be used,
* or the class is transformed to Jakarta using some transformer means.
* </p>
* @param response the servlet {@link HttpServletResponse}
* @param statusCode the status code
* @param reasonPhrase the reason phrase
*/
public static void setStatus(HttpServletResponse response, int statusCode, String reasonPhrase) {
try {
// noinspection deprecation
response.setStatus(statusCode, reasonPhrase);
} catch (NoSuchMethodError noSuchMethodError) {
response.setStatus(statusCode);
}
}

@Override
public void destroy() {
super.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ public Integer get() {

if (configSetStatusOverSendError) {
servletResponse.reset();
//noinspection deprecation
servletResponse.setStatus(status.getStatusCode(), status.getReasonPhrase());
ServletContainer.setStatus(servletResponse, status.getStatusCode(), status.getReasonPhrase());
} else {
servletResponse.sendError(status.getStatusCode(), status.getReasonPhrase());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -37,6 +37,7 @@
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.internal.JerseyRequestTimeoutHandler;
import org.glassfish.jersey.server.spi.ContainerResponseWriter;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.spi.AsyncContextDelegate;

/**
Expand Down Expand Up @@ -144,7 +145,7 @@ public OutputStream writeResponseStatusAndHeaders(final long contentLength, fina

final String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
if (reasonPhrase != null) {
response.setStatus(responseContext.getStatus(), reasonPhrase);
ServletContainer.setStatus(response, responseContext.getStatus(), reasonPhrase);
} else {
response.setStatus(responseContext.getStatus());
}
Expand Down Expand Up @@ -214,12 +215,12 @@ public void failure(final Throwable error) {
try {
if (!response.isCommitted()) {
try {
final int statusCode = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
if (configSetStatusOverSendError) {
response.reset();
//noinspection deprecation
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Request failed.");
ServletContainer.setStatus(response, statusCode, "Request failed.");
} else {
response.sendError(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Request failed.");
response.sendError(statusCode, "Request failed.");
}
} catch (final IllegalStateException ex) {
// a race condition externally committing the response can still occur...
Expand Down

0 comments on commit a4f455e

Please sign in to comment.