You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Jacques Le Roux <ja...@les7arts.com> on 2013/10/24 19:12:24 UTC
Re: svn commit: r1535432 - in
/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23: ./
framework/base/src/org/ofbiz/base/util/template/
framework/common/src/org/ofbiz/common/email/
framework/security/src/org/ofbiz/security/ framework/webapp/config/ fr
No pb Adrian, was not a big deal ;)
Thanks to care
Jacques
Adrian Crum wrote:
> I apologize for stepping on your work - it wasn't intentional. I still
> have more commits around the URL generation stuff.
>
> Adrian Crum
> Sandglass Software
> www.sandglass-software.com
>
> On 10/24/2013 9:31 AM, jleroux@apache.org wrote:
>> Author: jleroux
>> Date: Thu Oct 24 16:31:49 2013
>> New Revision: 1535432
>>
>> URL: http://svn.apache.org/r1535432
>> Log:
>> Merge trunk head after Adrian's revert, resolving a conflict in RequestHandler.java
>>
>> Modified:
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/ (props changed)
>>
>>
>>
>>
>>
>>
>>
>>
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/common/src/org/ofbiz/common/email/NotificationServices.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/security/src/org/ofbiz/security/SecurityFactory.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/config/freemarkerTransforms.properties
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/OfbizUrlBuilder.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java
>>
>> Propchange: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/
>> ------------------------------------------------------------------------------
>> Merged /ofbiz/trunk:r1535158-1535430
>>
>> Modified:
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>> (original) +++
>>
>>
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>> Thu Oct 24 16:31:49 2013 @@ -450,7 +450,8 @@ public class FreeMarkerWorker { return returnObj; }
>>
>> - public static Object unwrap(Object o) {
>> + @SuppressWarnings("unchecked")
>> + public static <T> T unwrap(Object o) {
>> Object returnObj = null;
>>
>> if (o == TemplateModel.NOTHING) {
>> @@ -461,7 +462,7 @@ public class FreeMarkerWorker {
>> returnObj = ((BeanModel) o).getWrappedObject();
>> }
>>
>> - return returnObj;
>> + return (T) returnObj;
>> }
>>
>> public static void checkForLoop(String path, Map<String, Object> ctx) throws IOException {
>>
>> Modified:
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/common/src/org/ofbiz/common/email/NotificationServices.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/common/src/org/ofbiz/common/email/NotificationServices.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/common/src/org/ofbiz/common/email/NotificationServices.java
>> (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/common/src/org/ofbiz/common/email/NotificationServices.java
>> Thu Oct 24 16:31:49 2013 @@ -21,29 +21,26 @@ package org.ofbiz.common.email; import java.io.IOException; import
>> java.io.StringWriter; import java.io.Writer;
>> -import java.net.InetAddress;
>> import java.net.URL;
>> -import java.net.UnknownHostException;
>> import java.util.Locale;
>> import java.util.Map;
>>
>> import javolution.util.FastMap;
>>
>> +import org.ofbiz.base.component.ComponentConfig.WebappInfo;
>> import org.ofbiz.base.util.Debug;
>> import org.ofbiz.base.util.UtilGenerics;
>> -import org.ofbiz.base.util.UtilMisc;
>> import org.ofbiz.base.util.UtilProperties;
>> import org.ofbiz.base.util.UtilURL;
>> -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.service.DispatchContext;
>> import org.ofbiz.service.GenericServiceException;
>> import org.ofbiz.service.LocalDispatcher;
>> import org.ofbiz.service.ModelService;
>> import org.ofbiz.service.ServiceUtil;
>> +import org.ofbiz.webapp.OfbizUrlBuilder;
>> +import org.ofbiz.webapp.WebAppUtil;
>>
>> import freemarker.template.TemplateException;
>>
>> @@ -255,84 +252,17 @@ public class NotificationServices {
>> public static void setBaseUrl(Delegator delegator, String webSiteId, Map<String, Object> context) {
>> // If the baseUrl was not specified we can do a best effort instead
>> if (!context.containsKey("baseUrl")) {
>> - StringBuilder httpBase = null;
>> - StringBuilder httpsBase = null;
>> -
>> - String localServer = null;
>> -
>> - String httpsPort = null;
>> - String httpsServer = null;
>> - String httpPort = null;
>> - String httpServer = null;
>> - Boolean enableHttps = null;
>> -
>> try {
>> - // using just the IP address of localhost if we don't have a defined server
>> - InetAddress localHost = InetAddress.getLocalHost();
>> - localServer = localHost.getHostAddress();
>> - } catch (UnknownHostException hostException) {
>> - Debug.logWarning(hostException, "Could not determine localhost, using '127.0.0.1'", module);
>> - localServer = "127.0.0.1";
>> - }
>> -
>> - // load the properties from the website entity
>> - GenericValue webSite = null;
>> - if (webSiteId != null) {
>> - try {
>> - webSite = delegator.findOne("WebSite", UtilMisc.toMap("webSiteId", webSiteId), true);
>> - 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", localServer);
>> - }
>> - if (UtilValidate.isEmpty(httpPort)) {
>> - httpPort = UtilProperties.getPropertyValue("url.properties", "port.http", "80");
>> - }
>> - if (UtilValidate.isEmpty(httpServer)) {
>> - httpServer = UtilProperties.getPropertyValue("url.properties", "force.http.host", localServer);
>> - }
>> - if (UtilValidate.isEmpty(enableHttps)) {
>> - enableHttps = (UtilProperties.propertyValueEqualsIgnoreCase("url.properties", "port.https.enabled", "Y")) ?
>> Boolean.TRUE : Boolean.FALSE;
>> - }
>> -
>> - // prepare the (non-secure) URL
>> - httpBase = new StringBuilder("http://");
>> - httpBase.append(httpServer);
>> - if (!"80".equals(httpPort)) {
>> - httpBase.append(":");
>> - httpBase.append(httpPort);
>> - }
>> -
>> - // set the base (non-secure) URL for any messages requiring it
>> - context.put("baseUrl", httpBase.toString());
>> -
>> - if (enableHttps.booleanValue()) {
>> - // prepare the (secure) URL
>> - httpsBase = new StringBuilder("https://");
>> - httpsBase.append(httpsServer);
>> - if (!"443".equals(httpsPort)) {
>> - httpsBase.append(":");
>> - httpsBase.append(httpsPort);
>> - }
>> -
>> - // set the base (secure) URL for any messages requiring it
>> - context.put("baseSecureUrl", httpsBase.toString());
>> - } else {
>> - context.put("baseSecureUrl", httpBase.toString());
>> + WebappInfo webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
>> + OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
>> + StringBuilder newURL = new StringBuilder();
>> + builder.buildHostPart(newURL, "", false);
>> + context.put("baseUrl", newURL.toString());
>> + newURL = new StringBuilder();
>> + builder.buildHostPart(newURL, "", true);
>> + context.put("baseSecureUrl", newURL.toString());
>> + } catch (Exception e) {
>> + Debug.logWarning(e, "Exception thrown while adding baseUrl to context: ", module);
>> }
>> }
>> }
>>
>> Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/security/src/org/ofbiz/security/SecurityFactory.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/security/src/org/ofbiz/security/SecurityFactory.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/security/src/org/ofbiz/security/SecurityFactory.java
>> (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/security/src/org/ofbiz/security/SecurityFactory.java Thu Oct
>> 24 16:31:49 2013 @@ -120,7 +120,7 @@ public final class SecurityFactory {
>>
>> @Override
>> public boolean hasEntityPermission(String entity, String action, GenericValue userLogin) {
>> - if (userLogin == null) return false;
>> + if (userLogin == null || entity == null || action == null) return false;
>> String permission = entity.concat(action);
>> String adminPermission = entity.concat("_ADMIN");
>> Iterator<GenericValue> iterator = findUserLoginSecurityGroupByUserLoginId(userLogin.getString("userLoginId"));
>>
>> Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/config/freemarkerTransforms.properties
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/config/freemarkerTransforms.properties?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/config/freemarkerTransforms.properties (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/config/freemarkerTransforms.properties Thu Oct 24
>> 16:31:49 2013 @@ -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
>>
>> Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/OfbizUrlBuilder.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/OfbizUrlBuilder.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/OfbizUrlBuilder.java (original)
>> +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/OfbizUrlBuilder.java Thu Oct
>> 24 16:31:49 2013 @@ -166,9 +166,6 @@ public final class OfbizUrlBuilder {
>> buffer.append(":").append(webSiteProps.getHttpPort()); }
>> }
>> - if (Debug.warningOn() && requestMap == null) {
>> - Debug.logWarning("The request-map URI '" + requestMapUri + "' was not found in controller.xml", module);
>> - }
>> return makeSecure;
>> }
>>
>>
>> Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/WebAppUtil.java Thu Oct 24
>> 16:31:49 2013 @@ -29,6 +29,7 @@ import org.apache.catalina.deploy.WebXml import org.apache.catalina.startup.DigesterFactory;
>> import org.apache.catalina.startup.WebRuleSet;
>> import org.apache.tomcat.util.digester.Digester;
>> +import org.ofbiz.base.component.ComponentConfig;
>> import org.ofbiz.base.component.ComponentConfig.WebappInfo;
>> import org.ofbiz.base.util.Assert;
>> import org.ofbiz.base.util.Debug;
>> @@ -86,6 +87,24 @@ public final class WebAppUtil {
>> }
>>
>> /**
>> + * Returns the <code>WebappInfo</code> instance associated to the specified web site ID.
>> + * Throws <code>IllegalArgumentException</code> if the web site ID was not found.
>> + *
>> + * @param webSiteId
>> + * @throws IOException
>> + * @throws SAXException
>> + */
>> + public static WebappInfo getWebappInfoFromWebsiteId(String webSiteId) throws IOException, SAXException {
>> + Assert.notNull("webSiteId", webSiteId);
>> + for (WebappInfo webAppInfo : ComponentConfig.getAllWebappResourceInfos()) {
>> + if (webSiteId.equals(WebAppUtil.getWebSiteId(webAppInfo))) {
>> + return webAppInfo;
>> + }
>> + }
>> + throw new IllegalArgumentException("Web site ID '" + webSiteId + "' not found.");
>> + }
>> +
>> + /**
>> * Returns the web site ID - as configured in the web application's <code>web.xml</code> file,
>> * or <code>null</code> if no web site ID was found.
>> *
>>
>> Modified:
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java
>> (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/control/RequestHandler.java Thu
>> Oct 24 16:31:49 2013 @@ -20,7 +20,6 @@ package org.ofbiz.webapp.control;
>>
>> import static org.ofbiz.base.util.UtilGenerics.checkMap;
>>
>> -import java.io.IOException;
>> import java.io.Serializable;
>> import java.io.UnsupportedEncodingException;
>> import java.net.URL;
>> @@ -50,7 +49,6 @@ import org.ofbiz.base.util.UtilValidate;
>> import org.ofbiz.entity.Delegator;
>> import org.ofbiz.entity.GenericEntityException;
>> import org.ofbiz.entity.GenericValue;
>> -import org.ofbiz.webapp.OfbizUrlBuilder;
>> import org.ofbiz.webapp.event.EventFactory;
>> import org.ofbiz.webapp.event.EventHandler;
>> import org.ofbiz.webapp.event.EventHandlerException;
>> @@ -870,10 +868,11 @@ public class RequestHandler {
>> // attempt to override the default view with a call back into the control servlet,
>> // so just get the target view name and use that
>> String servletName = req.getServletPath();
>> - if (UtilValidate.isNotEmpty(servletName) && servletName.length() > 1) {
>> + if (UtilValidate.isNotEmpty(servletName) && servletName.length() > 1
>> + || servletName.startsWith("/")) {
>> servletName = servletName.substring(1);
>> }
>> -
>> +
>> if (Debug.infoOn()) Debug.logInfo("Rendering View [" + view + "], sessionId=" + UtilHttp.getSessionId(req), module);
>> if (view.startsWith(servletName + "/")) {
>> view = view.substring(servletName.length() + 1);
>> @@ -1011,6 +1010,15 @@ public class RequestHandler {
>> }
>> }
>>
>> + /**
>> + * Returns a URL String that contains only the scheme and host parts. This method
>> + * should not be used because it ignores settings in the WebSite entity.
>> + *
>> + * @param request
>> + * @param secure
>> + * @deprecated Use OfbizUrlBuilder
>> + */
>> + @Deprecated
>> public static String getDefaultServerRootUrl(HttpServletRequest request, boolean secure) {
>> String httpsPort = UtilProperties.getPropertyValue("url.properties", "port.https", "443");
>> String httpsServer = UtilProperties.getPropertyValue("url.properties", "force.https.host");
>> @@ -1117,40 +1125,55 @@ public class RequestHandler {
>> }
>>
>> public String makeLink(HttpServletRequest request, HttpServletResponse response, String url, boolean fullPath, boolean
>> secure, boolean encode) {
>> - OfbizUrlBuilder builder = (OfbizUrlBuilder) request.getAttribute("_OFBIZ_URL_BUILDER_");
>> - if (builder == null) {
>> + WebSiteProperties webSiteProps = (WebSiteProperties) request.getAttribute("_WEBSITE_PROPS_");
>> + if (webSiteProps == null) {
>> try {
>> - builder = OfbizUrlBuilder.from(request);
>> - request.setAttribute("_OFBIZ_URL_BUILDER_", builder);
>> + webSiteProps = WebSiteProperties.from(request);
>> + request.setAttribute("_WEBSITE_PROPS_", webSiteProps);
>> } catch (GenericEntityException e) {
>> // If the entity engine is throwing exceptions, then there is no point in continuing.
>> Debug.logError(e, "Exception thrown while getting web site properties: ", module);
>> return null;
>> + }
>> + }
>> + String requestUri = RequestHandler.getRequestUri(url);
>> + ConfigXMLReader.RequestMap requestMap = null;
>> + if (requestUri != null) {
>> + try {
>> + requestMap = getControllerConfig().getRequestMapMap().get(requestUri);
>> } catch (WebAppConfigurationException e) {
>> // If we can't read the controller.xml file, then there is no point in continuing.
>> Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
>> return null;
>> }
>> }
>> + StringBuilder newURL = new StringBuilder();
>> boolean didFullSecure = false;
>> boolean didFullStandard = false;
>> - StringBuilder newURL = new StringBuilder(250);
>> - if (fullPath) {
>> - try {
>> - boolean usesHttps = builder.buildHostPart(newURL, url, secure);
>> - if (usesHttps) {
>> - didFullSecure = true;
>> - } else {
>> - didFullStandard = true;
>> + if (requestMap != null && (webSiteProps.getEnableHttps() || fullPath || secure)) {
>> + if (Debug.verboseOn()) Debug.logVerbose("In makeLink requestUri=" + requestUri, module);
>> + if (secure || (webSiteProps.getEnableHttps() && requestMap.securityHttps && !request.isSecure())) {
>> + String server = webSiteProps.getHttpsHost();
>> + if (server.isEmpty()) {
>> + server = request.getServerName();
>> + }
>> + newURL.append("https://");
>> + newURL.append(server);
>> + if (!webSiteProps.getHttpsPort().isEmpty()) {
>> + newURL.append(":").append(webSiteProps.getHttpsPort());
>> + }
>> + didFullSecure = true;
>> + } else if (fullPath || (webSiteProps.getEnableHttps() && !requestMap.securityHttps && request.isSecure())) {
>> + String server = webSiteProps.getHttpHost();
>> + if (server.isEmpty()) {
>> + server = request.getServerName();
>> + }
>> + newURL.append("http://");
>> + newURL.append(server);
>> + if (!webSiteProps.getHttpPort().isEmpty()) {
>> + newURL.append(":").append(webSiteProps.getHttpPort());
>> }
>> - } catch (WebAppConfigurationException e) {
>> - // If we can't read the controller.xml file, then there is no point in continuing.
>> - Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
>> - return null;
>> - } catch (IOException e) {
>> - // If we can't write to the buffer, then there is no point in continuing.
>> - Debug.logError(e, "Exception thrown while appending to StringBuilder: ", module);
>> - return null;
>> + didFullStandard = true;
>> }
>> }
>> // create the path to the control servlet
>>
>> Modified:
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java?rev=1535432&r1=1535431&r2=1535432&view=diff
>> ============================================================================== ---
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java
>> (original) +++
>> ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/webapp/src/org/ofbiz/webapp/ftl/OfbizUrlTransform.java Thu
>> Oct 24 16:31:49 2013 @@ -18,253 +18,153 @@ *******************************************************************************/
>> 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.net.URLEncoder;
>> 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.base.util.template.FreeMarkerWorker;
>> import org.ofbiz.entity.Delegator;
>> -import org.ofbiz.entity.GenericEntityException;
>> -import org.ofbiz.entity.GenericValue;
>> +import org.ofbiz.webapp.OfbizUrlBuilder;
>> +import org.ofbiz.webapp.WebAppUtil;
>> 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;
>> import freemarker.template.SimpleScalar;
>> import freemarker.template.TemplateModelException;
>> import freemarker.template.TemplateScalarModel;
>> import freemarker.template.TemplateTransformModel;
>>
>> /**
>> - * OfbizUrlTransform - Freemarker Transform for URLs (links)
>> + * Freemarker Transform for creating OFBiz URLs (links).
>> + * <p>This transform accepts several arguments:<br>
>> + * <ul>
>> + * <li><b>fullPath</b> (true/false) - generate a full URL including scheme and host, defaults to false.</li>
>> + * <li><b>secure</b> (true/false) - generate a secure (https) URL, defaults to false. Server settings will
>> + * override this argument.</li>
>> + * <li><b>encode</b> (true/false) - encode the URL, defaults to true. Encoding is UTF-8.</li>
>> + * <li><b>webSiteId</b> - generate a full URL using the web site settings found in the WebSite entity.</li>
>> + * </ul></p>
>> + * <p>In addition, this transform accepts an environment variable - <b>urlPrefix</b>. If the variable
>> + * exists, it is prepended to the contents of the transform (the part between
>> + * <code><@ofbizUrl></code> and <code></@ofbizUrl></code>), and all transform arguments are
>> + * ignored.</p>
>> + *
>> */
>> public class OfbizUrlTransform implements TemplateTransformModel {
>>
>> public final static String module = OfbizUrlTransform.class.getName();
>>
>> - @SuppressWarnings("unchecked")
>> - 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());
>> + @SuppressWarnings("rawtypes")
>> + private static boolean checkBooleanArg(Map args, String key, boolean defaultValue) {
>> + Object o = args.get(key);
>> + if (o instanceof SimpleScalar) {
>> + SimpleScalar s = (SimpleScalar) o;
>> + return "true".equalsIgnoreCase(s.getAsString());
>> + }
>> + return defaultValue;
>> + }
>> +
>> + private static String convertToString(Object o) {
>> + String result = "";
>> + if (o != null) {
>> + if (Debug.verboseOn())
>> + Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
>> + if (o instanceof TemplateScalarModel) {
>> + TemplateScalarModel s = (TemplateScalarModel) o;
>> + try {
>> + result = s.getAsString();
>> + } catch (TemplateModelException e) {
>> + Debug.logError(e, "Template Exception", module);
>> + }
>> + } else {
>> + result = o.toString();
>> }
>> - return defaultValue;
>> }
>> + return result;
>> }
>>
>> - @SuppressWarnings("unchecked")
>> + @Override
>> + @SuppressWarnings("rawtypes")
>> public Writer getWriter(final Writer out, Map args) {
>> final StringBuilder buf = new StringBuilder();
>> - 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");
>> + final boolean fullPath = checkBooleanArg(args, "fullPath", false);
>> + final boolean secure = checkBooleanArg(args, "secure", false);
>> + final boolean encode = checkBooleanArg(args, "encode", true);
>> + final String webSiteId = convertToString(args.get("webSiteId"));
>>
>> return new Writer(out) {
>> - @Override
>> - public void write(char cbuf[], int off, int len) {
>> - 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");
>> - BeanModel res = (BeanModel) env.getVariable("response");
>> - Object prefix = env.getVariable("urlPrefix");
>> - 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.findOne("WebSite", UtilMisc.toMap("webSiteId", webSiteId), true);
>> - 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");
>> + // Handle prefix.
>> + String prefixString = convertToString(env.getVariable("urlPrefix"));
>> + if (!prefixString.isEmpty()) {
>> + 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;
>> }
>> - 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);
>> + out.write(prefixString + bufString);
>> + return;
>> + }
>> + HttpServletRequest request = FreeMarkerWorker.unwrap(env.getVariable("request"));
>> + // Handle web site ID.
>> + if (!webSiteId.isEmpty()) {
>> + Delegator delegator = FreeMarkerWorker.unwrap(env.getVariable("delegator"));
>> + if (request != null && delegator == null) {
>> + delegator = (Delegator) request.getAttribute("delegator");
>> + }
>> + if (delegator == null) {
>> + throw new IllegalStateException("Delegator not found");
>> + }
>> + WebappInfo webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
>> + StringBuilder newUrlBuff = new StringBuilder(250);
>> + OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
>> + builder.buildFullUrl(newUrlBuff, buf.toString(), secure);
>> + String newUrl = newUrlBuff.toString();
>> + if (encode) {
>> + newUrl = URLEncoder.encode(newUrl, "UTF-8");
>> }
>> - newURL.append("/");
>> - // make requestUrl
>> - String requestUrl = buf.toString();
>> - newURL.append(requestUrl);
>> - out.write(newURL.toString());
>> - } else if (req != null) {
>> - HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
>> + out.write(newUrl);
>> + return;
>> + }
>> + if (request != null) {
>> ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
>> - HttpServletResponse response = null;
>> - if (res != null) {
>> - response = (HttpServletResponse) res.getWrappedObject();
>> - }
>> -
>> + HttpServletResponse response = FreeMarkerWorker.unwrap(env.getVariable("response"));
>> String requestUrl = buf.toString();
>> -
>> - // make the link
>> RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
>> out.write(rh.makeLink(request, response, requestUrl, fullPath, secure, encode));
>> - } 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 (TemplateModelException e) {
>> - throw new IOException(e.getMessage());
>> + } catch (Exception e) {
>> + Debug.logWarning(e, "Exception thrown while running ofbizUrl transform", module);
>> + throw new IOException(e);
>> }
>> }
>> - };
>> - }
>> - 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();
>> +
>> + @Override
>> + public void flush() throws IOException {
>> + out.flush();
>> }
>> - }
>> - return result;
>> - }
>> - private static String getTagValue(String sTag, Element eElement){
>> - String value = "";
>> - try{
>> - NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
>> - Node nValue = nlList.item(0);
>> - return value = nValue.getNodeValue();
>> - } catch (Exception e) {
>> - return value;
>> - }
>> +
>> + @Override
>> + public void write(char cbuf[], int off, int len) {
>> + buf.append(cbuf, off, len);
>> + }
>> + };
>> }
>> }