diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java index 038217db70..d4d5fbc184 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java @@ -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()); } } + /** + *
+ * Set status and reason-phrase if the API still contains the method. Otherwise, only a status is sent. + *
+ *+ * 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. + *
+ * @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(); diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java index ab4e927649..5cd2f21db1 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java @@ -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()); } diff --git a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/internal/ResponseWriter.java b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/internal/ResponseWriter.java index 38301f898a..e166fe1fb2 100644 --- a/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/internal/ResponseWriter.java +++ b/containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/internal/ResponseWriter.java @@ -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 @@ -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; /** @@ -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()); } @@ -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...