Package org.apache.xindice.webadmin.util

Source Code of org.apache.xindice.webadmin.util.DAVOperations

/*
* 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: DAVOperations.java 600169 2007-12-01 17:25:52Z vgritsenko $
*/

package org.apache.xindice.webadmin.util;

import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.FaultCodes;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.core.data.Entry;
import org.apache.xindice.webadmin.webdav.WebdavStatus;
import org.apache.xindice.webadmin.Location;
import org.apache.xindice.util.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;

import java.util.HashMap;
import java.util.Map;
import java.util.Collections;

/**
* Helper for copy/move operations.
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @version $Revision: 600169 $, $Date: 2007-12-01 12:25:52 -0500 (Sat, 01 Dec 2007) $
*/
public class DAVOperations {

    private static final Log log = LogFactory.getLog(DAVOperations.class);

    /**
     * Copies collection to the new destination
     *
     * @param col Collection to be copied
     * @param dest Destination path (includes DB name)
     * @param overwrite true if previously existed collection at dest path can be overwritten
     * @return map containing status for a path
     * @throws DBException
     */
    public static Map copy(Collection col, String dest, boolean overwrite, boolean deep) throws DBException {

        Location destLocation = new Location(dest);
        Collection destCollection = destLocation.getCollection();
        String destName = destLocation.getName();
        Map results = new HashMap();

        if (destLocation.isRoot()) {
            results.put(dest, new Integer(WebdavStatus.SC_BAD_REQUEST));
            return results;
        } else if (destCollection == null) {
            // either intermidiate collection does not exist or destination points to
            // the first level collection (database).
            results.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
            return results;
        } else if (deep && destCollection.getCanonicalName().startsWith(col.getCanonicalName())) {
            // destination points to a location inside target collection subtree.
            // deep copy does not make sense in that case
            results.put(dest, new Integer(WebdavStatus.SC_FORBIDDEN));
            return results;
        }

        // FIXME: the operations are not atomic, they can fail because of another
        // thread deleting or creating collections. Introduce locks.
        if (destName != null) { // destination collection does not exist

            results.putAll(copyCollection(col, destCollection, destName, deep));
            if (results.size() == 0) {
                results.put(dest, new Integer(WebdavStatus.SC_CREATED));
            }
        } else { // existing collection

            if (overwrite) {
                // overwrites the collection
                String name = destCollection.getName();
                Collection parent = destCollection.getParentCollection();
                // delete collection
                parent.dropCollection(destCollection);
                results.putAll(copyCollection(col, parent, name, deep));
                if (results.size() == 0) {
                    results.put(dest, new Integer(WebdavStatus.SC_NO_CONTENT));
                }
            } else {
                results.put(dest, new Integer(WebdavStatus.SC_PRECONDITION_FAILED));
            }

        }

        return results;
    }

