svn commit: r1859543 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java

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

svn commit: r1859543 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java

jleroux@apache.org
Author: jleroux
Date: Mon May 20 10:10:34 2019
New Revision: 1859543

URL: http://svn.apache.org/viewvc?rev=1859543&view=rev
Log:
Fixed: Redirection of pathAlias to aliasTo does not work properly
(OFBIZ-11018)

Link of discussion: https://markmail.org/message/s37whdteejqdzkha

In CMS, we can redirect the user to different path alias using 'aliasTo' field
of WebSitePathAlias entity.

As per my observation, the content for  "newDemoHome" is rendered properly
(as expected) but the URL of the page (in browser) doesn't change.
We should also update the URL also, i.e. change browser URL from

Example -
<WebSitePathAlias aliasTo="demoPage2" fromDate="2016-06-09 13:37:30.43"
pathAlias="demoHome2" webSiteId="CmsSite"/>
<WebSitePathAlias contentId="CMSS_DEMO_PAGE1" fromDate="2016-06-09 13:37:30.43"
pathAlias="demoPage2" webSiteId="CmsSite"/>

Based on the above data, if the user hit the
URL https://localhost:8443/cmssite/cms/demoHome2 
the CMS should internally render the content for 'DemoHome2' at
https://localhost:8443/cmssite/cms/demoPage2

Thanks: Pawan Verma

Modified:
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java?rev=1859543&r1=1859542&r2=1859543&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/cms/CmsEvents.java Mon May 20 10:10:34 2019
@@ -31,6 +31,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.base.util.GeneralRuntimeException;
@@ -167,7 +168,7 @@ public class CmsEvents {
 
             GenericValue pathAlias = null;
             try {
-                pathAlias = EntityQuery.use(delegator).from("WebSitePathAlias").where("webSiteId", webSiteId, "pathAlias", pathInfo).orderBy("-fromDate").cache().filterByDate().queryOne();
+                pathAlias = EntityQuery.use(delegator).from("WebSitePathAlias").where("webSiteId", webSiteId, "pathAlias", pathInfo).orderBy("-fromDate").cache().filterByDate().queryFirst();
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
             }
@@ -179,16 +180,20 @@ public class CmsEvents {
                     if (!alias.startsWith("/")) {
                         alias = "/" + alias;
                     }
-                    RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath() + alias);
-                    try {
-                        rd.forward(request, response);
-                    } catch (ServletException e) {
-                        Debug.logError(e, module);
-                        return "error";
-                    } catch (IOException e) {
-                        Debug.logError(e, module);
-                        return "error";
-                    }
+
+                    String context = request.getContextPath();
+                    String location = context + request.getServletPath();
+                    GenericValue webSite = WebSiteWorker.getWebSite(request);
+                    if (webSite != null && webSite.getString("hostedPathAlias") != null && !"ROOT".equals(pathInfo))
+                        location += "/" + webSite.getString("hostedPathAlias");
+
+                    String uriWithContext = request.getRequestURI();
+                    String uri = uriWithContext.substring(context.length());
+                    uri = uri.substring(0, uri.lastIndexOf('/'));
+
+                    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+                    response.setHeader("Location", location + uri + alias);
+                    response.setHeader("Connection", "close");
 
                     return null; // null to not process any views
                 }