Package org.apache.xindice.webadmin.webdav.components

Source Code of org.apache.xindice.webadmin.webdav.components.Move

/*
* 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: Move.java 541515 2007-05-25 02:45:06Z vgritsenko $
*/

package org.apache.xindice.webadmin.webdav.components;

import java.io.IOException;
import java.util.Map;
import java.util.Iterator;

import javax.servlet.ServletException;

import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.FaultCodes;
import org.apache.xindice.webadmin.util.DAVOperations;
import org.apache.xindice.webadmin.webdav.WebdavStatus;
import org.apache.xindice.webadmin.webdav.DAVRequest;
import org.apache.xindice.webadmin.webdav.DAVResponse;
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 Move command for WebDAV operations on
* Xindice.<br>
* <br>
* MOVE commans instructs that the collection or resource be moved to the
* URI specified in the Destination header. In case of collection, all
* resources and child collections are to be moved to locations relative
* to it, recursively through all levels of the collection hierarchy.<br>
* <br>
* MOVE can overwrite existing resource/collection, and this behavior can
* be changed by using Overwrite flag in request header.<br>
* <br>
* For collections, Depth header cannot have any value other than infinity.<br>
* <br>
* Operation possible status codes include: <ul>
* <li>
* 201 (Created) - The source resource was successfully moved, and a new
* resource was created at the destination.
*
* 204 (No Content) - The source resource was successfully moved to a
* pre-existing destination resource.
*
* 403 (Forbidden) _ The source and destination URIs are the same.
*
* 409 (Conflict) _ A resource cannot be created at the destination
* until one or more intermediate collections have been created.
*
* 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.
*
* @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 Move implements DAVComponent {
    private static final Log log = LogFactory.getLog(Move.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;
        }

        int depth = req.getDepth();
        if (depth != DAVRequest.DEPTH_INFINITY) {
            res.setStatus(WebdavStatus.SC_BAD_REQUEST);
            return;
        }

        if (name == null) { // moving collection
            try {
                Map results = DAVOperations.copy(col, req.getDestinationPath(), req.getOverwrite(), true);
                interpretResults(results, res, dest, col);
            } catch (DBException e) {
                log.error("Failed to move collection " + col.getCanonicalName(), e);
                throw new ServletException(e);
            }
        } else { // moving resource
            try {
                int status = DAVOperations.copy(col, name, req.getDestinationPath(), req.getOverwrite());
                if (status == WebdavStatus.SC_NO_CONTENT || status == WebdavStatus.SC_CREATED) {
                    col.remove(name);
                }
                res.setStatus(status);
            } catch (DBException e) {
                log.error("Failed to move resource " + name + " from collection " + col.getCanonicalName(), e);
                throw new ServletException(e);
            }
        }
    }

    private void interpretResults(Map results, DAVResponse res, String dest, Collection col) 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)) {
                if (status == WebdavStatus.SC_CREATED || status == WebdavStatus.SC_NO_CONTENT) {
                    try {
                        col.getDatabase().dropCollection(col);
                    } catch (DBException e) {
                        if (e.faultCode == FaultCodes.COL_CANNOT_DROP) {
                            // cannot drop database
                            status = WebdavStatus.SC_FORBIDDEN;
                        } else if (e.faultCode != FaultCodes.COL_COLLECTION_NOT_FOUND) {
                            status = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
                        }
                    }
                }

                res.setStatus(status);
                return;
            }

            /*
             * If an error in executing the MOVE 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();
    }
}
TOP

Related Classes of org.apache.xindice.webadmin.webdav.components.Move

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.