[ofbiz-framework] branch trunk updated: Improved: Rename MultiBlockHtmlTemplateUtil to ScriptLinkHelper (OFBIZ-12134)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: Rename MultiBlockHtmlTemplateUtil to ScriptLinkHelper (OFBIZ-12134)

James Yong-2
This is an automated email from the ASF dual-hosted git repository.

jamesyong pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 9c7dc7b  Improved: Rename MultiBlockHtmlTemplateUtil to ScriptLinkHelper (OFBIZ-12134)
9c7dc7b is described below

commit 9c7dc7b8778022bb91733b74f7e0fc57e3867f65
Author: James Yong <[hidden email]>
AuthorDate: Wed Jan 13 09:26:42 2021 +0800

    Improved: Rename MultiBlockHtmlTemplateUtil to ScriptLinkHelper (OFBIZ-12134)
   
    Rename + Refactor
---
 .../java/org/apache/ofbiz/common/CommonEvents.java |  4 +-
 .../ofbiz/webapp/control/ControlEventListener.java |  4 +-
 .../webapp/ftl/ScriptTagsFooterTransform.java      |  4 +-
 .../org/apache/ofbiz/widget/model/HtmlWidget.java  | 26 ++----------
 ...HtmlTemplateUtil.java => ScriptLinkHelper.java} | 46 +++++++++++++++++-----
 .../ofbiz/widget/renderer/ScreenRenderer.java      |  6 +--
 6 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
index 89aad37..debfbcf 100644
--- a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
+++ b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
@@ -62,7 +62,7 @@ import org.apache.ofbiz.entity.util.EntityUtilProperties;
 import org.apache.ofbiz.webapp.control.JWTManager;
 import org.apache.ofbiz.webapp.control.LoginWorker;
 import org.apache.ofbiz.widget.model.ModelWidget;
-import org.apache.ofbiz.widget.model.MultiBlockHtmlTemplateUtil;
+import org.apache.ofbiz.widget.model.ScriptLinkHelper;
 import org.apache.ofbiz.widget.model.ThemeFactory;
 import org.apache.ofbiz.widget.renderer.VisualTheme;
 
