Package org.apache.slide.webdav.method

Source Code of org.apache.slide.webdav.method.DeleteMethod

/*
* $Header: /home/cvspublic/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/DeleteMethod.java,v 1.8 2001/06/22 05:47:27 msmith Exp $
* $Revision: 1.8 $
* $Date: 2001/06/22 05:47:27 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution, if
*    any, must include the following acknowlegement:
*       "This product includes software developed by the
*        Apache Software Foundation (http://www.apache.org/)."
*    Alternately, this acknowlegement may appear in the software itself,
*    if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
*    Foundation" must not be used to endorse or promote products derived
*    from this software without prior written permission. For written
*    permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
*    nor may "Apache" appear in their names without prior written
*    permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation.  For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/

package org.apache.slide.webdav.method;

import java.security.Principal;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.util.XMLPrinter;
import org.apache.util.WebdavStatus;
import org.apache.slide.common.*;
import org.apache.slide.webdav.*;
import org.apache.slide.macro.*;
import org.apache.slide.lock.*;
import org.apache.slide.content.*;
import org.apache.slide.security.AccessDeniedException;
import org.apache.slide.structure.*;

/**
* DELETE method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
public class DeleteMethod extends WebdavMethod {
   
   
    // ----------------------------------------------------- Instance Variables
   
   
    /**
     * Id of the resosurce or collection which is to be deleted.
     */
    private String toDelete;
   
   
    // ----------------------------------------------------------- Constructors
   
   
    /**
     * DELETE method constructor.
     *
     * @param token Namespace access token
     * @param req HTTP request
     * @param resp HTTP response
     */
    public DeleteMethod(GenericServlet servlet, NamespaceAccessToken token,
                        HttpServletRequest req, HttpServletResponse resp) {
        super(servlet, token, req, resp);
    }
   
   
    // ------------------------------------------------------ Protected Methods
   
   
    /**
     * Parse request.
     *
     * @exception WebdavException Does not happen
     */
    protected void parseRequest()
        throws WebdavException {
        toDelete = requestUri;
        if (toDelete == null) {
            toDelete = "/";
        }
    }
   
   
    /**
     * Execute request.
     *
     * @exception WebdavException Unrecoverable error while deleting
     */
    protected void executeRequest()
    throws WebdavException {
        boolean isCollection = isCollection(toDelete);
        try {
            macro.delete(slideToken, toDelete);
            resp.setStatus(WebdavStatus.SC_NO_CONTENT);
        } catch (DeleteMacroException dme) {
      // If it's not a collection, we don't want to give a 207,
            // because it's silly, and it confuses many clients (such as
            // MS Web Folders).
      if(isCollection) {
                String errorMessage = generateErrorMessage(dme);
                // Write it on the servlet writer
                resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
                try {
                    resp.getWriter().write(errorMessage);
                } catch(IOException ex) {
                    // Critical error ... Servlet container is dead or something
                    ex.printStackTrace();
                    throw new WebdavException
                        (WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                }
            } else {
                Enumeration nestedExceptionsList =
                    dme.enumerateExceptions();
       
                SlideException ex =
                    (SlideException) nestedExceptionsList.nextElement();
                try {
                    throw ex;
                } catch(ObjectNotFoundException e) {
                    resp.setStatus(WebdavStatus.SC_NOT_FOUND);
                } catch(AccessDeniedException e) {
                    resp.setStatus(WebdavStatus.SC_FORBIDDEN);
                } catch(ObjectAlreadyExistsException e) {
                    resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                } catch(ServiceAccessException e) {
                    resp.setStatus(WebdavStatus.SC_BAD_GATEWAY);
                } catch(LinkedObjectNotFoundException e) {
                    resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                } catch(RevisionNotFoundException e) {
                    resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                } catch(ObjectLockedException e) {
                    resp.setStatus(WebdavStatus.SC_LOCKED);
                } catch(SlideException e) {
                    resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                }
           
            }
        }
       
    }
   
    /**
     * Generate an XML error message.
     *
     * @param macroException Nested exception
     * @return String XML message
     */
    protected String generateErrorMessage
        (DeleteMacroException macroException) {
       
        XMLPrinter errorMessage = new XMLPrinter();
       
        errorMessage.writeXMLHeader();
        errorMessage.writeElement("d", "DAV", "multistatus",
                                  XMLPrinter.OPENING);
       
        Enumeration nestedExceptionsList =
            macroException.enumerateExceptions();
       
        while (nestedExceptionsList.hasMoreElements()) {
           
            errorMessage.writeElement("d", "DAV", "response",
                                      XMLPrinter.OPENING);
           
            SlideException ex =
                (SlideException) nestedExceptionsList.nextElement();
           
            String status = new String();
           
            try {
                throw ex;
            } catch(ObjectNotFoundException e) {
                generateStatusText(errorMessage, e.getObjectUri(),
                                   WebdavStatus.SC_NOT_FOUND);
            } catch(AccessDeniedException e) {
                generateStatusText(errorMessage, e.getObjectUri(),
                                   WebdavStatus.SC_FORBIDDEN);
            } catch(ObjectAlreadyExistsException e) {
                generateStatusText(errorMessage, e.getObjectUri(),
                                   WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } catch(ServiceAccessException e) {
                generateStatusText(errorMessage, e.getMessage(),
                                   WebdavStatus.SC_BAD_GATEWAY);
            } catch(LinkedObjectNotFoundException e) {
                generateStatusText(errorMessage, e.getTargetUri(),
                                   WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } catch(RevisionNotFoundException e) {
                generateStatusText(errorMessage, e.getObjectUri(),
                                   WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } catch(ObjectLockedException e) {
                generateStatusText(errorMessage, e.getObjectUri(),
                                   WebdavStatus.SC_LOCKED);
            } catch(SlideException e) {
                generateStatusText(errorMessage, e.getMessage(),
                                   WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            }
           
            errorMessage.writeElement("d", "DAV", "response",
                                      XMLPrinter.CLOSING);
           
        }
       
        errorMessage.writeElement("d", "DAV", "multistatus",
                                  XMLPrinter.CLOSING);
       
        return errorMessage.toString();
       
    }
   
   
    /**
     * Generate status text.
     *
     * @param printer XML Printer
     * @param href Uri of the object
     * @param statusCode HTTP status code of the error
     */
    protected void generateStatusText(XMLPrinter printer, String href,
                                      int statusCode) {
        printer.writeElement("d", "href", XMLPrinter.OPENING);
        printer.writeText(URLEncode(href));
        printer.writeElement("d", "href", XMLPrinter.CLOSING);
        printer.writeElement("d", "status", XMLPrinter.OPENING);
        printer.writeText("HTTP/1.1 " + statusCode + " "
                          + WebdavStatus.getStatusText(statusCode));
        printer.writeElement("d", "status", XMLPrinter.CLOSING);
    }
  
   
   
    /**
     * Returns true
     */
    protected boolean methodNeedsTransactionSupport() {
        return true;
    }
   
   
  
}
TOP

Related Classes of org.apache.slide.webdav.method.DeleteMethod

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.