You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ju...@apache.org on 2001/07/18 13:36:18 UTC
cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/method AbstractCopyMoveMethod.java
juergen 01/07/18 04:36:18
Added: src/webdav/server/org/apache/slide/webdav/method
AbstractCopyMoveMethod.java
Log:
the error message generation for copy and move is identical. Implemented in this new class.
Revision Changes Path
1.1 jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractCopyMoveMethod.java
Index: AbstractCopyMoveMethod.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractCopyMoveMethod.java,v 1.1 2001/07/18 11:36:18 juergen Exp $
* $Revision: 1.1 $
* $Date: 2001/07/18 11:36:18 $
*
* ====================================================================
*
* 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.*;
/**
* COPY Method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
public abstract class AbstractCopyMoveMethod 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
/**
* COPY / MOVE method 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 AbstractCopyMoveMethod(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 {
sourceUri = requestUri;
if (sourceUri == null) {
sourceUri = "/";
}
destinationUri = req.getHeader("Destination");
String HTTPHeader = "http://";
if (destinationUri.startsWith(HTTPHeader)) {
destinationUri = destinationUri.substring(HTTPHeader.length());
}
String hostName = req.getServerName();
if ((hostName != null) && (destinationUri.startsWith(hostName))) {
destinationUri = destinationUri.substring(hostName.length());
}
if (destinationUri.indexOf(":") != (-1)) {
destinationUri = destinationUri.substring(destinationUri.indexOf(":"));
}
if (destinationUri.startsWith(":")) {
int firstSeparator = destinationUri.indexOf("/");
if (firstSeparator < 0) {
destinationUri = "/";
} else {
destinationUri = destinationUri.substring(firstSeparator);
}
}
destinationUri = URLDecode(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());
}
}
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(MacroException 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();
generateStatusText(errorMessage, getErrorMessage(ex), getErrorCode(ex));
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;
}
/**
* Get return status based on exception type.
*/
protected int getErrorCode(SlideException ex) {
try {
throw ex;
} catch(ObjectNotFoundException e) {
return WebdavStatus.SC_NOT_FOUND;
} catch(ConflictException e) {
return WebdavStatus.SC_CONFLICT;
} catch(ForbiddenException e) {
return WebdavStatus.SC_FORBIDDEN;
} catch(AccessDeniedException e) {
return WebdavStatus.SC_FORBIDDEN;
} catch(ObjectAlreadyExistsException e) {
return WebdavStatus.SC_PRECONDITION_FAILED;
} catch(ServiceAccessException e) {
return WebdavStatus.SC_BAD_GATEWAY;
} catch(LinkedObjectNotFoundException e) {
return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
} catch(RevisionNotFoundException e) {
return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
} catch(ObjectLockedException e) {
return WebdavStatus.SC_LOCKED;
} catch(SlideException e) {
return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
}
}
/**
* Get the return status message info on exception type.
*/
protected 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 e.getMessage();
} catch(LinkedObjectNotFoundException e) {
return e.getTargetUri();
} catch(RevisionNotFoundException e) {
return e.getObjectUri();
} catch(ObjectLockedException e) {
return e.getObjectUri();
} catch(SlideException e) {
return e.getMessage();
}
}
}