@@ -189,7 +189,7 @@ public class CommonEvents {
     public static String jsResponseFromRequest(HttpServletRequest request, HttpServletResponse response) {
 
         String fileName = request.getParameter("name");
-        String script = MultiBlockHtmlTemplateUtil.getScriptFromCache(request.getSession(), fileName);
+        String script = ScriptLinkHelper.getScriptFromCache(request.getSession(), fileName);
 
         // return the JS String
         Writer out;
diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlEventListener.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlEventListener.java
index d8198ef..3435e3b 100644
--- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlEventListener.java
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlEventListener.java
@@ -38,7 +38,7 @@ import org.apache.ofbiz.entity.serialize.XmlSerializer;
 import org.apache.ofbiz.entity.transaction.TransactionUtil;
 import org.apache.ofbiz.entity.util.EntityQuery;
 import org.apache.ofbiz.security.CsrfUtil;
-import org.apache.ofbiz.widget.model.MultiBlockHtmlTemplateUtil;
+import org.apache.ofbiz.widget.model.ScriptLinkHelper;
 
 /**
  * HttpSessionListener that gathers and tracks various information and statistics
@@ -74,7 +74,7 @@ public class ControlEventListener implements HttpSessionListener {
         HttpSession session = event.getSession();
 
         CsrfUtil.cleanupTokenMap(session);
-        MultiBlockHtmlTemplateUtil.cleanupScriptCache(session);
+        ScriptLinkHelper.cleanupScriptCache(session);
 
         // Finalize the Visit
         boolean beganTransaction = false;
diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/ScriptTagsFooterTransform.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/ScriptTagsFooterTransform.java
index 8288ab5..fa7598e 100644
--- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/ScriptTagsFooterTransform.java
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/ScriptTagsFooterTransform.java
@@ -24,7 +24,7 @@ import java.util.Map;
 import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 
-import org.apache.ofbiz.widget.model.MultiBlockHtmlTemplateUtil;
+import org.apache.ofbiz.widget.model.ScriptLinkHelper;
 
 import freemarker.core.Environment;
 import freemarker.ext.beans.BeanModel;
@@ -51,7 +51,7 @@ public class ScriptTagsFooterTransform implements TemplateTransformModel {
                     BeanModel req = (BeanModel) env.getVariable("request");
                     if (req != null) {
                         HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
-                        Set<String> scriptSrcSet = MultiBlockHtmlTemplateUtil.getScriptLinksForFoot(request);
+                        Set<String> scriptSrcSet = ScriptLinkHelper.getScriptLinksForBodyEnd(request);
                         if (scriptSrcSet != null) {
                             String srcList = "";
                             for (String scriptSrc : scriptSrcSet) {
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
index cde3114..f175044 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
@@ -38,8 +38,6 @@ import org.apache.ofbiz.base.util.cache.UtilCache;
 import org.apache.ofbiz.base.util.collections.MapStack;
 import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
 import org.apache.ofbiz.base.util.template.FreeMarkerWorker;
-import org.apache.ofbiz.security.CsrfUtil;
-import org.apache.ofbiz.webapp.SeoConfigUtil;
 import org.apache.ofbiz.widget.renderer.ScreenRenderer;
 import org.apache.ofbiz.widget.renderer.ScreenStringRenderer;
 import org.apache.ofbiz.widget.renderer.html.HtmlWidgetRenderer;
@@ -268,17 +266,17 @@ public class HtmlWidget extends ModelScreenWidget {
              */
             String location = locationExdr.expandString(context);
             StringWriter stringWriter = new StringWriter();
-            Stack<StringWriter> stringWriterStack = UtilGenerics.cast(context.get(MultiBlockHtmlTemplateUtil.FTL_WRITER));
+            Stack<StringWriter> stringWriterStack = UtilGenerics.cast(context.get(ScriptLinkHelper.FTL_WRITER));
             if (stringWriterStack == null) {
                 stringWriterStack = new Stack<>();
             }
             stringWriterStack.push(stringWriter);
-            context.put(MultiBlockHtmlTemplateUtil.FTL_WRITER, stringWriterStack);
+            context.put(ScriptLinkHelper.FTL_WRITER, stringWriterStack);
             renderHtmlTemplate(stringWriter, locationExdr, context);
             stringWriterStack.pop();
             // check if no more parent freemarker template before removing from context
             if (stringWriterStack.empty()) {
-                context.remove(MultiBlockHtmlTemplateUtil.FTL_WRITER);
+                context.remove(ScriptLinkHelper.FTL_WRITER);
             }
             String data = stringWriter.toString();
             stringWriter.close();
@@ -323,24 +321,8 @@ public class HtmlWidget extends ModelScreenWidget {
                         if (fileName.endsWith(".ftl")) {
                             fileName = fileName.substring(0, fileName.length() - 4);
                         }
-                        String key = MultiBlockHtmlTemplateUtil.putScriptInCache(context, fileName, scripts.toString());
-
                         HttpServletRequest request = (HttpServletRequest) context.get("request");
-                        // construct script link
-                        String contextPath = request.getContextPath();
-                        String url = null;
-                        if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
-                            url = contextPath + "/getJs?name=" + key;
-                        } else {
-                            url = contextPath + "/control/getJs?name=" + key;
-                        }
-
-                        // add csrf token to script link
-                        String tokenValue = CsrfUtil.generateTokenForNonAjax(request, "getJs");
-                        url = CsrfUtil.addOrUpdateTokenInUrl(url, tokenValue);
-
-                        // store script link to be output by scriptTagsFooter freemarker macro
-                        MultiBlockHtmlTemplateUtil.addScriptLinkForFoot(request, url);
+                        ScriptLinkHelper.prepareScriptLinkForBodyEnd(request, fileName, scripts.toString());
                     }
                 }
                 // the 'template' block
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ScriptLinkHelper.java
similarity index 73%
rename from framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java
rename to framework/widget/src/main/java/org/apache/ofbiz/widget/model/ScriptLinkHelper.java
index 2352945..ffd5958 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ScriptLinkHelper.java
@@ -19,9 +19,12 @@
 package org.apache.ofbiz.widget.model;
 
 import org.apache.ofbiz.base.util.UtilGenerics;
+import org.apache.ofbiz.security.CsrfUtil;
+import org.apache.ofbiz.webapp.SeoConfigUtil;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
@@ -33,17 +36,17 @@ import java.util.Set;
  *    These external javascript tags are placed at the bottom of the html page. The scripts are retrieved via the getJs
  *    request handler.
  */
