From cc803c5d08830edd5bfae57e925eafee3922a4fa Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Thu, 29 Jan 2015 19:06:49 +0000 Subject: [PATCH] This implements OFBIZ-5312 "Proposal: URL-Generation Changes (mostly for SEO reasons but not only)" by merging the "SEO branch" in trunk After some efforts to take into consideration Anil's justified concerns (simply put: keep the current behaviour), the modifications are finally light, which is greater for reviews. Most important are: * Product component ** I have moved the OfbizCatalogAltUrlTransform and OfbizCatalogUrlTransform classes to a ftl subdirectory with the new FTL macros implementation: CatalogAltUrlSeoTransform, CatalogUrlSeoFilter, CatalogAltUrlSeoTransform, CatalogUrlSeoTransform, SeoTransform, UrlRegexpTransform ** A bunch of new filters have been added CatalogUrlSeoFilter, controlServlet, SeoCatalogUrlServlet, SeoContentUrlFilter, SeoContextFilter, SeoControlServlet, UrlRegexpContextFilter ** 2 util classes: SeoConfigUtil, SeoUrlUtil ** By default ofbizCatalogAltUrl and ofbizCatalogUrl macros now use the new implementations by CatalogAltUrlSeoTransform and CatalogUrlSeoTransform. This can be changed back in freemarkerTransforms.properties ** A SeoConfig.xml file define some SEO parameters, the SeoConfig.xsd can be sued as documentation, globally please refer to OFBIZ-5312. * Framework webapp component ** By default ofbizUrl macro now uses the new implementations by UrlRegexpTransform. This can be changed back in freemarkerTransforms.properties * Ecommerce component ** A new ecomseo webapp clones the ecommerce webapp on the model of ecomclone. There is not much there: mostly a controller with a new JspViewHandler class (in ecommerce) and a web.xml file With these changes we can have both worlds running side by side. So you can use eitherb http://localhost:8080/ecommerce/control/main as root or http://localhost:8080/ecomseo, but also if you want http://localhost:8080/ecommerce/. Then everything follows. From my reviews and tests all is working as expected. I only found a trivial bug that I can also reproduce in current trunk see OFBIZ-5305 (sometimes not locally, weird). Of course, all the tests passes. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1655803 13f79535-47bb-0310-9956-ffa450edef68 --- applications/product/build.xml | 2 + applications/product/config/SeoConfig.xml | 116 +++ .../config/freemarkerTransforms.properties | 6 +- applications/product/dtd/SeoConfig.xsd | 112 +++ .../product/category/CatalogUrlSeoFilter.java | 71 ++ .../product/category/CategoryServices.java | 2 +- .../product/category/ControlServlet.java | 68 ++ .../category/SeoCatalogUrlServlet.java | 206 +++++ .../ofbiz/product/category/SeoConfigUtil.java | 537 +++++++++++ .../product/category/SeoContentUrlFilter.java | 166 ++++ .../product/category/SeoContextFilter.java | 395 ++++++++ .../product/category/SeoControlServlet.java | 91 ++ .../ofbiz/product/category/SeoUrlUtil.java | 43 + .../category/UrlRegexpContextFilter.java | 322 +++++++ .../ftl/CatalogAltUrlSeoTransform.java | 181 ++++ .../category/ftl/CatalogUrlSeoTransform.java | 840 ++++++++++++++++++ .../OfbizCatalogAltUrlTransform.java | 7 +- .../{ => ftl}/OfbizCatalogUrlTransform.java | 6 +- .../product/category/ftl/SeoTransform.java | 189 ++++ .../category/ftl/UrlRegexpTransform.java | 231 +++++ .../config/freemarkerTransforms.properties | 3 +- .../src/org/ofbiz/webapp/WebAppUtil.java | 2 +- specialpurpose/ecommerce/ofbiz-component.xml | 6 + .../ecommerce/webapp/view/JspViewHandler.java | 103 +++ .../webapp/ecomseo/WEB-INF/controller.xml | 27 + .../ecommerce/webapp/ecomseo/WEB-INF/web.xml | 166 ++++ .../ecommerce/webapp/ecomseo/error/error.jsp | 53 ++ .../ecommerce/webapp/ecomseo/index.jsp | 20 + 28 files changed, 3959 insertions(+), 12 deletions(-) create mode 100644 applications/product/config/SeoConfig.xml create mode 100644 applications/product/dtd/SeoConfig.xsd create mode 100644 applications/product/src/org/ofbiz/product/category/CatalogUrlSeoFilter.java create mode 100644 applications/product/src/org/ofbiz/product/category/ControlServlet.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoCatalogUrlServlet.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoConfigUtil.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoContentUrlFilter.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoContextFilter.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoControlServlet.java create mode 100644 applications/product/src/org/ofbiz/product/category/SeoUrlUtil.java create mode 100644 applications/product/src/org/ofbiz/product/category/UrlRegexpContextFilter.java create mode 100644 applications/product/src/org/ofbiz/product/category/ftl/CatalogAltUrlSeoTransform.java create mode 100644 applications/product/src/org/ofbiz/product/category/ftl/CatalogUrlSeoTransform.java rename applications/product/src/org/ofbiz/product/category/{ => ftl}/OfbizCatalogAltUrlTransform.java (98%) rename applications/product/src/org/ofbiz/product/category/{ => ftl}/OfbizCatalogUrlTransform.java (96%) create mode 100644 applications/product/src/org/ofbiz/product/category/ftl/SeoTransform.java create mode 100644 applications/product/src/org/ofbiz/product/category/ftl/UrlRegexpTransform.java create mode 100644 specialpurpose/ecommerce/src/org/ofbiz/ecommerce/webapp/view/JspViewHandler.java create mode 100644 specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/controller.xml create mode 100644 specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/web.xml create mode 100644 specialpurpose/ecommerce/webapp/ecomseo/error/error.jsp create mode 100644 specialpurpose/ecommerce/webapp/ecomseo/index.jsp diff --git a/applications/product/build.xml b/applications/product/build.xml index da199ea22f..d3c027472d 100644 --- a/applications/product/build.xml +++ b/applications/product/build.xml @@ -34,7 +34,9 @@ under the License. + + diff --git a/applications/product/config/SeoConfig.xml b/applications/product/config/SeoConfig.xml new file mode 100644 index 0000000000..dcacf8e1b3 --- /dev/null +++ b/applications/product/config/SeoConfig.xml @@ -0,0 +1,116 @@ + + + ^.*/.*$ + + + enable + + /:/ecomseo + + disable + + .html + + + + + + disable + + + + disable + + + ^.*/(keywordsearch|logout).*$ + + + + + + + + sample: remove jsessionid + ^(.*);jsessionid=(.*)jvm[1-9](.*)$ + + $1$3 + + + $1$3 + 301 + + + + + sample: remove /ecomseo/main + ^/ecomseo/main$ + + /ecomseo/ + + + /ecomseo/ + 301 + + + + + sample: remove /main + ^/main$ + + / + + + / + 301 + + + + + + + \u00fc + ue + + + \u00e4 + ae + + + \u00f6 + oe + + + \u00df + ss + + + \\+ + und + + + \u0026 + und + + + รจ + e + + + + [^A-Za-z0-9+-] + - + + + -{2,} + - + + + diff --git a/applications/product/config/freemarkerTransforms.properties b/applications/product/config/freemarkerTransforms.properties index 98d314c634..450c52d249 100644 --- a/applications/product/config/freemarkerTransforms.properties +++ b/applications/product/config/freemarkerTransforms.properties @@ -21,6 +21,8 @@ # entries are in the form: key=transform name, property=transform class name -ofbizCatalogAltUrl=org.ofbiz.product.category.OfbizCatalogAltUrlTransform -ofbizCatalogUrl=org.ofbiz.product.category.CatalogUrlDirective +#ofbizCatalogAltUrl=org.ofbiz.product.category.ftl.OfbizCatalogAltUrlTransform +#ofbizCatalogUrl=org.ofbiz.product.category.ftl.CatalogUrlDirective +ofbizCatalogAltUrl=org.ofbiz.product.category.ftl.CatalogAltUrlSeoTransform +ofbizCatalogUrl=org.ofbiz.product.category.ftl.CatalogUrlSeoTransform diff --git a/applications/product/dtd/SeoConfig.xsd b/applications/product/dtd/SeoConfig.xsd new file mode 100644 index 0000000000..194e8308c0 --- /dev/null +++ b/applications/product/dtd/SeoConfig.xsd @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/applications/product/src/org/ofbiz/product/category/CatalogUrlSeoFilter.java b/applications/product/src/org/ofbiz/product/category/CatalogUrlSeoFilter.java new file mode 100644 index 0000000000..a292c55075 --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/CatalogUrlSeoFilter.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.common.UrlServletHelper; +import org.ofbiz.entity.Delegator; +import org.ofbiz.product.category.ftl.CatalogUrlSeoTransform; + +public class CatalogUrlSeoFilter extends CatalogUrlFilter { + + public final static String module = CatalogUrlSeoFilter.class.getName(); + + protected static String defaultLocaleString = null; + protected static String redirectUrl = null; + + /** + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) + */ + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpServletResponse httpResponse = (HttpServletResponse) response; + Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator"); + + // Get ServletContext + ServletContext servletContext = config.getServletContext(); + + // Set request attribute and session + UrlServletHelper.setRequestAttributes(request, delegator, servletContext); + + // set initial parameters + String initDefaultLocalesString = config.getInitParameter("defaultLocaleString"); + String initRedirectUrl = config.getInitParameter("redirectUrl"); + defaultLocaleString = UtilValidate.isNotEmpty(initDefaultLocalesString) ? initDefaultLocalesString : ""; + redirectUrl = UtilValidate.isNotEmpty(initRedirectUrl) ? initRedirectUrl : ""; + + // set the ServletContext in the request for future use + httpRequest.setAttribute("servletContext", config.getServletContext()); + if (CatalogUrlSeoTransform.forwardUri(httpRequest, httpResponse, delegator, ControlServlet.controlServlet)) { + return; + } + super.doFilter(httpRequest, httpResponse, chain); + } + +} diff --git a/applications/product/src/org/ofbiz/product/category/CategoryServices.java b/applications/product/src/org/ofbiz/product/category/CategoryServices.java index 2a81c999d3..bceec73a80 100644 --- a/applications/product/src/org/ofbiz/product/category/CategoryServices.java +++ b/applications/product/src/org/ofbiz/product/category/CategoryServices.java @@ -524,7 +524,7 @@ public class CategoryServices { } } } catch (GenericEntityException e) { - e.printStackTrace(); + Debug.logWarning(e, module); return "error"; } return "success"; diff --git a/applications/product/src/org/ofbiz/product/category/ControlServlet.java b/applications/product/src/org/ofbiz/product/category/ControlServlet.java new file mode 100644 index 0000000000..efc21c5c1f --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/ControlServlet.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.ofbiz.base.util.UtilValidate; + +/** + * ControlServlet.java - Master servlet for the web application. + */ +@SuppressWarnings("serial") +public class ControlServlet extends org.ofbiz.webapp.control.ControlServlet { + + public static final String module = ControlServlet.class.getName(); + + protected static String defaultPage = null; + protected static String pageNotFound = null; + protected static String controlServlet = null; + + public ControlServlet() { + super(); + } + + /** + * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + + ServletContext context = this.getServletContext(); + if (UtilValidate.isEmpty(defaultPage)) { + defaultPage = context.getInitParameter("defaultPage"); + } + if (UtilValidate.isEmpty(defaultPage)) { + defaultPage = "/main"; + } + if (UtilValidate.isEmpty(pageNotFound)) { + pageNotFound = context.getInitParameter("pageNotFound"); + } + if (UtilValidate.isEmpty(pageNotFound)) { + pageNotFound = "/pagenotfound"; + } + + if (defaultPage.startsWith("/") && defaultPage.lastIndexOf("/") > 0) { + controlServlet = defaultPage.substring(1); + controlServlet = controlServlet.substring(0, controlServlet.indexOf("/")); + } + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoCatalogUrlServlet.java b/applications/product/src/org/ofbiz/product/category/SeoCatalogUrlServlet.java new file mode 100644 index 0000000000..376818679e --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoCatalogUrlServlet.java @@ -0,0 +1,206 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javolution.util.FastList; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericEntityException; + +/** + * SeoCatalogUrlServlet.java + */ +@SuppressWarnings("serial") +public class SeoCatalogUrlServlet extends HttpServlet { + + public static final String module = SeoCatalogUrlServlet.class.getName(); + public static final String CATALOG_URL_MOUNT_POINT = "products"; + public static final String PRODUCT_REQUEST = "product"; + public static final String CATEGORY_REQUEST = "category"; + + public SeoCatalogUrlServlet() { + super(); + } + + /** + * @see javax.servlet.http.HttpServlet#init(javax.servlet.ServletConfig) + */ + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + } + + /** + * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } + + /** + * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Delegator delegator = (Delegator) getServletContext().getAttribute("delegator"); + + String pathInfo = request.getPathInfo(); + List pathElements = StringUtil.split(pathInfo, "/"); + + // look for productId + String productId = null; + try { + String lastPathElement = pathElements.get(pathElements.size() - 1); + if (lastPathElement.startsWith("p_") || delegator.findOne("Product", UtilMisc.toMap("productId", lastPathElement), true) != null) { + if (lastPathElement.startsWith("p_")) { + productId = lastPathElement.substring(2); + } else { + productId = lastPathElement; + } + pathElements.remove(pathElements.size() - 1); + } + } catch (GenericEntityException e) { + Debug.logError(e, "Error looking up product info for ProductUrl with path info [" + pathInfo + "]: " + e.toString(), module); + } + + // get category info going with the IDs that remain + String categoryId = null; + if (pathElements.size() == 1) { + CategoryWorker.setTrail(request, pathElements.get(0), null); + categoryId = pathElements.get(0); + } else if (pathElements.size() == 2) { + CategoryWorker.setTrail(request, pathElements.get(1), pathElements.get(0)); + categoryId = pathElements.get(1); + } else if (pathElements.size() > 2) { + List trail = CategoryWorker.getTrail(request); + if (trail == null) { + trail = FastList.newInstance(); + } + + if (trail.contains(pathElements.get(0))) { + // first category is in the trail, so remove it everything after that and fill it in with the list from the pathInfo + int firstElementIndex = trail.indexOf(pathElements.get(0)); + while (trail.size() > firstElementIndex) { + trail.remove(firstElementIndex); + } + trail.addAll(pathElements); + } else { + // first category is NOT in the trail, so clear out the trail and use the pathElements list + trail.clear(); + trail.addAll(pathElements); + } + CategoryWorker.setTrail(request, trail); + categoryId = pathElements.get(pathElements.size() - 1); + } + if (categoryId != null) { + request.setAttribute("productCategoryId", categoryId); + } + + String rootCategoryId = null; + if (pathElements.size() >= 1) { + rootCategoryId = pathElements.get(0); + } + if (rootCategoryId != null) { + request.setAttribute("rootCategoryId", rootCategoryId); + } + + if (productId != null) { + request.setAttribute("product_id", productId); + request.setAttribute("productId", productId); + } + + RequestDispatcher rd = request.getRequestDispatcher("/" + (UtilValidate.isEmpty(SeoControlServlet.controlServlet) ? "" : (SeoControlServlet.controlServlet + "/")) + + (productId != null ? PRODUCT_REQUEST : CATEGORY_REQUEST)); + rd.forward(request, response); + } + + /** + * @see javax.servlet.http.HttpServlet#destroy() + */ + @Override + public void destroy() { + super.destroy(); + } + + public static String makeCatalogUrl(HttpServletRequest request, String productId, String currentCategoryId, String previousCategoryId) { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(request.getSession().getServletContext().getContextPath()); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + urlBuilder.append(CATALOG_URL_MOUNT_POINT); + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + List trail = CategoryWorker.getTrail(request); + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + for (String trailCategoryId : trail) { + if ("TOP".equals(trailCategoryId)) continue; + urlBuilder.append("/"); + urlBuilder.append(trailCategoryId); + } + } + + if (UtilValidate.isNotEmpty(productId)) { + urlBuilder.append("/p_"); + urlBuilder.append(productId); + } + + return urlBuilder.toString(); + } + + public static String makeCatalogUrl(String contextPath, List crumb, String productId, String currentCategoryId, String previousCategoryId) { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(contextPath); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + urlBuilder.append(CATALOG_URL_MOUNT_POINT); + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + crumb = CategoryWorker.adjustTrail(crumb, currentCategoryId, previousCategoryId); + for (String trailCategoryId : crumb) { + if ("TOP".equals(trailCategoryId)) continue; + urlBuilder.append("/"); + urlBuilder.append(trailCategoryId); + } + } + + if (UtilValidate.isNotEmpty(productId)) { + urlBuilder.append("/p_"); + urlBuilder.append(productId); + } + + return urlBuilder.toString(); + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoConfigUtil.java b/applications/product/src/org/ofbiz/product/category/SeoConfigUtil.java new file mode 100644 index 0000000000..fdbc46f3fa --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoConfigUtil.java @@ -0,0 +1,537 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.ParserConfigurationException; + +import javolution.util.FastList; +import javolution.util.FastMap; +import javolution.util.FastSet; + +import org.apache.oro.text.regex.MalformedPatternException; +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.Perl5Compiler; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilURL; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * SeoConfigUtil - SEO Configuration file utility. + * + */ +public class SeoConfigUtil { + private static final String module = SeoConfigUtil.class.getName(); + private static Perl5Compiler perlCompiler = new Perl5Compiler(); + private static boolean isInitialed = false; + private static boolean categoryUrlEnabled = true; + private static boolean categoryNameEnabled = false; + private static String categoryUrlSuffix = null; + public static final String DEFAULT_REGEXP = "^.*/.*$"; + private static Pattern regexpIfMatch = null; + private static boolean useUrlRegexp = false; + private static boolean jSessionIdAnonEnabled = false; + private static boolean jSessionIdUserEnabled = false; + private static Map seoReplacements = null; + private static Map seoPatterns = null; + private static Map forwardReplacements = null; + private static Map forwardResponseCodes = null; + private static Map charFilters = null; + private static List userExceptionPatterns = null; + private static Set allowedContextPaths = null; + private static Map specialProductIds = null; + public static final String ELEMENT_REGEXPIFMATCH = "regexpifmatch"; + public static final String ELEMENT_URL_CONFIG = "url-config"; + public static final String ELEMENT_DESCRIPTION = "description"; + public static final String ELEMENT_FORWARD = "forward"; + public static final String ELEMENT_SEO = "seo"; + public static final String ELEMENT_URLPATTERN = "url-pattern"; + public static final String ELEMENT_REPLACEMENT = "replacement"; + public static final String ELEMENT_RESPONSECODE = "responsecode"; + public static final String ELEMENT_JSESSIONID = "jsessionid"; + public static final String ELEMENT_ANONYMOUS = "anonymous"; + public static final String ELEMENT_VALUE = "value"; + public static final String ELEMENT_USER = "user"; + public static final String ELEMENT_EXCEPTIONS = "exceptions"; + public static final String ELEMENT_CHAR_FILTERS = "char-filters"; + public static final String ELEMENT_CHAR_FILTER = "char-filter"; + public static final String ELEMENT_CHARACTER_PATTERN = "character-pattern"; + public static final String ELEMENT_CATEGORY_URL = "category-url"; + public static final String ELEMENT_ALLOWED_CONTEXT_PATHS = "allowed-context-paths"; + public static final String ELEMENT_CATEGORY_NAME = "category-name"; + public static final String ELEMENT_CATEGORY_URL_SUFFIX = "category-url-suffix"; + public static final String SEO_CONFIG_FILENAME = "SeoConfig.xml"; + public static final int DEFAULT_RESPONSECODE = HttpServletResponse.SC_MOVED_PERMANENTLY; + public static final String DEFAULT_ANONYMOUS_VALUE = "disable"; + public static final String DEFAULT_USER_VALUE = "disable"; + public static final String DEFAULT_CATEGORY_URL_VALUE = "enable"; + public static final String DEFAULT_CATEGORY_NAME_VALUE = "disable"; + public static final String ALLOWED_CONTEXT_PATHS_SEPERATOR = ":"; + + /** + * Initialize url regular express configuration. + * + * @return result to indicate the status of initialization. + */ + public static void init() { + FileInputStream configFileIS = null; + String result = "success"; + seoPatterns = new HashMap(); + seoReplacements = new HashMap(); + forwardReplacements = new HashMap(); + forwardResponseCodes = new HashMap(); + userExceptionPatterns = FastList.newInstance(); + specialProductIds = FastMap.newInstance(); + charFilters = FastMap.newInstance(); + try { + URL seoConfigFilename = UtilURL.fromResource(SEO_CONFIG_FILENAME); + Document configDoc = UtilXml.readXmlDocument(seoConfigFilename, false); + Element rootElement = configDoc.getDocumentElement(); + + String regexIfMatch = UtilXml.childElementValue(rootElement, ELEMENT_REGEXPIFMATCH, DEFAULT_REGEXP); + Debug.logInfo("Parsing " + regexIfMatch, module); + try { + regexpIfMatch = perlCompiler.compile(regexIfMatch, Perl5Compiler.READ_ONLY_MASK); + } catch (MalformedPatternException e1) { + Debug.logWarning(e1, "Error while parsing " + regexIfMatch, module); + } + + // parse category-url element + try { + Element categoryUrlElement = UtilXml.firstChildElement(rootElement, ELEMENT_CATEGORY_URL); + Debug.logInfo("Parsing " + ELEMENT_CATEGORY_URL + " [" + (categoryUrlElement != null) + "]:", module); + if (categoryUrlElement != null) { + String enableCategoryUrlValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_VALUE, DEFAULT_CATEGORY_URL_VALUE); + if (DEFAULT_CATEGORY_URL_VALUE.equalsIgnoreCase(enableCategoryUrlValue)) { + categoryUrlEnabled = true; + } else { + categoryUrlEnabled = false; + } + + if (categoryUrlEnabled) { + String allowedContextValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_ALLOWED_CONTEXT_PATHS, null); + allowedContextPaths = FastSet.newInstance(); + if (UtilValidate.isNotEmpty(allowedContextValue)) { + List allowedContextPathList = StringUtil.split(allowedContextValue, ALLOWED_CONTEXT_PATHS_SEPERATOR); + for (String path : allowedContextPathList) { + if (UtilValidate.isNotEmpty(path)) { + path = path.trim(); + if (!allowedContextPaths.contains(path)) { + allowedContextPaths.add(path); + Debug.logInfo(" " + ELEMENT_ALLOWED_CONTEXT_PATHS + ": " + path, module); + } + } + } + } + + String categoryNameValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_CATEGORY_NAME, DEFAULT_CATEGORY_NAME_VALUE); + if (DEFAULT_CATEGORY_NAME_VALUE.equalsIgnoreCase(categoryNameValue)) { + categoryNameEnabled = false; + } else { + categoryNameEnabled = true; + } + Debug.logInfo(" " + ELEMENT_CATEGORY_NAME + ": " + categoryNameEnabled, module); + + categoryUrlSuffix = UtilXml.childElementValue(categoryUrlElement, ELEMENT_CATEGORY_URL_SUFFIX, null); + if (UtilValidate.isNotEmpty(categoryUrlSuffix)) { + categoryUrlSuffix = categoryUrlSuffix.trim(); + if (categoryUrlSuffix.contains("/")) { + categoryUrlSuffix = null; + } + } + Debug.logInfo(" " + ELEMENT_CATEGORY_URL_SUFFIX + ": " + categoryUrlSuffix, module); + } + } + } catch (NullPointerException e) { + // no "category-url" element + Debug.logWarning("No category-url element found in " + seoConfigFilename.toString(), module); + } + + // parse jsessionid element + try { + Element jSessionId = UtilXml.firstChildElement(rootElement, ELEMENT_JSESSIONID); + Debug.logInfo("Parsing " + ELEMENT_JSESSIONID + " [" + (jSessionId != null) + "]:", module); + if (jSessionId != null) { + Element anonymous = UtilXml.firstChildElement(jSessionId, ELEMENT_ANONYMOUS); + if (anonymous != null) { + String anonymousValue = UtilXml.childElementValue(anonymous, ELEMENT_VALUE, DEFAULT_ANONYMOUS_VALUE); + if (DEFAULT_ANONYMOUS_VALUE.equalsIgnoreCase(anonymousValue)) { + jSessionIdAnonEnabled = false; + } else { + jSessionIdAnonEnabled = true; + } + } else { + jSessionIdAnonEnabled = Boolean.valueOf(DEFAULT_ANONYMOUS_VALUE).booleanValue(); + } + Debug.logInfo(" " + ELEMENT_ANONYMOUS + ": " + jSessionIdAnonEnabled, module); + + Element user = UtilXml.firstChildElement(jSessionId, ELEMENT_USER); + if (user != null) { + String userValue = UtilXml.childElementValue(user, ELEMENT_VALUE, DEFAULT_USER_VALUE); + if (DEFAULT_USER_VALUE.equalsIgnoreCase(userValue)) { + jSessionIdUserEnabled = false; + } else { + jSessionIdUserEnabled = true; + } + + Element exceptions = UtilXml.firstChildElement(user, ELEMENT_EXCEPTIONS); + if (exceptions != null) { + Debug.logInfo(" " + ELEMENT_EXCEPTIONS + ": ", module); + List exceptionUrlPatterns = UtilXml.childElementList(exceptions, ELEMENT_URLPATTERN); + for (int i = 0; i < exceptionUrlPatterns.size(); i++) { + Element element = exceptionUrlPatterns.get(i); + String urlpattern = element.getTextContent(); + if (UtilValidate.isNotEmpty(urlpattern)) { + try { + Pattern pattern = perlCompiler.compile(urlpattern, Perl5Compiler.READ_ONLY_MASK); + userExceptionPatterns.add(pattern); + Debug.logInfo(" " + ELEMENT_URLPATTERN + ": " + urlpattern, module); + } catch (MalformedPatternException e) { + Debug.logWarning("Can NOT parse " + urlpattern + " in element " + ELEMENT_URLPATTERN + " of " + ELEMENT_EXCEPTIONS + ". Error: " + e.getMessage(), module); + } + } + } + } + } else { + jSessionIdUserEnabled = Boolean.valueOf(DEFAULT_USER_VALUE).booleanValue(); + } + Debug.logInfo(" " + ELEMENT_USER + ": " + jSessionIdUserEnabled, module); + } + } catch (NullPointerException e) { + Debug.logWarning("No jsessionid element found in " + seoConfigFilename.toString(), module); + } + + // parse url-config elements + try { + NodeList configs = rootElement.getElementsByTagName(ELEMENT_URL_CONFIG); + Debug.logInfo("Parsing " + ELEMENT_URL_CONFIG, module); + for (int j = 0; j < configs.getLength(); j++) { + Element config = (Element) configs.item(j); + String urlpattern = UtilXml.childElementValue(config, ELEMENT_URLPATTERN, null); + if (UtilValidate.isEmpty(urlpattern)) { + continue; + } + Debug.logInfo(" " + ELEMENT_URLPATTERN + ": " + urlpattern, module); + Pattern pattern; + try { + pattern = perlCompiler.compile(urlpattern, Perl5Compiler.READ_ONLY_MASK); + seoPatterns.put(urlpattern, pattern); + } catch (MalformedPatternException e) { + Debug.logWarning("Error while creating parttern for seo url-pattern: " + urlpattern, module); + continue; + } + + // construct seo patterns + Element seo = UtilXml.firstChildElement(config, ELEMENT_SEO); + if (UtilValidate.isNotEmpty(seo)) { + String replacement = UtilXml.childElementValue(seo, ELEMENT_REPLACEMENT, null); + if (UtilValidate.isNotEmpty(replacement)) { + seoReplacements.put(urlpattern, replacement); + Debug.logInfo(" " + ELEMENT_SEO + " " + ELEMENT_REPLACEMENT + ": " + replacement, module); + } + } + + // construct forward patterns + Element forward = UtilXml.firstChildElement(config, ELEMENT_FORWARD); + if (UtilValidate.isNotEmpty(forward)) { + String replacement = UtilXml.childElementValue(forward, ELEMENT_REPLACEMENT, null); + String responseCode = UtilXml.childElementValue(forward, + ELEMENT_RESPONSECODE, String.valueOf(DEFAULT_RESPONSECODE)); + if (UtilValidate.isNotEmpty(replacement)) { + forwardReplacements.put(urlpattern, replacement); + Debug.logInfo(" " + ELEMENT_FORWARD + " " + ELEMENT_REPLACEMENT + ": " + replacement, module); + if (UtilValidate.isNotEmpty(responseCode)) { + Integer responseCodeInt = DEFAULT_RESPONSECODE; + try { + responseCodeInt = Integer.valueOf(responseCode); + } catch (NumberFormatException nfe) { + Debug.logWarning(nfe, "Error while parsing response code number: " + responseCode, module); + } + forwardResponseCodes.put(urlpattern, responseCodeInt); + Debug.logInfo(" " + ELEMENT_FORWARD + " " + ELEMENT_RESPONSECODE + ": " + responseCodeInt, module); + } + } + } + } + } catch (NullPointerException e) { + // no "url-config" element + Debug.logWarning("No " + ELEMENT_URL_CONFIG + " element found in " + seoConfigFilename.toString(), module); + } + + // parse char-filters elements + try { + NodeList nameFilterNodes = rootElement + .getElementsByTagName(ELEMENT_CHAR_FILTER); + Debug.logInfo("Parsing " + ELEMENT_CHAR_FILTER + ": ", module); + for (int i = 0; i < nameFilterNodes.getLength(); i++) { + Element element = (Element) nameFilterNodes.item(i); + String charaterPattern = UtilXml.childElementValue(element, ELEMENT_CHARACTER_PATTERN, null); + String replacement = UtilXml.childElementValue(element, ELEMENT_REPLACEMENT, null); + if (UtilValidate.isNotEmpty(charaterPattern) + && UtilValidate.isNotEmpty(replacement)) { + try { + perlCompiler.compile(charaterPattern, Perl5Compiler.READ_ONLY_MASK); + charFilters.put(charaterPattern, replacement); + Debug.logInfo(" " + ELEMENT_CHARACTER_PATTERN + ": " + charaterPattern, module); + Debug.logInfo(" " + ELEMENT_REPLACEMENT + ": " + replacement, module); + } catch (MalformedPatternException e) { + // skip this filter (character-pattern replacement) if any error happened + Debug.logWarning(e, "Error while parsing " + ELEMENT_CHARACTER_PATTERN + ": " + charaterPattern, module); + } + } + } + } catch (NullPointerException e) { + // no "char-filters" element + Debug.logWarning("No " + ELEMENT_CHAR_FILTER + " element found in " + seoConfigFilename.toString(), module); + } + } catch (SAXException e) { + result = "error"; + Debug.logError(e, module); + } catch (ParserConfigurationException e) { + result = "error"; + Debug.logError(e, module); + } catch (IOException e) { + result = "error"; + Debug.logError(e, module); + } finally { + if (configFileIS != null) { + try { + configFileIS.close(); + } catch (IOException e) { + result = "error"; + Debug.logError(e, module); + } + } + } + if (seoReplacements.keySet().isEmpty()) { + useUrlRegexp = false; + } else { + useUrlRegexp = true; + } + if (result.equals("success")) { + isInitialed = true; + } + } + + /** + * Check whether the configuration file has been read. + * + * @return a boolean value to indicate whether the configuration file has been read. + */ + public static boolean isInitialed() { + return isInitialed; + } + + /** + * Check whether url regexp should be used. + * + * @return a boolean value to indicate whether url regexp should be used. + */ + public static boolean checkUseUrlRegexp() { + return useUrlRegexp; + } + + /** + * Get the general regexp pattern. + * + * @return the general regexp pattern. + */ + public static Pattern getGeneralRegexpPattern() { + return regexpIfMatch; + } + + /** + * Check whether category url is enabled. + * + * @return a boolean value to indicate whether category url is enabled. + */ + public static boolean checkCategoryUrl() { + return categoryUrlEnabled; + } + + /** + * Check whether the context path is enabled. + * + * @return a boolean value to indicate whether the context path is enabled. + */ + public static boolean isCategoryUrlEnabled(String contextPath) { + if (contextPath == null) { + return false; + } + if (UtilValidate.isEmpty(contextPath)) { + contextPath = "/"; + } + if (categoryUrlEnabled) { + if (allowedContextPaths.contains(contextPath.trim())) { + return true; + } else { + return false; + } + } + return false; + } + + /** + * Check whether category name is enabled. + * + * @return a boolean value to indicate whether category name is enabled. + */ + public static boolean isCategoryNameEnabled() { + return categoryNameEnabled; + } + + /** + * Get category url suffix. + * + * @return String category url suffix. + */ + public static String getCategoryUrlSuffix() { + return categoryUrlSuffix; + } + + /** + * Check whether jsessionid is enabled for anonymous. + * + * @return a boolean value to indicate whether jsessionid is enabled for anonymous. + */ + public static boolean isJSessionIdAnonEnabled() { + return jSessionIdAnonEnabled; + } + + /** + * Check whether jsessionid is enabled for user. + * + * @return a boolean value to indicate whether jsessionid is enabled for user. + */ + public static boolean isJSessionIdUserEnabled() { + return jSessionIdUserEnabled; + } + + /** + * Get user exception url pattern configures. + * + * @return user exception url pattern configures (java.util.List) + */ + public static List getUserExceptionPatterns() { + return userExceptionPatterns; + } + + /** + * Get char filters. + * + * @return char filters (java.util.Map) + */ + public static Map getCharFilters() { + return charFilters; + } + + /** + * Get seo url pattern configures. + * + * @return seo url pattern configures (java.util.Map) + */ + public static Map getSeoPatterns() { + return seoPatterns; + } + + /** + * Get seo replacement configures. + * + * @return seo replacement configures (java.util.Map) + */ + public static Map getSeoReplacements() { + return seoReplacements; + } + + /** + * Get forward replacement configures. + * + * @return forward replacement configures (java.util.Map) + */ + public static Map getForwardReplacements() { + return forwardReplacements; + } + + /** + * Get forward response codes. + * + * @return forward response code configures (java.util.Map) + */ + public static Map getForwardResponseCodes() { + return forwardResponseCodes; + } + + /** + * Check whether a product id is in the special list. If we cannot get a product from a lower cased + * or upper cased product id, then it's special. + * + * @return boolean to indicate whether the product id is special. + */ + @Deprecated + public static boolean isSpecialProductId(String productId) { + return specialProductIds.containsKey(productId); + } + + /** + * Add a special product id to the special list. + * + * @param productId a product id get from database. + * @return true to indicate it has been added to special product id; false to indicate it's not special. + * @throws Exception to indicate there's already same lower cased product id in the list but value is a different product id. + */ + @Deprecated + public static boolean addSpecialProductId(String productId) throws Exception { + if (productId.toLowerCase().equals(productId) || productId.toUpperCase().equals(productId)) { + return false; + } + if (isSpecialProductId(productId.toLowerCase())) { + if (specialProductIds.containsValue(productId)) { + return true; + } else { + throw new Exception("This product Id cannot be lower cased for SEO URL purpose: " + productId); + } + } + specialProductIds.put(productId.toLowerCase(), productId); + return true; + } + + /** + * Get a product id is in the special list. + * + * @return String of the original product id + */ + @Deprecated + public static String getSpecialProductId(String productId) { + return specialProductIds.get(productId); + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoContentUrlFilter.java b/applications/product/src/org/ofbiz/product/category/SeoContentUrlFilter.java new file mode 100644 index 0000000000..209097868b --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoContentUrlFilter.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ + +package org.ofbiz.product.category; + +import java.io.IOException; +import java.util.List; +import java.util.Set; + +import javax.servlet.FilterChain; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javolution.util.FastList; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilCodec; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.common.UrlServletHelper; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; +import org.ofbiz.entity.condition.EntityOperator; +import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.webapp.control.ContextFilter; + +public class SeoContentUrlFilter extends ContextFilter { + public final static String module = SeoContentUrlFilter.class.getName(); + protected static String defaultLocaleString = null; + protected static String redirectUrl = null; + public static String defaultViewRequest = "contentViewInfo"; + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpServletResponse httpResponse = (HttpServletResponse) response; + Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator"); + + // Get ServletContext + ServletContext servletContext = config.getServletContext(); + // Set request attribute and session + UrlServletHelper.setRequestAttributes(request, delegator, servletContext); + String urlContentId = null; + String pathInfo = UtilHttp.getFullRequestUrl(httpRequest); + if (UtilValidate.isNotEmpty(pathInfo)) { + String alternativeUrl = pathInfo.substring(pathInfo.lastIndexOf("/")); + if (alternativeUrl.endsWith("-content")) { + try { + List contentDataResourceViews = delegator.findByAnd("ContentDataResourceView", UtilMisc.toMap("drObjectInfo", alternativeUrl), null, false); + if (contentDataResourceViews.size() > 0) { + contentDataResourceViews = EntityUtil.orderBy(contentDataResourceViews, UtilMisc.toList("createdDate DESC")); + GenericValue contentDataResourceView = EntityUtil.getFirst(contentDataResourceViews); + List contents = EntityUtil.filterByDate(delegator.findByAnd("ContentAssoc", + UtilMisc.toMap("contentAssocTypeId", "ALTERNATIVE_URL", "contentIdTo", contentDataResourceView.getString("contentId")), null, false)); + if (contents.size() > 0) { + GenericValue content = EntityUtil.getFirst(contents); + urlContentId = content.getString("contentId"); + } + } + } catch (Exception e) { + Debug.logWarning(e.getMessage(), module); + } + } + if (UtilValidate.isNotEmpty(urlContentId)) { + StringBuilder urlBuilder = new StringBuilder(); + if (UtilValidate.isNotEmpty(SeoControlServlet.controlServlet)) { + urlBuilder.append("/" + SeoControlServlet.controlServlet); + } + urlBuilder.append("/" + config.getInitParameter("viewRequest") + "?contentId=" + urlContentId); + + // Set view query parameters + UrlServletHelper.setViewQueryParameters(request, urlBuilder); + Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module); + RequestDispatcher dispatch = request.getRequestDispatcher(urlBuilder.toString()); + dispatch.forward(request, response); + return; + } + + // Check path alias + UrlServletHelper.checkPathAlias(request, httpResponse, delegator, pathInfo); + } + // we're done checking; continue on + chain.doFilter(request, response); + } + + public static String makeContentAltUrl(HttpServletRequest request, HttpServletResponse response, String contentId, String viewContent) { + if (UtilValidate.isEmpty(contentId)) { + return null; + } + Delegator delegator = (Delegator) request.getAttribute("delegator"); + String url = null; + try { + List expr = FastList.newInstance(); + expr.add(EntityCondition.makeCondition("caContentAssocTypeId", EntityOperator.EQUALS, "ALTERNATIVE_URL")); + expr.add(EntityCondition.makeCondition("caThruDate", EntityOperator.EQUALS, null)); + expr.add(EntityCondition.makeCondition("contentIdStart", EntityOperator.EQUALS, contentId)); + Set fieldsToSelect = UtilMisc.toSet("contentIdStart", "drObjectInfo", "dataResourceId", "caFromDate", "caThruDate", "caCreatedDate"); + List contentAssocDataResources = delegator.findList("ContentAssocDataResourceViewTo", EntityCondition.makeCondition(expr), fieldsToSelect, + UtilMisc.toList("-caFromDate"), null, true); + if (contentAssocDataResources.size() > 0) { + GenericValue contentAssocDataResource = EntityUtil.getFirst(contentAssocDataResources); + url = contentAssocDataResource.getString("drObjectInfo"); + url = UtilCodec.getDecoder("url").decode(url); + String mountPoint = request.getContextPath(); + if (!(mountPoint.equals("/")) && !(mountPoint.equals(""))) { + url = mountPoint + url; + } + } + } catch (Exception e) { + Debug.logWarning("[Exception] : " + e.getMessage(), module); + } + + if (UtilValidate.isEmpty(url)) { + if (UtilValidate.isEmpty(viewContent)) { + viewContent = defaultViewRequest; + } + url = makeContentUrl(request, response, contentId, viewContent); + } + return url; + } + + public static String makeContentUrl(HttpServletRequest request, HttpServletResponse response, String contentId, String viewContent) { + if (UtilValidate.isEmpty(contentId)) { + return null; + } + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(request.getSession().getServletContext().getContextPath()); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + if (UtilValidate.isNotEmpty(SeoControlServlet.controlServlet)) { + urlBuilder.append(SeoControlServlet.controlServlet + "/"); + } + + if (UtilValidate.isNotEmpty(viewContent)) { + urlBuilder.append(viewContent); + } else { + urlBuilder.append(defaultViewRequest); + } + urlBuilder.append("?contentId=" + contentId); + return urlBuilder.toString(); + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoContextFilter.java b/applications/product/src/org/ofbiz/product/category/SeoContextFilter.java new file mode 100644 index 0000000000..0530eea21c --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoContextFilter.java @@ -0,0 +1,395 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import static org.ofbiz.base.util.UtilGenerics.checkMap; + +import java.io.IOException; +import java.net.URL; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javolution.util.FastSet; + +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.Perl5Matcher; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilObject; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.DelegatorFactory; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; +import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.security.Security; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.webapp.control.ConfigXMLReader; +import org.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig; +import org.ofbiz.webapp.control.ContextFilter; +import org.ofbiz.webapp.control.WebAppConfigurationException; +import org.ofbiz.webapp.website.WebSiteWorker; + +/** + * SeoContextFilter - Restricts access to raw files and configures servlet objects. + */ +public class SeoContextFilter extends ContextFilter { + + public static final String module = SeoContextFilter.class.getName(); + + protected Set WebServlets = FastSet.newInstance(); + + public void init(FilterConfig config) throws ServletException { + super.init(config); + + Map servletRegistrations = config.getServletContext().getServletRegistrations(); + for (String key : servletRegistrations.keySet()) { + Collection servlets = servletRegistrations.get(key).getMappings(); + for (String servlet : servlets) { + if (servlet.endsWith("/*")) { + servlet = servlet.substring(0, servlet.length() - 2); + if (UtilValidate.isNotEmpty(servlet) && !WebServlets.contains(servlet)) { + WebServlets.add(servlet); + } + } + } + } + } + + /** + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) + */ + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpServletResponse httpResponse = (HttpServletResponse) response; + + String uri = httpRequest.getRequestURI(); + boolean forwarded = forwardUri(httpResponse, uri); + if (forwarded) { + return; + } + + URL controllerConfigURL = ConfigXMLReader.getControllerConfigURL(config.getServletContext()); + ControllerConfig controllerConfig = null; + Map requestMaps = null; + try { + controllerConfig = ConfigXMLReader.getControllerConfig(controllerConfigURL); + requestMaps = controllerConfig.getRequestMapMap(); + } catch (WebAppConfigurationException e) { + Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module); + throw new ServletException(e); + } + Set uris = requestMaps.keySet(); + + // NOTE: the following part is copied from org.ofbiz.webapp.control.ContextFilter.doFilter method, please update this if framework is updated. + // Debug.logInfo("Running ContextFilter.doFilter", module); + + // ----- Servlet Object Setup ----- + // set the cached class loader for more speedy running in this thread + + // set the ServletContext in the request for future use + httpRequest.setAttribute("servletContext", config.getServletContext()); + + // set the webSiteId in the session + if (UtilValidate.isEmpty(httpRequest.getSession().getAttribute("webSiteId"))) { + httpRequest.getSession().setAttribute("webSiteId", WebSiteWorker.getWebSiteId(httpRequest)); + } + + // set the filesystem path of context root. + httpRequest.setAttribute("_CONTEXT_ROOT_", config.getServletContext().getRealPath("/")); + + // set the server root url + String serverRootUrl = UtilHttp.getServerRootUrl(httpRequest); + httpRequest.setAttribute("_SERVER_ROOT_URL_", serverRootUrl); + + // request attributes from redirect call + String reqAttrMapHex = (String) httpRequest.getSession().getAttribute("_REQ_ATTR_MAP_"); + if (UtilValidate.isNotEmpty(reqAttrMapHex)) { + byte[] reqAttrMapBytes = StringUtil.fromHexString(reqAttrMapHex); + Map reqAttrMap = checkMap(UtilObject.getObject(reqAttrMapBytes), String.class, Object.class); + if (reqAttrMap != null) { + for (Map.Entry entry : reqAttrMap.entrySet()) { + httpRequest.setAttribute(entry.getKey(), entry.getValue()); + } + } + httpRequest.getSession().removeAttribute("_REQ_ATTR_MAP_"); + } + + // ----- Context Security ----- + // check if we are disabled + String disableSecurity = config.getInitParameter("disableContextSecurity"); + if (disableSecurity != null && "Y".equalsIgnoreCase(disableSecurity)) { + chain.doFilter(httpRequest, httpResponse); + return; + } + + // check if we are told to redirect everthing + String redirectAllTo = config.getInitParameter("forceRedirectAll"); + if (UtilValidate.isNotEmpty(redirectAllTo)) { + // little trick here so we don't loop on ourself + if (httpRequest.getSession().getAttribute("_FORCE_REDIRECT_") == null) { + httpRequest.getSession().setAttribute("_FORCE_REDIRECT_", "true"); + Debug.logWarning("Redirecting user to: " + redirectAllTo, module); + + if (!redirectAllTo.toLowerCase().startsWith("http")) { + redirectAllTo = httpRequest.getContextPath() + redirectAllTo; + } + httpResponse.sendRedirect(redirectAllTo); + return; + } else { + httpRequest.getSession().removeAttribute("_FORCE_REDIRECT_"); + chain.doFilter(httpRequest, httpResponse); + return; + } + } + + // test to see if we have come through the control servlet already, if not do the processing + String requestPath = null; + String contextUri = null; + if (httpRequest.getAttribute(ContextFilter.FORWARDED_FROM_SERVLET) == null) { + // Debug.logInfo("In ContextFilter.doFilter, FORWARDED_FROM_SERVLET is NOT set", module); + String allowedPath = config.getInitParameter("allowedPaths"); + String redirectPath = config.getInitParameter("redirectPath"); + String errorCode = config.getInitParameter("errorCode"); + + List allowList = StringUtil.split(allowedPath, ":"); + + if (debug) Debug.logInfo("[Domain]: " + httpRequest.getServerName() + " [Request]: " + httpRequest.getRequestURI(), module); + + requestPath = httpRequest.getServletPath(); + if (requestPath == null) requestPath = ""; + if (requestPath.lastIndexOf("/") > 0) { + if (requestPath.indexOf("/") == 0) { + requestPath = "/" + requestPath.substring(1, requestPath.indexOf("/", 1)); + } else { + requestPath = requestPath.substring(1, requestPath.indexOf("/")); + } + } + + String requestInfo = httpRequest.getServletPath(); + if (requestInfo == null) requestInfo = ""; + if (requestInfo.lastIndexOf("/") >= 0) { + requestInfo = requestInfo.substring(0, requestInfo.lastIndexOf("/")) + "/*"; + } + + StringBuilder contextUriBuffer = new StringBuilder(); + if (httpRequest.getContextPath() != null) { + contextUriBuffer.append(httpRequest.getContextPath()); + } + if (httpRequest.getServletPath() != null) { + contextUriBuffer.append(httpRequest.getServletPath()); + } + if (httpRequest.getPathInfo() != null) { + contextUriBuffer.append(httpRequest.getPathInfo()); + } + contextUri = contextUriBuffer.toString(); + + List pathItemList = StringUtil.split(httpRequest.getPathInfo(), "/"); + String viewName = ""; + if (pathItemList != null) { + viewName = pathItemList.get(0); + } + + String requestUri = UtilHttp.getRequestUriFromTarget(httpRequest.getRequestURI()); + + // Verbose Debugging + if (Debug.verboseOn()) { + for (String allow : allowList) { + Debug.logVerbose("[Allow]: " + allow, module); + } + Debug.logVerbose("[View Name]: " + viewName, module); + Debug.logVerbose("[Request Uri]: " + requestUri, module); + Debug.logVerbose("[Request path]: " + requestPath, module); + Debug.logVerbose("[Request info]: " + requestInfo, module); + Debug.logVerbose("[Servlet path]: " + httpRequest.getServletPath(), module); + Debug.logVerbose( + "[Not In AllowList]: " + (!allowList.contains(requestPath) && !allowList.contains(requestInfo) && !allowList.contains(httpRequest.getServletPath()) && !allowList.contains(requestUri) && !allowList.contains("/" + viewName)), + module); + Debug.logVerbose("[Not In controller]: " + (UtilValidate.isEmpty(requestPath) && UtilValidate.isEmpty(httpRequest.getServletPath()) && !uris.contains(viewName)), + module); + } + + // check to make sure the requested url is allowed + if (!allowList.contains(requestPath) && !allowList.contains(requestInfo) && !allowList.contains(httpRequest.getServletPath()) + && !allowList.contains(requestUri) && !allowList.contains("/" + viewName) + && (UtilValidate.isEmpty(requestPath) && UtilValidate.isEmpty(httpRequest.getServletPath()) && !uris.contains(viewName))) { + String filterMessage = "[Filtered request]: " + contextUri; + + if (redirectPath == null) { + if (UtilValidate.isEmpty(viewName)) { + // redirect without any url change in browser + RequestDispatcher rd = request.getRequestDispatcher(SeoControlServlet.defaultPage); + rd.forward(request, response); + } else { + int error = 404; + if (UtilValidate.isNotEmpty(errorCode)) { + try { + error = Integer.parseInt(errorCode); + } catch (NumberFormatException nfe) { + Debug.logWarning(nfe, "Error code specified would not parse to Integer : " + errorCode, module); + } + } + filterMessage = filterMessage + " (" + error + ")"; + httpResponse.sendError(error, contextUri); + request.setAttribute("filterRequestUriError", contextUri); + } + } else { + filterMessage = filterMessage + " (" + redirectPath + ")"; + if (!redirectPath.toLowerCase().startsWith("http")) { + redirectPath = httpRequest.getContextPath() + redirectPath; + } + // httpResponse.sendRedirect(redirectPath); + if (uri.equals("") || uri.equals("/")) { + // redirect without any url change in browser + RequestDispatcher rd = request.getRequestDispatcher(redirectPath); + rd.forward(request, response); + } else { + // redirect with url change in browser + httpResponse.setStatus(SeoConfigUtil.DEFAULT_RESPONSECODE); + httpResponse.setHeader("Location", redirectPath); + } + } + Debug.logWarning(filterMessage, module); + return; + } else if ((allowList.contains(requestPath) || allowList.contains(requestInfo) || allowList.contains(httpRequest.getServletPath()) + || allowList.contains(requestUri) || allowList.contains("/" + viewName)) + && !WebServlets.contains(httpRequest.getServletPath())) { + request.setAttribute(SeoControlServlet.REQUEST_IN_ALLOW_LIST, Boolean.TRUE); + } + } + + // check if multi tenant is enabled + String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant"); + if ("Y".equals(useMultitenant)) { + // get tenant delegator by domain name + String serverName = httpRequest.getServerName(); + try { + // if tenant was specified, replace delegator with the new per-tenant delegator and set tenantId to session attribute + Delegator delegator = getDelegator(config.getServletContext()); + List tenants = delegator.findList("Tenant", EntityCondition.makeCondition("domainName", serverName), null, UtilMisc.toList("-createdStamp"), null, false); + if (UtilValidate.isNotEmpty(tenants)) { + GenericValue tenant = EntityUtil.getFirst(tenants); + String tenantId = tenant.getString("tenantId"); + + // if the request path is a root mount then redirect to the initial path + if (UtilValidate.isNotEmpty(requestPath) && requestPath.equals(contextUri)) { + String initialPath = tenant.getString("initialPath"); + if (UtilValidate.isNotEmpty(initialPath) && !"/".equals(initialPath)) { + ((HttpServletResponse) response).sendRedirect(initialPath); + return; + } + } + + // make that tenant active, setup a new delegator and a new dispatcher + String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId; + httpRequest.getSession().setAttribute("delegatorName", tenantDelegatorName); + + // after this line the delegator is replaced with the new per-tenant delegator + delegator = DelegatorFactory.getDelegator(tenantDelegatorName); + config.getServletContext().setAttribute("delegator", delegator); + + // clear web context objects + config.getServletContext().setAttribute("security", null); + config.getServletContext().setAttribute("dispatcher", null); + + // initialize security + Security security = getSecurity(); + // initialize the services dispatcher + LocalDispatcher dispatcher = getDispatcher(config.getServletContext()); + + // set web context objects + request.setAttribute("dispatcher", dispatcher); + request.setAttribute("security", security); + + request.setAttribute("tenantId", tenantId); + } + + // NOTE DEJ20101130: do NOT always put the delegator name in the user's session because the user may + // have logged in and specified a tenant, and even if no Tenant record with a matching domainName field + // is found this will change the user's delegator back to the base one instead of the one for the + // tenant specified on login + // httpRequest.getSession().setAttribute("delegatorName", delegator.getDelegatorName()); + } catch (GenericEntityException e) { + Debug.logWarning(e, "Unable to get Tenant", module); + } + } + + // we're done checking; continue on + chain.doFilter(httpRequest, httpResponse); + } + + /** + * Forward a uri according to forward pattern regular expressions. Note: this is developed for Filter usage. + * + * @param uri String to reverse transform + * @return String + */ + protected static boolean forwardUri(HttpServletResponse response, String uri) { + Perl5Matcher matcher = new Perl5Matcher(); + boolean foundMatch = false; + Integer responseCodeInt = null; + + if (SeoConfigUtil.checkUseUrlRegexp() && SeoConfigUtil.getSeoPatterns() != null && SeoConfigUtil.getForwardReplacements() != null) { + Iterator keys = SeoConfigUtil.getSeoPatterns().keySet().iterator(); + while (keys.hasNext()) { + String key = keys.next(); + Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key); + String replacement = SeoConfigUtil.getForwardReplacements().get(key); + if (matcher.matches(uri, pattern)) { + for (int i = matcher.getMatch().groups(); i > 0; i--) { + replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i)); + } + uri = replacement; + responseCodeInt = SeoConfigUtil.getForwardResponseCodes().get(key); + foundMatch = true; + // be careful, we don't break after finding a match + } + } + } + + if (foundMatch) { + if (responseCodeInt == null) { + response.setStatus(SeoConfigUtil.DEFAULT_RESPONSECODE); + } else { + response.setStatus(responseCodeInt.intValue()); + } + response.setHeader("Location", uri); + } else { + Debug.logInfo("Can NOT forward this url: " + uri, module); + } + return foundMatch; + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoControlServlet.java b/applications/product/src/org/ofbiz/product/category/SeoControlServlet.java new file mode 100644 index 0000000000..817ed1a406 --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoControlServlet.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import java.io.IOException; +import java.net.URLEncoder; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.catalina.servlets.DefaultServlet; +import org.apache.jasper.servlet.JspServlet; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.webapp.control.ControlServlet; + +/** + * SeoControlServlet.java - SEO Master servlet for the web application. + */ +@SuppressWarnings("serial") +public class SeoControlServlet extends ControlServlet { + + public static final String module = SeoControlServlet.class.getName(); + + protected static String defaultPage = null; + protected static String controlServlet = null; + + public static final String REQUEST_IN_ALLOW_LIST = "_REQUEST_IN_ALLOW_LIST_"; + + public SeoControlServlet() { + super(); + } + + /** + * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + + ServletContext context = this.getServletContext(); + if (UtilValidate.isEmpty(defaultPage)) { + defaultPage = context.getInitParameter("defaultPage"); + } + if (UtilValidate.isEmpty(defaultPage)) { + defaultPage = "/main"; + } + + if (defaultPage.startsWith("/") && defaultPage.lastIndexOf("/") > 0) { + controlServlet = defaultPage.substring(1); + controlServlet = controlServlet.substring(0, controlServlet.indexOf("/")); + } + + SeoConfigUtil.init(); + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String uri = URLEncoder.encode(request.getRequestURI(), "UTF-8"); + if (request.getAttribute(REQUEST_IN_ALLOW_LIST) != null || request.getAttribute("_jsp_" + uri) != null) { + if (request.getRequestURI().toLowerCase().endsWith(".jsp") || request.getRequestURI().toLowerCase().endsWith(".jspx") ) { + JspServlet jspServlet = new JspServlet(); + jspServlet.init(this.getServletConfig()); + jspServlet.service(request, response); + } else { + DefaultServlet defaultServlet = new DefaultServlet(); + defaultServlet.init(this.getServletConfig()); + defaultServlet.service(request, response); + } + return; + } + super.doGet(request, response); + } +} diff --git a/applications/product/src/org/ofbiz/product/category/SeoUrlUtil.java b/applications/product/src/org/ofbiz/product/category/SeoUrlUtil.java new file mode 100644 index 0000000000..66540dcdaa --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/SeoUrlUtil.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import org.ofbiz.base.util.UtilValidate; + +public class SeoUrlUtil { + public static String replaceSpecialCharsUrl(String url) { + if (UtilValidate.isEmpty(url)) { + url = ""; + } + for (String characterPattern : SeoConfigUtil.getCharFilters().keySet()) { + url = url.replaceAll(characterPattern, SeoConfigUtil.getCharFilters().get(characterPattern)); + } + return url; + } + + public static String removeContextPath(String uri, String contextPath) { + if (UtilValidate.isEmpty(contextPath) || UtilValidate.isEmpty(uri)) { + return uri; + } + if (uri.length() > contextPath.length() && uri.startsWith(contextPath)) { + return uri.substring(contextPath.length()); + } + return uri; + } +} diff --git a/applications/product/src/org/ofbiz/product/category/UrlRegexpContextFilter.java b/applications/product/src/org/ofbiz/product/category/UrlRegexpContextFilter.java new file mode 100644 index 0000000000..ed21da36ab --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/UrlRegexpContextFilter.java @@ -0,0 +1,322 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category; + +import static org.ofbiz.base.util.UtilGenerics.checkMap; + +import java.io.IOException; +import java.net.URL; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.FilterChain; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilObject; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.DelegatorFactory; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; +import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.product.category.ftl.UrlRegexpTransform; +import org.ofbiz.security.Security; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.webapp.control.ConfigXMLReader; +import org.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig; +import org.ofbiz.webapp.control.ContextFilter; +import org.ofbiz.webapp.control.WebAppConfigurationException; + +/** + * UrlRegexpContextFilter - Restricts access to raw files and configures servlet objects. + */ +public class UrlRegexpContextFilter extends ContextFilter { + + public static final String module = UrlRegexpContextFilter.class.getName(); + + /** + * @throws GeneralException + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) + */ + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpServletResponse httpResponse = (HttpServletResponse) response; + + String uri = httpRequest.getRequestURI(); + boolean forwarded = UrlRegexpTransform.forwardUri(httpResponse, uri); + if (forwarded) { + return; + } + + URL controllerConfigURL = ConfigXMLReader.getControllerConfigURL(config.getServletContext()); + ControllerConfig controllerConfig = null; + Map requestMaps = null; + try { + controllerConfig = ConfigXMLReader.getControllerConfig(controllerConfigURL); + requestMaps = controllerConfig.getRequestMapMap(); + } catch (WebAppConfigurationException e) { + Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module); + throw new ServletException(e); + } + Set uris = requestMaps.keySet(); + + // NOTE: the following part is copied from org.ofbiz.webapp.control.ContextFilter.doFilter method, please update this if framework is updated. + // Debug.logInfo("Running ContextFilter.doFilter", module); + + // ----- Servlet Object Setup ----- + // set the cached class loader for more speedy running in this thread + + + // set the ServletContext in the request for future use + httpRequest.setAttribute("servletContext", config.getServletContext()); + + // set the webSiteId in the session + if (UtilValidate.isEmpty(httpRequest.getSession().getAttribute("webSiteId"))) { + httpRequest.getSession().setAttribute("webSiteId", config.getServletContext().getAttribute("webSiteId")); + } + + // set the filesystem path of context root. + httpRequest.setAttribute("_CONTEXT_ROOT_", config.getServletContext().getRealPath("/")); + + // set the server root url + String serverRootUrl = UtilHttp.getServerRootUrl(httpRequest); + httpRequest.setAttribute("_SERVER_ROOT_URL_", serverRootUrl); + + // request attributes from redirect call + String reqAttrMapHex = (String) httpRequest.getSession().getAttribute("_REQ_ATTR_MAP_"); + if (UtilValidate.isNotEmpty(reqAttrMapHex)) { + byte[] reqAttrMapBytes = StringUtil.fromHexString(reqAttrMapHex); + Map reqAttrMap = checkMap(UtilObject.getObject(reqAttrMapBytes), String.class, Object.class); + if (reqAttrMap != null) { + for (Map.Entry entry : reqAttrMap.entrySet()) { + httpRequest.setAttribute(entry.getKey(), entry.getValue()); + } + } + httpRequest.getSession().removeAttribute("_REQ_ATTR_MAP_"); + } + + // ----- Context Security ----- + // check if we are disabled + String disableSecurity = config.getInitParameter("disableContextSecurity"); + if (disableSecurity != null && "Y".equalsIgnoreCase(disableSecurity)) { + chain.doFilter(httpRequest, httpResponse); + return; + } + + // check if we are told to redirect everthing + String redirectAllTo = config.getInitParameter("forceRedirectAll"); + if (UtilValidate.isNotEmpty(redirectAllTo)) { + // little trick here so we don't loop on ourself + if (httpRequest.getSession().getAttribute("_FORCE_REDIRECT_") == null) { + httpRequest.getSession().setAttribute("_FORCE_REDIRECT_", "true"); + Debug.logWarning("Redirecting user to: " + redirectAllTo, module); + + if (!redirectAllTo.toLowerCase().startsWith("http")) { + redirectAllTo = httpRequest.getContextPath() + redirectAllTo; + } + httpResponse.sendRedirect(redirectAllTo); + return; + } else { + httpRequest.getSession().removeAttribute("_FORCE_REDIRECT_"); + chain.doFilter(httpRequest, httpResponse); + return; + } + } + + // test to see if we have come through the control servlet already, if not do the processing + String requestPath = null; + String contextUri = null; + if (httpRequest.getAttribute(ContextFilter.FORWARDED_FROM_SERVLET) == null) { + if (debug) Debug.log("[Domain]: " + httpRequest.getServerName() + " [Request]: " + httpRequest.getRequestURI(), module); + + requestPath = httpRequest.getServletPath(); + if (requestPath == null) requestPath = ""; + if (requestPath.lastIndexOf("/") > 0) { + if (requestPath.indexOf("/") == 0) { + requestPath = "/" + requestPath.substring(1, requestPath.indexOf("/", 1)); + } else { + requestPath = requestPath.substring(1, requestPath.indexOf("/")); + } + } + + String requestInfo = httpRequest.getServletPath(); + if (requestInfo == null) requestInfo = ""; + if (requestInfo.lastIndexOf("/") >= 0) { + requestInfo = requestInfo.substring(0, requestInfo.lastIndexOf("/")) + "/*"; + } + + StringBuilder contextUriBuffer = new StringBuilder(); + if (httpRequest.getContextPath() != null) { + contextUriBuffer.append(httpRequest.getContextPath()); + } + if (httpRequest.getServletPath() != null) { + contextUriBuffer.append(httpRequest.getServletPath()); + } + if (httpRequest.getPathInfo() != null) { + contextUriBuffer.append(httpRequest.getPathInfo()); + } + contextUri = contextUriBuffer.toString(); + + List pathItemList = StringUtil.split(httpRequest.getPathInfo(), "/"); + String viewName = ""; + if (pathItemList != null) { + viewName = pathItemList.get(0); + } + + // Debug.logInfo("In ContextFilter.doFilter, FORWARDED_FROM_SERVLET is NOT set", module); + String allowedPath = config.getInitParameter("allowedPaths"); + String redirectPath = config.getInitParameter("redirectPath"); + String errorCode = config.getInitParameter("errorCode"); + + List allowList = StringUtil.split(allowedPath, ":"); + allowList.add("/"); // No path is allowed. + if (UtilValidate.isNotEmpty(httpRequest.getServletPath())) { + allowList.add(""); // No path is allowed if servlet path is not empty. + } + + // Verbose Debugging + if (Debug.verboseOn()) { + for (String allow : allowList) { + Debug.logVerbose("[Allow]: " + allow, module); + } + Debug.logVerbose("[Request path]: " + requestPath, module); + Debug.logVerbose("[Request info]: " + requestInfo, module); + Debug.logVerbose("[Servlet path]: " + httpRequest.getServletPath(), module); + Debug.logVerbose("[View name]: " + viewName, module); + Debug.logVerbose("[Not In AllowList]: " + (!allowList.contains(requestPath) && !allowList.contains(requestInfo) && !allowList.contains(httpRequest.getServletPath())), module); + Debug.logVerbose("[Not In controller]: " + (UtilValidate.isEmpty(requestPath) && UtilValidate.isEmpty(httpRequest.getServletPath()) && !uris.contains(viewName)), module); + } + + // check to make sure the requested url is allowed + if (!allowList.contains(requestPath) && !allowList.contains(requestInfo) && !allowList.contains(httpRequest.getServletPath()) + && (UtilValidate.isEmpty(requestPath) && UtilValidate.isEmpty(httpRequest.getServletPath()) && !uris.contains(viewName))) { + String filterMessage = "[Filtered request]: " + contextUri; + + if (redirectPath == null) { + int error = 404; + if (UtilValidate.isNotEmpty(errorCode)) { + try { + error = Integer.parseInt(errorCode); + } catch (NumberFormatException nfe) { + Debug.logWarning(nfe, "Error code specified would not parse to Integer : " + errorCode, module); + } + } + filterMessage = filterMessage + " (" + error + ")"; + httpResponse.sendError(error, contextUri); + } else { + filterMessage = filterMessage + " (" + redirectPath + ")"; + if (!redirectPath.toLowerCase().startsWith("http")) { + redirectPath = httpRequest.getContextPath() + redirectPath; + } + // httpResponse.sendRedirect(redirectPath); + if (uri.equals("") || uri.equals("/")) { + // redirect without any url change in browser + RequestDispatcher rd = request.getRequestDispatcher(redirectPath); + rd.forward(request, response); + } else { + // redirect with url change in browser + httpResponse.setStatus(SeoConfigUtil.DEFAULT_RESPONSECODE); + httpResponse.setHeader("Location", redirectPath); + } + } + Debug.logWarning(filterMessage, module); + return; + } + } + + // check if multi tenant is enabled + String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant"); + if ("Y".equals(useMultitenant)) { + // get tenant delegator by domain name + String serverName = httpRequest.getServerName(); + try { + // if tenant was specified, replace delegator with the new per-tenant delegator and set tenantId to session attribute + Delegator delegator = getDelegator(config.getServletContext()); + List tenants = delegator.findList("Tenant", EntityCondition.makeCondition("domainName", serverName), null, UtilMisc.toList("-createdStamp"), null, false); + if (UtilValidate.isNotEmpty(tenants)) { + GenericValue tenant = EntityUtil.getFirst(tenants); + String tenantId = tenant.getString("tenantId"); + + // if the request path is a root mount then redirect to the initial path + if (UtilValidate.isNotEmpty(requestPath) && requestPath.equals(contextUri)) { + String initialPath = tenant.getString("initialPath"); + if (UtilValidate.isNotEmpty(initialPath) && !"/".equals(initialPath)) { + ((HttpServletResponse) response).sendRedirect(initialPath); + return; + } + } + + // make that tenant active, setup a new delegator and a new dispatcher + String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId; + httpRequest.getSession().setAttribute("delegatorName", tenantDelegatorName); + + // after this line the delegator is replaced with the new per-tenant delegator + delegator = DelegatorFactory.getDelegator(tenantDelegatorName); + config.getServletContext().setAttribute("delegator", delegator); + + // clear web context objects + // config.getServletContext().setAttribute("authorization", null); + config.getServletContext().setAttribute("security", null); + config.getServletContext().setAttribute("dispatcher", null); + + // initialize authorizer + // getAuthz(); + // initialize security + Security security = getSecurity(); + // initialize the services dispatcher + LocalDispatcher dispatcher = getDispatcher(config.getServletContext()); + + // set web context objects + httpRequest.getSession().setAttribute("dispatcher", dispatcher); + httpRequest.getSession().setAttribute("security", security); + + httpRequest.setAttribute("tenantId", tenantId); + } + + // NOTE DEJ20101130: do NOT always put the delegator name in the user's session because the user may + // have logged in and specified a tenant, and even if no Tenant record with a matching domainName field + // is found this will change the user's delegator back to the base one instead of the one for the + // tenant specified on login + // httpRequest.getSession().setAttribute("delegatorName", delegator.getDelegatorName()); + } catch (GenericEntityException e) { + Debug.logWarning(e, "Unable to get Tenant", module); + } + } + + // we're done checking; continue on + chain.doFilter(httpRequest, httpResponse); + + // reset thread local security + // AbstractAuthorization.clearThreadLocal(); + } + +} diff --git a/applications/product/src/org/ofbiz/product/category/ftl/CatalogAltUrlSeoTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/CatalogAltUrlSeoTransform.java new file mode 100644 index 0000000000..0aebcdbff7 --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/ftl/CatalogAltUrlSeoTransform.java @@ -0,0 +1,181 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category.ftl; + +import java.io.IOException; +import java.io.Writer; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.template.FreeMarkerWorker; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.product.category.CatalogUrlFilter; +import org.ofbiz.product.category.CategoryContentWrapper; +import org.ofbiz.product.category.SeoConfigUtil; +import org.ofbiz.product.product.ProductContentWrapper; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.webapp.OfbizUrlBuilder; +import org.ofbiz.webapp.control.WebAppConfigurationException; + +import freemarker.core.Environment; +import freemarker.ext.beans.BeanModel; +import freemarker.ext.beans.NumberModel; +import freemarker.ext.beans.StringModel; +import freemarker.template.SimpleNumber; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; +import freemarker.template.TemplateTransformModel; + +public class CatalogAltUrlSeoTransform implements TemplateTransformModel { + public final static String module = CatalogUrlSeoTransform.class.getName(); + + public String getStringArg(Map args, String key) { + Object o = args.get(key); + if (o instanceof SimpleScalar) { + return ((SimpleScalar) o).getAsString(); + } else if (o instanceof StringModel) { + return ((StringModel) o).getAsString(); + } else if (o instanceof SimpleNumber) { + return ((SimpleNumber) o).getAsNumber().toString(); + } else if (o instanceof NumberModel) { + return ((NumberModel) o).getAsNumber().toString(); + } + return null; + } + + public boolean checkArg(Map args, String key, boolean defaultValue) { + if (!args.containsKey(key)) { + return defaultValue; + } else { + Object o = args.get(key); + if (o instanceof SimpleScalar) { + SimpleScalar s = (SimpleScalar) o; + return "true".equalsIgnoreCase(s.getAsString()); + } + return defaultValue; + } + } + + @Override + public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException { + final StringBuilder buf = new StringBuilder(); + final boolean fullPath = checkArg(args, "fullPath", false); + final boolean secure = checkArg(args, "secure", false); + + return new Writer(out) { + + public void write(char[] cbuf, int off, int len) throws IOException { + buf.append(cbuf, off, len); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + try { + Environment env = Environment.getCurrentEnvironment(); + BeanModel req = (BeanModel) env.getVariable("request"); + String previousCategoryId = getStringArg(args, "previousCategoryId"); + String productCategoryId = getStringArg(args, "productCategoryId"); + String productId = getStringArg(args, "productId"); + String url = ""; + + Object prefix = env.getVariable("urlPrefix"); + String viewSize = getStringArg(args, "viewSize"); + String viewIndex = getStringArg(args, "viewIndex"); + String viewSort = getStringArg(args, "viewSort"); + String searchString = getStringArg(args, "searchString"); + if (req != null) { + HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); + StringBuilder newURL = new StringBuilder(); + if (UtilValidate.isNotEmpty(productId)) { + if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) { + url = CatalogUrlSeoTransform.makeProductUrl(request, productId, productCategoryId, previousCategoryId); + } else { + url = CatalogUrlFilter.makeProductUrl(request, previousCategoryId, productCategoryId, productId); + } + } else { + if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) { + url = CatalogUrlSeoTransform.makeCategoryUrl(request, productCategoryId, previousCategoryId, viewSize, viewIndex, viewSort, searchString); + } else { + url = CatalogUrlFilter.makeCategoryUrl(request, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString); + } + } + // make the link + if (fullPath) { + try { + OfbizUrlBuilder builder = OfbizUrlBuilder.from(request); + builder.buildHostPart(newURL, "", secure); + } catch (WebAppConfigurationException e) { + Debug.logError(e.getMessage(), module); + } + } + newURL.append(url); + out.write(newURL.toString()); + } else if (prefix != null) { + Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env); + LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env); + Locale locale = (Locale) args.get("locale"); + String prefixString = ((StringModel) prefix).getAsString(); + prefixString = prefixString.replaceAll("/", "/"); + String contextPath = prefixString; + int lastSlashIndex = prefixString.lastIndexOf("/"); + if (lastSlashIndex > -1 && lastSlashIndex < prefixString.length()) { + contextPath = prefixString.substring(prefixString.lastIndexOf("/")); + } + if (UtilValidate.isNotEmpty(productId)) { + GenericValue product = delegator.findOne("Product", UtilMisc.toMap("productId", productId), false); + ProductContentWrapper wrapper = new ProductContentWrapper(dispatcher, product, locale, "text/html"); + if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + url = CatalogUrlSeoTransform.makeProductUrl(delegator, wrapper, prefixString, contextPath, productCategoryId, previousCategoryId, productId); + } else { + url = CatalogUrlFilter.makeProductUrl(delegator, wrapper, null, prefixString, previousCategoryId, productCategoryId, + productId); + } + } else { + GenericValue productCategory = delegator.findOne("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryId), false); + CategoryContentWrapper wrapper = new CategoryContentWrapper(dispatcher, productCategory, locale, "text/html"); + if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + url = CatalogUrlSeoTransform.makeCategoryUrl(delegator, wrapper, prefixString, productCategoryId, previousCategoryId, productId, viewSize, viewIndex, viewSort, searchString); + } else { + url = CatalogUrlFilter.makeCategoryUrl(delegator, wrapper, null, prefixString, previousCategoryId, productCategoryId, + productId, viewSize, viewIndex, viewSort, searchString); + } + } + out.write(url.toString()); + } else { + out.write(buf.toString()); + } + } catch (TemplateModelException e) { + throw new IOException(e.getMessage()); + } catch (GenericEntityException e) { + throw new IOException(e.getMessage()); + } + } + }; + } +} diff --git a/applications/product/src/org/ofbiz/product/category/ftl/CatalogUrlSeoTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/CatalogUrlSeoTransform.java new file mode 100644 index 0000000000..ed9c3b0b34 --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/ftl/CatalogUrlSeoTransform.java @@ -0,0 +1,840 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category.ftl; + +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javolution.util.FastList; +import javolution.util.FastMap; + +import org.apache.oro.text.regex.MalformedPatternException; +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.Perl5Compiler; +import org.apache.oro.text.regex.Perl5Matcher; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.StringUtil.StringWrapper; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.common.UrlServletHelper; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; +import org.ofbiz.entity.condition.EntityExpr; +import org.ofbiz.entity.condition.EntityOperator; +import org.ofbiz.product.category.CatalogUrlServlet; +import org.ofbiz.product.category.CategoryContentWrapper; +import org.ofbiz.product.category.CategoryWorker; +import org.ofbiz.product.category.SeoConfigUtil; +import org.ofbiz.product.category.SeoUrlUtil; +import org.ofbiz.product.product.ProductContentWrapper; + +import freemarker.core.Environment; +import freemarker.ext.beans.BeanModel; +import freemarker.ext.beans.StringModel; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; +import freemarker.template.TemplateTransformModel; + +public class CatalogUrlSeoTransform implements TemplateTransformModel { + public final static String module = CatalogUrlSeoTransform.class.getName(); + + private static Map categoryNameIdMap = null; + private static Map categoryIdNameMap = null; + private static boolean categoryMapInitialed = false; + private static final String asciiRegexp = "^[0-9-_a-zA-Z]*$"; + private static Pattern asciiPattern = null; + public static final String URL_HYPHEN = "-"; + + static { + if (!SeoConfigUtil.isInitialed()) { + SeoConfigUtil.init(); + } + try { + Perl5Compiler perlCompiler = new Perl5Compiler(); + asciiPattern = perlCompiler.compile(asciiRegexp, Perl5Compiler.READ_ONLY_MASK); + } catch (MalformedPatternException e1) { + Debug.logWarning(e1, module); + } + } + + public String getStringArg(Map args, String key) { + Object o = args.get(key); + if (o instanceof SimpleScalar) { + return ((SimpleScalar) o).getAsString(); + } else if (o instanceof StringModel) { + return ((StringModel) o).getAsString(); + } + return null; + } + + @Override + public Writer getWriter(final Writer out, final Map args) + throws TemplateModelException, IOException { + final StringBuilder buf = new StringBuilder(); + + return new Writer(out) { + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + buf.append(cbuf, off, len); + } + + @Override + public void flush() throws IOException { + out.flush(); + } + + @Override + public void close() throws IOException { + try { + Environment env = Environment.getCurrentEnvironment(); + BeanModel req = (BeanModel) env.getVariable("request"); + if (req != null) { + String productId = getStringArg(args, "productId"); + String currentCategoryId = getStringArg(args, "currentCategoryId"); + String previousCategoryId = getStringArg(args, "previousCategoryId"); + HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); + + if (!isCategoryMapInitialed()) { + initCategoryMap(request); + } + + String catalogUrl = ""; + if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) { + if (UtilValidate.isEmpty(productId)) { + catalogUrl = makeCategoryUrl(request, currentCategoryId, previousCategoryId, null, null, null, null); + } else { + catalogUrl = makeProductUrl(request, productId, currentCategoryId, previousCategoryId); + } + } else { + catalogUrl = CatalogUrlServlet.makeCatalogUrl(request, productId, currentCategoryId, previousCategoryId); + } + out.write(catalogUrl); + } + } catch (TemplateModelException e) { + throw new IOException(e.getMessage()); + } + } + }; + } + + /** + * Check whether the category map is initialed. + * + * @return a boolean value to indicate whether the category map has been initialized. + */ + public static boolean isCategoryMapInitialed() { + return categoryMapInitialed; + } + + /** + * Get the category name/id map. + * + * @return the category name/id map + */ + public static Map getCategoryNameIdMap() { + return categoryNameIdMap; + } + + /** + * Get the category id/name map. + * + * @return the category id/name map + */ + public static Map getCategoryIdNameMap() { + return categoryIdNameMap; + } + + /** + * Initial category-name/category-id map. + * Note: as a key, the category-name should be: + * 1. ascii + * 2. lower cased and use hyphen between the words. + * If not, the category id will be used. + * + */ + public static synchronized void initCategoryMap(HttpServletRequest request) { + Delegator delegator = (Delegator) request.getAttribute("delegator"); + initCategoryMap(request, delegator); + } + + public static synchronized void initCategoryMap(HttpServletRequest request, Delegator delegator) { + if (SeoConfigUtil.checkCategoryUrl()) { + categoryNameIdMap = FastMap.newInstance(); + categoryIdNameMap = FastMap.newInstance(); + Perl5Matcher matcher = new Perl5Matcher(); + + try { + Collection allCategories = delegator.findList("ProductCategory", null, UtilMisc.toSet("productCategoryId", "categoryName"), null, null, false); + for (GenericValue category : allCategories) { + String categoryName = category.getString("categoryName"); + String categoryNameId = null; + String categoryIdName = null; + String categoryId = category.getString("productCategoryId"); + if (UtilValidate.isNotEmpty(categoryName)) { + categoryName = SeoUrlUtil.replaceSpecialCharsUrl(categoryName.trim()); + if (matcher.matches(categoryName, asciiPattern)) { + categoryIdName = categoryName.replaceAll(" ", URL_HYPHEN); + categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN); + } else { + categoryIdName = categoryId.trim().replaceAll(" ", URL_HYPHEN); + categoryNameId = categoryIdName; + } + } else { + GenericValue productCategory = delegator.findOne("ProductCategory", UtilMisc.toMap("productCategoryId", categoryId), true); + CategoryContentWrapper wrapper = new CategoryContentWrapper(productCategory, request); + StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL"); + if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) { + categoryIdName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString()); + categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN); + } else { + categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN); + categoryIdName = categoryNameId; + } + } + if (categoryNameIdMap.containsKey(categoryNameId)) { + categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN); + categoryIdName = categoryNameId; + } + if (!matcher.matches(categoryNameId, asciiPattern) || categoryNameIdMap.containsKey(categoryNameId)) { + continue; + } + categoryNameIdMap.put(categoryNameId, categoryId); + categoryIdNameMap.put(categoryId, categoryIdName); + } + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + } + categoryMapInitialed = true; + } + + /** + * Make product url according to the configurations. + * + * @return String a catalog url + */ + public static String makeProductUrl(HttpServletRequest request, String productId, String currentCategoryId, String previousCategoryId) { + Delegator delegator = (Delegator) request.getAttribute("delegator"); + if (!isCategoryMapInitialed()) { + initCategoryMap(request); + } + + String contextPath = request.getContextPath(); + StringBuilder urlBuilder = new StringBuilder(); + GenericValue product = null; + urlBuilder.append((request.getSession().getServletContext()).getContextPath()); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + if (UtilValidate.isNotEmpty(productId)) { + try { + product = delegator.findOne("Product", UtilMisc.toMap("productId", productId), true); + } catch (GenericEntityException e) { + Debug.logError(e, "Error looking up product info for productId [" + productId + "]: " + e.toString(), module); + } + } + if (product != null) { + urlBuilder.append(CatalogUrlServlet.PRODUCT_REQUEST + "/"); + } + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + List trail = CategoryWorker.getTrail(request); + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + for (String trailCategoryId: trail) { + if ("TOP".equals(trailCategoryId)) continue; + urlBuilder.append("/"); + urlBuilder.append(trailCategoryId); + } + } else { + if (trail.size() > 1) { + String lastCategoryId = trail.get(trail.size() - 1); + if (!"TOP".equals(lastCategoryId)) { + if (SeoConfigUtil.isCategoryNameEnabled()) { + String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId); + if (UtilValidate.isNotEmpty(categoryName)) { + urlBuilder.append(categoryName); + if (product != null) { + urlBuilder.append(URL_HYPHEN); + } + } + } + } + } + } + } + + if (UtilValidate.isNotEmpty(productId)) { + if (product != null) { + String productName = product.getString("productName"); + productName = SeoUrlUtil.replaceSpecialCharsUrl(productName); + if (UtilValidate.isNotEmpty(productName)) { + urlBuilder.append(productName + URL_HYPHEN); + } else { + ProductContentWrapper wrapper = new ProductContentWrapper(product, request); + StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL"); + if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) { + productName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString()); + if (UtilValidate.isNotEmpty(productName)) { + urlBuilder.append(productName + URL_HYPHEN); + } + } + } + } + try { + //SeoConfigUtil.addSpecialProductId(productId); + urlBuilder.append(productId); + } catch (Exception e) { + urlBuilder.append(productId); + } + } + + if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix()); + } + + return urlBuilder.toString(); + } + + /** + * Make category url according to the configurations. + * + * @return String a category url + */ + public static String makeCategoryUrl(HttpServletRequest request, String currentCategoryId, String previousCategoryId, String viewSize, String viewIndex, String viewSort, String searchString) { + + if (!isCategoryMapInitialed()) { + initCategoryMap(request); + } + + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append((request.getSession().getServletContext()).getContextPath()); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + urlBuilder.append(CatalogUrlServlet.CATEGORY_REQUEST + "/"); + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + List trail = CategoryWorker.getTrail(request); + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + if (trail.size() > 1) { + String lastCategoryId = trail.get(trail.size() - 1); + if (!"TOP".equals(lastCategoryId)) { + String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId); + if (UtilValidate.isNotEmpty(categoryName)) { + urlBuilder.append(categoryName); + urlBuilder.append(URL_HYPHEN); + urlBuilder.append(lastCategoryId.trim().replaceAll(" ", URL_HYPHEN)); + } else { + urlBuilder.append(lastCategoryId.trim().replaceAll(" ", URL_HYPHEN)); + } + } + } + } + + if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix()); + } + + // append view index + if (UtilValidate.isNotEmpty(viewIndex)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewIndex=" + viewIndex + "&"); + } + // append view size + if (UtilValidate.isNotEmpty(viewSize)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewSize=" + viewSize + "&"); + } + // append view sort + if (UtilValidate.isNotEmpty(viewSort)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewSort=" + viewSort + "&"); + } + // append search string + if (UtilValidate.isNotEmpty(searchString)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("searchString=" + searchString + "&"); + } + if (urlBuilder.toString().endsWith("&")) { + return urlBuilder.toString().substring(0, urlBuilder.toString().length()-1); + } + + return urlBuilder.toString(); + } + + /** + * Make product url according to the configurations. + * + * @return String a catalog url + */ + public static String makeProductUrl(String contextPath, List trail, String productId, String productName, String currentCategoryId, String previousCategoryId) { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(contextPath); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + urlBuilder.append(CatalogUrlServlet.CATALOG_URL_MOUNT_POINT); + } else { + urlBuilder.append(CatalogUrlServlet.PRODUCT_REQUEST + "/"); + } + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + for (String trailCategoryId: trail) { + if ("TOP".equals(trailCategoryId)) continue; + urlBuilder.append("/"); + urlBuilder.append(trailCategoryId); + } + } else { + if (trail.size() > 1) { + String lastCategoryId = trail.get(trail.size() - 1); + if (!"TOP".equals(lastCategoryId)) { + if (SeoConfigUtil.isCategoryNameEnabled()) { + String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId); + if (UtilValidate.isNotEmpty(categoryName)) { + urlBuilder.append(categoryName + URL_HYPHEN); + } + } + } + } + } + } + + if (UtilValidate.isNotEmpty(productId)) { + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + urlBuilder.append("/p_"); + } else { + productName = SeoUrlUtil.replaceSpecialCharsUrl(productName); + if (UtilValidate.isNotEmpty(productName)) { + urlBuilder.append(productName + URL_HYPHEN); + } + } + urlBuilder.append(productId); + } + + if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix()); + } + + return urlBuilder.toString(); + } + + /** + * Get a string lower cased and hyphen connected. + * + * @param name a String to be transformed + * @return String nice name + */ + protected static String getNiceName(String name) { + Perl5Matcher matcher = new Perl5Matcher(); + String niceName = null; + if (UtilValidate.isNotEmpty(name)) { + name = name.trim().replaceAll(" ", URL_HYPHEN); + if (UtilValidate.isNotEmpty(name) && matcher.matches(name, asciiPattern)) { + niceName = name; + } + } + return niceName; + } + + public static boolean forwardProductUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator) throws ServletException, IOException { + return forwardProductUri(request, response, delegator, null); + } + + public static boolean forwardProductUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator, String controlServlet) throws ServletException, IOException { + return forwardUri(request, response, delegator, controlServlet); + } + + /** + * Forward a uri according to forward pattern regular expressions. + * + * @param uri + * String to reverse transform + * @return boolean to indicate whether the uri is forwarded. + * @throws IOException + * @throws ServletException + */ + public static boolean forwardUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator, String controlServlet) throws ServletException, IOException { + String pathInfo = request.getRequestURI(); + String contextPath = request.getContextPath(); + if (!isCategoryMapInitialed()) { + initCategoryMap(request, delegator); + } + + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + return false; + } + List pathElements = StringUtil.split(pathInfo, "/"); + if (UtilValidate.isEmpty(pathElements)) { + return false; + } + // remove context path + pathInfo = SeoUrlUtil.removeContextPath(pathInfo, contextPath); + // remove servlet path + pathInfo = SeoUrlUtil.removeContextPath(pathInfo, request.getServletPath()); + if (pathInfo.startsWith("/" + CatalogUrlServlet.CATEGORY_REQUEST + "/")) { + return forwardCategoryUri(request, response, delegator, controlServlet); + } + + String lastPathElement = pathElements.get(pathElements.size() - 1); + String categoryId = null; + String productId = null; + if (UtilValidate.isNotEmpty(lastPathElement)) { + if (UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + if (lastPathElement.endsWith(SeoConfigUtil.getCategoryUrlSuffix())) { + lastPathElement = lastPathElement.substring(0, lastPathElement.length() - SeoConfigUtil.getCategoryUrlSuffix().length()); + } else { + return false; + } + } + if (SeoConfigUtil.isCategoryNameEnabled() || pathInfo.startsWith("/" + CatalogUrlServlet.CATEGORY_REQUEST + "/")) { + for (String categoryName : categoryNameIdMap.keySet()) { + if (lastPathElement.startsWith(categoryName)) { + categoryId = categoryNameIdMap.get(categoryName); + if (!lastPathElement.equals(categoryName)) { + lastPathElement = lastPathElement.substring(categoryName.length() + URL_HYPHEN.length()); + } + break; + } + } + if (UtilValidate.isEmpty(categoryId)) { + categoryId = lastPathElement; + } + } + + if (UtilValidate.isNotEmpty(lastPathElement)) { + List urlElements = StringUtil.split(lastPathElement, URL_HYPHEN); + if (UtilValidate.isEmpty(urlElements)) { + try { + if (delegator.findOne("Product", UtilMisc.toMap("productId", lastPathElement), true) != null) { + productId = lastPathElement; + } + } catch (GenericEntityException e) { + Debug.logError(e, "Error looking up product info for ProductUrl with path info [" + pathInfo + "]: " + e.toString(), module); + } + } else { + int i = urlElements.size() - 1; + String tempProductId = urlElements.get(i); + while (i >= 0) { + try { + List exprs = FastList.newInstance(); + exprs.add(EntityCondition.makeCondition("productId", EntityOperator.EQUALS, lastPathElement)); +// if (SeoConfigUtil.isSpecialProductId(tempProductId)) { +// exprs.add(EntityCondition.makeCondition("productId", EntityOperator.EQUALS, SeoConfigUtil.getSpecialProductId(tempProductId))); +// } else { + exprs.add(EntityCondition.makeCondition("productId", EntityOperator.EQUALS, tempProductId)); +// } + List products = delegator.findList("Product", EntityCondition.makeCondition(exprs, EntityOperator.OR), UtilMisc.toSet("productId", "productName"), null, null, true); + + if (products != null && products.size() > 0) { + if (products.size() == 1) { + productId = products.get(0).getString("productId"); + break; + } else { + productId = tempProductId; + break; + } + } else if (i > 0) { + tempProductId = urlElements.get(i - 1) + URL_HYPHEN + tempProductId; + } + } catch (GenericEntityException e) { + Debug.logError(e, "Error looking up product info for ProductUrl with path info [" + pathInfo + "]: " + e.toString(), module); + } + i--; + } + } + } + } + + if (UtilValidate.isNotEmpty(productId) || UtilValidate.isNotEmpty(categoryId)) { + if (categoryId != null) { + request.setAttribute("productCategoryId", categoryId); + } + + if (productId != null) { + request.setAttribute("product_id", productId); + request.setAttribute("productId", productId); + } + + StringBuilder urlBuilder = new StringBuilder(); + if (UtilValidate.isNotEmpty(controlServlet)) { + urlBuilder.append("/" + controlServlet); + } + urlBuilder.append("/" + (productId != null ? CatalogUrlServlet.PRODUCT_REQUEST : CatalogUrlServlet.CATEGORY_REQUEST)); + UrlServletHelper.setViewQueryParameters(request, urlBuilder); + Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module); + RequestDispatcher rd = request.getRequestDispatcher(urlBuilder.toString()); + rd.forward(request, response); + return true; + } + return false; + } + + /** + * Forward a category uri according to forward pattern regular expressions. + * + * @param uri + * String to reverse transform + * @return String + * @throws IOException + * @throws ServletException + */ + public static boolean forwardCategoryUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator, String controlServlet) throws ServletException, IOException { + String pathInfo = request.getRequestURI(); + String contextPath = request.getContextPath(); + if (!isCategoryMapInitialed()) { + initCategoryMap(request); + } + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + return false; + } + List pathElements = StringUtil.split(pathInfo, "/"); + if (UtilValidate.isEmpty(pathElements)) { + return false; + } + String lastPathElement = pathElements.get(pathElements.size() - 1); + String categoryId = null; + if (UtilValidate.isNotEmpty(lastPathElement)) { + if (UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + if (lastPathElement.endsWith(SeoConfigUtil.getCategoryUrlSuffix())) { + lastPathElement = lastPathElement.substring(0, lastPathElement.length() - SeoConfigUtil.getCategoryUrlSuffix().length()); + } else { + return false; + } + } + for (String categoryName : categoryNameIdMap.keySet()) { + if (lastPathElement.startsWith(categoryName)) { + categoryId = categoryNameIdMap.get(categoryName); + break; + } + } + if (UtilValidate.isEmpty(categoryId)) { + categoryId = lastPathElement.trim(); + } + } + if (UtilValidate.isNotEmpty(categoryId)) { + request.setAttribute("productCategoryId", categoryId); + StringBuilder urlBuilder = new StringBuilder(); + if (UtilValidate.isNotEmpty(controlServlet)) { + urlBuilder.append("/" + controlServlet); + } + urlBuilder.append("/" + CatalogUrlServlet.CATEGORY_REQUEST); + UrlServletHelper.setViewQueryParameters(request, urlBuilder); + Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module); + RequestDispatcher rd = request.getRequestDispatcher(urlBuilder.toString()); + rd.forward(request, response); + return true; + } + return false; + } + + /** + * This is used when building product url in services. + * + * @param delegator + * @param wrapper + * @param prefix + * @param contextPath + * @param productCategoryId + * @param previousCategoryId + * @param productId + * @return + */ + public static String makeProductUrl(Delegator delegator, ProductContentWrapper wrapper, String prefix, String contextPath, String currentCategoryId, String previousCategoryId, + String productId) { + StringBuilder urlBuilder = new StringBuilder(); + GenericValue product = null; + urlBuilder.append(prefix); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + if (UtilValidate.isNotEmpty(productId)) { + try { + product = delegator.findOne("Product", UtilMisc.toMap("productId", productId), true); + } catch (GenericEntityException e) { + Debug.logError(e, "Error looking up product info for productId [" + productId + "]: " + e.toString(), module); + } + } + if (product != null) { + urlBuilder.append(CatalogUrlServlet.PRODUCT_REQUEST + "/"); + } + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + List trail = null; + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) { + for (String trailCategoryId: trail) { + if ("TOP".equals(trailCategoryId)) continue; + urlBuilder.append("/"); + urlBuilder.append(trailCategoryId); + } + } else { + if (trail != null && trail.size() > 1) { + String lastCategoryId = trail.get(trail.size() - 1); + if (!"TOP".equals(lastCategoryId)) { + if (SeoConfigUtil.isCategoryNameEnabled()) { + String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId); + if (UtilValidate.isNotEmpty(categoryName)) { + urlBuilder.append(categoryName); + if (product != null) { + urlBuilder.append(URL_HYPHEN); + } + } + } + } + } + } + } + + if (UtilValidate.isNotEmpty(productId)) { + if (product != null) { + String productName = product.getString("productName"); + productName = SeoUrlUtil.replaceSpecialCharsUrl(productName); + if (UtilValidate.isNotEmpty(productName)) { + urlBuilder.append(productName + URL_HYPHEN); + } else { + StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL"); + if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) { + productName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString()); + if (UtilValidate.isNotEmpty(productName)) { + urlBuilder.append(productName + URL_HYPHEN); + } + } + } + } + try { + //SeoConfigUtil.addSpecialProductId(productId); + urlBuilder.append(productId); + } catch (Exception e) { + urlBuilder.append(productId); + } + } + + if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix()); + } + + return urlBuilder.toString(); + } + + /** + * This is used when building category url in services. + * + * @param delegator + * @param wrapper + * @param prefix + * @param productCategoryId + * @param previousCategoryId + * @param productId + * @param viewSize + * @param viewIndex + * @param viewSort + * @param searchString + * @return + */ + public static String makeCategoryUrl(Delegator delegator, CategoryContentWrapper wrapper, String prefix, + String currentCategoryId, String previousCategoryId, String productId, String viewSize, String viewIndex, + String viewSort, String searchString) { + StringBuilder urlBuilder = new StringBuilder(); + urlBuilder.append(prefix); + if (urlBuilder.charAt(urlBuilder.length() - 1) != '/') { + urlBuilder.append("/"); + } + urlBuilder.append(CatalogUrlServlet.CATEGORY_REQUEST + "/"); + + if (UtilValidate.isNotEmpty(currentCategoryId)) { + List trail = null; + trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId); + if (trail != null && trail.size() > 1) { + String lastCategoryId = trail.get(trail.size() - 1); + if (!"TOP".equals(lastCategoryId)) { + String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId); + if (UtilValidate.isNotEmpty(categoryName)) { + urlBuilder.append(categoryName); + urlBuilder.append(URL_HYPHEN); + urlBuilder.append(lastCategoryId.trim().replaceAll(" ", URL_HYPHEN)); + } else { + urlBuilder.append(lastCategoryId.trim().replaceAll(" ", URL_HYPHEN)); + } + } + } + } + + if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) { + urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix()); + } + + // append view index + if (UtilValidate.isNotEmpty(viewIndex)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewIndex=" + viewIndex + "&"); + } + // append view size + if (UtilValidate.isNotEmpty(viewSize)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewSize=" + viewSize + "&"); + } + // append view sort + if (UtilValidate.isNotEmpty(viewSort)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("viewSort=" + viewSort + "&"); + } + // append search string + if (UtilValidate.isNotEmpty(searchString)) { + if (!urlBuilder.toString().endsWith("?") && !urlBuilder.toString().endsWith("&")) { + urlBuilder.append("?"); + } + urlBuilder.append("searchString=" + searchString + "&"); + } + if (urlBuilder.toString().endsWith("&")) { + return urlBuilder.toString().substring(0, urlBuilder.toString().length()-1); + } + + return urlBuilder.toString(); + } +} diff --git a/applications/product/src/org/ofbiz/product/category/OfbizCatalogAltUrlTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogAltUrlTransform.java similarity index 98% rename from applications/product/src/org/ofbiz/product/category/OfbizCatalogAltUrlTransform.java rename to applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogAltUrlTransform.java index f8f6373ddf..92b6570c19 100644 --- a/applications/product/src/org/ofbiz/product/category/OfbizCatalogAltUrlTransform.java +++ b/applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogAltUrlTransform.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. *******************************************************************************/ -package org.ofbiz.product.category; +package org.ofbiz.product.category.ftl; import java.io.IOException; import java.io.Writer; @@ -25,13 +25,14 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; -import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.template.FreeMarkerWorker; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.util.EntityQuery; +import org.ofbiz.product.category.CatalogUrlFilter; +import org.ofbiz.product.category.CategoryContentWrapper; import org.ofbiz.product.product.ProductContentWrapper; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.webapp.OfbizUrlBuilder; @@ -49,7 +50,6 @@ import freemarker.template.TemplateTransformModel; public class OfbizCatalogAltUrlTransform implements TemplateTransformModel { public final static String module = OfbizCatalogUrlTransform.class.getName(); - @SuppressWarnings("unchecked") public String getStringArg(Map args, String key) { Object o = args.get(key); if (o instanceof SimpleScalar) { @@ -78,7 +78,6 @@ public class OfbizCatalogAltUrlTransform implements TemplateTransformModel { } @Override - @SuppressWarnings("unchecked") public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException { final StringBuilder buf = new StringBuilder(); diff --git a/applications/product/src/org/ofbiz/product/category/OfbizCatalogUrlTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogUrlTransform.java similarity index 96% rename from applications/product/src/org/ofbiz/product/category/OfbizCatalogUrlTransform.java rename to applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogUrlTransform.java index bd9c451e82..a51708ebf8 100644 --- a/applications/product/src/org/ofbiz/product/category/OfbizCatalogUrlTransform.java +++ b/applications/product/src/org/ofbiz/product/category/ftl/OfbizCatalogUrlTransform.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. *******************************************************************************/ -package org.ofbiz.product.category; +package org.ofbiz.product.category.ftl; import java.io.IOException; import java.io.Writer; @@ -24,6 +24,8 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; +import org.ofbiz.product.category.CatalogUrlServlet; + import freemarker.core.Environment; import freemarker.ext.beans.BeanModel; import freemarker.ext.beans.StringModel; @@ -34,7 +36,6 @@ import freemarker.template.TemplateTransformModel; public class OfbizCatalogUrlTransform implements TemplateTransformModel { public final static String module = OfbizCatalogUrlTransform.class.getName(); - @SuppressWarnings("unchecked") public String getStringArg(Map args, String key) { Object o = args.get(key); if (o instanceof SimpleScalar) { @@ -46,7 +47,6 @@ public class OfbizCatalogUrlTransform implements TemplateTransformModel { } @Override - @SuppressWarnings("unchecked") public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException { final StringBuilder buf = new StringBuilder(); return new Writer(out) { diff --git a/applications/product/src/org/ofbiz/product/category/ftl/SeoTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/SeoTransform.java new file mode 100644 index 0000000000..1f780870a8 --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/ftl/SeoTransform.java @@ -0,0 +1,189 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category.ftl; + +import java.io.IOException; +import java.io.Writer; +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; + +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.Perl5Matcher; +import org.ofbiz.base.util.Debug; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.product.category.SeoConfigUtil; +import org.ofbiz.webapp.control.RequestHandler; + +import freemarker.core.Environment; +import freemarker.ext.beans.BeanModel; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateScalarModel; +import freemarker.template.TemplateTransformModel; + +/** + * SeoTransform - Freemarker Transform for URLs (links) + * + */ +public class SeoTransform implements TemplateTransformModel { + + private static final String module = SeoTransform.class.getName(); + + public boolean checkArg(Map args, String key, boolean defaultValue) { + if (!args.containsKey(key)) { + return defaultValue; + } else { + Object o = args.get(key); + if (o instanceof SimpleScalar) { + SimpleScalar s = (SimpleScalar) o; + return "true".equalsIgnoreCase(s.getAsString()); + } + return defaultValue; + } + } + + public Writer getWriter(final Writer out, Map args) { + final StringBuffer buf = new StringBuffer(); + final boolean fullPath = checkArg(args, "fullPath", false); + final boolean secure = checkArg(args, "secure", false); + final boolean encode = checkArg(args, "encode", true); + + return new Writer(out) { + + public void write(char cbuf[], int off, int len) { + buf.append(cbuf, off, len); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + try { + Environment env = Environment.getCurrentEnvironment(); + BeanModel req = (BeanModel) env.getVariable("request"); + BeanModel res = (BeanModel) env.getVariable("response"); + Object prefix = env.getVariable("urlPrefix"); + if (req != null) { + HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); + ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); + HttpServletResponse response = null; + if (res != null) { + response = (HttpServletResponse) res.getWrappedObject(); + } + HttpSession session = request.getSession(); + GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); + + // anonymous shoppers are not logged in + if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) { + userLogin = null; + } + + RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); + out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null)); + } else if (prefix != null) { + if (prefix instanceof TemplateScalarModel) { + TemplateScalarModel s = (TemplateScalarModel) prefix; + String prefixString = s.getAsString(); + String bufString = buf.toString(); + boolean prefixSlash = prefixString.endsWith("/"); + boolean bufSlash = bufString.startsWith("/"); + if (prefixSlash && bufSlash) { + bufString = bufString.substring(1); + } else if (!prefixSlash && !bufSlash) { + bufString = "/" + bufString; + } + out.write(prefixString + bufString); + } + } else { + out.write(buf.toString()); + } + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + }; + } + + /** + * Transform a url according to seo pattern regular expressions. + * + * @param url , String to do the seo transform + * @param isAnon , boolean to indicate whether it's an anonymous visit. + * + * @return String, the transformed url. + */ + public static String seoUrl(String url, boolean isAnon) { + Perl5Matcher matcher = new Perl5Matcher(); + if (SeoConfigUtil.checkUseUrlRegexp() && matcher.matches(url, SeoConfigUtil.getGeneralRegexpPattern())) { + Iterator keys = SeoConfigUtil.getSeoPatterns().keySet().iterator(); + boolean foundMatch = false; + while (keys.hasNext()) { + String key = keys.next(); + Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key); + if (pattern.getPattern().contains(";jsessionid=")) { + if (isAnon) { + if (SeoConfigUtil.isJSessionIdAnonEnabled()) { + continue; + } + } else { + if (SeoConfigUtil.isJSessionIdUserEnabled()) { + continue; + } else { + boolean foundException = false; + for (int i = 0; i < SeoConfigUtil.getUserExceptionPatterns().size(); i++) { + if (matcher.matches(url, SeoConfigUtil.getUserExceptionPatterns().get(i))) { + foundException = true; + break; + } + } + if (foundException) { + continue; + } + } + } + } + String replacement = SeoConfigUtil.getSeoReplacements().get(key); + if (matcher.matches(url, pattern)) { + for (int i = 1; i < matcher.getMatch().groups(); i++) { + replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i)); + } + // break if found any matcher + url = replacement; + foundMatch = true; + break; + } + } + if (!foundMatch) { + Debug.logVerbose("Can NOT find a seo transform pattern for this url: " + url, module); + } + } + return url; + } + + static { + if (!SeoConfigUtil.isInitialed()) { + SeoConfigUtil.init(); + } + } +} diff --git a/applications/product/src/org/ofbiz/product/category/ftl/UrlRegexpTransform.java b/applications/product/src/org/ofbiz/product/category/ftl/UrlRegexpTransform.java new file mode 100644 index 0000000000..58f554716c --- /dev/null +++ b/applications/product/src/org/ofbiz/product/category/ftl/UrlRegexpTransform.java @@ -0,0 +1,231 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.category.ftl; + +import java.io.IOException; +import java.io.Writer; +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; + +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.Perl5Matcher; +import org.ofbiz.base.util.Debug; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.product.category.SeoConfigUtil; +import org.ofbiz.webapp.control.RequestHandler; + +import freemarker.core.Environment; +import freemarker.ext.beans.BeanModel; +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateScalarModel; +import freemarker.template.TemplateTransformModel; + +/** + * UrlRegexpTransform - Freemarker Transform for Products URLs (links) + * + */ +public class UrlRegexpTransform implements TemplateTransformModel { + + private static final String module = UrlRegexpTransform.class.getName(); + + public boolean checkArg(Map args, String key, boolean defaultValue) { + if (!args.containsKey(key)) { + return defaultValue; + } else { + Object o = args.get(key); + if (o instanceof SimpleScalar) { + SimpleScalar s = (SimpleScalar) o; + return "true".equalsIgnoreCase(s.getAsString()); + } + return defaultValue; + } + } + + public Writer getWriter(final Writer out, Map args) { + final StringBuffer buf = new StringBuffer(); + final boolean fullPath = checkArg(args, "fullPath", false); + final boolean secure = checkArg(args, "secure", false); + final boolean encode = checkArg(args, "encode", true); + + return new Writer(out) { + + public void write(char cbuf[], int off, int len) { + buf.append(cbuf, off, len); + } + + public void flush() throws IOException { + out.flush(); + } + + public void close() throws IOException { + try { + Environment env = Environment.getCurrentEnvironment(); + BeanModel req = (BeanModel) env.getVariable("request"); + BeanModel res = (BeanModel) env.getVariable("response"); + Object prefix = env.getVariable("urlPrefix"); + if (req != null) { + HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); + ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); + HttpServletResponse response = null; + if (res != null) { + response = (HttpServletResponse) res.getWrappedObject(); + } + HttpSession session = request.getSession(); + GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); + + // anonymous shoppers are not logged in + if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) { + userLogin = null; + } + + RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); + out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null)); + } else if (prefix != null) { + if (prefix instanceof TemplateScalarModel) { + TemplateScalarModel s = (TemplateScalarModel) prefix; + String prefixString = s.getAsString(); + String bufString = buf.toString(); + boolean prefixSlash = prefixString.endsWith("/"); + boolean bufSlash = bufString.startsWith("/"); + if (prefixSlash && bufSlash) { + bufString = bufString.substring(1); + } else if (!prefixSlash && !bufSlash) { + bufString = "/" + bufString; + } + out.write(prefixString + bufString); + } + } else { + out.write(buf.toString()); + } + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + }; + } + + /** + * Transform a url according to seo pattern regular expressions. + * + * @param url + * , String to do the seo transform + * @param isAnon + * , boolean to indicate whether it's an anonymous visit. + * + * @return String, the transformed url. + */ + public static String seoUrl(String url, boolean isAnon) { + Perl5Matcher matcher = new Perl5Matcher(); + if (SeoConfigUtil.checkUseUrlRegexp() && matcher.matches(url, SeoConfigUtil.getGeneralRegexpPattern())) { + Iterator keys = SeoConfigUtil.getSeoPatterns().keySet().iterator(); + boolean foundMatch = false; + while (keys.hasNext()) { + String key = keys.next(); + Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key); + if (pattern.getPattern().contains(";jsessionid=")) { + if (isAnon) { + if (SeoConfigUtil.isJSessionIdAnonEnabled()) { + continue; + } + } else { + if (SeoConfigUtil.isJSessionIdUserEnabled()) { + continue; + } else { + boolean foundException = false; + for (int i = 0; i < SeoConfigUtil.getUserExceptionPatterns().size(); i++) { + if (matcher.matches(url, SeoConfigUtil.getUserExceptionPatterns().get(i))) { + foundException = true; + break; + } + } + if (foundException) { + continue; + } + } + } + } + String replacement = SeoConfigUtil.getSeoReplacements().get(key); + if (matcher.matches(url, pattern)) { + for (int i = 1; i < matcher.getMatch().groups(); i++) { + replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i)); + } + // break if found any matcher + url = replacement; + foundMatch = true; + break; + } + } + if (!foundMatch) { + Debug.logVerbose("Can NOT find a seo transform pattern for this url: " + url, module); + } + } + return url; + } + + static { + SeoConfigUtil.init(); + } + + /** + * Forward a uri according to forward pattern regular expressions. Note: this is developed for Filter usage. + * + * @param uri + * String to reverse transform + * @return String + */ + public static boolean forwardUri(HttpServletResponse response, String uri) { + Perl5Matcher matcher = new Perl5Matcher(); + boolean foundMatch = false; + Integer responseCodeInt = null; + if (SeoConfigUtil.checkUseUrlRegexp() && SeoConfigUtil.getSeoPatterns() != null && SeoConfigUtil.getForwardReplacements() != null) { + Iterator keys = SeoConfigUtil.getSeoPatterns().keySet().iterator(); + while (keys.hasNext()) { + String key = keys.next(); + Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key); + String replacement = SeoConfigUtil.getForwardReplacements().get(key); + if (matcher.matches(uri, pattern)) { + for (int i = 1; i < matcher.getMatch().groups(); i++) { + replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i)); + } + // break if found any matcher + uri = replacement; + responseCodeInt = SeoConfigUtil.getForwardResponseCodes().get(key); + foundMatch = true; + break; + } + } + } + if (foundMatch) { + if (responseCodeInt == null) { + response.setStatus(SeoConfigUtil.DEFAULT_RESPONSECODE); + } else { + response.setStatus(responseCodeInt.intValue()); + } + response.setHeader("Location", uri); + } else { + Debug.logInfo("Can NOT forward this url: " + uri, module); + } + return foundMatch; + } +} diff --git a/framework/webapp/config/freemarkerTransforms.properties b/framework/webapp/config/freemarkerTransforms.properties index ae3a32a14e..3866359180 100644 --- a/framework/webapp/config/freemarkerTransforms.properties +++ b/framework/webapp/config/freemarkerTransforms.properties @@ -21,7 +21,8 @@ # entries are in the form: key=transform name, property=transform class name -ofbizUrl=org.ofbiz.webapp.ftl.OfbizUrlTransform +#ofbizUrl=org.ofbiz.webapp.ftl.OfbizUrlTransform +ofbizUrl=org.ofbiz.product.category.ftl.UrlRegexpTransform ofbizContentUrl=org.ofbiz.webapp.ftl.OfbizContentTransform ofbizCurrency=org.ofbiz.webapp.ftl.OfbizCurrencyTransform ofbizAmount=org.ofbiz.webapp.ftl.OfbizAmountTransform diff --git a/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java b/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java index 0e0adce298..cc62d08064 100644 --- a/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java +++ b/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java @@ -66,7 +66,7 @@ public final class WebAppUtil { String servletMapping = null; WebXml webXml = getWebXml(webAppInfo); for (ServletDef servletDef : webXml.getServlets().values()) { - if ("org.ofbiz.webapp.control.ControlServlet".equals(servletDef.getServletClass())) { + if ("org.ofbiz.webapp.control.ControlServlet".equals(servletDef.getServletClass()) || "org.ofbiz.product.category.SeoControlServlet".equals(servletDef.getServletClass())) { String servletName = servletDef.getServletName(); // Catalina servlet mappings: key = url-pattern, value = servlet-name. for (Entry entry : webXml.getServletMappings().entrySet()) { diff --git a/specialpurpose/ecommerce/ofbiz-component.xml b/specialpurpose/ecommerce/ofbiz-component.xml index 9ad17bc776..988e9a2748 100644 --- a/specialpurpose/ecommerce/ofbiz-component.xml +++ b/specialpurpose/ecommerce/ofbiz-component.xml @@ -68,5 +68,11 @@ under the License. location="webapp/ecomclone" mount-point="/ecomclone" app-bar-display="false"/> + diff --git a/specialpurpose/ecommerce/src/org/ofbiz/ecommerce/webapp/view/JspViewHandler.java b/specialpurpose/ecommerce/src/org/ofbiz/ecommerce/webapp/view/JspViewHandler.java new file mode 100644 index 0000000000..316cf8e3ea --- /dev/null +++ b/specialpurpose/ecommerce/src/org/ofbiz/ecommerce/webapp/view/JspViewHandler.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.ecommerce.webapp.view; + +import java.io.IOException; +import java.net.URLEncoder; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.jsp.JspException; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.webapp.control.ContextFilter; +import org.ofbiz.webapp.view.AbstractViewHandler; +import org.ofbiz.webapp.view.ViewHandlerException; + +/** + * JspViewHandler - Java Server Pages View Handler + */ +public class JspViewHandler extends AbstractViewHandler { + + public static final String module = JspViewHandler.class.getName(); + + protected ServletContext context; + + public void init(ServletContext context) throws ViewHandlerException { + this.context = context; + } + + public void render(String name, String page, String contentType, String encoding, String info, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { + // some containers call filters on EVERY request, even forwarded ones, + // so let it know that it came from the control servlet + + if (request == null) { + throw new ViewHandlerException("Null HttpServletRequest object"); + } + if (UtilValidate.isEmpty(page)) { + throw new ViewHandlerException("Null or empty source"); + } + + // Debug.logInfo("Requested Page : " + page, module); + // Debug.logInfo("Physical Path : " + context.getRealPath(page)); + + // tell the ContextFilter we are forwarding + request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, Boolean.TRUE); + RequestDispatcher rd = request.getRequestDispatcher(page); + + if (rd == null) { + Debug.logInfo("HttpServletRequest.getRequestDispatcher() failed; trying ServletContext", module); + rd = context.getRequestDispatcher(page); + if (rd == null) { + Debug.logInfo("ServletContext.getRequestDispatcher() failed; trying ServletContext.getNamedDispatcher(\"jsp\")", module); + rd = context.getNamedDispatcher("jsp"); + if (rd == null) { + throw new ViewHandlerException("Source returned a null dispatcher (" + page + ")"); + } + } + } + + try { + if (UtilValidate.isEmpty(request.getServletPath())) { + // no context or filter to service this page, so we have to forward it directly and let SeoControlServlet to resolve it + String uri = URLEncoder.encode(request.getContextPath() + page, "UTF-8"); + request.setAttribute("_jsp_" + uri, Boolean.TRUE); + rd.forward(request, response); + } else { + rd.include(request, response); + } + } catch (IOException ie) { + throw new ViewHandlerException("IO Error in view", ie); + } catch (ServletException e) { + Throwable throwable = e.getRootCause() != null ? e.getRootCause() : e; + + if (throwable instanceof JspException) { + JspException jspe = (JspException) throwable; + + throwable = jspe.getCause() != null ? jspe.getCause() : jspe; + } + Debug.logError(throwable, "ServletException rendering JSP view", module); + throw new ViewHandlerException(e.getMessage(), throwable); + } + } +} diff --git a/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/controller.xml b/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/controller.xml new file mode 100644 index 0000000000..ba0824d154 --- /dev/null +++ b/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/controller.xml @@ -0,0 +1,27 @@ + + + + + + OFBiz: eCommerce SEO Controller Configuration File + + + diff --git a/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/web.xml b/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/web.xml new file mode 100644 index 0000000000..ba92866b13 --- /dev/null +++ b/specialpurpose/ecommerce/webapp/ecomseo/WEB-INF/web.xml @@ -0,0 +1,166 @@ + + + + + + + Open For Business - demostore + Demo Store for the Open For Business Project + + + webSiteId + WebStore + A unique ID used to look up the WebSite entity + + + localDispatcherName + ecommerce + A unique name used to identify/recognize the local dispatcher for the Service Engine + + + entityDelegatorName + default + The Name of the Entity Delegator to use, defined in entityengine.xml + + + mainDecoratorLocation + component://ecommerce/widget/CommonScreens.xml + The location of the main-decorator screen to use for this webapp; referred to as a context variable in screen def XML files. + + + forceHttpSession + true + + Forces the JSESSIONID cookie to be sent via http rather https, helps prevent lost sessions in web apps that + frequently switch between http and https. + + + + defaultPage + /main + Default page uri. Important: please DO add or remove /control to match url-pattern of SeoControlServlet. + + + + + SeoCatalogUrlFilter + SeoCatalogUrlFilter + org.ofbiz.product.category.CatalogUrlSeoFilter + defaultLocaleStringen_US + + + SeoContentUrlFilter + SeoContentUrlFilter + org.ofbiz.product.category.SeoContentUrlFilter + + defaultLocaleString + en_US + + viewRequestViewBlogArticle + + + SeoContextFilter + SeoContextFilter + org.ofbiz.product.category.SeoContextFilter + + disableContextSecurity + N + + + allowedPaths + /error:/control:/products:/select:/index.html:/index.jsp:/robots.txt:/default.html:/default.jsp:/images + + + + SeoCatalogUrlFilter + /* + + + SeoContentUrlFilter + /* + + + SeoContextFilter + /* + + + org.ofbiz.webapp.control.ControlEventListener + + + + + org.ofbiz.order.shoppingcart.CartEventListener + + org.ofbiz.webapp.control.LoginEventListener + + + SeoControlServlet + SeoControlServlet + Main SEO Control Servlet + org.ofbiz.product.category.SeoControlServlet + 1 + + + + SeoCatalogUrlServlet + SeoCatalogUrlServlet + SEO Catalog (Category/Product) URL Servlet + org.ofbiz.product.category.SeoCatalogUrlServlet + 1 + + + + SeoControlServlet + /* + + + + SeoCatalogUrlServlet + /products/* + + + + 60 + + + + index.jsp + index.html + index.htm + + + + 404 + /error/404.jsp + + diff --git a/specialpurpose/ecommerce/webapp/ecomseo/error/error.jsp b/specialpurpose/ecommerce/webapp/ecomseo/error/error.jsp new file mode 100644 index 0000000000..123c910fb3 --- /dev/null +++ b/specialpurpose/ecommerce/webapp/ecomseo/error/error.jsp @@ -0,0 +1,53 @@ +<%-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--%> + +<%@ page import="org.ofbiz.base.util.*" %> + + +Open For Business Message + + + +<% String errorMsg = (String) request.getAttribute("_ERROR_MESSAGE_"); %> + + +
+
+ + + + +
+ + + + + + + +
+
:ERROR MESSAGE:
+
+
<%=UtilFormatOut.replaceString(errorMsg, "\n", "
")%>
+
+
+
+
+ + diff --git a/specialpurpose/ecommerce/webapp/ecomseo/index.jsp b/specialpurpose/ecommerce/webapp/ecomseo/index.jsp new file mode 100644 index 0000000000..0ad9a3f911 --- /dev/null +++ b/specialpurpose/ecommerce/webapp/ecomseo/index.jsp @@ -0,0 +1,20 @@ +<%-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--%> + +<%response.sendRedirect("control/main");%>