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