    /**
     * Copies a resource to a new location.
     *
     * @param col Source collection
     * @param name Resource name
     * @param dest Path to the new location. It includes db name and new resource name
     * @param overwrite true if previously existed resource at dest path can be overwritten
     * @return status
     * @throws DBException
     */
    public static int copy(Collection col, String name, String dest, boolean overwrite) throws DBException {

        Location destLocation = new Location(dest);
        Collection destCollection = destLocation.getCollection();
        String destName = destLocation.getName();

        if (destLocation.isRoot()) {
            // resource cannot be added to root directly, database has to be created manually
            return WebdavStatus.SC_FORBIDDEN;
        } else if (destCollection == null) {
            return WebdavStatus.SC_CONFLICT;
        } else if (col == destCollection && name.equals(destName)) {
            return WebdavStatus.SC_FORBIDDEN;
        }

        // get source
        Entry object = col.getEntry(name);
        if (object == null) {
            return WebdavStatus.SC_NOT_FOUND;
        }

        int status;
        try {
            if (object.getEntryType() == Entry.BINARY) { // insert Binary
                if (overwrite) {
                    if (destCollection.setBinary(new Key(destName), (byte[]) object.getValue())) {
                        status = WebdavStatus.SC_CREATED;
                    } else {
                        status = WebdavStatus.SC_NO_CONTENT;
                    }
                } else {
                    destCollection.insertBinary(destName, (byte[]) object.getValue());
                    status = WebdavStatus.SC_CREATED;
                }
            } else if (object.getEntryType() == Entry.DOCUMENT) {
                if (overwrite) {
                    if (destCollection.setDocument(new Key(destName), (Document) object.getValue())) {
                        status = WebdavStatus.SC_CREATED;
                    } else {
                        status = WebdavStatus.SC_NO_CONTENT;
                    }
                } else {
                    destCollection.insertDocument(destName, (Document) object.getValue());
                    status = WebdavStatus.SC_CREATED;
                }
            } else {
                // placeholder, won't get here
                status = WebdavStatus.SC_FORBIDDEN;
            }
        } catch (DBException e) {
            if (e.faultCode == FaultCodes.COL_DUPLICATE_RESOURCE) {
                status = WebdavStatus.SC_PRECONDITION_FAILED;
            } else if (e.faultCode == FaultCodes.COL_NO_FILER) {
                status = WebdavStatus.SC_FORBIDDEN;
            } else if (e.faultCode == FaultCodes.COL_COLLECTION_NOT_FOUND) {
                status = WebdavStatus.SC_CONFLICT;
            } else if (e.faultCode == FaultCodes.COL_CANNOT_STORE) {
                if (log.isDebugEnabled()) {
                    log.debug("Collection Inline Metadata is not enabled!");
                }
                status = WebdavStatus.SC_FORBIDDEN;
            } else {
                throw e;
            }
        }

        return status;
    }

    private static Map copyCollection(Collection col, Collection parent, String name, boolean deep)
            throws DBException {

        Configuration config = CollectionConfigurationHelper.copyConfiguration(name, col.getConfig());
        Collection newCol = parent.createCollection(name, config);

        if (deep) {
            return copyCollectionContent(col, newCol);
        } else {
            return Collections.EMPTY_MAP;
        }
    }

    private static Map copyCollectionContent(Collection srcCol, Collection destCol) throws DBException {
        HashMap result = new HashMap();

        // copy resources
        String[] resList = srcCol.listDocuments();
        for (int i = 0; i < resList.length; i++) {
            Entry object = srcCol.getEntry(resList[i]);
            if (object.getEntryType() == Entry.DOCUMENT) {
                destCol.setDocument(resList[i], (Document) object.getValue());
            } else {
                destCol.setBinary(resList[i], (byte[]) object.getValue());
            }
        }

        // copy child collections content collections
        /* From the specification:
           If an error occurs while copying an internal collection, the server MUST NOT
           copy any resources identified by members of this collection (i.e., the server
           must skip this subtree), as this would create an inconsistent namespace.
           After detecting an error, the COPY operation SHOULD try to finish as much
           of the original copy operation as possible (i.e., the server should still
           attempt to copy other subtrees and their members, that are not descendents
           of an error-causing collection).
          */
        String[] colList = srcCol.listCollections();
        for (int i = 0; i < colList.length; i++) {
            Collection srcChild = srcCol.getCollection(colList[i]);
            Collection destChild = destCol.getCollection(colList[i]);
            try {
                result.putAll(copyCollectionContent(srcChild, destChild));
            } catch (DBException e) {
                int code;
                if (e.faultCode == FaultCodes.COL_NO_FILER) {
                    code = WebdavStatus.SC_FORBIDDEN;
                } else if (e.faultCode == FaultCodes.COL_COLLECTION_NOT_FOUND) {
                    code = WebdavStatus.SC_CONFLICT;
                } else if (e.faultCode == FaultCodes.COL_CANNOT_STORE) {
                    if (log.isDebugEnabled()) {
                        log.debug("Collection Inline Metadata is not enabled!");
                    }
                    code = WebdavStatus.SC_FORBIDDEN;
                } else {
                    throw e;
                }

                result.put(destChild.getCanonicalName(), new Integer(code));
            }
        }

        return result;
    }
}
TOP

Related Classes of org.apache.xindice.webadmin.util.DAVOperations

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.