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