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