001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.io.client; 021 022import java.io.IOException; 023import java.io.Serializable; 024import java.util.List; 025 026import javax.faces.context.FacesContext; 027import javax.servlet.ServletRequest; 028import javax.servlet.ServletResponse; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.jboss.seam.ScopeType; 035import org.jboss.seam.annotations.In; 036import org.jboss.seam.annotations.Name; 037import org.jboss.seam.annotations.Scope; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.Blobs; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.core.io.DocumentPipe; 042import org.nuxeo.ecm.core.io.DocumentReader; 043import org.nuxeo.ecm.core.io.DocumentWriter; 044import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 045import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter; 046import org.nuxeo.ecm.platform.io.selectionReader.DocumentModelListReader; 047import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 048import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants; 049import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 050import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; 051import org.nuxeo.ecm.webapp.clipboard.ClipboardActions; 052 053@Name("importExportAction") 054@Scope(ScopeType.EVENT) 055public class ImportExportActionBean implements Serializable { 056 057 private static final String RESTLET_PREFIX = "restAPI"; 058 059 private static final Log log = LogFactory.getLog(ImportExportActionBean.class); 060 061 private static final long serialVersionUID = 1770386525984671333L; 062 063 @In(create = true) 064 protected transient NavigationContext navigationContext; 065 066 @In(create = true) 067 protected transient ClipboardActions clipboardActions; 068 069 private static StringBuffer getRestletBaseURL(DocumentModel doc) { 070 StringBuffer urlb = new StringBuffer(); 071 072 urlb.append(BaseURL.getBaseURL()); 073 urlb.append(RESTLET_PREFIX); 074 urlb.append('/'); 075 urlb.append(doc.getRepositoryName()); 076 urlb.append('/'); 077 urlb.append(doc.getRef().toString()); 078 urlb.append('/'); 079 return urlb; 080 } 081 082 private static HttpServletResponse getHttpServletResponse() { 083 ServletResponse response = null; 084 final FacesContext facesContext = FacesContext.getCurrentInstance(); 085 if (facesContext != null) { 086 response = (ServletResponse) facesContext.getExternalContext().getResponse(); 087 } 088 089 if (response != null && response instanceof HttpServletResponse) { 090 return (HttpServletResponse) response; 091 } 092 return null; 093 } 094 095 private static HttpServletRequest getHttpServletRequest() { 096 ServletRequest request = null; 097 final FacesContext facesContext = FacesContext.getCurrentInstance(); 098 if (facesContext != null) { 099 request = (ServletRequest) facesContext.getExternalContext().getRequest(); 100 } 101 102 if (request != null && request instanceof HttpServletRequest) { 103 return (HttpServletRequest) request; 104 } 105 return null; 106 } 107 108 private static void handleRedirect(HttpServletResponse response, String url) throws IOException { 109 response.resetBuffer(); 110 response.sendRedirect(url); 111 response.flushBuffer(); 112 getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, true); 113 FacesContext.getCurrentInstance().responseComplete(); 114 } 115 116 public String doExportDocument() throws IOException { 117 HttpServletResponse response = getHttpServletResponse(); 118 if (response != null) { 119 handleRedirect(response, getDocumentExportURL()); 120 } 121 return null; 122 } 123 124 public String doExportFolder() throws IOException { 125 HttpServletResponse response = getHttpServletResponse(); 126 if (response != null) { 127 handleRedirect(response, getFolderExportURL()); 128 } 129 return null; 130 } 131 132 /** 133 * Returns the REST URL for export of given document. 134 * 135 * @since 5.4.2 136 * @param doc the document to export 137 * @param exportAsZip a boolean stating if export should be given in ZIP format. When exporting the tree, ZIP format 138 * is forced. 139 * @param exportAsTree a boolean stating if export should include the document children. 140 */ 141 public String getExportURL(DocumentModel doc, boolean exportAsZip, boolean exportAsTree) { 142 if (doc == null) { 143 return null; 144 } 145 StringBuffer urlb = getRestletBaseURL(doc); 146 if (exportAsTree) { 147 urlb.append("exportTree"); 148 } else { 149 if (exportAsZip) { 150 urlb.append("export?format=ZIP"); 151 } else { 152 urlb.append("export?format=XML"); 153 } 154 } 155 return urlb.toString(); 156 } 157 158 /** 159 * Generates URL to call export restlet on a leaf. 160 * 161 * @return export restlet URL 162 */ 163 public String getDocumentExportURL() { 164 return getExportURL(navigationContext.getCurrentDocument(), true, true); 165 } 166 167 /** 168 * Generates URL to call export restlet on a folder. 169 * 170 * @return export restlet URL 171 */ 172 public String getFolderExportURL() { 173 return getExportURL(navigationContext.getCurrentDocument(), true, true); 174 } 175 176 /** 177 * Returns the Rest URL for a document export in XML format 178 * 179 * @since 5.4.2 180 */ 181 public String getDocumentXMLExportURL() { 182 return getExportURL(navigationContext.getCurrentDocument(), false, false); 183 } 184 185 /** 186 * Returns the Rest URL for a document export in ZIP format 187 * 188 * @since 5.4.2 189 */ 190 public String getDocumentZIPExportURL() { 191 return getExportURL(navigationContext.getCurrentDocument(), true, false); 192 } 193 194 /** 195 * Returns the Rest URL for a document tree export in ZIP format 196 * 197 * @since 5.4.2 198 * @return 199 */ 200 public String getDocumentZIPTreeExportURL() { 201 return getExportURL(navigationContext.getCurrentDocument(), true, true); 202 } 203 204 public String exportCurrentList() { 205 List<DocumentModel> docList = clipboardActions.getCurrentSelectedList(); 206 if (docList != null) { 207 export(docList); 208 } 209 return null; 210 } 211 212 public static void export(List<DocumentModel> docList) { 213 DocumentReader reader = null; 214 DocumentWriter writer = null; 215 Blob blob = null; 216 try { 217 reader = new DocumentModelListReader(docList); 218 blob = Blobs.createBlobWithExtension("zip"); 219 writer = new NuxeoArchiveWriter(blob.getFile()); 220 DocumentPipe pipe = new DocumentPipeImpl(10); 221 pipe.setReader(reader); 222 pipe.setWriter(writer); 223 pipe.run(); 224 } catch (IOException e) { 225 log.error("Error during XML export " + e.getMessage()); 226 } finally { 227 if (reader != null) { 228 reader.close(); 229 } 230 if (writer != null) { 231 writer.close(); 232 } 233 } 234 235 if (blob != null) { 236 ComponentUtils.download(null, null, blob, "export.zip", "workListXML"); 237 if (blob.getFile() != null) { 238 blob.getFile().delete(); 239 } 240 } 241 242 } 243 244}