/*
* $Header: /home/cvspublic/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/MoveMethod.java,v 1.11 2001/05/17 13:37:54 juergen Exp $
* $Revision: 1.11 $
* $Date: 2001/05/17 13:37:54 $
*
* ====================================================================
*
* 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.*;
/**
* MOVE Method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
*/
public class MoveMethod extends WebdavMethod {
// ----------------------------------------------------- Instance Variables
/**
* Id of the resosurce or collection which is to be move.
*/
private String sourceUri;
/**
* Uri of the target.
*/
private String destinationUri;
/**
* Overwrite.
*/
private boolean overwrite;
// ----------------------------------------------------------- Constructors
/**
* MOVE method constructor.
*
* @param token Namespace access token
* @param req HTTP request
* @param resp HTTP response
*/
public MoveMethod(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;
}
}
/**
* Execute request.
*
* @exception WebdavException Unrecoverable error while moving the files
*/
protected void executeRequest()
throws WebdavException {
MacroParameters macroParameters = null;
if (overwrite) {
macroParameters = Macro.RECURSIVE_OVERWRITE_PARAMETERS;
} else {
macroParameters = Macro.DEFAULT_PARAMETERS;
}
try {
macro.move(slideToken, sourceUri, destinationUri, macroParameters);
if (overwrite) {
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
} else {
resp.setStatus(WebdavStatus.SC_CREATED);
}
} catch (DeleteMacroException e) {
String errorMessage = generateErrorMessage(e);
// 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);
}
} catch (CopyMacroException e) {
String errorMessage = generateErrorMessage(e);
// 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);
}
}
}
/**
* 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();
String status = new String();
try {
throw ex;
} catch(ObjectNotFoundException e) {
generateStatusText(errorMessage, e.getObjectUri(),
WebdavStatus.SC_INTERNAL_SERVER_ERROR);
} catch(AccessDeniedException e) {
generateStatusText(errorMessage, e.getObjectUri(),
WebdavStatus.SC_FORBIDDEN);
} catch(ForbiddenException e) {
generateStatusText(errorMessage, e.getObjectUri(),
WebdavStatus.SC_FORBIDDEN);
} catch(ConflictException e) {
generateStatusText(errorMessage, e.getObjectUri(),
WebdavStatus.SC_CONFLICT);
} 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;
}
}