Skip to content
Permalink
Browse files

Improved: Use ‘ServletRequest#getServletContext’ when possible

(OFBIZ-11129)

Avoid unecessary casts by using ‘ServletRequest#getServletContext’
instead of ‘ServletRequest#getAttribute’. This method is available
since Servlet specification 3.0.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1862294 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jun 28, 2019
1 parent faa035e commit 21135cb104df653690bcc45a38176fa26af81d0a
Showing with 35 additions and 52 deletions.
  1. +1 −2 applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
  2. +2 −2 applications/order/src/main/java/org/apache/ofbiz/order/task/TaskEvents.java
  3. +1 −1 applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/SeoTransform.java
  4. +1 −1 applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java
  5. +1 −1 applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java
  6. +2 −4 framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java
  7. +1 −1 ...ework/minilang/src/main/java/org/apache/ofbiz/minilang/method/eventops/WebappPropertyToField.java
  8. +2 −2 framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/RequestHandler.java
  9. +1 −1 framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/FreeMarkerViewHandler.java
  10. +1 −1 framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/OfbizUrlTransform.java
  11. +2 −4 framework/webapp/src/main/java/org/apache/ofbiz/webapp/website/WebSiteWorker.java
  12. +1 −3 framework/widget/src/main/java/org/apache/ofbiz/widget/model/FormFactory.java
  13. +1 −1 framework/widget/src/main/java/org/apache/ofbiz/widget/model/GridFactory.java
  14. +1 −3 framework/widget/src/main/java/org/apache/ofbiz/widget/model/IterateSectionWidget.java
  15. +1 −1 framework/widget/src/main/java/org/apache/ofbiz/widget/model/MenuFactory.java
  16. +1 −1 framework/widget/src/main/java/org/apache/ofbiz/widget/model/ScreenFactory.java
  17. +3 −4 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/html/HtmlMenuRenderer.java
  18. +2 −5 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/html/HtmlMenuWrapper.java
  19. +1 −1 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/html/HtmlTreeRenderer.java
  20. +1 −3 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java
  21. +1 −3 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroMenuRenderer.java
  22. +6 −6 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenRenderer.java
  23. +1 −1 framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroTreeRenderer.java
