Skip to content

Commit e62ee8f

Browse files
authored
Merge pull request #1034 from apache/feature/WW-5450-use-contstants
WW-5450 Uses existing Jakarta constants instead of free hand strings
2 parents 4679792 + e743dde commit e62ee8f

File tree

9 files changed

+25
-21
lines changed

9 files changed

+25
-21
lines changed

core/src/main/java/org/apache/struts2/RequestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.struts2;
2020

21+
import jakarta.servlet.RequestDispatcher;
2122
import jakarta.servlet.http.HttpServletRequest;
2223
import org.apache.commons.lang3.StringUtils;
2324
import org.apache.commons.lang3.time.FastDateFormat;
@@ -89,7 +90,7 @@ public static String getServletPath(HttpServletRequest request) {
8990
*/
9091
public static String getUri(HttpServletRequest request) {
9192
// handle http dispatcher includes.
92-
String uri = (String) request.getAttribute("jakarta.servlet.include.servlet_path");
93+
String uri = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
9394
if (uri != null) {
9495
return uri;
9596
}

core/src/main/java/org/apache/struts2/components/Include.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.PrintWriter;
4343
import java.io.Writer;
4444
import java.net.URLEncoder;
45+
import java.nio.charset.Charset;
4546
import java.nio.charset.StandardCharsets;
4647
import java.util.ArrayList;
4748
import java.util.List;
@@ -100,7 +101,7 @@ public class Include extends Component {
100101

101102
private static final Logger LOG = LogManager.getLogger(Include.class);
102103

103-
private static final String systemEncoding = System.getProperty("file.encoding");
104+
private static final String SYSTEM_ENCODING = Charset.defaultCharset().displayName();
104105

105106
protected String value;
106107
private final HttpServletRequest req;
@@ -149,8 +150,7 @@ public boolean end(Writer writer, String body) {
149150
String concat = "";
150151

151152
// Set parameters
152-
for (Object next : parameters.entrySet()) {
153-
Map.Entry entry = (Map.Entry) next;
153+
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
154154
Object name = entry.getKey();
155155
List values = (List) entry.getValue();
156156

@@ -191,7 +191,7 @@ public static String getContextRelativePath(ServletRequest request, String relat
191191
} else if (!(request instanceof HttpServletRequest hrequest)) {
192192
returnValue = relativePath;
193193
} else {
194-
String uri = (String) request.getAttribute("jakarta.servlet.include.servlet_path");
194+
String uri = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
195195

196196
if (uri == null) {
197197
uri = RequestUtils.getServletPath(hrequest);
@@ -203,7 +203,7 @@ public static String getContextRelativePath(ServletRequest request, String relat
203203
// .. is illegal in an absolute path according to the Servlet Spec and will cause
204204
// known problems on Orion application servers.
205205
if (returnValue.contains("..")) {
206-
Stack stack = new Stack();
206+
Stack<String> stack = new Stack<>();
207207
StringTokenizer pathParts = new StringTokenizer(returnValue.replace('\\', '/'), "/");
208208

209209
while (pathParts.hasMoreTokens()) {
@@ -279,7 +279,7 @@ public static void include( String relativePath, Writer writer, ServletRequest r
279279
pageResponse.getContent().writeTo(writer, encoding);
280280
} else {
281281
// Use the platform specific encoding
282-
pageResponse.getContent().writeTo(writer, systemEncoding);
282+
pageResponse.getContent().writeTo(writer, SYSTEM_ENCODING);
283283
}
284284
}
285285

core/src/main/java/org/apache/struts2/components/ServletUrlRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.opensymphony.xwork2.config.entities.ActionConfig;
2424
import com.opensymphony.xwork2.inject.Inject;
2525
import com.opensymphony.xwork2.util.ValueStack;
26+
import jakarta.servlet.RequestDispatcher;
2627
import org.apache.commons.lang3.StringUtils;
2728
import org.apache.logging.log4j.LogManager;
2829
import org.apache.logging.log4j.Logger;
@@ -302,7 +303,7 @@ private String extractQueryString(UrlProvider urlComponent) {
302303
// Parse the query string to make sure that the parameters come from the query, and not some posted data
303304
String query = urlComponent.getHttpServletRequest().getQueryString();
304305
if (query == null) {
305-
query = (String) urlComponent.getHttpServletRequest().getAttribute("jakarta.servlet.forward.query_string");
306+
query = (String) urlComponent.getHttpServletRequest().getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
306307
}
307308

308309
if (query != null) {

core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.opensymphony.xwork2.util.location.Location;
2323
import com.opensymphony.xwork2.util.location.LocationUtils;
2424
import freemarker.template.Template;
25+
import jakarta.servlet.RequestDispatcher;
2526
import jakarta.servlet.ServletContext;
2627
import jakarta.servlet.http.HttpServletRequest;
2728
import jakarta.servlet.http.HttpServletResponse;
@@ -87,7 +88,7 @@ protected void sendErrorResponse(HttpServletRequest request, HttpServletResponse
8788
LOG.error("Exception occurred during processing request: {}", e.getMessage(), e);
8889
// send a http error response to use the servlet defined error handler
8990
// make the exception available to the web.xml defined error page
90-
request.setAttribute("jakarta.servlet.error.exception", e);
91+
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);
9192

9293
// for compatibility
9394
request.setAttribute("jakarta.servlet.jsp.jspException", e);

core/src/main/java/org/apache/struts2/result/ServletDispatcherResult.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* {@link PageContext#include(String) include} method is called.</li>
5656
*
5757
* <li>If there is no PageContext and we're not in any sort of include (there is no
58-
* "jakarta.servlet.include.servlet_path" in the request attributes), then a call to
58+
* {@link RequestDispatcher#INCLUDE_SERVLET_PATH} in the request attributes), then a call to
5959
* {@link RequestDispatcher#forward(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse) forward}
6060
* is made.</li>
6161
*
@@ -125,7 +125,7 @@ public void setQueryStringParser(QueryStringParser queryStringParser) {
125125
* HTTP request.
126126
*/
127127
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
128-
LOG.debug("Forwarding to location: {}", finalLocation);
128+
LOG.debug("Processing location: {}", finalLocation);
129129

130130
PageContext pageContext = ServletActionContext.getPageContext();
131131

@@ -145,8 +145,6 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
145145
if (!queryParams.isEmpty()) {
146146
parameters = HttpParameters.create(queryParams.getQueryParams()).withParent(parameters).build();
147147
invocation.getInvocationContext().withParameters(parameters);
148-
// put to extraContext, see Dispatcher#createContextMap
149-
invocation.getInvocationContext().getContextMap().put("parameters", parameters);
150148
}
151149
}
152150

@@ -158,17 +156,19 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
158156
}
159157

160158
//if we are inside an action tag, we always need to do an include
161-
Boolean insideActionTag = (Boolean) ObjectUtils.defaultIfNull(request.getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION), Boolean.FALSE);
159+
boolean insideActionTag = (Boolean) ObjectUtils.defaultIfNull(request.getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION), Boolean.FALSE);
162160

163161
// If we're included, then include the view
164162
// Otherwise do forward
165163
// This allow the page to, for example, set content type
166-
if (!insideActionTag && !response.isCommitted() && (request.getAttribute("jakarta.servlet.include.servlet_path") == null)) {
164+
if (!insideActionTag && !response.isCommitted() && (request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) == null)) {
165+
LOG.debug("Forwarding to location: {}", finalLocation);
167166
request.setAttribute("struts.view_uri", finalLocation);
168167
request.setAttribute("struts.request_uri", request.getRequestURI());
169168

170169
dispatcher.forward(request, response);
171170
} else {
171+
LOG.debug("Including location: {}", finalLocation);
172172
dispatcher.include(request, response);
173173
}
174174
}

core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import freemarker.template.Version;
4444
import freemarker.template.utility.StringUtil;
4545
import jakarta.servlet.GenericServlet;
46+
import jakarta.servlet.RequestDispatcher;
4647
import jakarta.servlet.ServletContext;
4748
import jakarta.servlet.http.HttpServletRequest;
4849
import jakarta.servlet.http.HttpServletResponse;
@@ -550,7 +551,7 @@ protected void populateContext(ScopesHashModel model, ValueStack stack, Object a
550551
model.putAll(standard);
551552

552553
// support for JSP exception pages, exposing the servlet or JSP exception
553-
Throwable exception = (Throwable) request.getAttribute("jakarta.servlet.error.exception");
554+
Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
554555

555556
if (exception == null) {
556557
exception = (Throwable) request.getAttribute("jakarta.servlet.error.JspException");

core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.struts2.views.util;
2020

2121
import com.opensymphony.xwork2.inject.Inject;
22+
import jakarta.servlet.RequestDispatcher;
2223
import org.apache.commons.lang3.StringUtils;
2324
import org.apache.commons.text.StringEscapeUtils;
2425
import org.apache.logging.log4j.LogManager;
@@ -126,7 +127,7 @@ public String buildUrl(String action, HttpServletRequest request, HttpServletRes
126127

127128
// (Applicable to Servlet 2.4 containers)
128129
// If the request was forwarded, the attribute below will be set with the original URL
129-
String uri = (String) request.getAttribute("jakarta.servlet.forward.request_uri");
130+
String uri = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
130131

131132
// If the attribute wasn't found, default to the value in the request object
132133
if (uri == null) {
@@ -145,7 +146,7 @@ public String buildUrl(String action, HttpServletRequest request, HttpServletRes
145146
// (Applicable to Servlet 2.4 containers)
146147
// If the request was forwarded, the attribute below will be set with the original URL
147148
if (requestURI == null) {
148-
requestURI = (String) request.getAttribute("jakarta.servlet.forward.request_uri");
149+
requestURI = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
149150
}
150151

151152
// If neither request attributes were found, default to the value in the request object

core/src/test/java/org/apache/struts2/result/ServletDispatcherResultTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.struts2.ServletActionContext;
2727
import org.apache.struts2.StrutsInternalTestCase;
2828
import org.apache.struts2.StrutsStatics;
29-
import org.apache.struts2.dispatcher.HttpParameters;
3029

3130
import jakarta.servlet.RequestDispatcher;
3231
import jakarta.servlet.http.HttpServletRequest;
@@ -130,7 +129,6 @@ public void testWithParameter() {
130129

131130
assertTrue(mockActionInvocation.getInvocationContext().getParameters().contains("bar"));
132131
assertEquals("1", mockActionInvocation.getInvocationContext().getParameters().get("bar").getValue());
133-
assertEquals("1", ((HttpParameters) mockActionInvocation.getInvocationContext().getContextMap().get("parameters")).get("bar").getValue());
134132
dispatcherMock.verify();
135133
requestMock.verify();
136134
dispatcherMock.verify();

plugins/tiles/src/main/java/org/apache/tiles/web/util/TilesDispatchServlet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.tiles.web.util;
2020

21+
import jakarta.servlet.RequestDispatcher;
2122
import org.apache.logging.log4j.LogManager;
2223
import org.apache.logging.log4j.Logger;
2324
import org.apache.tiles.api.AttributeContext;
@@ -113,7 +114,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) {
113114
* @return The definition name to render.
114115
*/
115116
protected String getDefinitionName(HttpServletRequest request) {
116-
String path = (String) request.getAttribute("jakarta.servlet.include.servlet_path");
117+
String path = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
117118
if (path == null) {
118119
path = request.getServletPath();
119120
}

0 commit comments

Comments
 (0)