/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: Copy.java 541515 2007-05-25 02:45:06Z vgritsenko $
*/
package org.apache.xindice.webadmin.webdav.components;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.webadmin.util.DAVOperations;
import org.apache.xindice.webadmin.webdav.DAVRequest;
import org.apache.xindice.webadmin.webdav.DAVResponse;
import org.apache.xindice.webadmin.webdav.WebdavStatus;
import org.apache.xindice.webadmin.PartialResponse;
import org.apache.xindice.webadmin.Location;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class implements the Copy command for WebDAV operations on
* Xindice. <br>
* <br>
* COPY commans instructs that the collection or resource be moved to
* the URI specified in the Destination header. It can overwrite
* existing collection/resource, and this behavior can be changed by
* using Overwrite flag in request header.<br>
* <br>
* COPY request for collection can have header Depth set to 0 or
* infinity, where infinity means that the entire subtree of child
* collections and resources will be copied to the new location,
* and 0 means only collection itself will be copied.<br>
* <br>
* Operation possible status codes include: <ul>
* <li>
* 201 (Created) - The source resource was successfully copied. The
* copy operation resulted in the creation of a new resource.
* <li>
* 204 (No Content) - The source resource was successfully copied to a
* pre-existing destination resource.
* <li>
* 403 (Forbidden) - The source and destination URIs are the same.
* <li>
* 409 (Conflict) - A resource cannot be created at the destination
* until one or more intermediate collections have been created.
* <li>
* 412 (Precondition Failed) - The server was unable to maintain the
* liveness of the properties listed in the propertybehavior XML element
* or the Overwrite header is "F" and the state of the destination
* resource is non-null.
* </ul>
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
* @version $Revision: 541515 $, $Date: 2007-05-24 22:45:06 -0400 (Thu, 24 May 2007) $
*/
public class Copy implements DAVComponent {
private static final Log log = LogFactory.getLog(Copy.class);
public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
if (target.isRoot()) {
res.setStatus(WebdavStatus.SC_FORBIDDEN);
return;
}
Collection col = target.getCollection();
String name = target.getName();
if (col == null) {
res.setStatus(WebdavStatus.SC_NOT_FOUND);
return;
}
String dest = req.getDestinationPath();
if (dest == null) {
res.setStatus(WebdavStatus.SC_BAD_REQUEST);
return;
}
if (dest.equals(req.getPath())) {
res.setStatus(WebdavStatus.SC_FORBIDDEN);
return;
}
if (name == null) { // copying collection
try {
boolean deep = req.getDepth() != 0;
Map results = DAVOperations.copy(col, dest, req.getOverwrite(), deep);
interpretResults(results, res, dest);
} catch (DBException e) {
log.error("Failed to copy collection " + col.getCanonicalName(), e);
throw new ServletException(e);
}
} else { // copying resource
try {
int status = DAVOperations.copy(col, name, dest, req.getOverwrite());
res.setStatus(status);
} catch (DBException e) {
log.error("Failed to copy resource " + name + " from collection " + col.getCanonicalName(), e);
throw new ServletException(e);
}
}
}
private void interpretResults(Map results, DAVResponse res, String dest) throws IOException {
for (Iterator i = results.keySet().iterator(); i.hasNext(); ) {
String url = (String) i.next();
int status = ((Integer) results.get(url)).intValue();
if (url.equals(dest)) {
res.setStatus(status);
return;
}
/*
* If an error in executing the COPY method occurs with a resource other
* than the resource identified in the Request-URI then the response
* MUST be a 207 (Multi-Status).
*/
PartialResponse part = new PartialResponse(dest);
String st = res.getProtocol() + " " + status + " " + WebdavStatus.getStatusText(status);
part.addContent("status", st);
res.sendMultiStatusResponse(part);
}
res.closeMultistatusResponse();
}
}