Author: hansbak
Date: Wed Dec 7 01:21:56 2011 New Revision: 1211258 URL: http://svn.apache.org/viewvc?rev=1211258&view=rev Log: extend the <@ofbizUrl macro with a new parameter: webSiteId to be able to generate url's for a specific website in generated emails Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java?rev=1211258&r1=1211257&r2=1211258&view=diff ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java Wed Dec 7 01:21:56 2011 @@ -18,15 +18,34 @@ *******************************************************************************/ package org.ofbiz.webapp.ftl; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.Writer; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.ParserConfigurationException; +import org.ofbiz.base.component.ComponentConfig; +import org.ofbiz.base.component.ComponentConfig.WebappInfo; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; import org.ofbiz.webapp.control.RequestHandler; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; import freemarker.core.Environment; import freemarker.ext.beans.BeanModel; @@ -62,6 +81,7 @@ public class OfbizUrlTransform implement final boolean fullPath = checkArg(args, "fullPath", false); final boolean secure = checkArg(args, "secure", false); final boolean encode = checkArg(args, "encode", true); + final String webSiteId = getArg(args, "webSiteId"); return new Writer(out) { @Override @@ -81,7 +101,109 @@ public class OfbizUrlTransform implement BeanModel req = (BeanModel) env.getVariable("request"); BeanModel res = (BeanModel) env.getVariable("response"); Object prefix = env.getVariable("urlPrefix"); - if (req != null) { + if (UtilValidate.isNotEmpty(webSiteId)) { + HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); + Delegator delegator = (Delegator) request.getAttribute("delegator"); + String httpsPort = null; + String httpsServer = null; + String httpPort = null; + String httpServer = null; + Boolean enableHttps = null; + StringBuilder newURL = new StringBuilder(); + // make prefix url + try { + GenericValue webSite = delegator.findByPrimaryKeyCache("WebSite", UtilMisc.toMap("webSiteId", webSiteId)); + if (webSite != null) { + httpsPort = webSite.getString("httpsPort"); + httpsServer = webSite.getString("httpsHost"); + httpPort = webSite.getString("httpPort"); + httpServer = webSite.getString("httpHost"); + enableHttps = webSite.getBoolean("enableHttps"); + } + } catch (GenericEntityException e) { + Debug.logWarning(e, "Problems with WebSite entity; using global defaults", module); + } + // fill in any missing properties with fields from the global file + if (UtilValidate.isEmpty(httpsPort)) { + httpsPort = UtilProperties.getPropertyValue("url.properties", "port.https", "443"); + } + if (UtilValidate.isEmpty(httpsServer)) { + httpsServer = UtilProperties.getPropertyValue("url.properties", "force.https.host"); + } + if (UtilValidate.isEmpty(httpPort)) { + httpPort = UtilProperties.getPropertyValue("url.properties", "port.http", "80"); + } + if (UtilValidate.isEmpty(httpServer)) { + httpServer = UtilProperties.getPropertyValue("url.properties", "force.http.host"); + } + if (enableHttps == null) { + enableHttps = UtilProperties.propertyValueEqualsIgnoreCase("url.properties", "port.https.enabled", "Y"); + } + if (secure && enableHttps) { + String server = httpsServer; + if (UtilValidate.isEmpty(server)) { + server = request.getServerName(); + } + newURL.append("https://"); + newURL.append(httpsServer); + newURL.append(":").append(httpsPort); + } else { + newURL.append("http://"); + newURL.append(httpServer); + if (!"80".equals(httpPort)) { + newURL.append(":").append(httpPort); + } + } + // make mount point + String mountPoint = null; + for (WebappInfo webAppInfo : ComponentConfig.getAllWebappResourceInfos()) { + File file = new File(webAppInfo.getLocation() + "/WEB-INF/web.xml"); + if (!file.exists()) { + continue; + } + InputStream is = new FileInputStream(file); + try { + Document doc = UtilXml.readXmlDocument(is, true, null); + NodeList nList = doc.getElementsByTagName("context-param"); + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + String paramName = getTagValue("param-name",eElement); + String paramValue = getTagValue("param-value",eElement); + if ("webSiteId".equals(paramName) && webSiteId.equals(paramValue)) { + mountPoint = webAppInfo.getContextRoot(); + break; + } + } + } + } catch (SAXException e) { + Debug.logWarning(e, e.getMessage(), module); + } catch (ParserConfigurationException e) { + Debug.logWarning(e, e.getMessage(), module); + } + if (UtilValidate.isNotEmpty(mountPoint)) { + if (mountPoint.length() > 1) newURL.append(mountPoint); + break; + } + } + // make the path the the control servlet + String controlPath = (String) request.getAttribute("_CONTROL_PATH_"); + String[] patch = controlPath.split("/"); + String patchStr = null; + if (patch.length > 0) { + patchStr = patch[patch.length-1]; + } + if (UtilValidate.isNotEmpty(patchStr)) { + newURL.append("/"); + newURL.append(patchStr); + } + newURL.append("/"); + // make requestUrl + String requestUrl = buf.toString(); + newURL.append(requestUrl); + out.write(newURL.toString()); + } else if (req != null) { HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); HttpServletResponse response = null; @@ -117,4 +239,32 @@ public class OfbizUrlTransform implement } }; } + private static String getArg(Map args, String key) { + String result = ""; + Object o = args.get(key); + if (o != null) { + if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); + if (o instanceof TemplateScalarModel) { + TemplateScalarModel s = (TemplateScalarModel) o; + try { + result = s.getAsString(); + } catch (TemplateModelException e) { + Debug.logError(e, "Template Exception", module); + } + } else { + result = o.toString(); + } + } + return result; + } + private static String getTagValue(String sTag, Element eElement){ + String value = ""; + try{ + NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); + Node nValue = (Node) nlList.item(0); + return value = nValue.getNodeValue(); + } catch (Exception e) { + return value; + } + } } |
Free forum by Nabble | Edit this page |