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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
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.InputStream;
026import java.io.Serializable;
027import java.io.UnsupportedEncodingException;
028import java.net.URLDecoder;
029
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.Blobs;
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.core.api.PropertyException;
040import org.nuxeo.ecm.core.api.VersioningOption;
041import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
042import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
043import org.nuxeo.ecm.platform.util.RepositoryLocation;
044import org.nuxeo.runtime.api.Framework;
045import org.restlet.data.Request;
046import org.restlet.data.Response;
047
048/**
049 * Restlet to help LiveEdit clients update the blob content of a document
050 *
051 * @author Sun Tan <stan@nuxeo.com>
052 * @author Olivier Grisel <ogrisel@nuxeo.com>
053 */
054@Name("uploadFileRestlet")
055@Scope(EVENT)
056public class UploadFileRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable {
057
058    public static final String LIVED_AUTOVERSIONING_PROP = "org.nuxeo.ecm.platform.liveedit.autoversioning";
059
060    public static final String POLICY_MINOR_INCR = "minor";
061
062    private static final long serialVersionUID = -6167207806181917456L;
063
064    @In(create = true)
065    protected transient NavigationContext navigationContext;
066
067    protected CoreSession documentManager;
068
069    @SuppressWarnings("deprecation")
070    @Override
071    public void handle(Request req, Response res) {
072        String repo = (String) req.getAttributes().get("repo");
073        String docid = (String) req.getAttributes().get("docid");
074        String filename = (String) req.getAttributes().get("filename");
075        try {
076            filename = URLDecoder.decode(filename, URL_ENCODE_CHARSET);
077        } catch (UnsupportedEncodingException e) {
078            handleError(res, e);
079            return;
080        }
081
082        if (repo == null || repo.equals("*")) {
083            handleError(res, "you must specify a repository");
084            return;
085        }
086
087        DocumentModel dm = null;
088        try {
089            navigationContext.setCurrentServerLocation(new RepositoryLocation(repo));
090            documentManager = navigationContext.getOrCreateDocumentManager();
091            if (docid != null) {
092                dm = documentManager.getDocument(new IdRef(docid));
093            }
094        } catch (NuxeoException e) {
095            handleError(res, e);
096            return;
097        }
098
099        try {
100
101            String blobPropertyName = getQueryParamValue(req, BLOB_PROPERTY_NAME, null);
102            String filenamePropertyName = getQueryParamValue(req, FILENAME_PROPERTY_NAME, null);
103
104            if (blobPropertyName == null || filenamePropertyName == null) {
105                // find the names of the fields from the optional request
106                // parameters with fallback to defaults if none is provided
107                String schemaName = getQueryParamValue(req, SCHEMA, DEFAULT_SCHEMA);
108                String blobFieldName = getQueryParamValue(req, BLOB_FIELD, DEFAULT_BLOB_FIELD);
109                String filenameFieldName = getQueryParamValue(req, FILENAME_FIELD, DEFAULT_FILENAME_FIELD);
110                blobPropertyName = schemaName + ":" + blobFieldName;
111                filenamePropertyName = schemaName + ":" + filenameFieldName;
112            }
113
114            InputStream is = req.getEntity().getStream();
115
116            saveFileToDocument(filename, dm, blobPropertyName, filenamePropertyName, is);
117        } catch (NuxeoException | IOException e) {
118            handleError(res, e);
119        }
120    }
121
122    protected CoreSession getDocumentManager() {
123        return documentManager;
124    }
125
126    /**
127     * Save the file into the document.
128     */
129    protected void saveFileToDocument(String filename, DocumentModel dm, String blobPropertyName,
130            String filenamePropertyName, InputStream is) throws IOException, PropertyException {
131        // persisting the blob makes it possible to read the binary content
132        // of the request stream several times (mimetype sniffing, digest
133        // computation, core binary storage)
134        Blob blob = Blobs.createBlob(is);
135        blob.setFilename(filename);
136
137        dm.setPropertyValue(blobPropertyName, (Serializable) blob);
138        dm.setPropertyValue(filenamePropertyName, filename);
139
140        getDocumentManager().saveDocument(dm);
141        // autoversioning see https://jira.nuxeo.org/browse/NXP-5849 for more
142        // details
143        String versioningPolicy = Framework.getProperty(LIVED_AUTOVERSIONING_PROP);
144        if (doAutoMinorIncrement(versioningPolicy, dm)) {
145            if (dm.isCheckedOut()) {
146                dm.checkIn(VersioningOption.MINOR, "Live edit (UploadFileRestlet) autoversioning");
147            }
148        }
149
150        getDocumentManager().save();
151    }
152
153    /**
154     * According to the policy, decide to auto minor increment or not
155     *
156     * @param policy
157     * @return return true if the the version should be minor increment
158     */
159    protected boolean doAutoMinorIncrement(String policy, DocumentModel doc) {
160        if (POLICY_MINOR_INCR.equals(policy)) {
161            return true;
162        }
163        return false;
164
165    }
166}