Skip to content

Commit

Permalink
Merge pull request #1034 from apache/feature/WW-5450-use-contstants
Browse files Browse the repository at this point in the history
WW-5450 Uses existing Jakarta constants instead of free hand strings
  • Loading branch information
lukaszlenart committed Aug 31, 2024
2 parents 4679792 + e743dde commit e62ee8f
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/org/apache/struts2/RequestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.struts2;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static String getServletPath(HttpServletRequest request) {
*/
public static String getUri(HttpServletRequest request) {
// handle http dispatcher includes.
String uri = (String) request.getAttribute("jakarta.servlet.include.servlet_path");
String uri = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
if (uri != null) {
return uri;
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/apache/struts2/components/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -100,7 +101,7 @@ public class Include extends Component {

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

private static final String systemEncoding = System.getProperty("file.encoding");
private static final String SYSTEM_ENCODING = Charset.defaultCharset().displayName();

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

// Set parameters
for (Object next : parameters.entrySet()) {
Map.Entry entry = (Map.Entry) next;
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
Object name = entry.getKey();
List values = (List) entry.getValue();

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

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

while (pathParts.hasMoreTokens()) {
Expand Down Expand Up @@ -279,7 +279,7 @@ public static void include( String relativePath, Writer writer, ServletRequest r
pageResponse.getContent().writeTo(writer, encoding);
} else {
// Use the platform specific encoding
pageResponse.getContent().writeTo(writer, systemEncoding);
pageResponse.getContent().writeTo(writer, SYSTEM_ENCODING);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import jakarta.servlet.RequestDispatcher;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -302,7 +303,7 @@ private String extractQueryString(UrlProvider urlComponent) {
// Parse the query string to make sure that the parameters come from the query, and not some posted data
String query = urlComponent.getHttpServletRequest().getQueryString();
if (query == null) {
query = (String) urlComponent.getHttpServletRequest().getAttribute("jakarta.servlet.forward.query_string");
query = (String) urlComponent.getHttpServletRequest().getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
}

if (query != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.opensymphony.xwork2.util.location.Location;
import com.opensymphony.xwork2.util.location.LocationUtils;
import freemarker.template.Template;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -87,7 +88,7 @@ protected void sendErrorResponse(HttpServletRequest request, HttpServletResponse
LOG.error("Exception occurred during processing request: {}", e.getMessage(), e);
// send a http error response to use the servlet defined error handler
// make the exception available to the web.xml defined error page
request.setAttribute("jakarta.servlet.error.exception", e);
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);

// for compatibility
request.setAttribute("jakarta.servlet.jsp.jspException", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* {@link PageContext#include(String) include} method is called.</li>
*
* <li>If there is no PageContext and we're not in any sort of include (there is no
* "jakarta.servlet.include.servlet_path" in the request attributes), then a call to
* {@link RequestDispatcher#INCLUDE_SERVLET_PATH} in the request attributes), then a call to
* {@link RequestDispatcher#forward(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse) forward}
* is made.</li>
*
Expand Down Expand Up @@ -125,7 +125,7 @@ public void setQueryStringParser(QueryStringParser queryStringParser) {
* HTTP request.
*/
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
LOG.debug("Forwarding to location: {}", finalLocation);
LOG.debug("Processing location: {}", finalLocation);

PageContext pageContext = ServletActionContext.getPageContext();

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

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

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

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

dispatcher.forward(request, response);
} else {
LOG.debug("Including location: {}", finalLocation);
dispatcher.include(request, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import freemarker.template.Version;
import freemarker.template.utility.StringUtil;
import jakarta.servlet.GenericServlet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -550,7 +551,7 @@ protected void populateContext(ScopesHashModel model, ValueStack stack, Object a
model.putAll(standard);

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

if (exception == null) {
exception = (Throwable) request.getAttribute("jakarta.servlet.error.JspException");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.struts2.views.util;

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

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

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

// If neither request attributes were found, default to the value in the request object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.HttpParameters;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -130,7 +129,6 @@ public void testWithParameter() {

assertTrue(mockActionInvocation.getInvocationContext().getParameters().contains("bar"));
assertEquals("1", mockActionInvocation.getInvocationContext().getParameters().get("bar").getValue());
assertEquals("1", ((HttpParameters) mockActionInvocation.getInvocationContext().getContextMap().get("parameters")).get("bar").getValue());
dispatcherMock.verify();
requestMock.verify();
dispatcherMock.verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.tiles.web.util;

import jakarta.servlet.RequestDispatcher;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tiles.api.AttributeContext;
Expand Down Expand Up @@ -113,7 +114,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) {
* @return The definition name to render.
*/
protected String getDefinitionName(HttpServletRequest request) {
String path = (String) request.getAttribute("jakarta.servlet.include.servlet_path");
String path = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
if (path == null) {
path = request.getServletPath();
}
Expand Down

0 comments on commit e62ee8f

Please sign in to comment.