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$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI;
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.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.CloseableCoreSession;
033import org.nuxeo.ecm.core.api.CoreInstance;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.ecm.core.api.PathRef;
038import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
039import org.nuxeo.ecm.platform.filemanager.api.FileManager;
040import org.nuxeo.ecm.platform.ui.web.util.FileUploadHelper;
041import org.nuxeo.runtime.api.Framework;
042import org.restlet.Request;
043import org.restlet.Response;
044import org.restlet.data.CharacterSet;
045import org.restlet.data.MediaType;
046import org.restlet.representation.Representation;
047import org.restlet.representation.StringRepresentation;
048
049public class PluginUploadRestlet extends BaseNuxeoRestlet implements Serializable {
050
051    private static final long serialVersionUID = 1L;
052
053    @Override
054    public void handle(Request req, Response res) {
055        logDeprecation();
056        String repo = (String) req.getAttributes().get("repo");
057        String docid = (String) req.getAttributes().get("docid");
058        String returnCode = "TRANSF_ERROR";
059        String relativePath = "";
060        List<String> segments = req.getResourceRef().getSegments();
061        int pos = segments.indexOf("restAPI") + 4;
062        List<String> pathElements = segments.subList(pos, segments.size() - 1);
063        String fileName = segments.get(segments.size() - 1);
064
065        for (String pathElement : pathElements) {
066            if (pathElement != null && !pathElement.trim().equals("")) {
067                try {
068                    relativePath = relativePath + '/' + URLDecoder.decode(pathElement, "utf-8");
069                } catch (UnsupportedEncodingException e) {
070                    throw new IllegalArgumentException(e);
071                }
072            }
073        }
074
075        try (CloseableCoreSession session = CoreInstance.openCoreSession(repo)) {
076            DocumentModel doc = session.getDocument(new IdRef(docid));
077            DocumentModel folder = session.getDocument(new PathRef(doc.getPathAsString() + relativePath));
078            List<Blob> blobs;
079            try {
080                blobs = FileUploadHelper.parseRequest(req);
081            } catch (FileUploadException | IOException e) {
082                handleError(res, e);
083                return;
084            }
085
086            Blob blob = blobs.get(0);
087            try {
088                blob.setFilename(fileName);
089                returnCode = addBinaryFileFromPlugin(blob, folder);
090            } catch (NuxeoException e) {
091                handleError(res, e);
092                return;
093            }
094        } catch (NuxeoException | IOException e) {
095            handleError(res, e);
096            return;
097        }
098
099        Representation rep = new StringRepresentation(returnCode, MediaType.TEXT_PLAIN);
100        rep.setCharacterSet(CharacterSet.UTF_8);
101        res.setEntity(rep);
102    }
103
104    protected String addBinaryFileFromPlugin(Blob blob, DocumentModel folder) throws IOException {
105        FileManager fileManager = Framework.getService(FileManager.class);
106        FileImporterContext context = FileImporterContext.builder(folder.getCoreSession(), blob,
107                folder.getPathAsString()).overwrite(true).build();
108        DocumentModel doc = fileManager.createOrUpdateDocument(context);
109        return doc.getName();
110    }
111
112}