001/*
002 * (C) Copyright 2006-2009 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
022/**
023 * This RESTLET allows to update document properties
024 * @author jthimonier
025 */
026package org.nuxeo.ecm.platform.ui.web.restAPI;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.dom4j.Element;
031import org.dom4j.dom.DOMDocument;
032import org.dom4j.dom.DOMDocumentFactory;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
035import org.restlet.Request;
036import org.restlet.Response;
037import org.restlet.data.CharacterSet;
038import org.restlet.data.Form;
039import org.restlet.data.MediaType;
040
041public class UpdateDocumentRestlet extends BaseStatelessNuxeoRestlet implements LiveEditConstants {
042
043    private static final Log log = LogFactory.getLog(UpdateDocumentRestlet.class);
044
045    @Override
046    protected void doHandleStatelessRequest(Request req, Response res) {
047        logDeprecation();
048        String repoId = (String) req.getAttributes().get("repo");
049        String docId = (String) req.getAttributes().get("docid");
050
051        DOMDocumentFactory domFactory = new DOMDocumentFactory();
052        DOMDocument result = (DOMDocument) domFactory.createDocument();
053
054        // init repo and document
055        boolean initOk = initRepositoryAndTargetDocument(res, repoId, docId);
056        if (!initOk) {
057            return;
058        }
059
060        try {
061            Form queryParameters = req.getResourceRef().getQueryAsForm();
062            for (String paramName : queryParameters.getNames()) {
063                if (!DOC_TYPE.equals(paramName)) {
064                    // treat all non doctype parameters as string fields
065                    targetDocument.setPropertyValue(paramName, getQueryParamValue(req, paramName, null));
066                }
067            }
068            session.saveDocument(targetDocument);
069            session.save();
070
071            // build the XML response document holding the ref
072            Element docElement = result.addElement(documentTag);
073            docElement.addElement(docRefTag).setText("Document " + docId + " has been updated");
074            res.setEntity(result.asXML(), MediaType.APPLICATION_XML);
075            res.getEntity().setCharacterSet(CharacterSet.UTF_8);
076        } catch (NuxeoException e) {
077            log.error(e.getMessage(), e);
078            handleError(res, e);
079        }
080    }
081
082}