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.ServletRequest; 030import javax.servlet.ServletResponse; 031import javax.servlet.http.HttpServletRequest; 032import javax.servlet.http.HttpServletResponse; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.jboss.seam.ScopeType; 037import org.jboss.seam.annotations.In; 038import org.jboss.seam.annotations.Name; 039import org.jboss.seam.annotations.Scope; 040import org.nuxeo.ecm.core.api.Blob; 041import org.nuxeo.ecm.core.api.Blobs; 042import org.nuxeo.ecm.core.api.DocumentModel; 043import org.nuxeo.ecm.core.io.DocumentPipe; 044import org.nuxeo.ecm.core.io.DocumentReader; 045import org.nuxeo.ecm.core.io.DocumentWriter; 046import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; 047import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter; 048import org.nuxeo.ecm.platform.io.selectionReader.DocumentModelListReader; 049import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 050import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants; 051import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 052import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; 053import org.nuxeo.ecm.webapp.clipboard.ClipboardActions; 054 055@Name("importExportAction") 056@Scope(ScopeType.EVENT) 057public class ImportExportActionBean implements Serializable { 058 059 private static final String RESTLET_PREFIX = "restAPI"; 060 061 private static final Log log = LogFactory.getLog(ImportExportActionBean.class); 062 063 private static final long serialVersionUID = 1770386525984671333L; 064 065 @In(create = true) 066 protected transient NavigationContext navigationContext; 067 068 @In(create = true) 069 protected transient ClipboardActions clipboardActions; 070 071 private static StringBuffer getRestletBaseURL(DocumentModel doc) { 072 StringBuffer urlb = new StringBuffer(); 073 074 urlb.append(BaseURL.getBaseURL()); 075 urlb.append(RESTLET_PREFIX); 076 urlb.append('/'); 077 urlb.append(doc.getRepositoryName()); 078 urlb.append('/'); 079 urlb.append(doc.getRef().toString()); 080 urlb.append('/'); 081 return urlb; 082 } 083 084 private static HttpServletResponse getHttpServletResponse() { 085 ServletResponse response = null; 086 final FacesContext facesContext = FacesContext.getCurrentInstance(); 087 if (facesContext != null) { 088 response = (ServletResponse) facesContext.getExternalContext().getResponse(); 089 } 090 091 if (response != null && response instanceof HttpServletResponse) { 092 return (HttpServletResponse) response; 093 } 094 return null; 095 } 096 097 private static HttpServletRequest getHttpServletRequest() { 098 ServletRequest request = null; 099 final FacesContext facesContext = FacesContext.getCurrentInstance(); 100 if (facesContext != null) { 101 request = (ServletRequest) facesContext.getExternalContext().getRequest(); 102 } 103 104 if (request != null && request instanceof HttpServletRequest) { 105 return (HttpServletRequest) request; 106 } 107 return null; 108 } 109 110 private static void handleRedirect(HttpServletResponse response, String url) throws IOException { 111 response.resetBuffer(); 112 response.sendRedirect(url); 113 response.flushBuffer(); 114 getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, true); 115 FacesContext.getCurrentInstance().responseComplete(); 116 } 117 118 public String doExportDocument() throws IOException { 119 HttpServletResponse response = getHttpServletResponse(); 120 if (response != null) { 121 handleRedirect(response, getDocumentExportURL()); 122 } 123 return null; 124 } 125 126 public String doExportFolder() throws IOException { 127 HttpServletResponse response = getHttpServletResponse(); 128 if (response != null) { 129 handleRedirect(response, getFolderExportURL()); 130 } 131 return null; 132 } 133 134 /** 135 * Returns the REST URL for export of given document. 136 * 137 * @since 5.4.2 138 * @param doc the document to export 139 * @param exportAsZip a boolean stating if export should be given in ZIP format. When exporting the tree, ZIP format 140 * is forced. 141 * @param exportAsTree a boolean stating if export should include the document children. 142 */ 143 public String getExportURL(DocumentModel doc, boolean exportAsZip, boolean exportAsTree) { 144 if (doc == null) { 145 return null; 146 } 147 StringBuffer urlb = getRestletBaseURL(doc); 148 if (exportAsTree) { 149 urlb.append("exportTree"); 150 } else { 151 if (exportAsZip) { 152 urlb.append("export?format=ZIP"); 153 } else { 154 urlb.append("export?format=XML"); 155 } 156 } 157 return urlb.toString(); 158 } 159 160 /** 161 * Generates URL to call export restlet on a leaf. 162 * 163 * @return export restlet URL 164 */ 165 public String getDocumentExportURL() { 166 return getExportURL(navigationContext.getCurrentDocument(), true, true); 167 } 168 169 /** 170 * Generates URL to call export restlet on a folder. 171 * 172 * @return export restlet URL 173 */ 174 public String getFolderExportURL() { 175 return getExportURL(navigationContext.getCurrentDocument(), true, true); 176 } 177 178 /** 179 * Returns the Rest URL for a document export in XML format 180 * 181 * @since 5.4.2 182 */ 183 public String getDocumentXMLExportURL() { 184 return getExportURL(navigationContext.getCurrentDocument(), false, false); 185 } 186 187 /** 188 * Returns the Rest URL for a document export in ZIP format 189 * 190 * @since 5.4.2 191 */ 192 public String getDocumentZIPExportURL() { 193 return getExportURL(navigationContext.getCurrentDocument(), true, false); 194 } 195 196 /** 197 * Returns the Rest URL for a document tree export in ZIP format 198 * 199 * @since 5.4.2 200 * @return 201 */ 202 public String getDocumentZIPTreeExportURL() { 203 return getExportURL(navigationContext.getCurrentDocument(), true, true); 204 } 205 206 public String exportCurrentList() { 207 List<DocumentModel> docList = clipboardActions.getCurrentSelectedList(); 208 if (docList != null) { 209 export(docList); 210 } 211 return null; 212 } 213 214 public static void export(List<DocumentModel> docList) { 215 DocumentReader reader = null; 216 DocumentWriter writer = null; 217 Blob blob = null; 218 try { 219 reader = new DocumentModelListReader(docList); 220 blob = Blobs.createBlobWithExtension("zip"); 221 writer = new NuxeoArchiveWriter(blob.getFile()); 222 DocumentPipe pipe = new DocumentPipeImpl(10); 223 pipe.setReader(reader); 224 pipe.setWriter(writer); 225 pipe.run(); 226 } catch (IOException e) { 227 log.error("Error during XML export " + e.getMessage()); 228 } finally { 229 if (reader != null) { 230 reader.close(); 231 } 232 if (writer != null) { 233 writer.close(); 234 } 235 } 236 237 if (blob != null) { 238 ComponentUtils.download(null, null, blob, "export.zip", "workListXML"); 239 if (blob.getFile() != null) { 240 blob.getFile().delete(); 241 } 242 } 243 244 } 245 246}