Package org.apache.slide.webdav.method

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

/*
* $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractMultistatusResponseMethod.java,v 1.11 2001/09/04 12:04:25 juergen Exp $
* $Revision: 1.11 $
* $Date: 2001/09/04 12:04:25 $
*
* ====================================================================
*
* 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.*;

/**
* Abstract class for methods which return a multistatus error report, like
* MOVE, DELETE, COPY, LOCK.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author Juergen Pill
*/
public abstract class AbstractMultistatusResponseMethod extends WebdavMethod {
   
   
    // ----------------------------------------------------- Instance Variables
   
   
    /**
     * Id of the resosurce or collection which is to be copied.
     */
    protected String sourceUri;
   
   
    /**
     * Uri of the target.
     */
    protected String destinationUri;
   
   
    /**
     * Overwrite.
     */
    protected boolean overwrite;
   
   
    // ----------------------------------------------------------- Constructors
   
   
    /**
     * Constructor.
     *
     * @param token Namespace access token
     * @param requestUri Request URI
     * @param principal Principal object, given by the servlet container
     * @param req HTTP request
     * @param resp HTTP response
     */
    public AbstractMultistatusResponseMethod
        (NamespaceAccessToken token, HttpServletRequest req,
         HttpServletResponse resp, WebdavServletConfig config) {
        super(token, req, resp, config);
    }
   
   
    // ------------------------------------------------------ Protected Methods
   
   
    /**
     * Parse request.
     *
     * @exception WebdavException Does not happen
     */
    protected void parseRequest()
        throws WebdavException {
       
        sourceUri = requestUri;
        if (sourceUri == null) {
            sourceUri = "/";
        }
       
        destinationUri = req.getHeader("Destination");

        int protocolIndex = destinationUri.indexOf("://");
        if (protocolIndex >= 0) {
            // if the Destination URL contains the protocol, we can safely
            // trim everything upto the first "/" character after "://"
            int firstSeparator =
                destinationUri.indexOf("/", protocolIndex + 4);
            if (firstSeparator < 0) {
                destinationUri = "/";
            } else {
                destinationUri = destinationUri.substring(firstSeparator);
            }
        } else {
            String hostName = req.getServerName();
            if ((hostName != null) && (destinationUri.startsWith(hostName))) {
                destinationUri = destinationUri.substring(hostName.length());
            }

            int portIndex = destinationUri.indexOf(":");
            if (portIndex >= 0) {
                destinationUri = destinationUri.substring(portIndex);
            }

            if (destinationUri.startsWith(":")) {
                int firstSeparator = destinationUri.indexOf("/");
                if (firstSeparator < 0) {
                    destinationUri = "/";
                } else {
                    destinationUri = destinationUri.substring(firstSeparator);
                }
            }
        }
       
        destinationUri = WebdavUtils.decodeURL(destinationUri);
       
        String contextPath = req.getContextPath();
        if ((contextPath != null) &&
                (destinationUri.startsWith(contextPath))) {
            destinationUri = destinationUri.substring(contextPath.length());
        }
       
        String pathInfo = req.getPathInfo();
        if (pathInfo != null) {
            String servletPath = req.getServletPath();
            if ((servletPath != null) &&
                    (destinationUri.startsWith(servletPath))) {
                destinationUri = destinationUri
                    .substring(servletPath.length());
            }
        }
       
        destinationUri = getConfig().getScope() + destinationUri;

        String overwriteHeader = req.getHeader("Overwrite");
       
        if (overwriteHeader != null) {
            if (overwriteHeader.equalsIgnoreCase("T")) {
                overwrite = true;
            } else {
                overwrite = false;
            }
        } else {
            overwrite = true;
        }
       
    }
   
   
    /**
     * Generate an XML error message.
     *
     * @param macroException Nested exception
     * @return String XML message
     */
    protected String generateErrorMessage
        (NestedSlideException nestedException) {
       
        XMLPrinter errorMessage = new XMLPrinter();
       
        errorMessage.writeXMLHeader();
        errorMessage.writeElement("d", NodeProperty.DEFAULT_NAMESPACE,
                                  "multistatus", XMLPrinter.OPENING);
       
        Enumeration nestedExceptionsList =
            nestedException.enumerateExceptions();
        while (nestedExceptionsList.hasMoreElements()) {
           
            errorMessage.writeElement("d", "response", XMLPrinter.OPENING);
            SlideException ex =
                (SlideException) nestedExceptionsList.nextElement();
            generateStatusText(errorMessage, getErrorMessage(ex),
                               getErrorCode(ex));
            errorMessage.writeElement("d", "response", XMLPrinter.CLOSING);
           
        }
       
        errorMessage.writeElement("d", "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(WebdavUtils.encodeURL(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;
    }
   
       
   
    /**
     * Checks if a 207 should be generated. A 207 is generated when the request URI is a
     * collection and exactely one nested exception is present and the request URI and
     * the nested exception URI are identical.
     *
     * @return boolean return true, if a 207 response code should be generated
     */

    protected boolean generate207Response(boolean isCollection, NestedSlideException causeException, String resourceURI) {
        SlideException unpackedException = causeException.unpackSingleException();
        if(!(isCollection && unpackedException != null)) return false;
        return !getURI(unpackedException).equals(resourceURI);
    }
   
    // -------------------------------------------------------- Private Methods
   
    // -------------------------------------------------------- Private Methods
   
   
    /**
     * Get return status based on exception type.
     */
    protected String getErrorMessage(Throwable ex) {
        if ( !(ex instanceof SlideException) ) {
            return "";
        } else {
            return getErrorMessage((SlideException)ex);
        }
    }
   
   
   
    /**
     * Get the return status message info on exception type.
     */
    private String getErrorMessage(SlideException ex) {
        try {
            throw ex;
        } catch(ObjectNotFoundException e) {
            return e.getObjectUri();
        } catch(ConflictException e) {
            return e.getObjectUri();
        } catch(ForbiddenException e) {
            return e.getObjectUri();
        } catch(AccessDeniedException e) {
            return e.getObjectUri();
        } catch(ObjectAlreadyExistsException e) {
            return e.getObjectUri();
        } catch(ServiceAccessException e) {
            return getErrorMessage((ServiceAccessException)e);
        } catch(LinkedObjectNotFoundException e) {
            return e.getTargetUri();
        } catch(RevisionNotFoundException e) {
            return e.getObjectUri();
        } catch(ObjectLockedException e) {
            return e.getObjectUri();
        } catch(SlideException e) {
            return e.getMessage();
        }
    }
   


   
    /**
     * Get return status based on exception type.
     */
    private String getErrorMessage(ServiceAccessException ex) {
        Throwable cause = ex.getCauseException();
        if (cause == null || !(cause instanceof SlideException) )  {
            return ""// this is the default}
        } else  {
            return getErrorMessage((SlideException)cause);
        }
    }

   
    /**
     * Get return status based on exception type.
     */
    private String getURI(Throwable ex) {
        if ( !(ex instanceof SlideException) ) {
            return "";
        } else {
            return getURI((SlideException)ex);
        }
    }
   
   
    /**
     * Get the URI contained in the exception, if available.
     */
    private String getURI(SlideException ex) {
        try {
            throw ex;
        } catch(ObjectNotFoundException e) {
            return e.getObjectUri();
        } catch(ConflictException e) {
            return e.getObjectUri();
        } catch(ForbiddenException e) {
            return e.getObjectUri();
        } catch(AccessDeniedException e) {
            return e.getObjectUri();
        } catch(ObjectAlreadyExistsException e) {
            return e.getObjectUri();
        } catch(ServiceAccessException e) {
            return getURI((ServiceAccessException)ex);
        } catch(LinkedObjectNotFoundException e) {
            return e.getTargetUri();
        } catch(RevisionNotFoundException e) {
            return e.getObjectUri();
        } catch(ObjectLockedException e) {
            return e.getObjectUri();
        } catch(SlideException e) {
            return "";
        }
    }


   
    /**
     * Get return status based on exception type.
     */
    private String getURI(ServiceAccessException ex) {
        Throwable cause = ex.getCauseException();
        if (cause == null || !(cause instanceof SlideException) )  {
            return ""// this is the default}
        } else  {
            return getURI((SlideException)cause);
        }
    }

   
   
   
       
   
   
}


TOP

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

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.