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
022package org.nuxeo.ecm.platform.ui.web.restAPI;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.dom4j.Element;
027import org.dom4j.dom.DOMDocument;
028import org.dom4j.dom.DOMDocumentFactory;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.PathRef;
031import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
032import org.restlet.data.CharacterSet;
033import org.restlet.data.MediaType;
034import org.restlet.data.Request;
035import org.restlet.data.Response;
036
037/**
038 * This RESTLET allows to delete documents
039 *
040 * @author jthimonier
041 */
042public class DeleteDocumentRestlet extends BaseStatelessNuxeoRestlet implements LiveEditConstants {
043
044    private static final Log log = LogFactory.getLog(DeleteDocumentRestlet.class);
045
046    @Override
047    protected void doHandleStatelessRequest(Request req, Response res) {
048
049        String repoId = (String) req.getAttributes().get("repo");
050        String docId = (String) req.getAttributes().get("docid");
051
052        DOMDocumentFactory domFactory = new DOMDocumentFactory();
053        DOMDocument result = (DOMDocument) domFactory.createDocument();
054
055        if (docId != null) {
056            // init repo and document
057            boolean initOk = initRepositoryAndTargetDocument(res, repoId, docId);
058            if (!initOk) {
059                return;
060            }
061        } else {
062            // init repo
063            boolean initOk = initRepository(res, repoId);
064            if (!initOk) {
065                return;
066            }
067
068            // init document
069            String path = getQueryParamValue(req, "path", null);
070            if (path == null) {
071                return;
072            }
073            targetDocRef = new PathRef(path);
074            try {
075                targetDocument = session.getDocument(targetDocRef);
076            } catch (NuxeoException e) {
077                handleError(result, res, "Unable to get document " + path);
078                return;
079            }
080            docId = targetDocument.getId();
081        }
082
083        try {
084            if (!session.canRemoveDocument(targetDocRef)) {
085                handleError(res, "This document can't be removed");
086                return;
087            }
088
089            session.removeDocument(targetDocRef);
090            session.save();
091
092            // build the XML response document holding the ref
093            Element docElement = result.addElement(documentTag);
094            docElement.addElement(docRefTag).setText("Document " + docId + " deleted");
095            res.setEntity(result.asXML(), MediaType.TEXT_XML);
096            res.getEntity().setCharacterSet(CharacterSet.UTF_8);
097        } catch (NuxeoException e) {
098            log.error(e.getMessage(), e);
099            handleError(res, e);
100        }
101    }
102
103}