Package org.apache.webdav.ant

Source Code of org.apache.webdav.ant.CollectionScanner

/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/ant/CollectionScanner.java,v 1.4 2001/05/01 21:29:40 remm Exp $
* $Revision: 1.4 $
* $Date: 2001/05/01 21:29:40 $
*
* ====================================================================
* 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/>.
*/

package org.apache.webdav.ant;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;

import org.apache.webdav.lib.Property;
import org.apache.webdav.lib.methods.PropFindMethod;
import org.apache.webdav.lib.methods.OptionsMethod;

import org.apache.webdav.lib.properties.ResourceTypeProperty;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
* Scan a collection of resources to find ones that match a specified pattern.
*
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
*/
public class CollectionScanner extends Scanner {

    private URL baseURL = null;

    private HttpClient client = null;

    private static final Vector PROPERTY_NAMES = new Vector();

    static {
        PROPERTY_NAMES.addElement("DAV:resourcetype");
    }

    /**
     * Scans the base URL for resources that match at least one include
     * pattern, and don't match any exclude patterns.
     *
     * @exception IllegalStateException when baseurl was set incorrecly
     * @exception ScanException when a WebDAV or other error occurs
     */
    public void scan() {

        if (baseURL == null) {
            throw new IllegalStateException(
                "BaseURL must be set before calling the scan() method");
        }

        // initialize member variables
        filesIncluded = new Vector();
        filesExcluded = new Vector();
        filesNotIncluded = new Vector();

        try {

            // check to make sure that DAV is supported...
            OptionsMethod options = new OptionsMethod();
            options.setPath(baseURL.getFile());
            client.startSession(baseURL.getHost(),
                                (baseURL.getPort() < 0 ? 80 : baseURL.getPort()));
            client.executeMethod(options);

            if (!options.isAllowed("PROPFIND")) {
                throw new ScanException(
                    "Web server doesn't support PROPFIND; can't find resources");
            }

            // get a list of all resources from the given URL
            PropFindMethod propFind = new PropFindMethod();
            propFind.setPath(baseURL.getFile());
            propFind.setPropertyNames(PROPERTY_NAMES.elements());
            propFind.setType(PropFindMethod.NAMES);
            client.executeMethod(propFind);

            for (Enumeration e = propFind.getAllResponseURLs();
                 e.hasMoreElements(); ) {

                String href = (String) e.nextElement();

                for (Enumeration properties =
                        propFind.getResponseProperties(href);
                     properties.hasMoreElements(); ) {

                    try {
                        ResourceTypeProperty property =
                            (ResourceTypeProperty) properties.nextElement();
                        addResource(href, property.isCollection());
                    } catch (ClassCastException exception) {
                    }
                }
            }

        } catch (ScanException e) {
            throw e;
        } catch (HttpException e) {
            throw new ScanException(e.getMessage(), e);
        } catch (UnknownHostException e) {
            throw new ScanException(e.getMessage(), e);
        } catch (IOException e) {
            throw new ScanException(e.getMessage(), e);
        }
    }

    protected void addResource(String href, boolean isCollection)
            throws ScanException {

        try {
            String url = (new URL(getBaseURL(), href)).getFile();
            String relativeURL = url.substring(getBaseURL().getFile().length());

            if (isCollection) {
                if (isIncluded(relativeURL)) {
                    if (isExcluded(relativeURL)) {
                        dirsExcluded.addElement(relativeURL);
                    } else {
                        dirsIncluded.addElement(relativeURL);
                    }
                } else {
                    dirsNotIncluded.addElement(relativeURL);
                }
            } else {
                if (isIncluded(relativeURL)) {
                    if (isExcluded(relativeURL)) {
                        filesExcluded.addElement(relativeURL);
                    } else {
                        filesIncluded.addElement(relativeURL);
                    }
                } else {
                    filesNotIncluded.addElement(relativeURL);
                }
            }

        } catch (MalformedURLException e) {
            throw new ScanException(
                "The XML response returned an invalid URL: " + e.getMessage(), e);
        }

    }

    public URL getBaseURL() {
        return this.baseURL;
    }

    public void setBaseURL(URL baseURL) {
        this.baseURL = baseURL;
    }

    public void setHttpClient(HttpClient client) {
        this.client = client;
    }

    protected String getSeparator() {
        return "/";
    }

    protected char getSeparatorChar() {
        return '/';
    }
}
TOP

Related Classes of org.apache.webdav.ant.CollectionScanner

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.