From dc47dca54d91cf76c978484fadc6a28b90d61b16 Mon Sep 17 00:00:00 2001 From: Daniel Laguna Date: Fri, 28 Apr 2017 09:07:34 -0600 Subject: [PATCH] =?UTF-8?q?#11303:=20Get=20Velocity=20root=20folder=20meth?= =?UTF-8?q?od=20created.=20Classes=20that=20had=20the=E2=80=A6=20(#11335)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #11303: Get Velocity root folder method created. Classes that had the hardcoded path were modified. * #10370: Final adjustments to Velocity folder capabilities. Use the VELOCITY_ROOT property to override the default path --- .../com/dotcms/velocity/VelocityUtilTest.java | 55 ++++++++++++++ .../com/dotmarketing/osgi/ActivatorUtil.java | 9 ++- .../services/ContainerServices.java | 11 +-- .../services/ContentletMapServices.java | 14 ++-- .../services/ContentletServices.java | 14 +--- .../dotmarketing/services/FieldServices.java | 12 +--- .../dotmarketing/services/HostServices.java | 16 ++--- .../dotmarketing/services/PageServices.java | 13 +--- .../services/StructureServices.java | 11 ++- .../services/TemplateServices.java | 12 +--- .../com/dotmarketing/util/VelocityUtil.java | 22 +++++- .../velocity/DotResourceLoader.java | 71 ++++++++++++++++++- .../velocity/VelocityServlet.java | 13 ++-- 13 files changed, 184 insertions(+), 89 deletions(-) create mode 100644 dotCMS/src/integration-test/java/com/dotcms/velocity/VelocityUtilTest.java diff --git a/dotCMS/src/integration-test/java/com/dotcms/velocity/VelocityUtilTest.java b/dotCMS/src/integration-test/java/com/dotcms/velocity/VelocityUtilTest.java new file mode 100644 index 000000000000..cca51a03c2ee --- /dev/null +++ b/dotCMS/src/integration-test/java/com/dotcms/velocity/VelocityUtilTest.java @@ -0,0 +1,55 @@ +package com.dotcms.velocity; + +import com.dotcms.util.IntegrationTestInitService; +import com.dotmarketing.util.Config; +import com.dotmarketing.util.VelocityUtil; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.mockito.Mockito; + +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * VelocityUtilTest + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class VelocityUtilTest { + + @BeforeClass + public static void prepare() throws Exception { + IntegrationTestInitService.getInstance().init(); + Mockito.when(Config.CONTEXT.getRealPath("/WEB-INF/velocity")) + .thenReturn(Config.getStringProperty("VELOCITY_ROOT", "/WEB-INF/velocity")); + } + + /** + * Test the velocity path is the default path + */ + @Test + public void test01DefaultVelocityPath() throws Exception { + String velocityPath = VelocityUtil.getVelocityRootPath(); + + Assert.assertNotNull(velocityPath); + assertThat("Path ends with /WEB-INF/velocity", velocityPath.endsWith("/WEB-INF/velocity")); + } + + /** + * Test the velocity path is a custom path + */ + @Test + public void test02CustomVelocityPath() throws Exception { + String velocityPath = Config.getStringProperty("VELOCITY_ROOT", "/WEB-INF/velocity"); + velocityPath = velocityPath.replace("/WEB-INF/velocity", "/WEB-INF/customvelocity"); + Config.setProperty("VELOCITY_ROOT", velocityPath); + + String customVelocityPath = VelocityUtil.getVelocityRootPath(); + + Assert.assertNotNull(customVelocityPath); + assertThat("Path ends with /WEB-INF/customvelocity", customVelocityPath.endsWith("/WEB-INF/customvelocity")); + } + +} diff --git a/dotCMS/src/main/java/com/dotmarketing/osgi/ActivatorUtil.java b/dotCMS/src/main/java/com/dotmarketing/osgi/ActivatorUtil.java index 05339257d568..f9746e98de4c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/osgi/ActivatorUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/osgi/ActivatorUtil.java @@ -5,6 +5,7 @@ import com.dotcms.repackage.org.apache.struts.config.ModuleConfig; import com.dotcms.repackage.org.apache.struts.config.impl.ModuleConfigImpl; import com.dotmarketing.util.Config; +import com.dotmarketing.util.VelocityUtil; import com.liferay.portal.util.Constants; import com.liferay.util.FileUtil; import org.apache.felix.http.api.ExtHttpService; @@ -23,9 +24,8 @@ class ActivatorUtil { - static final String PATH_SEPARATOR = "/"; + static final String PATH_SEPARATOR = "/"; static final String OSGI_FOLDER = "/osgi"; - static final String VELOCITY_FOLDER = "/WEB-INF/velocity"; static String getBundleFolder ( BundleContext context, String separator ) { @@ -75,7 +75,7 @@ static void cleanResources ( BundleContext context ) { } //Now cleaning the resources under the velocity folder - resourcesPath = servletContext.getRealPath( VELOCITY_FOLDER + getBundleFolder( context, "/" ) ); + resourcesPath = VelocityUtil.getVelocityRootPath() + getBundleFolder( context, "/" ); resources = new File( resourcesPath ); if ( resources.exists() ) { FileUtil.deltree( resources ); @@ -91,8 +91,7 @@ static void cleanResources ( BundleContext context ) { */ static void moveVelocityResources ( BundleContext context, String referenceResourcePath ) throws Exception { - ServletContext servletContext = Config.CONTEXT; - String destinationPath = servletContext.getRealPath( VELOCITY_FOLDER + getBundleFolder( context, "/" ) ); + String destinationPath = VelocityUtil.getVelocityRootPath() + getBundleFolder( context, "/" ); moveResources( context, referenceResourcePath, destinationPath ); } diff --git a/dotCMS/src/main/java/com/dotmarketing/services/ContainerServices.java b/dotCMS/src/main/java/com/dotmarketing/services/ContainerServices.java index 2a09cdacfac6..1184709cef8f 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/ContainerServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/ContainerServices.java @@ -21,6 +21,7 @@ import com.dotmarketing.util.ConfigUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.util.FileUtil; @@ -285,11 +286,6 @@ public static InputStream buildVelocity(Container container, Identifier identifi try { String folderPath = (!EDIT_MODE) ? "live" + File.separator: "working" + File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += File.separator; String filePath = folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"); if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ @@ -332,10 +328,7 @@ public static void removeContainerFile(Container container, boolean EDIT_MODE) t public static void removeContainerFile (Container container, Identifier identifier, boolean EDIT_MODE) { String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); velocityRootPath += java.io.File.separator; String filePath = folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"); java.io.File f = new java.io.File(velocityRootPath + filePath); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/ContentletMapServices.java b/dotCMS/src/main/java/com/dotmarketing/services/ContentletMapServices.java index cdf19173fb44..4ef97ed14686 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/ContentletMapServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/ContentletMapServices.java @@ -1,6 +1,7 @@ package com.dotmarketing.services; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; @@ -34,6 +35,7 @@ import com.dotmarketing.util.InodeUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.portal.model.User; import com.liferay.util.FileUtil; @@ -402,14 +404,6 @@ else if (field.getFieldType().equals(Field.FieldType.BINARY.toString())) { if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ try { - - String velocityRootPath=Config.getStringProperty("VELOCITY_ROOT"); - - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath=FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - String veloExt=Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION"); String baseFilename=String.format("%s_%d.%s", content.getIdentifier(), content.getLanguageId(), veloExt); @@ -466,7 +460,9 @@ private static void removeContentletMapFile(Contentlet asset, boolean EDIT_MODE) private static void removeContentletMapFile(Contentlet asset, Identifier identifier, boolean EDIT_MODE) { String folderPath=(!EDIT_MODE) ? "live/" : "working/"; - String velocityRoot=FileUtil.getRealPath("/WEB-INF/velocity/") + folderPath; + + String velocityRootPath = VelocityUtil.getVelocityRootPath(); + String velocityRoot = velocityRootPath + File.separator + folderPath; String filePath= folderPath + identifier.getInode() + "_" + asset.getLanguageId() + "." + Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION"); java.io.File f=new java.io.File (velocityRoot + filePath); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/ContentletServices.java b/dotCMS/src/main/java/com/dotmarketing/services/ContentletServices.java index 0cdc50965b18..61f20cf5004c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/ContentletServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/ContentletServices.java @@ -23,6 +23,7 @@ import com.dotmarketing.util.InodeUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.dotmarketing.viewtools.LanguageWebAPI; import com.liferay.portal.model.User; @@ -478,14 +479,6 @@ else if (field.getFieldType().equals(Field.FieldType.CATEGORY.toString())) { if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ try { - - String velocityRootPath= Config.getStringProperty("VELOCITY_ROOT"); - - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath= FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - String veloExt= Config.getStringProperty("VELOCITY_CONTENT_EXTENSION"); String baseFilename= String.format("%s_%d.%s", identifier.getInode(), content.getLanguageId(), veloExt); @@ -554,10 +547,7 @@ private static void removeContentletFile(Contentlet asset, Identifier identifier CacheLocator.getContentletCache().remove(asset.getInode()); String folderPath= (!EDIT_MODE) ? "live" + java.io.File.separator : "working" + java.io.File.separator; - String velocityRootPath= Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath= FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); velocityRootPath += java.io.File.separator; Set langs = new HashSet(); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/FieldServices.java b/dotCMS/src/main/java/com/dotmarketing/services/FieldServices.java index 88256068686e..b5979b472531 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/FieldServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/FieldServices.java @@ -23,6 +23,7 @@ import com.dotmarketing.util.ConfigUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.util.FileUtil; @@ -66,12 +67,6 @@ public static InputStream buildVelocity(String fieldInode, String contentInode, } if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; String filePath=folderPath + contentInode + "_" + fieldInode + "." + Config.getStringProperty("VELOCITY_FIELD_EXTENSION"); //Specify a proper character encoding @@ -99,10 +94,7 @@ public static InputStream buildVelocity(String fieldInode, String contentInode, } public static void removeFieldFile (String fieldInode, String contentInode, boolean EDIT_MODE) { - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); velocityRootPath += java.io.File.separator; String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; String filePath=folderPath + contentInode + "_" + fieldInode + "." + Config.getStringProperty("VELOCITY_FIELD_EXTENSION"); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/HostServices.java b/dotCMS/src/main/java/com/dotmarketing/services/HostServices.java index 5066dbb4baed..3a6af253eb41 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/HostServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/HostServices.java @@ -21,6 +21,7 @@ import com.dotmarketing.util.ConfigUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.portal.model.User; import com.liferay.util.FileUtil; @@ -73,13 +74,9 @@ public static InputStream buildStream(Host host, boolean EDIT_MODE) throws DotDa if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ String realFolderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - String filePath = realFolderPath + host.getIdentifier() + "." + Config.getStringProperty("VELOCITY_HOST_EXTENSION"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - + + String filePath = realFolderPath + host.getIdentifier() + "." + Config.getStringProperty("VELOCITY_HOST_EXTENSION"); + java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream(new java.io.FileOutputStream(new java.io.File(ConfigUtils.getDynamicVelocityPath()+java.io.File.separator + filePath))); //Specify a proper character encoding OutputStreamWriter out = new OutputStreamWriter(tmpOut, UtilMethods.getCharsetConfiguration()); @@ -112,10 +109,7 @@ public static void unpublishPageFile(Host host) { public static void removeHostFile (Host host, boolean EDIT_MODE) { String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); String filePath = folderPath + host.getIdentifier() + "." + Config.getStringProperty("VELOCITY_HOST_EXTENSION"); velocityRootPath += java.io.File.separator; java.io.File f = new java.io.File(velocityRootPath + filePath); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/PageServices.java b/dotCMS/src/main/java/com/dotmarketing/services/PageServices.java index 198651c69078..bc9acecd8adb 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/PageServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/PageServices.java @@ -356,13 +356,9 @@ public static InputStream buildStream(IHTMLPage htmlPage, Identifier identifier, String languageStr = htmlPage.isContent() ? "_" + ((Contentlet)htmlPage).getLanguageId():""; String realFolderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); + String filePath = realFolderPath + identifier.getInode() + languageStr + "." + Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION","dotpage"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = com.liferay.util.FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - + java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream(new java.io.FileOutputStream(new java.io.File(ConfigUtils.getDynamicVelocityPath()+java.io.File.separator + filePath))); //Specify a proper character encoding OutputStreamWriter out = new OutputStreamWriter(tmpOut, UtilMethods.getCharsetConfiguration()); @@ -387,10 +383,7 @@ public static InputStream buildStream(IHTMLPage htmlPage, Identifier identifier, public static void removePageFile (IHTMLPage htmlPage, Identifier identifier, boolean EDIT_MODE) { String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = com.liferay.util.FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); String languageStr = htmlPage.isContent() ? "_" + ((Contentlet)htmlPage).getLanguageId():""; String filePath = folderPath + identifier.getInode() + languageStr + "." + Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION","dotpage"); velocityRootPath += java.io.File.separator; diff --git a/dotCMS/src/main/java/com/dotmarketing/services/StructureServices.java b/dotCMS/src/main/java/com/dotmarketing/services/StructureServices.java index 226a3db56475..2dbff143371c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/StructureServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/StructureServices.java @@ -1,6 +1,7 @@ package com.dotmarketing.services; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; @@ -16,6 +17,7 @@ import com.dotmarketing.util.ConfigUtils; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.util.FileUtil; @@ -151,11 +153,6 @@ public static InputStream buildVelocity(Structure structure,boolean EDIT_MODE) { try { if(Config.getBooleanProperty("SHOW_VELOCITYFILES", false)){ - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; String relativePath = "/working/" + structure.getInode() + "." + Config.getStringProperty("VELOCITY_STRUCTURE_EXTENSION"); @@ -181,7 +178,9 @@ public static InputStream buildVelocity(Structure structure,boolean EDIT_MODE) { public static void removeStructureFile(Structure structure) { String folderPath = "working/"; String filePath=folderPath + structure.getInode() + "." + Config.getStringProperty("VELOCITY_STRUCTURE_EXTENSION"); - String absolutPath = FileUtil.getRealPath("/WEB-INF/velocity/" +filePath); + + String velocityRootPath = VelocityUtil.getVelocityRootPath(); + String absolutPath = velocityRootPath + File.separator + filePath; java.io.File f = new java.io.File(absolutPath); f.delete(); DotResourceCache vc = CacheLocator.getVeloctyResourceCache(); diff --git a/dotCMS/src/main/java/com/dotmarketing/services/TemplateServices.java b/dotCMS/src/main/java/com/dotmarketing/services/TemplateServices.java index 884da5f2cacb..28c1a0ec96d0 100644 --- a/dotCMS/src/main/java/com/dotmarketing/services/TemplateServices.java +++ b/dotCMS/src/main/java/com/dotmarketing/services/TemplateServices.java @@ -18,6 +18,7 @@ import com.dotmarketing.util.Constants; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.VelocityUtil; import com.dotmarketing.velocity.DotResourceCache; import com.liferay.util.FileUtil; @@ -55,12 +56,6 @@ public static InputStream buildVelocity(Template template, Identifier identifier InputStream result; StringBuilder templateBody = new StringBuilder(); try { - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } - velocityRootPath += java.io.File.separator; - String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; String filePath=folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION"); @@ -112,10 +107,7 @@ public static void removeTemplateFile(Template asset, boolean EDIT_MODE) throws } public static void removeTemplateFile (Template asset, Identifier identifier, boolean EDIT_MODE) { - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); - if (velocityRootPath.startsWith("/WEB-INF")) { - velocityRootPath = FileUtil.getRealPath(velocityRootPath); - } + String velocityRootPath = VelocityUtil.getVelocityRootPath(); velocityRootPath += java.io.File.separator; String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; diff --git a/dotCMS/src/main/java/com/dotmarketing/util/VelocityUtil.java b/dotCMS/src/main/java/com/dotmarketing/util/VelocityUtil.java index f96d92479ee3..7a52d2bd51ad 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/VelocityUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/VelocityUtil.java @@ -45,7 +45,7 @@ import com.dotmarketing.velocity.VelocityServlet; import com.dotmarketing.viewtools.RequestWrapper; import com.liferay.portal.model.Company; -import com.liferay.util.SystemProperties; +import com.liferay.util.*; public class VelocityUtil { @@ -588,4 +588,24 @@ public static IHTMLPage getPage(Identifier id, HttpServletRequest request, boole return htmlPage; } + /** + * Gets the Velocity Root Path. Looks for it on the Config, if not found the it get defaulted to /WEB-INF/velocity + * + * @return String + */ + public static String getVelocityRootPath() { + Logger.debug(VelocityUtil.class, "Fetching the velocity ROOT path..."); + + String velocityRootPath; + + velocityRootPath = Config.getStringProperty("VELOCITY_ROOT", "/WEB-INF/velocity"); + if (velocityRootPath.startsWith("/WEB-INF")) { + Logger.debug(VelocityUtil.class, "Velocity ROOT Path not found, defaulting it to '/WEB-INF/velocity'"); + velocityRootPath = com.liferay.util.FileUtil.getRealPath(velocityRootPath); + } + + Logger.debug(VelocityUtil.class, String.format("Velocity ROOT path found: %s", velocityRootPath)); + return velocityRootPath; + } + } diff --git a/dotCMS/src/main/java/com/dotmarketing/velocity/DotResourceLoader.java b/dotCMS/src/main/java/com/dotmarketing/velocity/DotResourceLoader.java index c5245babb208..b1545a2ddfc3 100644 --- a/dotCMS/src/main/java/com/dotmarketing/velocity/DotResourceLoader.java +++ b/dotCMS/src/main/java/com/dotmarketing/velocity/DotResourceLoader.java @@ -13,6 +13,7 @@ import com.dotcms.contenttype.transform.contenttype.StructureTransformer; import com.dotcms.repackage.org.apache.commons.collections.ExtendedProperties; +import org.apache.commons.io.FileUtils; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.runtime.resource.loader.ResourceLoader; @@ -95,9 +96,15 @@ public void init(ExtendedProperties extProps) { VELOCITY_BANNER_EXTENSION = Config.getStringProperty("VELOCITY_BANNER_EXTENSION"); VELOCITY_HOST_EXTENSION=Config.getStringProperty("VELOCITY_HOST_EXTENSION"); - String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); + String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT", "/WEB-INF/velocity"); if (velocityRootPath.startsWith("/WEB-INF")) { velocityRootPath = FileUtil.getRealPath(velocityRootPath); + } else { + // verify folder exists or create it + verifyOrCreateVelocityRootPath(velocityRootPath); + + // verify and move velocity contents + verifyAndMoveVelocityContents(velocityRootPath, FileUtil.getRealPath("/WEB-INF/velocity")); } VELOCITY_ROOT = velocityRootPath + File.separator; @@ -522,4 +529,64 @@ public static DotResourceLoader getInstance(){ return instance; } -} \ No newline at end of file + /** + * Verifies the folder exists. + * If it does not exists then tries to create it + * + * @param path The path to verify + * @return boolean true when path exists or it was created successfully + */ + private boolean verifyOrCreateVelocityRootPath(String path) { + return new File(path).exists() || createVelocityFolder(path); + } + + /** + * Create the path if it does not exist. Required for velocity files + * + * @param path The path to create + * @return boolean + */ + private boolean createVelocityFolder(String path) { + boolean created = false; + File directory = new File(path); + if (!directory.exists()) { + Logger.debug(this, String.format("Velocity directory %s does not exist. Trying to create it...", path)); + created = directory.mkdirs(); + if (!created) { + Logger.error(this, String.format("Unable to create Velocity directory: %s", path)); + } + } + return created; + } + + /** + * Verify the velocity contents are in the right place if the default path has been overwritten + * If velocity contents path is different to the default one then move all contents to the new directory and get rid of the default one + * + * @param customPath The custom path for velocity files + * @param sourcePath The source path for velocity files + */ + private void verifyAndMoveVelocityContents(String customPath, String sourcePath) { + if (UtilMethods.isSet(customPath) && UtilMethods.isSet(sourcePath)) { + if (!customPath.trim().equals(sourcePath)) { + File customDirectory = new File(customPath); + File sourceDirectory = new File(sourcePath); + + if (sourceDirectory.exists() && customDirectory.exists()) { + try { + // copy all bundles + FileUtils.copyDirectory(sourceDirectory, customDirectory); + + // delete target folder since we don't need it + FileUtils.deleteDirectory(sourceDirectory); + } catch (IOException ioex) { + String errorMessage = String.format("There was a problem moving velocity contents from '%s' to '%s'", sourcePath, customPath); + Logger.error(this, errorMessage); + throw new RuntimeException(errorMessage, ioex); + } + } + } + } + } + +} diff --git a/dotCMS/src/main/java/com/dotmarketing/velocity/VelocityServlet.java b/dotCMS/src/main/java/com/dotmarketing/velocity/VelocityServlet.java index b73cec82cf47..37ce8c7377f2 100644 --- a/dotCMS/src/main/java/com/dotmarketing/velocity/VelocityServlet.java +++ b/dotCMS/src/main/java/com/dotmarketing/velocity/VelocityServlet.java @@ -304,11 +304,16 @@ public void init(ServletConfig config) throws ServletException { // build the dirs - String pathWorking = Config.CONTEXT_PATH + File.separator + "WEB-INF" + File.separator + "velocity" + File.separator + "working"; - String pathLive = Config.CONTEXT_PATH + File.separator + "WEB-INF" + File.separator + "velocity" + File.separator + "live"; + String pathWorking = VelocityUtil.getVelocityRootPath() + File.separator + "working"; + String pathLive = VelocityUtil.getVelocityRootPath() + File.separator + "live"; - new File(pathWorking).mkdirs(); - new File(pathLive).mkdir(); + if (!new File(pathWorking).exists()) { + new File(pathWorking).mkdirs(); + } + + if (!new File(pathLive).exists()) { + new File(pathLive).mkdirs(); + } Config.initializeConfig();