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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI;
023
024import java.io.IOException;
025import java.io.Serializable;
026import java.util.List;
027
028import org.apache.commons.fileupload.FileUploadException;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.dom4j.Element;
032import org.dom4j.dom.DOMDocument;
033import org.dom4j.dom.DOMDocumentFactory;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.CloseableCoreSession;
036import org.nuxeo.ecm.core.api.CoreInstance;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
041import org.nuxeo.ecm.platform.filemanager.api.FileManager;
042import org.nuxeo.ecm.platform.ui.web.util.FileUploadHelper;
043import org.nuxeo.runtime.api.Framework;
044import org.restlet.Request;
045import org.restlet.Response;
046import org.restlet.data.CharacterSet;
047import org.restlet.data.MediaType;
048import org.restlet.representation.Representation;
049import org.restlet.representation.StringRepresentation;
050
051/**
052 * Restlet to import files as nuxeo documents using the pluggable FileManager service. This restlet is mainly used for
053 * desktop integration with drag and drop browser plugins.
054 *
055 * @author tdelprat
056 */
057public class UploadRestlet extends BaseNuxeoRestlet implements Serializable {
058
059    private static final Log log = LogFactory.getLog(UploadRestlet.class);
060
061    private static final long serialVersionUID = -7858792615823015193L;
062
063    @Override
064    public void handle(Request req, Response res) {
065        logDeprecation();
066        String repo = (String) req.getAttributes().get("repo");
067        String docid = (String) req.getAttributes().get("docid");
068        String fileName = (String) req.getAttributes().get("filename");
069
070        DOMDocumentFactory domFactory = new DOMDocumentFactory();
071        DOMDocument result = (DOMDocument) domFactory.createDocument();
072
073        DocumentModel targetContainer;
074        try (CloseableCoreSession session = CoreInstance.openCoreSession(repo)) {
075            try {
076                targetContainer = session.getDocument(new IdRef(docid));
077            } catch (NuxeoException e) {
078                handleError(res, e);
079                return;
080            }
081
082            List<Blob> blobs = null;
083            try {
084                blobs = FileUploadHelper.parseRequest(req);
085            } catch (FileUploadException | IOException e) {
086                handleError(res, e);
087                return;
088            }
089
090            if (!FileUploadHelper.isMultipartRequest(req)) {
091                // mono import
092                String outcome;
093                try {
094                    Blob inputBlob = blobs.get(0);
095                    inputBlob.setFilename(fileName);
096                    outcome = addBinaryFileFromPlugin(inputBlob, targetContainer);
097                } catch (NuxeoException | IOException e) {
098                    outcome = "ERROR : " + e.getMessage();
099                }
100                result.addElement("upload").setText(outcome);
101            } else {
102                // multiple file upload
103                Element uploads = result.addElement("uploads");
104                for (Blob blob : blobs) {
105                    String outcome;
106                    try {
107                        outcome = addBinaryFileFromPlugin(blob, targetContainer);
108                    } catch (NuxeoException | IOException e) {
109                        log.error("error importing " + blob.getFilename() + ": " + e.getMessage(), e);
110                        outcome = "ERROR : " + e.getMessage();
111                    }
112                    uploads.addElement("upload").setText(outcome);
113                }
114            }
115        }
116        Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML);
117        rep.setCharacterSet(CharacterSet.UTF_8);
118        res.setEntity(rep);
119    }
120
121    protected String addBinaryFileFromPlugin(Blob blob, DocumentModel folder) throws IOException {
122        FileManager fileManager = Framework.getService(FileManager.class);
123        FileImporterContext context = FileImporterContext.builder(folder.getCoreSession(), blob,
124                folder.getPathAsString()).overwrite(true).build();
125        DocumentModel doc = fileManager.createOrUpdateDocument(context);
126        return doc.getName();
127    }
128
129}