KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > properties > AclProperty


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/properties/AclProperty.java,v 1.5 2004/08/02 15:45:50 unico Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/02 15:45:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.properties;
25
26 import java.util.ArrayList JavaDoc;
27 import org.apache.commons.httpclient.URIException;
28 import org.apache.commons.httpclient.util.URIUtil;
29 import org.apache.webdav.lib.Ace;
30 import org.apache.webdav.lib.BaseProperty;
31 import org.apache.webdav.lib.Privilege;
32 import org.apache.webdav.lib.ResponseEntity;
33 import org.apache.webdav.lib.util.DOMUtils;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * This interface models the <code>&lt;D:acl&gt;</code> property, which is
39  * defined in the WebDAV Access Control Protocol specification.
40  *
41  * @version $Revision: 1.5 $
42  */

43 public class AclProperty extends BaseProperty {
44
45
46     // -------------------------------------------------------------- Constants
47

48
49     /**
50      * The property name.
51      */

52     public static final String JavaDoc TAG_NAME = "acl";
53
54
55     // ----------------------------------------------------------- Constructors
56

57
58     /**
59      * Default constructor for the property.
60      */

61     public AclProperty(ResponseEntity response, Element JavaDoc element) {
62         super(response, element);
63     }
64
65
66     // --------------------------------------------------------- Public Methods
67

68
69     /**
70      * Returns the Aces present in this acl property.
71      *
72      * @return Ace[] An ace array or null when there is no ace.
73      */

74     public Ace[] getAces() {
75         NodeList JavaDoc children = element.getChildNodes();
76         if (children == null || children.getLength() == 0)
77             return null;
78         ArrayList JavaDoc aces = new ArrayList JavaDoc();
79         for (int i = 0; i < children.getLength(); i++) {
80             try {
81                 Element JavaDoc child = (Element JavaDoc) children.item(i);
82                 String JavaDoc namespace = DOMUtils.getElementNamespaceURI(child);
83                 if (namespace != null && namespace.equals("DAV:")) {
84                     String JavaDoc localName = DOMUtils.getElementLocalName(child);
85                     if ("ace".equals(localName)) {
86                         aces.add(parseAce(child));
87                     }
88                 }
89             } catch (ClassCastException JavaDoc e) {
90             }
91         }
92         return (Ace[]) aces.toArray(new Ace[aces.size()]);
93     }
94
95
96     // ------------------------------------------------------ Protected Methods
97

98
99     /**
100      * Parse an ace.
101      */

102     protected Ace parseAce(Element JavaDoc element) {
103
104         String JavaDoc principal = null;
105         Element JavaDoc child = DOMUtils.getFirstElement(element, "DAV:", "principal");
106         if (child == null) {
107             System.err.println("Error: mandatory element <principal> is missing !");
108             System.err.println("element: " + element);
109             return null;
110         }
111
112         Element JavaDoc href = DOMUtils.getFirstElement(child, "DAV:", "href");
113         if (href != null) {
114             principal = DOMUtils.getTextValue(href);
115             try {
116                 principal = URIUtil.decode(principal);
117             } catch (URIException e) {
118                 System.err.println("Warning: decoding href element failed!");
119                 System.err.println("reason: " + e.getReason());
120             }
121         }
122
123         String JavaDoc[] types={"all","authenticated","unauthenticated","property","self"};
124         for (int i=0 ; i<types.length && principal==null ; i++)
125         {
126             Element JavaDoc type = DOMUtils.getFirstElement(child, "DAV:", types[i]);
127             if (type!=null)
128             {
129                 principal=types[i];
130             }
131         }
132
133         if (principal==null)
134         {
135             System.err.println("Error: unknown type of principal");
136             System.err.println("element: " + element);
137             return null;
138         }
139
140         Ace ace = new Ace(principal);
141
142         child = DOMUtils.getFirstElement(element, "DAV:", "grant");
143         if (child == null) {
144             child = DOMUtils.getFirstElement(element, "DAV:", "deny");
145             ace.setNegative(true);
146          }
147         if (child != null) {
148             NodeList JavaDoc privilegeElements = child.getElementsByTagNameNS("DAV:", "privilege");
149             for (int i = 0; i < privilegeElements.getLength(); i++) {
150                 Element JavaDoc privilegeElement = (Element JavaDoc) privilegeElements.item(i);
151                 NodeList JavaDoc privileges = privilegeElement.getElementsByTagName("*");
152                 for (int j=0 ; j<privileges.getLength() ; j++)
153                 {
154                     Element JavaDoc privilege = (Element JavaDoc) privileges.item(j);
155                     ace.addPrivilege(parsePrivilege(privilege));
156                 }
157             }
158         }
159
160         child = DOMUtils.getFirstElement(element, "DAV:", "inherited");
161         if (child != null) {
162             href = DOMUtils.getFirstElement(child, "DAV:", "href");
163             String JavaDoc shref = null;
164             if (href != null)
165             {
166                 shref = DOMUtils.getTextValue(href);
167                 if (!shref.equals(response.getHref())) {
168                     ace.setInherited(true);
169                     ace.setInheritedFrom(shref);
170                 }
171             }
172             else
173             {
174                 System.err.println("Error: mandatory element <href> is missing !");
175                 return null;
176             }
177         }
178
179         child = DOMUtils.getFirstElement(element, "DAV:", "protected");
180         if (child != null) {
181             ace.setProtected(true);
182         }
183
184         return ace;
185
186     }
187
188
189     /**
190      * Parse a privilege element.
191      */

192     protected Privilege parsePrivilege(Element JavaDoc privilegeElement) {
193         return new Privilege(privilegeElement.getNamespaceURI(),
194                              privilegeElement.getLocalName(), null);
195     }
196
197     public String JavaDoc getPropertyAsString() {
198         Ace[] aces = getAces();
199
200         if ((aces==null) || (aces.length==0))
201             return "";
202
203         StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(aces[0].toString());
204         for (int i=1; i<aces.length ; i++) {
205             tmp.append(", ");
206             tmp.append(aces[i].toString());
207         }
208
209         return tmp.toString();
210     }
211
212 }
213
Popular Tags