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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.restAPI; 021 022import static org.jboss.seam.ScopeType.EVENT; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.Serializable; 027import java.util.List; 028 029import org.apache.commons.fileupload.FileUploadException; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.dom4j.Element; 033import org.dom4j.dom.DOMDocument; 034import org.dom4j.dom.DOMDocumentFactory; 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.CoreSession; 041import org.nuxeo.ecm.core.api.DocumentModel; 042import org.nuxeo.ecm.core.api.IdRef; 043import org.nuxeo.ecm.core.api.NuxeoException; 044import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 045import org.nuxeo.ecm.platform.ui.web.api.SimpleFileManager; 046import org.nuxeo.ecm.platform.ui.web.util.FileUploadHelper; 047import org.nuxeo.ecm.platform.util.RepositoryLocation; 048import org.restlet.data.CharacterSet; 049import org.restlet.data.MediaType; 050import org.restlet.data.Request; 051import org.restlet.data.Response; 052import org.restlet.resource.Representation; 053import org.restlet.resource.StringRepresentation; 054 055/** 056 * Restlet to import files as nuxeo documents using the pluggable FileManager service. This restlet is mainly used for 057 * desktop integration with drag and drop browser plugins. 058 * 059 * @author tdelprat 060 */ 061@Name("uploadRestlet") 062@Scope(EVENT) 063public class UploadRestlet extends BaseNuxeoRestlet implements Serializable { 064 065 private static final Log log = LogFactory.getLog(UploadRestlet.class); 066 067 private static final long serialVersionUID = -7858792615823015193L; 068 069 @In(create = true) 070 protected transient NavigationContext navigationContext; 071 072 protected CoreSession documentManager; 073 074 @In(create = true) 075 protected transient SimpleFileManager FileManageActions; 076 077 @Override 078 public void handle(Request req, Response res) { 079 String repo = (String) req.getAttributes().get("repo"); 080 String docid = (String) req.getAttributes().get("docid"); 081 String fileName = (String) req.getAttributes().get("filename"); 082 083 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 084 DOMDocument result = (DOMDocument) domFactory.createDocument(); 085 086 DocumentModel targetContainer; 087 try { 088 navigationContext.setCurrentServerLocation(new RepositoryLocation(repo)); 089 documentManager = navigationContext.getOrCreateDocumentManager(); 090 targetContainer = documentManager.getDocument(new IdRef(docid)); 091 } catch (NuxeoException e) { 092 handleError(res, e); 093 return; 094 } 095 096 if (targetContainer != null) { 097 List<Blob> blobs = null; 098 try { 099 blobs = FileUploadHelper.parseRequest(req); 100 } catch (FileUploadException | IOException e) { 101 handleError(res, e); 102 return; 103 } 104 105 if (blobs == null) { 106 // mono import 107 String outcome; 108 try { 109 Blob inputBlob; 110 try (InputStream in = req.getEntity().getStream()) { 111 inputBlob = Blobs.createBlob(in); 112 } 113 inputBlob.setFilename(fileName); 114 outcome = FileManageActions.addBinaryFileFromPlugin(inputBlob, fileName, targetContainer); 115 } catch (NuxeoException | IOException e) { 116 outcome = "ERROR : " + e.getMessage(); 117 } 118 result.addElement("upload").setText(outcome); 119 } else { 120 // multiple file upload 121 Element uploads = result.addElement("uploads"); 122 for (Blob blob : blobs) { 123 String outcome; 124 try { 125 outcome = FileManageActions.addBinaryFileFromPlugin(blob, blob.getFilename(), targetContainer); 126 } catch (NuxeoException e) { 127 log.error("error importing " + blob.getFilename() + ": " + e.getMessage(), e); 128 outcome = "ERROR : " + e.getMessage(); 129 } 130 uploads.addElement("upload").setText(outcome); 131 } 132 } 133 } 134 Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML); 135 rep.setCharacterSet(CharacterSet.UTF_8); 136 res.setEntity(rep); 137 } 138 139}