Changeset 30251
- Timestamp:
- 02/18/08 20:17:33 (9 months ago)
- Files:
-
- org.nuxeo.ecm.platform/branches/5.1/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRepresentation.java (added)
- org.nuxeo.ecm.platform/branches/5.1/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportResource.java (deleted)
- org.nuxeo.ecm.platform/branches/5.1/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRestlet.java (modified) (8 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
org.nuxeo.ecm.platform/branches/5.1/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRestlet.java
- Property svn:keywords set to Id
r29029 r30251 1 1 /* 2 * (C) Copyright 2006-200 7Nuxeo SAS (http://nuxeo.com/) and contributors.2 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 3 3 * 4 4 * All rights reserved. This program and the accompanying materials … … 13 13 * 14 14 * Contributors: 15 * Nuxeo - initial API and implementation 15 * Thierry Delprat 16 * Florent Guillaume 16 17 * 17 * $Id : JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier$18 * $Id$ 18 19 */ 19 20 20 21 package org.nuxeo.ecm.platform.ui.web.restAPI; 21 22 23 import static org.jboss.seam.ScopeType.STATELESS; 22 24 23 import static org.jboss.seam.ScopeType.EVENT; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.IOException; 25 import java.util.Map; 27 26 28 27 import org.jboss.seam.annotations.In; … … 33 32 import org.nuxeo.ecm.core.api.DocumentModel; 34 33 import org.nuxeo.ecm.core.api.IdRef; 35 import org.nuxeo.ecm.core.io.DocumentPipe;36 import org.nuxeo.ecm.core.io.DocumentReader;37 import org.nuxeo.ecm.core.io.DocumentWriter;38 import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;39 import org.nuxeo.ecm.core.io.impl.plugins.DocumentTreeReader;40 import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter;41 import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader;42 import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentTreeWriter;43 import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;44 34 import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 45 35 import org.nuxeo.ecm.platform.util.RepositoryLocation; 46 import org.restlet.data. MediaType;36 import org.restlet.data.Form; 47 37 import org.restlet.data.Request; 48 38 import org.restlet.data.Response; 49 39 40 import com.noelios.restlet.http.HttpConstants; 50 41 51 42 @Name("exportRestlet") 52 @Scope( EVENT)43 @Scope(STATELESS) 53 44 public class ExportRestlet extends BaseNuxeoRestlet { 54 45 … … 56 47 protected NavigationContext navigationContext; 57 48 58 protected CoreSession documentManager;59 60 49 @Override 61 50 public void handle(Request req, Response res) { 62 63 boolean exportAsTree = false; 64 boolean exportAsZip = false; 51 boolean exportAsTree; 52 boolean exportAsZip; 53 CoreSession documentManager; 54 DocumentModel root; 65 55 66 56 String action = req.getResourceRef().getSegments().get(4); … … 72 62 exportAsZip = false; 73 63 } else if (action.equals("export")) { 64 exportAsTree = false; 65 exportAsZip = false; 66 } else { 74 67 exportAsTree = false; 75 68 exportAsZip = false; … … 86 79 String repo = (String) req.getAttributes().get("repo"); 87 80 String docid = (String) req.getAttributes().get("docid"); 88 DocumentModel dm = null;89 81 90 82 if (repo == null || repo.equals("*")) { … … 98 90 documentManager = navigationContext.getOrCreateDocumentManager(); 99 91 if (docid == null || docid.equals("*")) { 100 dm= documentManager.getRootDocument();92 root = documentManager.getRootDocument(); 101 93 } else { 102 dm= documentManager.getDocument(new IdRef(docid));94 root = documentManager.getDocument(new IdRef(docid)); 103 95 } 104 96 } catch (ClientException e) { … … 107 99 } 108 100 109 DocumentReader reader = null; 110 DocumentWriter writer = null; 111 try { 112 if (exportAsTree) { 113 reader = new DocumentTreeReader(documentManager, dm, false); 114 if (!exportAsZip) { 115 ((DocumentTreeReader) reader).setInlineBlobs(true); 116 } 117 } else { 118 reader = new SingleDocumentReader(documentManager, dm); 101 if (exportAsZip) { 102 // set the content disposition and file name 103 String FILENAME = "export.zip"; 104 105 // use the Facelets APIs to set a new header 106 Map<String, Object> attributes = res.getAttributes(); 107 Form headers = (Form) attributes.get(HttpConstants.ATTRIBUTE_HEADERS); 108 if (headers == null) { 109 headers = new Form(); 119 110 } 120 121 ByteArrayOutputStream exportOut = new ByteArrayOutputStream(); 122 123 if (exportAsZip) { 124 writer = new NuxeoArchiveWriter(exportOut); 125 } else { 126 if (exportAsTree) { 127 writer = new XMLDocumentTreeWriter(exportOut); 128 } else { 129 writer = new XMLDocumentWriter(exportOut); 130 } 131 } 132 133 DocumentPipe pipe; 134 135 if (exportAsTree) { 136 pipe = new DocumentPipeImpl(10); 137 } else { 138 pipe = new DocumentPipeImpl(); 139 } 140 141 // pipe.addTransformer(transformer); 142 pipe.setReader(reader); 143 pipe.setWriter(writer); 144 pipe.run(); 145 146 if (exportAsZip) { 147 ExportResource result = new ExportResource(getContext(), req, 148 res, exportOut); 149 res.setEntity(result.getRepresentation(null)); 150 String filename="export.zip"; 151 getHttpResponse(res).setHeader("Content-Disposition", 152 "attachment; filename=\"" + filename + "\";"); 153 } else { 154 res.setEntity(exportOut.toString(), MediaType.TEXT_XML); 155 } 156 157 158 } catch (ClientException e) { 159 handleError(res, e); 160 } catch (IOException e) { 161 handleError(res, e); 162 } catch (Exception e) { 163 handleError(res, e); 164 } finally { 165 if (reader != null) { 166 reader.close(); 167 } 168 if (writer != null) { 169 writer.close(); 170 } 111 headers.add("Content-Disposition", String.format( 112 "attachment; filename=\"%s\";", FILENAME)); 113 attributes.put(HttpConstants.ATTRIBUTE_HEADERS, headers); 171 114 } 172 115 116 res.setEntity(new ExportRepresentation(exportAsTree, exportAsZip, 117 documentManager, root)); 173 118 } 174 119
