Author: sohil.shah(a)jboss.com
Date: 2008-07-01 13:53:31 -0400 (Tue, 01 Jul 2008)
New Revision: 11235
Added:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/util/ResourceUtil.java
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/CMSObjectImpl.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FileCreateCommand.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FolderCreateCommand.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFileCreate.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFolderCreate.java
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/ui/admin/CMSAdminPortlet.java
branches/JBoss_Portal_Branch_2_6/core-cms/src/resources/portal-cms-war/WEB-INF/jsp/cms/admin/folderlist.jsp
Log:
JBPORTAL-2031 - Create CMS content which contains a slash '/' fails
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/CMSObjectImpl.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/CMSObjectImpl.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/CMSObjectImpl.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -23,6 +23,7 @@
package org.jboss.portal.cms.impl;
import org.jboss.portal.cms.model.CMSObject;
+import org.jboss.portal.cms.util.FileUtil;
import java.io.Serializable;
import java.util.Date;
@@ -87,12 +88,12 @@
public String getBasePath()
{
- return basePath;
+ return FileUtil.cleanDoubleSlashes(basePath);
}
public void setBasePath(String basePath)
- {
- this.basePath = basePath;
+ {
+ this.basePath = FileUtil.cleanDoubleSlashes(basePath);
}
public String getBaseUUID()
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FileCreateCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FileCreateCommand.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FileCreateCommand.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -22,10 +22,14 @@
******************************************************************************/
package org.jboss.portal.cms.impl.jcr.command;
+import org.apache.log4j.Logger;
+
import org.apache.jackrabbit.value.DateValue;
+import org.jboss.portal.cms.CMSException;
import org.jboss.portal.cms.impl.jcr.JCRCommand;
import org.jboss.portal.cms.model.File;
import org.jboss.portal.cms.util.NodeUtil;
+import org.jboss.portal.cms.impl.jcr.util.ResourceUtil;
import javax.jcr.Node;
import java.util.Calendar;
@@ -38,6 +42,8 @@
{
/** The serialVersionUID */
private static final long serialVersionUID = -653823238247348749L;
+ private static Logger log = Logger.getLogger(FileCreateCommand.class);
+
File mFile;
public FileCreateCommand(File file)
@@ -49,28 +55,37 @@
{
try
{
+ //Validate the FilePath
+ boolean isValid = NodeUtil.isValidPath(mFile.getBasePath());
+ if(!isValid)
+ {
+ throw new CMSException("Path: "+mFile.getBasePath()+" is
invalid");
+ }
+
JCRCommand existsCMD =
(JCRCommand)context.getCommandFactory().createItemExistsCommand(mFile.getBasePath());
Boolean bExists = (Boolean)context.execute(existsCMD);
- if (!bExists.booleanValue()) // if fileNode exists already, ignore the
creation.
+
+ //If fileNode exists already, ignore the creation.
+ if (!bExists.booleanValue())
{
String parentPath = NodeUtil.getParentPath(mFile.getBasePath());
String nodeName = NodeUtil.getNodeName(mFile.getBasePath());
-
+
+ //Make sure the Path hierarchy is complete
+ ResourceUtil.createParentHierarchy(context, parentPath);
+
Node parentNode = (Node)context.getSession().getItem(parentPath);
Node fileNode = parentNode.addNode(nodeName, "portalcms:file");
parentNode.setProperty("jcr:lastModified", new
DateValue(Calendar.getInstance()));
parentNode.setProperty("portalcms:lastmodified", new
DateValue(Calendar.getInstance()));
fileNode.setProperty("jcr:lastModified", new
DateValue(Calendar.getInstance()));
parentNode.save();
- }
- else
- {
- // TODO: Do something !
- }
+ }
}
catch (Exception e)
{
- e.printStackTrace();
+ log.error(this, e);
+ throw new CMSException(e);
}
return null;
}
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FolderCreateCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FolderCreateCommand.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/FolderCreateCommand.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -22,9 +22,13 @@
******************************************************************************/
package org.jboss.portal.cms.impl.jcr.command;
+import org.apache.log4j.Logger;
import org.apache.jackrabbit.value.DateValue;
import org.apache.jackrabbit.value.StringValue;
+
+import org.jboss.portal.cms.CMSException;
import org.jboss.portal.cms.impl.jcr.JCRCommand;
+import org.jboss.portal.cms.impl.jcr.util.ResourceUtil;
import org.jboss.portal.cms.model.Folder;
import org.jboss.portal.cms.util.NodeUtil;
@@ -40,6 +44,8 @@
{
/** The serialVersionUID */
private static final long serialVersionUID = -3007711915681479942L;
+ private static Logger log = Logger.getLogger(FolderCreateCommand.class);
+
Folder mFolder;
public FolderCreateCommand(Folder folder)
@@ -51,8 +57,18 @@
{
try
{
+ //Validate the FolderPath
+ boolean isValid = NodeUtil.isValidPath(mFolder.getBasePath());
+ if(!isValid)
+ {
+ throw new CMSException("Path: "+mFolder.getBasePath()+" is
invalid");
+ }
+
String parentPath = NodeUtil.getParentPath(mFolder.getBasePath());
String nodeName = NodeUtil.getNodeName(mFolder.getBasePath());
+
+ //Make sure the Path hierarchy is complete
+ ResourceUtil.createParentHierarchy(context, parentPath);
Node parentNode = (Node)context.getSession().getItem(parentPath);
Node folderNode = parentNode.addNode(nodeName, "portalcms:folder");
@@ -71,7 +87,8 @@
}
catch (Exception e)
{
- e.printStackTrace();
+ log.error(this, e);
+ throw new CMSException(e);
}
return null;
}
Added:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/util/ResourceUtil.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/util/ResourceUtil.java
(rev 0)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/util/ResourceUtil.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -0,0 +1,121 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.cms.impl.jcr.util;
+
+import java.util.Calendar;
+import java.util.StringTokenizer;
+import java.util.Date;
+
+import javax.jcr.Node;
+
+import org.apache.jackrabbit.value.DateValue;
+import org.apache.jackrabbit.value.StringValue;
+import org.jboss.portal.cms.CMSException;
+import org.jboss.portal.cms.model.Folder;
+import org.jboss.portal.cms.util.NodeUtil;
+import org.jboss.portal.cms.impl.jcr.JCRCommand;
+import org.jboss.portal.cms.impl.jcr.JCRCommandContext;
+import org.jboss.portal.cms.impl.FolderImpl;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+public class ResourceUtil
+{
+ /** @param parentPath */
+ public static void createParentHierarchy(JCRCommandContext context, String parentPath)
throws CMSException
+ {
+ try
+ {
+ StringTokenizer tokenizer = new StringTokenizer(parentPath, "/");
+
+ StringBuffer buffer = new StringBuffer("/");
+ while (tokenizer.hasMoreTokens())
+ {
+ buffer.append(tokenizer.nextToken());
+ String cour = buffer.toString();
+
+ JCRCommand nodeExists =
(JCRCommand)context.getCommandFactory().createItemExistsCommand(cour);
+ Boolean bExists = (Boolean)context.execute(nodeExists);
+ if (!bExists.booleanValue())
+ {
+ ResourceUtil.createFolder(context, cour);
+ }
+ else
+ {
+ //Make Sure this resource is not a file. You don't want to create a
Folder inside a File
+ Node node = (Node)context.getSession().getItem(cour);
+ if(NodeTypeUtil.isFile(node))
+ {
+ throw new CMSException("Invalid Path: "+ parentPath + "
: Trying to create a Folder inside a File");
+ }
+ }
+
+ if (tokenizer.hasMoreTokens())
+ {
+ buffer.append("/");
+ }
+ }
+ }
+ catch(Exception e)
+ {
+ throw new CMSException(e);
+ }
+ }
+
+ /**
+ * @param folderPath
+ * @throws CMSException
+ */
+ private static void createFolder(JCRCommandContext context, String folderPath) throws
CMSException
+ {
+ try
+ {
+ Folder folder = new FolderImpl();
+ folder.setName(folderPath);
+ folder.setDescription(folderPath);
+ folder.setTitle(folderPath);
+ folder.setLastModified(new Date());
+ folder.setBasePath(folderPath);
+
+ String parentPath = NodeUtil.getParentPath(folder.getBasePath());
+ String nodeName = NodeUtil.getNodeName(folder.getBasePath());
+
+ Node parentNode = (Node)context.getSession().getItem(parentPath);
+ Node folderNode = parentNode.addNode(nodeName, "portalcms:folder");
+ folderNode.setProperty("portalcms:description", new
StringValue(folder.getDescription()));
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(folder.getLastModified());
+ DateValue dv = new DateValue(calendar);
+ parentNode.setProperty("portalcms:lastmodified", dv);
+ folderNode.setProperty("portalcms:lastmodified", dv);
+ parentNode.save();
+ }
+ catch(Exception e)
+ {
+ throw new CMSException(e);
+ }
+ }
+}
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFileCreate.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFileCreate.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFileCreate.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -23,6 +23,8 @@
package org.jboss.portal.test.cms.commands;
import junit.framework.TestSuite;
+
+import org.jboss.portal.cms.CMSException;
import org.jboss.portal.cms.Command;
import org.jboss.portal.cms.impl.ContentImpl;
import org.jboss.portal.cms.impl.FileImpl;
@@ -149,4 +151,73 @@
session.logout();
}
+
+
+ public void testRootFileCreate() throws Exception
+ {
+ try
+ {
+ this.fileCreate("/");
+ }
+ catch(CMSException e)
+ {
+ String message = e.getMessage();
+ assertTrue("Invalid Path Not Validated", message.contains("Path:
/ is invalid"));
+ }
+ }
+
+ public void testRecursiveFileCreate() throws Exception
+ {
+ //Create a legal file
+ this.fileCreate("/level-1/blah");
+
+ //Assert
+ Command fileGet =
service.getCommandFactory().createFileGetCommand("/level-1/blah",
Locale.ENGLISH);
+ File cour = (File)service.execute(fileGet);
+ assertNotNull("File was not created", cour);
+ assertEquals("Content Incorrect", new
String(cour.getContent().getBytes()), "1234567890");
+
+ //Try to create a file that should not be allowed
+ try
+ {
+ this.fileCreate("/level-1/blah/blah2");
+ }
+ catch(CMSException e)
+ {
+ String message = e.getMessage();
+ assertTrue("Invalid Parent Hierarchy Not Validated",
message.contains("Trying to create a Folder inside a File"));
+ }
+
+ //Try to create a file way down the path hierarchy
+ this.fileCreate("/level-1/level-2/level-3/blah");
+
+ //Assert
+ fileGet =
service.getCommandFactory().createFileGetCommand("/level-1/level-2/level-3/blah",
Locale.ENGLISH);
+ cour = (File)service.execute(fileGet);
+ assertNotNull("File was not created", cour);
+ assertEquals("Content Incorrect", new
String(cour.getContent().getBytes()), "1234567890");
+ }
+
+ /**
+ *
+ * @param path
+ */
+ private void fileCreate(String path)
+ {
+ //create file english
+ File file = new FileImpl();
+ file.setBasePath(path);
+
+ Content content = new ContentImpl();
+ content.setEncoding("UTF-8");
+ content.setTitle("English Title");
+ content.setDescription("English Description");
+ content.setBasePath(path + "/" + Locale.ENGLISH.getLanguage());
+ content.setBytes("1234567890".getBytes());
+ content.setMimeType("image/gif");
+ file.setContent(Locale.ENGLISH, content);
+
+ Command createFileCMD = service.getCommandFactory().createNewFileCommand(file,
content);
+ service.execute(createFileCMD);
+ }
}
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFolderCreate.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFolderCreate.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/test/cms/commands/TestFolderCreate.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -96,4 +96,45 @@
assertNotNull("Folder description is null", description);
assertTrue("Incorrect description for " + sFolderPath,
description.getString().equals("Folder description"));
}
+
+ public void testRecursiveFolderCreate() throws Exception
+ {
+ this.createFolder("/level-1/blah");
+ this.assertFolder("/level-1/blah");
+
+ //Create a deeper folder
+ this.createFolder("/level-1/level-2/level-3/blah");
+ this.assertFolder("/level-1/level-2/level-3/blah");
+ }
+
+ private void createFolder(String folderPath)
+ {
+ //create folder object
+ Folder folder = new FolderImpl();
+ folder.setCreationDate(new Date());
+ folder.setDescription("Folder description");
+ folder.setTitle("Folder Title");
+ folder.setLastModified(new Date());
+ folder.setName("Alpha Folder");
+ folder.setBasePath(folderPath);
+
+ // save folder
+ Command saveCMD = service.getCommandFactory().createFolderSaveCommand(folder);
+ service.execute(saveCMD);
+ }
+
+ private void assertFolder(String folderPath) throws Exception
+ {
+ String[] cour = folderPath.split("/");
+ String folderName = cour[cour.length-1];
+
+ Session session = RepositoryUtil.login(service.getRepository(), "anonid",
"");
+ Node folderNode = (Node)session.getItem(folderPath);
+ assertNotNull("FolderNodeis null", folderNode);
+ assertTrue("Incorrect primarynodetype",
folderNode.getPrimaryNodeType().getName().equals("portalcms:folder"));
+ assertTrue("Incorrect Name for " + sFolderPath,
folderNode.getName().equals(folderName));
+ Property description = folderNode.getProperty("portalcms:description");
+ assertNotNull("Folder description is null", description);
+ assertTrue("Incorrect description for " + sFolderPath,
description.getString().equals("Folder description"));
+ }
}
Modified:
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/ui/admin/CMSAdminPortlet.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/ui/admin/CMSAdminPortlet.java 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/ui/admin/CMSAdminPortlet.java 2008-07-01
17:53:31 UTC (rev 11235)
@@ -259,7 +259,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
@@ -289,7 +289,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
@@ -397,7 +397,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
@@ -425,7 +425,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
@@ -454,7 +454,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
@@ -610,7 +610,7 @@
List folders = this.getFolderList(sNavPath);
if((folders == null || folders.isEmpty()) &&
- !sNavPath.equals("/"))
+ (sNavPath != null && !sNavPath.equals("/")))
{
sNavPath = NodeUtil.getParentPath(sNavPath);
folders = this.getFolderList(sNavPath);
Modified:
branches/JBoss_Portal_Branch_2_6/core-cms/src/resources/portal-cms-war/WEB-INF/jsp/cms/admin/folderlist.jsp
===================================================================
---
branches/JBoss_Portal_Branch_2_6/core-cms/src/resources/portal-cms-war/WEB-INF/jsp/cms/admin/folderlist.jsp 2008-07-01
16:10:41 UTC (rev 11234)
+++
branches/JBoss_Portal_Branch_2_6/core-cms/src/resources/portal-cms-war/WEB-INF/jsp/cms/admin/folderlist.jsp 2008-07-01
17:53:31 UTC (rev 11235)
@@ -103,25 +103,5 @@
folder.getBasePath().substring(folder.getBasePath().lastIndexOf("/") + 1,
folder.getBasePath().length()) %></a>
<br>
-<%
- }
-}
-else
-{
-%>
-<a href="<portlet:renderURL>
- <portlet:param name="op" value="<%= OP %>"/>
- <portlet:param name="path" value="<%= sCurrPath %>"/>
- <portlet:param name="navpath" value="<%=
NodeUtil.getParentPath(sNavPath) %>"/>
- <%
- if(sCMType != null)
- {
- %>
- <portlet:param name="type" value="<%= sCMType %>"/>
- <%
- }
- %>
-</portlet:renderURL>">Up</a>
-<%
- }
-%>
+<%}
+}%>