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.data.CharacterSet;
036import org.restlet.data.Form;
037import org.restlet.data.MediaType;
038import org.restlet.data.Request;
039import org.restlet.data.Response;
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        String repoId = (String) req.getAttributes().get("repo");
048        String docId = (String) req.getAttributes().get("docid");
049
050        DOMDocumentFactory domFactory = new DOMDocumentFactory();
051        DOMDocument result = (DOMDocument) domFactory.createDocument();
052
053        // init repo and document
054        boolean initOk = initRepositoryAndTargetDocument(res, repoId, docId);
055        if (!initOk) {
056            return;
057        }
058
059        try {
060            Form queryParameters = req.getResourceRef().getQueryAsForm();
061            for (String paramName : queryParameters.getNames()) {
062                if (!DOC_TYPE.equals(paramName)) {
063                    // treat all non doctype parameters as string fields
064                    targetDocument.setPropertyValue(paramName, getQueryParamValue(req, paramName, null));
065                }
066            }
067            session.saveDocument(targetDocument);
068            session.save();
069
070            // build the XML response document holding the ref
071            Element docElement = result.addElement(documentTag);
072            docElement.addElement(docRefTag).setText("Document " + docId + " has been updated");
073            res.setEntity(result.asXML(), MediaType.TEXT_XML);
074            res.getEntity().setCharacterSet(CharacterSet.UTF_8);
075        } catch (NuxeoException e) {
076            log.error(e.getMessage(), e);
077            handleError(res, e);
078        }
079    }
080
081}