Author: sohil.shah(a)jboss.com
Date: 2007-08-10 01:44:19 -0400 (Fri, 10 Aug 2007)
New Revision: 7890
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/StoreArchiveCommand.java
Log:
Fix for JBPORTAL-1146 - java.lang.NegativeArraySizeException in uploading zip archive
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/StoreArchiveCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/StoreArchiveCommand.java 2007-08-10
05:01:23 UTC (rev 7889)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/StoreArchiveCommand.java 2007-08-10
05:44:19 UTC (rev 7890)
@@ -22,6 +22,7 @@
******************************************************************************/
package org.jboss.portal.cms.impl.jcr.command;
+import org.jboss.portal.cms.CMSException;
import org.jboss.portal.cms.impl.ContentImpl;
import org.jboss.portal.cms.impl.FileImpl;
import org.jboss.portal.cms.impl.FolderImpl;
@@ -32,12 +33,16 @@
import org.jboss.portal.cms.util.NodeUtil;
import java.io.InputStream;
+import java.io.FileOutputStream;
+import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
+import java.util.Enumeration;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
+import java.util.zip.ZipFile;
+import java.util.StringTokenizer;
/**
* Saves an uploaded archive to the repo.
@@ -52,6 +57,12 @@
InputStream mIS;
String msLanguage;
+ /**
+ *
+ * @param sRootPath
+ * @param is
+ * @param sLanguage
+ */
public StoreArchiveCommand(String sRootPath, InputStream is, String sLanguage)
{
this.msRootPath = sRootPath;
@@ -59,30 +70,36 @@
this.msLanguage = sLanguage;
}
+ /**
+ *
+ */
public Object execute()
{
List contents = new ArrayList();
+ File tmpFile = null;
try
{
- ZipInputStream zis = new ZipInputStream(this.mIS);
- for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry =
zis.getNextEntry())
+ tmpFile = this.getZipFile();
+
+ ZipFile zipFile = new ZipFile(tmpFile);
+ ZipEntry zipEntry;
+ Enumeration entries = zipFile.entries();
+ while(entries.hasMoreElements())
{
+ zipEntry = (ZipEntry)entries.nextElement();
String itemName = zipEntry.getName();
if (!zipEntry.isDirectory())
- {
+ {
long fileSize = zipEntry.getSize();
- if ((fileSize < 0) || (fileSize > Integer.MAX_VALUE))
- {
- throw new Exception("Error : " + itemName + " Cannot
determine uncompressed size!");
- }
+ byte[] zipBytes = new byte[(int)fileSize];
+ InputStream zipDataStream = zipFile.getInputStream(zipEntry);
+ long bytesRead = 0;
- byte[] zipBytes = new byte[(int)fileSize];
- int offset = 0;
- while (offset < zipBytes.length)
+ while (bytesRead < fileSize)
{
- offset += zis.read(zipBytes, offset, zipBytes.length - offset);
+ bytesRead += zipDataStream.read(zipBytes, (int)bytesRead,
(int)(fileSize - bytesRead));
}
-
+
org.jboss.portal.cms.model.File file = new FileImpl();
String sBasePath = FileUtil.cleanDoubleSlashes("/" +
this.msRootPath + "/" + itemName);
@@ -103,15 +120,7 @@
Boolean bExists = (Boolean)context.execute(nodeExists);
if (!bExists.booleanValue())
{
- Folder folder = new FolderImpl();
- folder.setName(sParentPath);
- folder.setDescription(sParentPath);
- folder.setTitle(sParentPath);
- folder.setLastModified(new Date());
- folder.setBasePath(sParentPath);
-
- JCRCommand parentSave =
(JCRCommand)context.getCommandFactory().createFolderSaveCommand(folder);
- context.execute(parentSave);
+ this.createParentHierarchy(sParentPath);
}
JCRCommand fileSave =
(JCRCommand)context.getCommandFactory().createFileSaveCommand(file);
@@ -139,15 +148,7 @@
Boolean bExists = (Boolean)context.execute(nodeExists);
if (!bExists.booleanValue())
{
- Folder folder = new FolderImpl();
- folder.setName(sParentPath);
- folder.setDescription(sParentPath);
- folder.setTitle(sParentPath);
- folder.setLastModified(new Date());
- folder.setBasePath(sParentPath);
-
- JCRCommand parentSave =
(JCRCommand)context.getCommandFactory().createFolderSaveCommand(folder);
- context.execute(parentSave);
+ this.createParentHierarchy(sParentPath);
}
JCRCommand nodeExists2 =
(JCRCommand)context.getCommandFactory().createItemExistsCommand(sBasePath);
@@ -167,14 +168,93 @@
}
}
}
- zis.close();
return contents;
}
catch (Exception e)
{
e.printStackTrace();
}
+ finally
+ {
+ if(tmpFile != null)
+ {
+ tmpFile.delete();
+ }
+ }
return null;
}
+
+ /**
+ *
+ * @param parentPath
+ */
+ private void createParentHierarchy(String parentPath) throws CMSException
+ {
+ 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())
+ {
+ this.createFolder(cour);
+ }
+
+ if(tokenizer.hasMoreTokens())
+ {
+ buffer.append("/");
+ }
+ }
+ }
+
+ /**
+ *
+ * @param folderPath
+ * @throws CMSException
+ */
+ private void createFolder(String folderPath) throws CMSException
+ {
+ Folder folder = new FolderImpl();
+ folder.setName(folderPath);
+ folder.setDescription(folderPath);
+ folder.setTitle(folderPath);
+ folder.setLastModified(new Date());
+ folder.setBasePath(folderPath);
-}
+ JCRCommand parentSave =
(JCRCommand)context.getCommandFactory().createFolderSaveCommand(folder);
+ context.execute(parentSave);
+ }
+
+ /**
+ *
+ * @return
+ */
+ private File getZipFile() throws Exception
+ {
+ File zipFile = null;
+
+ zipFile = File.createTempFile("jbportal_", "_cmsimport.zip");
+ FileOutputStream fos = new FileOutputStream(zipFile.getCanonicalPath());
+ try
+ {
+ int count;
+ byte[] data = new byte[1024];
+ while ((count = this.mIS.read(data, 0, 1024)) != -1)
+ {
+ fos.write(data, 0, count);
+ fos.flush();
+ }
+ }
+ finally
+ {
+ fos.close();
+ }
+
+ return zipFile;
+ }
+}
\ No newline at end of file
Show replies by date