-public final class MultiBlockHtmlTemplateUtil {
+public final class ScriptLinkHelper {
 
-    private static final String MODULE = MultiBlockHtmlTemplateUtil.class.getName();
+    private static final String MODULE = ScriptLinkHelper.class.getName();
     public static final String FTL_WRITER = "WriterForFTL";
-    private static final String SCRIPT_LINKS_FOR_BODY_END = "ScriptLinksForBodyEnd";
+    public static final String SCRIPT_LINKS_FOR_BODY_END = "ScriptLinksForBodyEnd";
     private static int maxScriptCacheSizePerUserSession = 15;
     private static int estimatedConcurrentUserSessions = 250;
     /**
      * Store inline script extracted from freemarker template for a user session.
-     * Number of inline scripts for a user session will be constraint by {@link MultiBlockHtmlTemplateUtil#maxScriptCacheSizePerUserSession}
-     * {@link MultiBlockHtmlTemplateUtil#cleanupScriptCache(HttpSession)} will be called to remove entry when session ends.
+     * Number of inline scripts for a user session will be constraint by {@link ScriptLinkHelper#maxScriptCacheSizePerUserSession}
+     * {@link ScriptLinkHelper#cleanupScriptCache(HttpSession)} will be called to remove entry when session ends.
      */
     private static LinkedHashMap<String, Map<String, String>> scriptCache =
             new LinkedHashMap<String, Map<String, String>>() {
@@ -53,14 +56,14 @@ public final class MultiBlockHtmlTemplateUtil {
                 }
             };
 
-    private MultiBlockHtmlTemplateUtil() { }
+    private ScriptLinkHelper() { }
 
     /**
      * add script link for page footer.
      * @param request
      * @param filePath
      */
-    public static void addScriptLinkForFoot(final HttpServletRequest request, final String filePath) {
+    private static void addScriptLinkForBodyEnd(final HttpServletRequest request, final String filePath) {
         Set<String> scriptLinks = UtilGenerics.cast(request.getAttribute(SCRIPT_LINKS_FOR_BODY_END));
         if (scriptLinks == null) {
             // use of LinkedHashSet to maintain insertion order
@@ -75,7 +78,7 @@ public final class MultiBlockHtmlTemplateUtil {
      * @param request
      * @return
      */
-    public static Set<String> getScriptLinksForFoot(HttpServletRequest request) {
+    public static Set<String> getScriptLinksForBodyEnd(HttpServletRequest request) {
         Set<String> scriptLinks = UtilGenerics.cast(request.getAttribute(SCRIPT_LINKS_FOR_BODY_END));
         return scriptLinks;
     }
@@ -87,7 +90,7 @@ public final class MultiBlockHtmlTemplateUtil {
      * @param fileContent
      * @return key used to store the script
      */
-    public static String putScriptInCache(Map<String, Object> context, String fileName, String fileContent) {
+    private static String putScriptInCache(Map<String, Object> context, String fileName, String fileContent) {
         HttpSession session = (HttpSession) context.get("session");
         String sessionId = session.getId();
         Map<String, String> scriptMap = UtilGenerics.cast(scriptCache.get(sessionId));
@@ -135,4 +138,29 @@ public final class MultiBlockHtmlTemplateUtil {
     public static void cleanupScriptCache(HttpSession session) {
         scriptCache.remove(session.getId());
     }
+
+    public static String prepareScriptLinkForBodyEnd(HttpServletRequest request, String fileName, String script) {
+
+        Map<String, Object> context = new HashMap<>();
+        context.put("session", request.getSession());
+        String key = putScriptInCache(context, fileName, script);
+
+        // construct script link
+        String contextPath = request.getContextPath();
+        String url = null;
+        if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
+            url = contextPath + "/getJs?name=" + key;
+        } else {
+            url = contextPath + "/control/getJs?name=" + key;
+        }
+
+        // add csrf token to script link
+        String tokenValue = CsrfUtil.generateTokenForNonAjax(request, "getJs");
+        url = CsrfUtil.addOrUpdateTokenInUrl(url, tokenValue);
+
+        // store script link to be output by scriptTagsFooter freemarker macro
+        addScriptLinkForBodyEnd(request, url);
+
+        return "success";
+    }
 }
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/ScreenRenderer.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/ScreenRenderer.java
index 64a518d..523a942 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/ScreenRenderer.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/ScreenRenderer.java
@@ -60,7 +60,7 @@ import org.apache.ofbiz.widget.cache.GenericWidgetOutput;
 import org.apache.ofbiz.widget.cache.ScreenCache;
 import org.apache.ofbiz.widget.cache.WidgetContextCacheKey;
 import org.apache.ofbiz.widget.model.ModelScreen;
-import org.apache.ofbiz.widget.model.MultiBlockHtmlTemplateUtil;
+import org.apache.ofbiz.widget.model.ScriptLinkHelper;
 import org.apache.ofbiz.widget.model.ScreenFactory;
 import org.apache.ofbiz.widget.model.ThemeFactory;
 import org.xml.sax.SAXException;
@@ -140,8 +140,8 @@ public class ScreenRenderer {
             }
         } else {
             context.put("renderFormSeqNumber", String.valueOf(renderFormSeqNumber));
-            if (context.get(MultiBlockHtmlTemplateUtil.FTL_WRITER) != null) {
-                Stack<StringWriter> stringWriterStack = UtilGenerics.cast(context.get(MultiBlockHtmlTemplateUtil.FTL_WRITER));
+            if (context.get(ScriptLinkHelper.FTL_WRITER) != null) {
+                Stack<StringWriter> stringWriterStack = UtilGenerics.cast(context.get(ScriptLinkHelper.FTL_WRITER));
                 modelScreen.renderScreenString(stringWriterStack.peek(), context, screenStringRenderer);
             } else {
                 modelScreen.renderScreenString(writer, context, screenStringRenderer);