/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PropFindMethod.java,v 1.26 2001/07/22 04:41:55 remm Exp $
* $Revision: 1.26 $
* $Date: 2001/07/22 04:41:55 $
*
* ====================================================================
*
* 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.webdav.lib.methods;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.apache.util.XMLPrinter;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.State;
import org.apache.webdav.lib.Property;
import org.apache.webdav.lib.properties.GetLastModifiedProperty;
import org.apache.webdav.lib.properties.ResourceTypeProperty;
/**
* This class implements the WebDAV PROPFIND Method.
*
* <P> The PROPFIND method retrieves properties defined on the resource
* identified by the Request-URI, if the resource does not have any internal
* members, or on the resource identified by the Request-URI and potentially
* its member resources, if the resource is a collection that has internal
* member URIs.
*
* <P> A typical request looks like this:
*
* <PRE>
*
* PROPFIND /file HTTP/1.1
* Host: www.foo.bar
* Content-type: text/xml; charset="utf-8"
* Content-Length: xxxx
*
* <?xml version="1.0" encoding="utf-8" ?>
* <D:propfind xmlns:D="DAV:">
* <D:prop xmlns:R="http://www.foo.bar/boxschema/">
* <R:bigbox/>
* <R:author/>
* <R:DingALing/>
* <R:Random/>
* </D:prop>
* </D:propfind>
* </PRE>
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
*/
public class PropFindMethod extends XMLResponseMethodBase
implements DepthSupport {
// -------------------------------------------------------------- Constants
/**
* Request of named properties.
*/
public static final int BY_NAME = 0;
/**
* Request of all properties name and value.
*/
public static final int ALL = 1;
/**
* Request of all properties name.
*/
public static final int NAMES = 2;
// ----------------------------------------------------------- Constructors
/**
* Method constructor.
*/
public PropFindMethod() {
name = "PROPFIND";
}
/**
* Method constructor.
*/
public PropFindMethod(String path) {
super(path);
name = "PROPFIND";
}
/**
* Method constructor.
*/
public PropFindMethod(String path, int depth) {
this(path);
setDepth(depth);
}
/**
* Method constructor.
*/
public PropFindMethod(String path, int depth, int type) {
this(path);
setDepth(depth);
setType(type);
}
/**
* Method constructor.
*/
public PropFindMethod(String path, Enumeration propertyNames) {
this(path);
setDepth(1);
setPropertyNames(propertyNames);
setType(BY_NAME);
}
/**
* Method constructor.
*/
public PropFindMethod(String path, int depth, Enumeration propertyNames) {
this(path);
setDepth(depth);
setPropertyNames(propertyNames);
setType(BY_NAME);
}
// ----------------------------------------------------- Instance Variables
/**
* Type of the Propfind.
*/
protected int type = ALL;
/**
* Property name list.
*/
protected Enumeration propertyNames;
/**
* Depth.
*/
protected int depth = DEPTH_INFINITY;
/**
* The namespace abbreviation that prefixes DAV tags
*/
protected String prefix = null;
// ------------------------------------------------------------- Properties
/**
* Set header. handle the special case of Depth.
*
* @param headerName Header name
* @param headerValue Header value
*/
public void setHeader(String headerName, String headerValue) {
if (headerName.equalsIgnoreCase("Depth")){
int depth = -1;
if (headerValue.equals("0")){
depth = DEPTH_0;
}
if (headerValue.equals("1")){
depth = DEPTH_1;
}
else if (headerValue.equalsIgnoreCase("infinity")){
depth = DEPTH_INFINITY;
}
setDepth(depth);
}
else{
super.setHeader(headerName, headerValue);
}
}
/**
* Type setter.
*
* @param type New type value
*/
public void setType(int type) {
checkNotUsed();
this.type = type;
}
/**
* Type getter.
*
* @return int type value
*/
public int getType() {
return type;
}
/**
* Depth setter.
*
* @param depth New depth value
*/
public void setDepth(int depth) {
checkNotUsed();
this.depth = depth;
}
/**
* Depth getter.
*
* @return int depth value
*/
public int getDepth() {
return depth;
}
/**
* Property names setter.
*
* @param propertyNames List of the property names
*/
public void setPropertyNames(Enumeration propertyNames) {
checkNotUsed();
this.propertyNames = propertyNames;
}
// --------------------------------------------------- WebdavMethod Methods
public void recycle() {
super.recycle();
prefix = null;
}
/**
* Generate additional headers needed by the request.
*
* @param host the host
* @param state State token
*/
public void generateHeaders(String host, State state) {
super.generateHeaders(host, state);
super.setHeader("Content-Type", "text/xml; charset=utf-8");
switch (depth) {
case DEPTH_0:
super.setHeader("Depth", "0");
break;
case DEPTH_1:
super.setHeader("Depth", "1");
break;
case DEPTH_INFINITY:
super.setHeader("Depth", "infinity");
break;
}
}
/**
* Generate the query body.
*
* @return String query
*/
public String generateQuery() {
if (query != null)
return query;
XMLPrinter printer = new XMLPrinter();
printer.writeXMLHeader();
printer.writeElement("D", "DAV:", "propfind",
XMLPrinter.OPENING);
switch (type) {
case ALL:
printer.writeElement("D", "allprop", XMLPrinter.NO_CONTENT);
break;
case NAMES:
printer.writeElement("D", "propname", XMLPrinter.NO_CONTENT);
break;
case BY_NAME:
printer.writeElement("D", "prop", XMLPrinter.OPENING);
while (propertyNames.hasMoreElements()) {
String propertyName = (String) propertyNames.nextElement();
int length = propertyName.length();
boolean found = false;
int i = 1;
while (!found && (i <= length)) {
char chr = propertyName.charAt(length - i);
if (!Character.isUnicodeIdentifierPart(chr)) {
found = true;
} else {
i++;
}
}
if ((i == 1) || (i >= length)) {
printer.writeElement("D", propertyName,
XMLPrinter.NO_CONTENT);
} else {
String prefix = propertyName.substring(0, length + 1 - i);
String local = propertyName.substring(length + 1 - i);
printer.writeElement("ZZ", prefix, local,
XMLPrinter.NO_CONTENT);
}
}
printer.writeElement("D", "prop", XMLPrinter.CLOSING);
break;
}
printer.writeElement("D", "propfind", XMLPrinter.CLOSING);
query = printer.toString();
return query;
}
/**
* This method returns an enumeration of URL paths. If the PropFindMethod
* was sent to the URL of a collection, then there will be multiple URLs.
* The URLs are picked out of the <code><D:href></code> elements
* of the response.
*
* @return an enumeration of URL paths as Strings
*/
public Enumeration getAllResponseURLs() {
checkUsed();
return getResponseHashtable().keys();
}
/**
* Returns an enumeration of <code>Property</code> objects.
*/
public Enumeration getResponseProperties(String urlPath) {
checkUsed();
Response response = (Response) getResponseHashtable().get(urlPath);
if (response != null) {
return response.getProperties();
} else {
return (new Vector()).elements();
}
}
}