Skip to content

Commit

Permalink
Improved: Use ‘ServletRequest#getServletContext’ when possible
Browse files Browse the repository at this point in the history
(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 21135cb
Show file tree
Hide file tree
Showing 23 changed files with 35 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -506,15 +505,14 @@ public static Map<String, Object> getServletContextMap(HttpServletRequest reques
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ public final class WebSiteWorker {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)" , "");
}
Expand All @@ -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)" , "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -85,8 +84,7 @@ private Map<String, Object> createImageParameters(Map<String, Object> context, I
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();
Expand Down
Loading

0 comments on commit 21135cb

Please sign in to comment.