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.Request; 033import org.restlet.Response; 034import org.restlet.data.CharacterSet; 035import org.restlet.data.MediaType; 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 logDeprecation(); 049 050 String repoId = (String) req.getAttributes().get("repo"); 051 String docId = (String) req.getAttributes().get("docid"); 052 053 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 054 DOMDocument result = (DOMDocument) domFactory.createDocument(); 055 056 if (docId != null) { 057 // init repo and document 058 boolean initOk = initRepositoryAndTargetDocument(res, repoId, docId); 059 if (!initOk) { 060 return; 061 } 062 } else { 063 // init repo 064 boolean initOk = initRepository(res, repoId); 065 if (!initOk) { 066 return; 067 } 068 069 // init document 070 String path = getQueryParamValue(req, "path", null); 071 if (path == null) { 072 return; 073 } 074 targetDocRef = new PathRef(path); 075 try { 076 targetDocument = session.getDocument(targetDocRef); 077 } catch (NuxeoException e) { 078 handleError(result, res, "Unable to get document " + path); 079 return; 080 } 081 docId = targetDocument.getId(); 082 } 083 084 try { 085 if (!session.canRemoveDocument(targetDocRef)) { 086 handleError(res, "This document can't be removed"); 087 return; 088 } 089 090 session.removeDocument(targetDocRef); 091 session.save(); 092 093 // build the XML response document holding the ref 094 Element docElement = result.addElement(documentTag); 095 docElement.addElement(docRefTag).setText("Document " + docId + " deleted"); 096 res.setEntity(result.asXML(), MediaType.APPLICATION_XML); 097 res.getEntity().setCharacterSet(CharacterSet.UTF_8); 098 } catch (NuxeoException e) { 099 log.error(e.getMessage(), e); 100 handleError(res, e); 101 } 102 } 103 104}