Changeset 30252
- Timestamp:
- 02/18/08 20:36:56 (9 months ago)
- Files:
-
- org.nuxeo.ecm.platform/trunk/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRepresentation.java (copied) (copied from org.nuxeo.ecm.platform/branches/5.1/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRepresentation.java)
- org.nuxeo.ecm.platform/trunk/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportResource.java (deleted)
- org.nuxeo.ecm.platform/trunk/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/trunk/nuxeo-platform-ui-web/src/main/java/org/nuxeo/ecm/platform/ui/web/restAPI/ExportRestlet.java
- Property svn:keywords set to Id
r29019 r30252 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 22 import static org.jboss.seam.ScopeType. EVENT;23 import static org.jboss.seam.ScopeType.STATELESS; 23 24 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 25 import java.util.Map; 26 26 27 27 import org.jboss.seam.annotations.In; … … 32 32 import org.nuxeo.ecm.core.api.DocumentModel; 33 33 import org.nuxeo.ecm.core.api.IdRef; 34 import org.nuxeo.ecm.core.io.DocumentPipe;35 import org.nuxeo.ecm.core.io.DocumentReader;36 import org.nuxeo.ecm.core.io.DocumentWriter;37 import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;38 import org.nuxeo.ecm.core.io.impl.plugins.DocumentTreeReader;39 import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveWriter;40 import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader;41 import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentTreeWriter;42 import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter;43 34 import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 44 35 import org.nuxeo.ecm.platform.util.RepositoryLocation; 45 import org.restlet.data. MediaType;36 import org.restlet.data.Form; 46 37 import org.restlet.data.Request; 47 38 import org.restlet.data.Response; 48 39 40 import com.noelios.restlet.http.HttpConstants; 41 49 42 @Name("exportRestlet") 50 @Scope( EVENT)43 @Scope(STATELESS) 51 44 public class ExportRestlet extends BaseNuxeoRestlet { 52 45 … … 54 47 protected NavigationContext navigationContext; 55 48 56 protected CoreSession documentManager;57 58 49 @Override 59 50 public void handle(Request req, Response res) { 60 61 boolean exportAsTree = false; 62 boolean exportAsZip = false; 51 boolean exportAsTree; 52 boolean exportAsZip; 53 CoreSession documentManager; 54 DocumentModel root; 63 55 64 56 String action = req.getResourceRef().getSegments().get(4); … … 70 62 exportAsZip = false; 71 63 } else if (action.equals("export")) { 64 exportAsTree = false; 65 exportAsZip = false; 66 } else { 72 67 exportAsTree = false; 73 68 exportAsZip = false; … … 84 79 String repo = (String) req.getAttributes().get("repo"); 85 80 String docid = (String) req.getAttributes().get("docid"); 86 DocumentModel dm = null;87 81 88 82 if (repo == null || repo.equals("*")) { … … 96 90 documentManager = navigationContext.getOrCreateDocumentManager(); 97 91 if (docid == null || docid.equals("*")) { 98 dm= documentManager.getRootDocument();92 root = documentManager.getRootDocument(); 99 93 } else { 100 dm= documentManager.getDocument(new IdRef(docid));94 root = documentManager.getDocument(new IdRef(docid)); 101 95 } 102 96 } catch (ClientException e) { … … 105 99 } 106 100 107 DocumentReader reader = null;108 DocumentWriter writer = null;109 try {101 if (exportAsZip) { 102 // set the content disposition and file name 103 String FILENAME = "export.zip"; 110 104 111 if (exportAsTree) { 112 reader = new DocumentTreeReader(documentManager, dm, false); 113 if (!exportAsZip) { 114 ((DocumentTreeReader) reader).setInlineBlobs(true); 115 } 116 } else { 117 reader = new SingleDocumentReader(documentManager, dm); 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(); 118 110 } 119 120 ByteArrayOutputStream exportOut = new ByteArrayOutputStream(); 121 122 if (exportAsZip) { 123 writer = new NuxeoArchiveWriter(exportOut); 124 } else { 125 if (exportAsTree) { 126 writer = new XMLDocumentTreeWriter(exportOut); 127 } else { 128 writer = new XMLDocumentWriter(exportOut); 129 } 130 } 131 132 DocumentPipe pipe; 133 134 if (exportAsTree) { 135 pipe = new DocumentPipeImpl(10); 136 } else { 137 pipe = new DocumentPipeImpl(); 138 } 139 140 // pipe.addTransformer(transformer); 141 pipe.setReader(reader); 142 pipe.setWriter(writer); 143 pipe.run(); 144 145 if (exportAsZip) { 146 ExportResource result = new ExportResource(getContext(), req, 147 res, exportOut); 148 res.setEntity(result.getRepresentation(null)); 149 String filename = "export.zip"; 150 getHttpResponse(res).setHeader("Content-Disposition", 151 "attachment; filename=\"" + filename + "\";"); 152 } else { 153 res.setEntity(exportOut.toString(), MediaType.TEXT_XML); 154 } 155 156 } catch (ClientException e) { 157 handleError(res, e); 158 } catch (IOException e) { 159 handleError(res, e); 160 } catch (Exception e) { 161 handleError(res, e); 162 } finally { 163 if (reader != null) { 164 reader.close(); 165 } 166 if (writer != null) { 167 writer.close(); 168 } 111 headers.add("Content-Disposition", String.format( 112 "attachment; filename=\"%s\";", FILENAME)); 113 attributes.put(HttpConstants.ATTRIBUTE_HEADERS, headers); 169 114 } 170 115 116 res.setEntity(new ExportRepresentation(exportAsTree, exportAsZip, 117 documentManager, root)); 171 118 } 172 119
