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$ 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.Serializable; 026import java.io.UnsupportedEncodingException; 027import java.net.URLDecoder; 028import java.util.List; 029 030import org.apache.commons.fileupload.FileUploadException; 031import org.jboss.seam.annotations.In; 032import org.jboss.seam.annotations.Name; 033import org.jboss.seam.annotations.Scope; 034import org.nuxeo.ecm.core.api.Blob; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.IdRef; 038import org.nuxeo.ecm.core.api.NuxeoException; 039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 040import org.nuxeo.ecm.platform.ui.web.api.SimpleFileManager; 041import org.nuxeo.ecm.platform.ui.web.util.FileUploadHelper; 042import org.nuxeo.ecm.platform.util.RepositoryLocation; 043import org.restlet.data.MediaType; 044import org.restlet.data.Request; 045import org.restlet.data.Response; 046 047@Name("pluginUploadRestlet") 048@Scope(EVENT) 049public class PluginUploadRestlet extends BaseNuxeoRestlet implements Serializable { 050 051 private static final long serialVersionUID = 1L; 052 053 @In(create = true) 054 protected transient NavigationContext navigationContext; 055 056 protected CoreSession documentManager; 057 058 @In(create = true) 059 protected transient SimpleFileManager FileManageActions; 060 061 @Override 062 public void handle(Request req, Response res) { 063 String repo = (String) req.getAttributes().get("repo"); 064 String docid = (String) req.getAttributes().get("docid"); 065 String returnCode = "TRANSF_ERROR"; 066 String relativePath = ""; 067 List<String> segments = req.getResourceRef().getSegments(); 068 List<String> pathElements = segments.subList(5, segments.size()); 069 070 for (String pathElement : pathElements) { 071 if (pathElement != null && !pathElement.trim().equals("")) { 072 try { 073 relativePath = relativePath + '/' + URLDecoder.decode(pathElement, "utf-8"); 074 } catch (UnsupportedEncodingException e) { 075 throw new IllegalArgumentException(e); 076 } 077 } 078 } 079 080 DocumentModel currentDocument; 081 082 try { 083 if (navigationContext.getCurrentServerLocation() == null) { 084 // init context if needed 085 navigationContext.setCurrentServerLocation(new RepositoryLocation(repo)); 086 } 087 088 documentManager = navigationContext.getOrCreateDocumentManager(); 089 currentDocument = navigationContext.getCurrentDocument(); 090 091 if (currentDocument == null || !currentDocument.getRef().toString().equals(docid)) { 092 // init context if needed 093 currentDocument = documentManager.getDocument(new IdRef(docid)); 094 navigationContext.setCurrentDocument(currentDocument); 095 } 096 } catch (NuxeoException e) { 097 handleError(res, e); 098 return; 099 } 100 101 if (currentDocument != null) { 102 List<Blob> blobs; 103 try { 104 blobs = FileUploadHelper.parseRequest(req); 105 } catch (FileUploadException | IOException e) { 106 handleError(res, e); 107 return; 108 } 109 110 Blob blob = blobs.get(0); 111 try { 112 returnCode = FileManageActions.addBinaryFileFromPlugin(blob, blob.getFilename(), relativePath); 113 } catch (NuxeoException e) { 114 handleError(res, e); 115 return; 116 } 117 } 118 res.setEntity(returnCode, MediaType.TEXT_PLAIN); 119 } 120 121}