1 23 24 package org.apache.webdav.lib.properties; 25 26 import java.util.ArrayList ; 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 ; 35 import org.w3c.dom.NodeList ; 36 37 43 public class AclProperty extends BaseProperty { 44 45 46 48 49 52 public static final String TAG_NAME = "acl"; 53 54 55 57 58 61 public AclProperty(ResponseEntity response, Element element) { 62 super(response, element); 63 } 64 65 66 68 69 74 public Ace[] getAces() { 75 NodeList children = element.getChildNodes(); 76 if (children == null || children.getLength() == 0) 77 return null; 78 ArrayList aces = new ArrayList (); 79 for (int i = 0; i < children.getLength(); i++) { 80 try { 81 Element child = (Element ) children.item(i); 82 String namespace = DOMUtils.getElementNamespaceURI(child); 83 if (namespace != null && namespace.equals("DAV:")) { 84 String localName = DOMUtils.getElementLocalName(child); 85 if ("ace".equals(localName)) { 86 aces.add(parseAce(child)); 87 } 88 } 89 } catch (ClassCastException e) { 90 } 91 } 92 return (Ace[]) aces.toArray(new Ace[aces.size()]); 93 } 94 95 96 98 99 102 protected Ace parseAce(Element element) { 103 104 String principal = null; 105 Element 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 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 [] types={"all","authenticated","unauthenticated","property","self"}; 124 for (int i=0 ; i<types.length && principal==null ; i++) 125 { 126 Element 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 privilegeElements = child.getElementsByTagNameNS("DAV:", "privilege"); 149 for (int i = 0; i < privilegeElements.getLength(); i++) { 150 Element privilegeElement = (Element ) privilegeElements.item(i); 151 NodeList privileges = privilegeElement.getElementsByTagName("*"); 152 for (int j=0 ; j<privileges.getLength() ; j++) 153 { 154 Element privilege = (Element ) 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 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 192 protected Privilege parsePrivilege(Element privilegeElement) { 193 return new Privilege(privilegeElement.getNamespaceURI(), 194 privilegeElement.getLocalName(), null); 195 } 196 197 public String getPropertyAsString() { 198 Ace[] aces = getAces(); 199 200 if ((aces==null) || (aces.length==0)) 201 return ""; 202 203 StringBuffer tmp = new StringBuffer (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 |