@@ -286,8 +286,7 @@ public static String cms(HttpServletRequest request, HttpServletResponse respons
templateMap.put("statusCode", statusCode);

// make the link prefix
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
templateMap.put("_REQUEST_HANDLER_", rh);

//Cache Headers
@@ -89,7 +89,7 @@ public static String completeAssignment(HttpServletRequest request, HttpServletR

/** Accept role assignment event */
public static String acceptRoleAssignment(HttpServletRequest request, HttpServletResponse response) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
Locale locale = UtilHttp.getLocale(request);

@@ -109,7 +109,7 @@ public static String acceptRoleAssignment(HttpServletRequest request, HttpServle

/** Delegate and accept assignment event */
public static String delegateAndAcceptAssignment(HttpServletRequest request, HttpServletResponse response) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
Locale locale = UtilHttp.getLocale(request);

@@ -90,7 +90,7 @@ public void close() throws IOException {
Object prefix = env.getVariable("urlPrefix");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
HttpServletResponse response = null;
if (res != null) {
response = (HttpServletResponse) res.getWrappedObject();
@@ -90,7 +90,7 @@ public void close() throws IOException {
Object prefix = env.getVariable("urlPrefix");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
HttpServletResponse response = null;
if (res != null) {
response = (HttpServletResponse) res.getWrappedObject();
@@ -432,7 +432,7 @@ public static final String checkDoKeywordOverride(HttpServletRequest request, Ht
if (keywords.contains(ovrdKeyword)) {
String targetTypeEnumId = productStoreKeywordOvrd.getString("targetTypeEnumId");
String target = productStoreKeywordOvrd.getString("target");
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
if ("KOTT_PRODCAT".equals(targetTypeEnumId)) {
String requestName = "/category/~category_id=" + target;
@@ -47,7 +47,6 @@
import java.util.TimeZone;

import javax.net.ssl.SSLContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@@ -506,15 +505,14 @@ public static String canonicalizeParameter(String paramValue) {
Map<String, Object> servletCtxMap = new HashMap<>();

// look at all servlet context attributes
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
Enumeration<String> applicationAttrNames = UtilGenerics.cast(servletContext.getAttributeNames());
Enumeration<String> applicationAttrNames = request.getServletContext().getAttributeNames();
while (applicationAttrNames.hasMoreElements()) {
String attrName = applicationAttrNames.nextElement();
if (namesToSkip != null && namesToSkip.contains(attrName)) {
continue;
}

Object attrValue = servletContext.getAttribute(attrName);
Object attrValue = request.getServletContext().getAttribute(attrName);
servletCtxMap.put(attrName, attrValue);
}

@@ -63,7 +63,7 @@ public WebappPropertyToField(Element element, SimpleMethod simpleMethod) throws
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (methodContext.getMethodType() == MethodContext.EVENT) {
String resource = resourceFse.expandString(methodContext.getEnvMap());
ServletContext servletContext = (ServletContext) methodContext.getRequest().getAttribute("servletContext");
ServletContext servletContext = methodContext.getRequest().getServletContext();
URL propsUrl = null;
try {
propsUrl = servletContext.getResource(resource);
@@ -1168,7 +1168,7 @@ public String makeLink(HttpServletRequest request, HttpServletResponse response,
try {
GenericValue webSiteValue = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).cache().queryOne();
if (webSiteValue != null) {
ServletContext application = ((ServletContext) request.getAttribute("servletContext"));
ServletContext application = (request.getServletContext());
String domainName = request.getLocalName();
if (application.getAttribute("MULTI_SITE_ENABLED") != null && UtilValidate.isNotEmpty(webSiteValue.getString("hostedPathAlias")) && !domainName.equals(webSiteValue.getString("httpHost"))) {
newURL.append('/');
@@ -1200,7 +1200,7 @@ public static String makeUrl(HttpServletRequest request, HttpServletResponse res
}

public static String makeUrl(HttpServletRequest request, HttpServletResponse response, String url, boolean fullPath, boolean secure, boolean encode) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
return rh.makeLink(request, response, url, fullPath, secure, encode);
}
@@ -81,7 +81,7 @@ public void render(String name, String page, String info, String contentType, St
}

public static void prepOfbizRoot(Map<String, Object> root, HttpServletRequest request, HttpServletResponse response) {
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
ServletContext servletContext = request.getServletContext();
HttpSession session = request.getSession();

// add in the OFBiz objects
@@ -142,7 +142,7 @@ public void close() throws IOException {
return;
}
if (request != null) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
HttpServletResponse response = FreeMarkerWorker.unwrap(env.getVariable("response"));
String requestUrl = buf.toString();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
@@ -37,10 +37,8 @@
private WebSiteWorker() {}

public static String getWebSiteId(ServletRequest request) {
ServletContext application = ((ServletContext) request.getAttribute("servletContext"));

if (application == null) return null;
return application.getInitParameter("webSiteId");
ServletContext ctx = request.getServletContext();
return (ctx == null) ? null : ctx.getInitParameter("webSiteId");
}

public static GenericValue getWebSite(ServletRequest request) {
@@ -24,7 +24,6 @@
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

@@ -83,10 +82,9 @@ public static ModelForm getFormFromWebappContext(String resourceName, String for
String cacheKey = webappName + "::" + resourceName + "::" + formName;
ModelForm modelForm = formWebappCache.get(cacheKey);
if (modelForm == null) {
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
URL formFileUrl = servletContext.getResource(resourceName);
URL formFileUrl = request.getServletContext().getResource(resourceName);
Document formFileDoc = UtilXml.readXmlDocument(formFileUrl, true, true);
Element formElement = UtilXml.firstChildElement(formFileDoc.getDocumentElement(), "form", "name", formName);
modelForm = createModelForm(formElement, delegator.getModelReader(), dispatcher.getDispatchContext(), resourceName, formName);
@@ -84,7 +84,7 @@ public static ModelGrid getGridFromWebappContext(String resourceName, String gri
String cacheKey = webappName + "::" + resourceName + "::" + gridName;
ModelGrid modelGrid = gridWebappCache.get(cacheKey);
if (modelGrid == null) {
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
ServletContext servletContext = request.getServletContext();
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
URL gridFileUrl = servletContext.getResource(resourceName);
@@ -28,7 +28,6 @@
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@@ -287,8 +286,7 @@ public void renderNextPrev(Appendable writer, Map<String, Object> context, int l
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");

ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");

writer.append("<table border=\"0\" width=\"100%\" cellpadding=\"2\">\n");
writer.append(" <tr>\n");
@@ -61,7 +61,7 @@ public static ModelMenu getMenuFromWebappContext(String resourceName, String men

Map<String, ModelMenu> modelMenuMap = menuWebappCache.get(cacheKey);
if (modelMenuMap == null) {
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
ServletContext servletContext = request.getServletContext();

URL menuFileUrl = servletContext.getResource(resourceName);
Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true, true);
@@ -144,7 +144,7 @@ public static ModelScreen getScreenFromWebappContext(String resourceName, String
synchronized (ScreenFactory.class) {
modelScreenMap = screenWebappCache.get(cacheKey);
if (modelScreenMap == null) {
ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
ServletContext servletContext = request.getServletContext();

URL screenFileUrl = servletContext.getResource(resourceName);
Document screenFileDoc = UtilXml.readXmlDocument(screenFileUrl, true, true);
@@ -63,7 +63,7 @@ public HtmlMenuRenderer(HttpServletRequest request, HttpServletResponse response
}

public void appendOfbizUrl(Appendable writer, String location) throws IOException {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
if (ctx == null) {
HttpSession session = request.getSession();
if (session != null) {
@@ -80,7 +80,7 @@ public void appendOfbizUrl(Appendable writer, String location) throws IOExceptio
}

public void appendContentUrl(Appendable writer, String location) throws IOException {
ServletContext ctx = (ServletContext) this.request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
if (ctx == null) {
HttpSession session = request.getSession();
if (session != null) {
@@ -492,8 +492,7 @@ public void renderImage(Appendable writer, Map<String, Object> context, Image im
HttpServletRequest request = (HttpServletRequest) context.get("request");
if (urlMode != null && "ofbiz".equalsIgnoreCase(urlMode)) {
if (request != null && response != null) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
String urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
writer.append(urlString);
} else {
@@ -24,7 +24,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@@ -104,8 +103,7 @@ public MenuStringRenderer getMenuRenderer() {

public String renderMenuString() throws IOException {
HttpServletRequest req = ((HtmlMenuRenderer)renderer).request;
ServletContext ctx = (ServletContext) req.getAttribute("servletContext");
if (ctx == null) {
if (req.getServletContext() == null) {
if (Debug.infoOn()) {
Debug.logInfo("in renderMenuString, ctx is null(0)" , "");
}
@@ -115,8 +113,7 @@ public String renderMenuString() throws IOException {
modelMenu.renderMenuString(writer, context, renderer);

HttpServletRequest req2 = ((HtmlMenuRenderer)renderer).request;
ServletContext ctx2 = (ServletContext) req2.getAttribute("servletContext");
if (ctx2 == null) {
if (req2.getServletContext() == null) {
if (Debug.infoOn()) {
Debug.logInfo("in renderMenuString, ctx is null(2)" , "");
}
@@ -315,7 +315,7 @@ public void renderImage(Appendable writer, Map<String, Object> context, ModelTre
HttpServletRequest request = (HttpServletRequest) context.get("request");
if (urlMode != null && "intra-app".equalsIgnoreCase(urlMode)) {
if (request != null && response != null) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
String urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
writer.append(urlString);
@@ -36,7 +36,6 @@
import java.util.WeakHashMap;
import java.util.stream.Collectors;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@@ -124,8 +123,7 @@ public MacroFormRenderer(String macroLibraryPath, HttpServletRequest request, Ht
this.request = request;
this.response = response;
this.visualTheme = ThemeFactory.resolveVisualTheme(request);
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
this.rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
this.rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
this.javaScriptEnabled = UtilHttp.isJavaScriptEnabled(request);
internalEncoder = UtilCodec.getEncoder("string");
}
@@ -27,7 +27,6 @@
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@@ -85,8 +84,7 @@ public MacroMenuRenderer(String macroLibraryPath, HttpServletRequest request, Ht
boolean fullPath = false;
boolean secure = false;
boolean encode = false;
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
src = rh.makeLink(request, response, src, fullPath, secure, encode);
} else if ("content".equalsIgnoreCase(urlMode)) {
StringBuilder newURL = new StringBuilder();

0 comments on commit 21135cb

Please sign in to comment.
You can’t perform that action at this time.