svn commit: r1868395 - /ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java

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

svn commit: r1868395 - /ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java

jleroux@apache.org
Author: jleroux
Date: Sun Oct 13 10:37:08 2019
New Revision: 1868395

URL: http://svn.apache.org/viewvc?rev=1868395&view=rev
Log:
Fixed: Merge UrlRegexpTransform and OfbizUrlTransform classes
(OFBIZ-11229)

OFBIZ-4361 depends on this issue. Here is a first version, the main identified
point was when the request isn't present (email send, service call) the website
isn't pull from context. This solves this aspect

Thanks: Nicolas for the patch

Modified:
    ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java

Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java?rev=1868395&r1=1868394&r2=1868395&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/ftl/UrlRegexpTransform.java Sun Oct 13 10:37:08 2019
@@ -20,6 +20,7 @@ package org.apache.ofbiz.product.categor
 
 import java.io.IOException;
 import java.io.Writer;
+import java.net.URLEncoder;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -27,12 +28,20 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
+import org.apache.ofbiz.base.component.ComponentConfig;
 import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.template.FreeMarkerWorker;
+import org.apache.ofbiz.entity.Delegator;
+import org.apache.ofbiz.entity.GenericEntityException;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.product.category.SeoConfigUtil;
+import org.apache.ofbiz.webapp.OfbizUrlBuilder;
+import org.apache.ofbiz.webapp.WebAppUtil;
 import org.apache.ofbiz.webapp.control.RequestHandler;
+import org.apache.ofbiz.webapp.control.WebAppConfigurationException;
 import org.apache.oro.text.regex.Pattern;
 import org.apache.oro.text.regex.Perl5Matcher;
+import org.xml.sax.SAXException;
 
 import freemarker.core.Environment;
 import freemarker.ext.beans.BeanModel;
@@ -49,6 +58,27 @@ public class UrlRegexpTransform implemen
 
     private static final String module = UrlRegexpTransform.class.getName();
 
+
+    private static String convertToString(Object o) {
+        String result = "";
+        if (o != null) {
+            if (Debug.verboseOn()) {
+                 Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
+            }
+            if (o instanceof TemplateScalarModel) {
+                TemplateScalarModel s = (TemplateScalarModel) o;
+                try {
+                    result = s.getAsString();
+                } catch (TemplateModelException e) {
+                    Debug.logError(e, "Template Exception", module);
+                }
+            } else {
+                result = o.toString();
+            }
+        }
+        return result;
+    }
+
     public boolean checkArg(Map<?, ?> args, String key, boolean defaultValue) {
         if (!args.containsKey(key)) {
             return defaultValue;
@@ -67,6 +97,7 @@ public class UrlRegexpTransform implemen
         final boolean fullPath = checkArg(args, "fullPath", false);
         final boolean secure = checkArg(args, "secure", false);
         final boolean encode = checkArg(args, "encode", true);
+        final String webSiteId = convertToString(args.get("webSiteId"));
 
         return new Writer(out) {
 
@@ -103,6 +134,20 @@ public class UrlRegexpTransform implemen
 
                         RequestHandler rh = RequestHandler.from(request);
                         out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure || request.isSecure() , encode), userLogin == null));
+                    } else if (!webSiteId.isEmpty()) {
+                        Delegator delegator = FreeMarkerWorker.unwrap(env.getVariable("delegator"));
+                        if (delegator == null) {
+                            throw new IllegalStateException("Delegator not found");
+                        }
+                        ComponentConfig.WebappInfo webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
+                        StringBuilder newUrlBuff = new StringBuilder(250);
+                        OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
+                        builder.buildFullUrl(newUrlBuff, buf.toString(), secure);
+                        String newUrl = newUrlBuff.toString();
+                        if (encode) {
+                            newUrl = URLEncoder.encode(newUrl, "UTF-8");
+                        }
+                        out.write(newUrl);
                     } else if (prefix != null) {
                         if (prefix instanceof TemplateScalarModel) {
                             TemplateScalarModel s = (TemplateScalarModel) prefix;
@@ -120,7 +165,11 @@ public class UrlRegexpTransform implemen
                     } else {
                         out.write(buf.toString());
                     }
-                } catch (IOException | TemplateModelException e) {
+                } catch (IOException |
+                        SAXException |
+                        TemplateModelException |
+                        GenericEntityException |
+                        WebAppConfigurationException e) {
                     throw new IOException(e.getMessage());
                 }
             }