Skip to content

Commit 4d4f314

Browse files
committed
Fix jakartaee#74 - provide more control over redirects
1 parent 13220f8 commit 4d4f314

File tree

4 files changed

+110
-11
lines changed

4 files changed

+110
-11
lines changed

api/src/main/java/jakarta/servlet/http/HttpServletResponse.java

+58-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022 Oracle and/or its affiliates and others.
2+
* Copyright (c) 1997, 2023 Oracle and/or its affiliates and others.
33
* All rights reserved.
44
* Copyright 2004 The Apache Software Foundation
55
*
@@ -145,9 +145,57 @@ public interface HttpServletResponse extends ServletResponse {
145145
public void sendError(int sc) throws IOException;
146146

147147
/**
148-
* Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.
149-
* The buffer will be replaced with the data set by this method. Calling this method sets the status code to
148+
* Sends a temporary redirect response to the client using the specified redirect location URL using the status code
149+
* {@link #SC_FOUND} 302 (Found) and clears the buffer. The buffer will be replaced with the data set by this method.
150+
*
151+
* @param location the redirect location URL
152+
*
153+
* @exception IOException If an input or output exception occurs
154+
* @exception IllegalStateException If the response was committed or if a partial URL is given and cannot be converted
155+
* into a valid URL
156+
*
157+
* @see #sendRedirect(String, int, boolean)
158+
*/
159+
public default void sendRedirect(String location) throws IOException {
160+
sendRedirect(location, SC_FOUND, true);
161+
}
162+
163+
/**
164+
* Sends a temporary redirect response to the client using the specified redirect location URL using the status code
150165
* {@link #SC_FOUND} 302 (Found).
166+
*
167+
* @param location the redirect location URL
168+
* @param clearBuffer if {@code true}, clear the buffer and replace it with the data set by this method otherwise retain
169+
* the existing buffer
170+
*
171+
* @exception IOException If an input or output exception occurs
172+
* @exception IllegalStateException If the response was committed or if a partial URL is given and cannot be converted
173+
* into a valid URL
174+
*
175+
* @see #sendRedirect(String, int, boolean)
176+
*/
177+
public default void sendRedirect(String location, boolean clearBuffer) throws IOException {
178+
sendRedirect(location, SC_FOUND, clearBuffer);
179+
}
180+
181+
/**
182+
* Sends a temporary redirect response to the client using the specified redirect location URL using the given status
183+
* code and clears the buffer. The buffer will be replaced with the data set by this method.
184+
*
185+
* @param location the redirect location URL
186+
* @param sc the status code to use for the redirect
187+
*
188+
* @exception IOException If an input or output exception occurs
189+
* @exception IllegalStateException If the response was committed or if a partial URL is given and cannot be converted
190+
* into a valid URL
191+
*
192+
* @see #sendRedirect(String, int, boolean)
193+
*/
194+
public default void sendRedirect(String location, int sc) throws IOException {
195+
sendRedirect(location, sc, true);
196+
}
197+
198+
/**
151199
* <p>
152200
* This method accepts both relative and absolute URLs. Absolute URLs passed to this method are used as provided as the
153201
* redirect location URL. Relative URLs are converted to absolute URLs unless a container specific feature/option is
@@ -162,17 +210,21 @@ public interface HttpServletResponse extends ServletResponse {
162210
* <a href="http://www.ietf.org/rfc/rfc3986.txt"> RFC 3986: Uniform Resource Identifier (URI): Generic Syntax</a>,
163211
* section 4.2 &quot;Relative Reference&quot;).</li>
164212
* </ul>
165-
*
213+
*
166214
* <p>
167215
* If the response has already been committed, this method throws an IllegalStateException. After using this method, the
168216
* response should be considered to be committed and should not be written to.
169-
*
217+
*
170218
* @param location the redirect location URL
219+
* @param sc the status code to use for the redirect
220+
* @param clearBuffer if {@code true}, clear the buffer and replace it with the data set by this method otherwise retain
221+
* the existing buffer
222+
*
171223
* @exception IOException If an input or output exception occurs
172224
* @exception IllegalStateException If the response was committed or if a partial URL is given and cannot be converted
173225
* into a valid URL
174226
*/
175-
public void sendRedirect(String location) throws IOException;
227+
public void sendRedirect(String location, int sc, boolean clearBuffer) throws IOException;
176228

177229
/**
178230
* Sets a response header with the given name and date-value. The date is specified in terms of milliseconds since the

api/src/main/java/jakarta/servlet/http/HttpServletResponseWrapper.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
2+
* Copyright (c) 1997, 2023 Oracle and/or its affiliates and others.
33
* All rights reserved.
44
* Copyright 2004 The Apache Software Foundation
55
*
@@ -108,6 +108,33 @@ public void sendRedirect(String location) throws IOException {
108108
this._getHttpServletResponse().sendRedirect(location);
109109
}
110110

111+
/**
112+
* The default behavior of this method is to return sendRedirect(String location, int sc) on the wrapped response
113+
* object.
114+
*/
115+
@Override
116+
public void sendRedirect(String location, int sc) throws IOException {
117+
this._getHttpServletResponse().sendRedirect(location, sc);
118+
}
119+
120+
/**
121+
* The default behavior of this method is to return sendRedirect(String location, boolean clearBuffer) on the wrapped
122+
* response object.
123+
*/
124+
@Override
125+
public void sendRedirect(String location, boolean clearBuffer) throws IOException {
126+
this._getHttpServletResponse().sendRedirect(location, clearBuffer);
127+
}
128+
129+
/**
130+
* The default behavior of this method is to return sendRedirect(String location, int sc, boolean clearBuffer) on the
131+
* wrapped response object.
132+
*/
133+
@Override
134+
public void sendRedirect(String location, int sc, boolean clearBuffer) throws IOException {
135+
this._getHttpServletResponse().sendRedirect(location, sc, clearBuffer);
136+
}
137+
111138
/**
112139
* The default behavior of this method is to call setDateHeader(String name, long date) on the wrapped response object.
113140
*/

api/src/test/java/jakarta/servlet/http/MockHttpServletResponse.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021 Contributors to the Eclipse Foundation.
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -56,6 +56,21 @@ public void sendRedirect(String location) throws IOException {
5656

5757
}
5858

59+
@Override
60+
public void sendRedirect(String location, boolean clearBuffer) throws IOException {
61+
62+
}
63+
64+
@Override
65+
public void sendRedirect(String location, int sc) throws IOException {
66+
67+
}
68+
69+
@Override
70+
public void sendRedirect(String location, int sc, boolean clearBuffer) throws IOException {
71+
72+
}
73+
5974
@Override
6075
public void setDateHeader(String name, long date) {
6176

spec/src/main/asciidoc/servlet-spec-body.adoc

+8-3
Original file line numberDiff line numberDiff line change
@@ -2499,14 +2499,14 @@ The following convenience methods exist in the
24992499

25002500
* `sendError`
25012501

2502-
The `sendRedirect` method will set the appropriate headers and content body to
2502+
The `sendRedirect` methods will set the appropriate headers and content body to
25032503
redirect the client to a different URL.
2504-
It is legal to call this method with a relative URL path.
2504+
It is legal to call these methods with a relative URL path.
25052505
The underlying container may provide an option to use the relative URL path as
25062506
provided but if no such option is provided it must translate the relative path
25072507
to a fully qualified URL for transmission back to the client.
25082508
If a partial URL is given and, for whatever reason, cannot be converted into a
2509-
valid URL, then this method must throw an `IllegalArgumentException`.
2509+
valid URL, then these methods must throw an `IllegalArgumentException`.
25102510

25112511
The `sendError` method will set the
25122512
appropriate headers and content body for an error message to return to
@@ -8541,6 +8541,11 @@ link:https://github.com/eclipse-ee4j/servlet-api/issues/59[Issue 59]::
85418541
A new attribute, `jakarta.servlet.error.query_string`, has been added to the
85428542
attributes that must be set on the request as part of an error dispatch.
85438543

8544+
link:https://github.com/eclipse-ee4j/servlet-api/issues/74[Issue 74]::
8545+
Add additional methods to `HttpServeltResponse` to allow the status code used
8546+
with the redirect to be chosen and/or the response body used for the redirect to
8547+
be controlled.
8548+
85448549
link:https://github.com/eclipse-ee4j/servlet-api/issues/95[Issue 95]::
85458550
Clarify that Servlet containers are required to support HTTPS.
85468551

0 commit comments

Comments
 (0)