Skip to content
Permalink
Browse files

Improved: Add ‘RequestHandler#from’ static method

(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 2a24e0ecc12ec6f5ad960b1f257c3193e817a19b
@@ -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);
@@ -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;

@@ -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)) {
@@ -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)) {
@@ -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;
@@ -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();
@@ -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) {
@@ -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;
@@ -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();
@@ -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) {
@@ -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;
@@ -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);
@@ -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);
}

@@ -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_"));
}
}
@@ -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);
@@ -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;

@@ -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());
@@ -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");
@@ -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;

@@ -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 {
@@ -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");
}
@@ -84,7 +84,7 @@ public MacroMenuRenderer(String macroLibraryPath, HttpServletRequest request, Ht
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();
@@ -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;
@@ -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<>();
@@ -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;
@@ -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);
}

@@ -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);
}

@@ -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);
}
}
@@ -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
@@ -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;

@@ -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;

0 comments on commit 2a24e0e

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