Author: sohil.shah(a)jboss.com
Date: 2008-10-18 15:45:12 -0400 (Sat, 18 Oct 2008)
New Revision: 12086
Added:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/DownloadArchiveCommand.java
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/CommandFactory.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/JCRCommandFactory.java
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/GetArchiveCommand.java
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/servlet/CMSExportServlet.java
Log:
JBPORTAL-2181 - Exporting folder on CMS slave node results in HTTP 404
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/CommandFactory.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/CommandFactory.java 2008-10-18
01:53:52 UTC (rev 12085)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/CommandFactory.java 2008-10-18
19:45:12 UTC (rev 12086)
@@ -86,9 +86,11 @@
Command createFileGetListCommand(String sFilePath);
- Command createGetArchiveCommand(String sRootPath, String sLanguage);
+ Command createGetArchiveCommand(String sRootPath, String sLanguage);
Command createSearchCommand(JCRQuery query);
Command createMakeLiveVersionCommand(String filePath, String language, String
version);
+
+ Command createDownloadArchiveCommand(String filePath);
}
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/JCRCommandFactory.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/JCRCommandFactory.java 2008-10-18
01:53:52 UTC (rev 12085)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/JCRCommandFactory.java 2008-10-18
19:45:12 UTC (rev 12086)
@@ -45,6 +45,7 @@
import org.jboss.portal.cms.impl.jcr.command.RenameCommand;
import org.jboss.portal.cms.impl.jcr.command.SearchCommand;
import org.jboss.portal.cms.impl.jcr.command.StoreArchiveCommand;
+import org.jboss.portal.cms.impl.jcr.command.DownloadArchiveCommand;
import org.jboss.portal.cms.impl.jcr.composite.NewFileCommand;
import org.jboss.portal.cms.impl.jcr.composite.UpdateFileCommand;
import org.jboss.portal.cms.model.Content;
@@ -56,6 +57,7 @@
import org.jboss.portal.cms.impl.jcr.command.AsyncStoreArchiveCommand;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
@@ -197,5 +199,10 @@
public Command createMakeLiveVersionCommand(String filePath, String language, String
version)
{
return new MakeLiveVersionCommand(filePath, language, version);
- }
+ }
+
+ public Command createDownloadArchiveCommand(String filePath)
+ {
+ return new DownloadArchiveCommand(filePath);
+ }
}
Added:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/DownloadArchiveCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/DownloadArchiveCommand.java
(rev 0)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/DownloadArchiveCommand.java 2008-10-18
19:45:12 UTC (rev 12086)
@@ -0,0 +1,95 @@
+/******************************************************************************
+ * 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.command;
+
+import java.util.Locale;
+import java.io.FileInputStream;
+
+import org.apache.log4j.Logger;
+
+import org.jboss.portal.cms.CMSException;
+import org.jboss.portal.cms.model.File;
+import org.jboss.portal.cms.model.Content;
+import org.jboss.portal.cms.impl.FileImpl;
+import org.jboss.portal.cms.impl.ContentImpl;
+import org.jboss.portal.cms.impl.jcr.JCRCommand;
+import org.jboss.portal.cms.impl.jcr.JCRCommandContext;
+
+/**
+ *
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+public class DownloadArchiveCommand extends JCRCommand
+{
+ private static final long serialVersionUID = 1L;
+ private static Logger log = Logger.getLogger(DownloadArchiveCommand.class);
+
+ String filePath = null;
+
+ JCRCommandContext mContext;
+
+ public DownloadArchiveCommand(String filePath)
+ {
+ this.filePath = filePath;
+ }
+
+ public Object execute()
+ {
+ java.io.File file = null;
+ try
+ {
+ file = new java.io.File(this.filePath);
+
+ //Create the Content
+ File downloadFile = new FileImpl();
+ downloadFile.setName(file.getName());
+
+ Content downloadFileContent = new ContentImpl();
+ downloadFileContent.setSize(file.length());
+ downloadFileContent.setStream(new FileInputStream(file));
+
+ //Locale does not matter. Just honoring the contract exposed by the File Object
+ downloadFile.setContent(Locale.getDefault(), downloadFileContent);
+
+
+ return downloadFile;
+ }
+ catch(Exception e)
+ {
+ log.error(this, e);
+ throw new CMSException(e);
+ }
+ finally
+ {
+ //File cleanup
+ if(file != null)
+ {
+ if (!file.delete())
+ {
+ file.deleteOnExit();
+ }
+ }
+ }
+ }
+}
Modified:
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/GetArchiveCommand.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/GetArchiveCommand.java 2008-10-18
01:53:52 UTC (rev 12085)
+++
branches/JBoss_Portal_Branch_2_6/cms/src/main/org/jboss/portal/cms/impl/jcr/command/GetArchiveCommand.java 2008-10-18
19:45:12 UTC (rev 12086)
@@ -52,9 +52,6 @@
private static final int BUFFER_SIZE = 1024;
- /** Root folder in finished zip file that contains all nodes. */
- private String rootFolderName = "contents";
-
public GetArchiveCommand(String sRootPath, String sLanguage)
{
this.msRootPath = sRootPath;
Modified:
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/servlet/CMSExportServlet.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/servlet/CMSExportServlet.java 2008-10-18
01:53:52 UTC (rev 12085)
+++
branches/JBoss_Portal_Branch_2_6/core-cms/src/main/org/jboss/portal/core/cms/servlet/CMSExportServlet.java 2008-10-18
19:45:12 UTC (rev 12086)
@@ -22,17 +22,29 @@
******************************************************************************/
package org.jboss.portal.core.cms.servlet;
-import org.jboss.portal.common.io.IOTools;
+import org.jboss.mx.util.MBeanProxy;
+import org.jboss.mx.util.MBeanServerLocator;
-import javax.servlet.ServletOutputStream;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.io.FileInputStream;
+import javax.servlet.UnavailableException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.log4j.Logger;
+
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.cms.CMS;
+import org.jboss.portal.cms.Command;
+import org.jboss.portal.cms.CMSException;
+import org.jboss.portal.cms.model.File;
+
+
/**
* Used for archive export in CMS.
*
@@ -42,27 +54,59 @@
{
/** The serialVersionUID */
private static final long serialVersionUID = 9192970961535895941L;
+
+ private static Logger log = Logger.getLogger(CMSExportServlet.class);
+
+ /**
+ *
+ */
+ private CMS cmsService = null;
+
+ /**
+ *
+ */
+ public void init() throws ServletException
+ {
+ try
+ {
+ MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
+ this.cmsService = (CMS)MBeanProxy.get(CMS.class, new
ObjectName("portal:service=CMS"), mbeanServer);
+ if(this.cmsService == null)
+ {
+ throw new CMSException("CMS Service was not found!!");
+ }
+ }
+ catch(Exception e)
+ {
+ throw new UnavailableException(e.getMessage());
+ }
+ }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException
{
String sPickupFile =
(String)request.getSession().getAttribute("pickupfile");
-
- File file = new File(sPickupFile);
try
{
- //response.setContentType(getServletContext().getMimeType(file.getName()));
+ //Get the file to be downloaded
+ Command downloadCommand =
this.cmsService.getCommandFactory().createDownloadArchiveCommand(sPickupFile);
+ File file = (File)this.cmsService.execute(downloadCommand);
+
+ //SetUp the response header
response.setContentType("application/zip");
- response.setContentLength((int)file.length());
+ response.setContentLength((int)file.getSize());
response.setHeader("Content-Disposition", "attachment;
filename=" + file.getName());
-
- InputStream in = IOTools.safeBufferedWrapper(new FileInputStream(file));
- ServletOutputStream sout = response.getOutputStream();
+
+ //Write the contents
+ InputStream in = IOTools.safeBufferedWrapper(file.getContent().getStream());
+ OutputStream sout = response.getOutputStream();
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0)
{
sout.write(buf, 0, len);
}
+
+ //Cleanup
in.close();
sout.flush();
sout.close();
@@ -75,16 +119,9 @@
}
catch (IOException e1)
{
- e1.printStackTrace();
+ log.error(this, e1);
}
- }
- finally
- {
- if (!file.delete())
- {
- file.deleteOnExit();
- }
- }
+ }
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
IOException