001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.io.client; 023 024import java.io.IOException; 025import java.io.Serializable; 026import java.util.List; 027 028import javax.faces.context.FacesContext; 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 StringBuilder getRestletBaseURL(DocumentModel doc) { 070 StringBuilder urlb = new StringBuilder(); 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 final FacesContext facesContext = FacesContext.getCurrentInstance(); 084 return facesContext == null ? null : (HttpServletResponse) facesContext.getExternalContext().getResponse(); 085 } 086 087 private static HttpServletRequest getHttpServletRequest() { 088 final FacesContext facesContext = FacesContext.getCurrentInstance(); 089 return (HttpServletRequest) facesContext.getExternalContext().getRequest(); 090 } 091 092 private static void handleRedirect(HttpServletResponse response, String url) throws IOException { 093 response.resetBuffer(); 094 response.sendRedirect(url); 095 response.flushBuffer(); 096 getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, true); 097 FacesContext.getCurrentInstance().responseComplete(); 098 } 099 100 public String doExportDocument() throws IOException { 101 HttpServletResponse response = getHttpServletResponse(); 102 if (response != null) { 103 handleRedirect(response, getDocumentExportURL()); 104 } 105 return null; 106 } 107 108 public String doExportFolder() throws IOException { 109 HttpServletResponse response = getHttpServletResponse(); 110 if (response != null) { 111 handleRedirect(response, getFolderExportURL()); 112 } 113 return null; 114 } 115 116 /** 117 * Returns the REST URL for export of given document. 118 * 119 * @since 5.4.2 120 * @param doc the document to export 121 * @param exportAsZip a boolean stating if export should be given in ZIP format. When exporting the tree, ZIP format 122 * is forced. 123 * @param exportAsTree a boolean stating if export should include the document children. 124 */ 125 public String getExportURL(DocumentModel doc, boolean exportAsZip, boolean exportAsTree) { 126 if (doc == null) { 127 return null; 128 } 129 StringBuilder urlb = getRestletBaseURL(doc); 130 if (exportAsTree) { 131 urlb.append("exportTree"); 132 } else { 133 if (exportAsZip) { 134 urlb.append("export?format=ZIP"); 135 } else { 136 urlb.append("export?format=XML"); 137 } 138 } 139 return urlb.toString(); 140 } 141 142 /** 143 * Generates URL to call export restlet on a leaf. 144 * 145 * @return export restlet URL 146 */ 147 public String getDocumentExportURL() { 148 return getExportURL(navigationContext.getCurrentDocument(), true, true); 149 } 150 151 /** 152 * Generates URL to call export restlet on a folder. 153 * 154 * @return export restlet URL 155 */ 156 public String getFolderExportURL() { 157 return getExportURL(navigationContext.getCurrentDocument(), true, true); 158 } 159 160 /** 161 * Returns the Rest URL for a document export in XML format 162 * 163 * @since 5.4.2 164 */ 165 public String getDocumentXMLExportURL() { 166 return getExportURL(navigationContext.getCurrentDocument(), false, false); 167 } 168 169 /** 170 * Returns the Rest URL for a document export in ZIP format 171 * 172 * @since 5.4.2 173 */ 174 public String getDocumentZIPExportURL() { 175 return getExportURL(navigationContext.getCurrentDocument(), true, false); 176 } 177 178 /** 179 * Returns the Rest URL for a document tree export in ZIP format 180 * 181 * @since 5.4.2 182 * @return 183 */ 184 public String getDocumentZIPTreeExportURL() { 185 return getExportURL(navigationContext.getCurrentDocument(), true, true); 186 } 187 188 public String exportCurrentList() { 189 List<DocumentModel> docList = clipboardActions.getCurrentSelectedList(); 190 if (docList != null) { 191 export(docList); 192 } 193 return null; 194 } 195 196 public static void export(List<DocumentModel> docList) { 197 DocumentReader reader = null; 198 DocumentWriter writer = null; 199 Blob blob = null; 200 try { 201 reader = new DocumentModelListReader(docList); 202 blob = Blobs.createBlobWithExtension("zip"); 203 writer = new NuxeoArchiveWriter(blob.getFile()); 204 DocumentPipe pipe = new DocumentPipeImpl(10); 205 pipe.setReader(reader); 206 pipe.setWriter(writer); 207 pipe.run(); 208 } catch (IOException e) { 209 log.error("Error during XML export " + e.getMessage()); 210 } finally { 211 if (reader != null) { 212 reader.close(); 213 } 214 if (writer != null) { 215 writer.close(); 216 } 217 } 218 219 if (blob != null) { 220 ComponentUtils.download(null, null, blob, "export.zip", "workListXML"); 221 if (blob.getFile() != null) { 222 blob.getFile().delete(); 223 } 224 } 225 226 } 227 228}