/*
* $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/AclMethod.java,v 1.3 2001/07/21 14:06:06 dirkv Exp $
* $Revision: 1.3 $
* $Date: 2001/07/21 14:06:06 $
*
* ====================================================================
*
* 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.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import org.apache.util.XMLPrinter;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.State;
import org.apache.commons.httpclient.Header;
import org.apache.webdav.lib.Ace;
import org.apache.webdav.lib.Privilege;
/**
* ACL Method.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author Dirk Verbeeck
*/
public class AclMethod
extends HttpMethodBase {
// -------------------------------------------------------------- Constants
// ----------------------------------------------------------- Constructors
/**
* Method constructor.
*/
public AclMethod() {
name = "ACL";
}
/**
* Method constructor.
*/
public AclMethod(String path) {
super(path);
name = "ACL";
}
// ----------------------------------------------------- Instance Variables
protected Vector aces = new Vector();
// --------------------------------------------------------- Public Methods
/**
* Add an ace to the ace list which will be set by the method.
*
* @param ace Access control entry
*/
public void addAce(Ace ace) {
checkNotUsed();
aces.addElement(ace);
}
// --------------------------------------------------- WebdavMethod Methods
public void recycle() {
super.recycle();
aces.clear();
}
/**
* 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");
}
/**
* 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:", "acl",
XMLPrinter.OPENING);
Enumeration aceList = aces.elements();
while (aceList.hasMoreElements()) {
Ace ace = (Ace) aceList.nextElement();
if (ace.isInherited() || ace.isProtected()) {
// draft-ietf-webdav-acl-06
// do not submit inherited and protected aces
// continue with next ace
continue;
}
printer.writeElement("D", null, "ace",
XMLPrinter.OPENING);
printer.writeElement("D", null, "principal",
XMLPrinter.OPENING);
boolean found=false;
String principal=ace.getPrincipal();
String[] types={"all","authenticated","unauthenticated","property","self"};
for (int i=0; i<types.length && found==false; i++)
{
if (types[i].equals(principal)) {
found=true;
printer.writeElement("D",null,principal, XMLPrinter.NO_CONTENT);
}
}
if (!found)
{
printer.writeElement("D", null, "href", XMLPrinter.OPENING);
printer.writeText(ace.getPrincipal());
printer.writeElement("D", null, "href", XMLPrinter.CLOSING);
}
printer.writeElement("D", null, "principal",
XMLPrinter.CLOSING);
String positive = (ace.isNegative()) ? "deny" : "grant";
printer.writeElement("D", null, positive,
XMLPrinter.OPENING);
Enumeration privilegeList = ace.enumeratePrivileges();
while (privilegeList.hasMoreElements()) {
Privilege privilege = (Privilege) privilegeList.nextElement();
printer.writeElement("D", null, "privilege",
XMLPrinter.OPENING);
printer.writeElement(null,privilege.getNamespace(),privilege.getName(), XMLPrinter.NO_CONTENT);
printer.writeElement("D", null, "privilege",
XMLPrinter.CLOSING);
}
printer.writeElement("D", null, positive,
XMLPrinter.CLOSING);
if (ace.isInherited()) {
printer.writeElement("D", null, "inherited",
XMLPrinter.NO_CONTENT);
}
printer.writeElement("D", null, "ace",
XMLPrinter.CLOSING);
}
printer.writeElement("D", "acl", XMLPrinter.CLOSING);
query = printer.toString();
return query;
}
/**
* Parse response.
*
* @param is Input stream
*/
public void parseResponse(InputStream is)
throws IOException {
// FIXME : In some cases, the response can be an XML document
}
/**
* Process response headers. The contract of this method is that it only
* parses the response headers.
*
* @param headers Headers list
*/
public void processResponseHeaders(Hashtable headers) {
}
}