Skip to content

Commit

Permalink
Improved: Add ‘RequestHandler#from’ static method
Browse files Browse the repository at this point in the history
(OFBIZ-11129)

Avoid some boiler-plate when retrieving the request handler inside a
servlet context.
--this line, and those below, will be ignored--

M    applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
M    applications/order/src/main/java/org/apache/ofbiz/order/task/TaskEvents.java
M    applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/SeoTransform.java
M    applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java
M    applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java
M    framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/RequestHandler.java
M    framework/webapp/src/main/java/org/apache/ofbiz/webapp/event/CoreEvents.java
M    framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/OfbizUrlTransform.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/model/IterateSectionWidget.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/html/HtmlTreeRenderer.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroFormRenderer.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroMenuRenderer.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenRenderer.java
M    framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroTreeRenderer.java


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1862295 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jun 28, 2019
1 parent 21135cb commit 2a24e0e
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 46 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
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
templateMap.put("_REQUEST_HANDLER_", rh);
templateMap.put("_REQUEST_HANDLER_", RequestHandler.from(request));

//Cache Headers
UtilHttp.setResponseBrowserProxyNoCache(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Locale;
import java.util.Map;

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

Expand Down Expand Up @@ -89,8 +88,7 @@ public static String completeAssignment(HttpServletRequest request, HttpServletR

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

if (addToOrderRole(request)) {
Expand All @@ -109,8 +107,7 @@ public static String acceptRoleAssignment(HttpServletRequest request, HttpServle

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

if (addToOrderRole(request)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Iterator;
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 @@ -90,7 +89,6 @@ public void close() throws IOException {
Object prefix = env.getVariable("urlPrefix");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
ServletContext ctx = request.getServletContext();
HttpServletResponse response = null;
if (res != null) {
response = (HttpServletResponse) res.getWrappedObject();
Expand All @@ -103,7 +101,7 @@ public void close() throws IOException {
userLogin = null;
}

RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null));
} else if (prefix != null) {
if (prefix instanceof TemplateScalarModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Iterator;
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 @@ -90,7 +89,6 @@ public void close() throws IOException {
Object prefix = env.getVariable("urlPrefix");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
ServletContext ctx = request.getServletContext();
HttpServletResponse response = null;
if (res != null) {
response = (HttpServletResponse) res.getWrappedObject();
Expand All @@ -103,7 +101,7 @@ public void close() throws IOException {
userLogin = null;
}

RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure || request.isSecure() , encode), userLogin == null));
} else if (prefix != null) {
if (prefix instanceof TemplateScalarModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Expand Down Expand Up @@ -432,8 +431,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 = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
if ("KOTT_PRODCAT".equals(targetTypeEnumId)) {
String requestName = "/category/~category_id=" + target;
target = rh.makeLink(request, response, requestName, false, false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,9 @@ public static String makeUrl(HttpServletRequest request, HttpServletResponse res
return makeUrl(request, response, url, false, false, false);
}

public static String makeUrl(HttpServletRequest request, HttpServletResponse response, String url, boolean fullPath, boolean secure, boolean encode) {
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
public static String makeUrl(HttpServletRequest request, HttpServletResponse response, String url, boolean fullPath,
boolean secure, boolean encode) {
RequestHandler rh = from(request);
return rh.makeLink(request, response, url, fullPath, secure, encode);
}

Expand Down Expand Up @@ -1331,4 +1331,15 @@ static boolean checkCertificates(HttpServletRequest request, Predicate<X509Certi
return false;
});
}

/**
* Retrieves the request handler which is stored inside an HTTP request.
*
* @param request the HTTP request containing the request handler
* @return a request handler or {@code null} when absent
* @throws NullPointerException when {@code request} or the servlet context is {@code null}.
*/
public static RequestHandler from(HttpServletRequest request) {
return UtilGenerics.cast(request.getServletContext().getAttribute("_REQUEST_HANDLER_"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,7 @@ public static String runService(HttpServletRequest request, HttpServletResponse
}

public static String streamFile(HttpServletRequest request, HttpServletResponse response) {
//RequestHandler rh = (RequestHandler) request.getAttribute("_REQUEST_HANDLER_");
String filePath = RequestHandler.getOverrideViewUri(request.getPathInfo());
//String fileName = filePath.substring(filePath.lastIndexOf("/")+1);

// load the file
File file = new File(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.net.URLEncoder;
import java.util.Map;

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

Expand Down Expand Up @@ -142,10 +141,9 @@ public void close() throws IOException {
return;
}
if (request != null) {
ServletContext ctx = request.getServletContext();
HttpServletResponse response = FreeMarkerWorker.unwrap(env.getVariable("response"));
String requestUrl = buf.toString();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
out.write(rh.makeLink(request, response, requestUrl, fullPath, secure, encode));
} else {
out.write(buf.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +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");

RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);

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 @@ -22,7 +22,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 @@ -315,8 +314,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 = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
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 @@ -123,7 +123,7 @@ public MacroFormRenderer(String macroLibraryPath, HttpServletRequest request, Ht
this.request = request;
this.response = response;
this.visualTheme = ThemeFactory.resolveVisualTheme(request);
this.rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
this.rh = RequestHandler.from(request);
this.javaScriptEnabled = UtilHttp.isJavaScriptEnabled(request);
internalEncoder = UtilCodec.getEncoder("string");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private Map<String, Object> createImageParameters(Map<String, Object> context, I
boolean fullPath = false;
boolean secure = false;
boolean encode = false;
RequestHandler rh = (RequestHandler) request.getServletContext().getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
src = rh.makeLink(request, response, src, fullPath, secure, encode);
} else if ("content".equalsIgnoreCase(urlMode)) {
StringBuilder newURL = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.WeakHashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -198,8 +197,7 @@ public void renderContainerBegin(Appendable writer, Map<String, Object> context,
containerId = getNextElementId();
}
HttpServletResponse response = (HttpServletResponse) context.get("response");
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
autoUpdateLink = rh.makeLink(request, response, autoUpdateTarget);
}
Map<String, Object> parameters = new HashMap<>();
Expand Down Expand Up @@ -349,8 +347,7 @@ public void renderImage(Appendable writer, Map<String, Object> context, ModelScr
String urlString = "";
if (urlMode != null && "intra-app".equalsIgnoreCase(urlMode)) {
if (request != null && response != null) {
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
} else {
urlString = src;
Expand Down Expand Up @@ -477,8 +474,7 @@ public void renderContentEnd(Appendable writer, Map<String, Object> context, Mod
editRequest += "&amp;";
}
editRequest += "contentId=" + expandedContentId;
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
urlString = rh.makeLink(request, response, editRequest, false, false, false);
}

Expand All @@ -500,8 +496,7 @@ public void renderContentFrame(Appendable writer, Map<String, Object> context, M
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
if (request != null && response != null) {
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
fullUrlString = rh.makeLink(request, response, urlString, true, false, false);
}

Expand Down Expand Up @@ -592,8 +587,7 @@ public void renderSubContentEnd(Appendable writer, Map<String, Object> context,
if (UtilValidate.isNotEmpty(expandedMapKey)) {
editRequest += "&amp;mapKey=" + expandedMapKey;
}
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
urlString = rh.makeLink(request, response, editRequest, false, false, false);
}
}
Expand Down Expand Up @@ -769,8 +763,7 @@ protected void renderScreenletPaginateMenu(Appendable writer, Map<String, Object
viewSizeParam = "VIEW_SIZE" + "_" + paginatorNumber;
}

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

Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
// strip out any multi form fields if the form is of type multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 @@ -324,8 +323,7 @@ public void renderImage(Appendable writer, Map<String, Object> context, ModelTre

if (urlMode != null && "intra-app".equalsIgnoreCase(urlMode)) {
if (request != null && response != null) {
ServletContext ctx = request.getServletContext();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
RequestHandler rh = RequestHandler.from(request);
urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
} else {
urlString = src;
Expand Down

0 comments on commit 2a24e0e

Please sign in to comment.