1 17 18 package org.apache.lenya.ac.impl; 19 20 import java.io.InputStream ; 21 22 import javax.xml.parsers.ParserConfigurationException ; 23 24 import org.apache.lenya.ac.AccessControlException; 25 import org.apache.lenya.ac.AccessController; 26 import org.apache.lenya.ac.Accreditable; 27 import org.apache.lenya.ac.AccreditableManager; 28 import org.apache.lenya.ac.Role; 29 import org.apache.lenya.ac.User; 30 import org.apache.lenya.ac.World; 31 import org.apache.lenya.ac.cache.BuildException; 32 import org.apache.lenya.ac.cache.InputStreamBuilder; 33 import org.apache.lenya.xml.DocumentHelper; 34 import org.apache.lenya.xml.NamespaceHelper; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 38 42 public class PolicyBuilder implements InputStreamBuilder { 43 44 48 public PolicyBuilder(AccreditableManager accreditableManager) { 49 assert accreditableManager != null; 50 this.accreditableManager = accreditableManager; 51 } 52 53 57 public AccreditableManager getAccreditableManager() { 58 return accreditableManager; 59 } 60 61 private AccreditableManager accreditableManager; 62 63 protected static final String POLICY_ELEMENT = "policy"; 64 protected static final String GROUP_ELEMENT = "group"; 65 protected static final String USER_ELEMENT = "user"; 66 protected static final String ROLE_ELEMENT = "role"; 67 protected static final String WORLD_ELEMENT = "world"; 68 protected static final String IP_RANGE_ELEMENT = "ip-range"; 69 protected static final String ID_ATTRIBUTE = "id"; 70 protected static final String SSL_ATTRIBUTE = "ssl"; 71 72 78 public DefaultPolicy buildPolicy(InputStream stream) 79 throws AccessControlException { 80 81 Document document; 82 83 try { 84 document = DocumentHelper.readDocument(stream); 85 } catch (Exception e) { 86 throw new AccessControlException(e); 87 } 88 89 return buildPolicy(document); 90 } 91 92 98 public DefaultPolicy buildPolicy(Document document) 99 throws AccessControlException { 100 101 DefaultPolicy policy = new DefaultPolicy(); 102 Element policyElement = document.getDocumentElement(); 103 assert policyElement.getLocalName().equals(POLICY_ELEMENT); 104 105 NamespaceHelper helper = 106 new NamespaceHelper( 107 AccessController.NAMESPACE, 108 AccessController.DEFAULT_PREFIX, 109 document); 110 111 Element [] credentialElements = helper.getChildren(policyElement); 112 113 for (int i = 0; i < credentialElements.length; i++) { 114 Accreditable accreditable = null; 115 116 String id = credentialElements[i].getAttribute(ID_ATTRIBUTE); 117 accreditable = getAccreditable(credentialElements[i].getLocalName(), id); 118 119 Credential credential = new Credential(accreditable); 120 121 Element [] roleElements = helper.getChildren(credentialElements[i], ROLE_ELEMENT); 122 123 for (int j = 0; j < roleElements.length; j++) { 124 String roleId = roleElements[j].getAttribute(ID_ATTRIBUTE); 125 Role role = getAccreditableManager().getRoleManager().getRole(roleId); 126 credential.addRole(role); 127 } 128 129 policy.addCredential(credential); 130 } 131 132 boolean ssl = false; 133 String sslString = policyElement.getAttribute(SSL_ATTRIBUTE); 134 if (sslString != null) { 135 ssl = Boolean.valueOf(sslString).booleanValue(); 136 } 137 policy.setSSL(ssl); 138 139 return policy; 140 } 141 142 149 protected Accreditable getAccreditable( 150 String elementName, 151 String id) 152 throws AccessControlException { 153 Accreditable accreditable = null; 154 155 if (elementName.equals(USER_ELEMENT)) { 156 accreditable = getAccreditableManager().getUserManager().getUser(id); 157 } else if (elementName.equals(GROUP_ELEMENT)) { 158 accreditable = getAccreditableManager().getGroupManager().getGroup(id); 159 } else if (elementName.equals(WORLD_ELEMENT)) { 160 accreditable = World.getInstance(); 161 } else if (elementName.equals(IP_RANGE_ELEMENT)) { 162 accreditable = getAccreditableManager().getIPRangeManager().getIPRange(id); 163 } 164 165 if (accreditable == null) { 166 throw new AccessControlException( 167 "Unknown accreditable [" + elementName + "] with ID [" + id + "]"); 168 } 169 170 return accreditable; 171 } 172 173 179 public static Document savePolicy(DefaultPolicy policy) throws AccessControlException { 180 NamespaceHelper helper; 181 182 try { 183 helper = 184 new NamespaceHelper( 185 AccessController.NAMESPACE, 186 AccessController.DEFAULT_PREFIX, 187 POLICY_ELEMENT); 188 } catch (ParserConfigurationException e) { 189 throw new AccessControlException(e); 190 } 191 192 Credential[] credentials = policy.getCredentials(); 193 Element policyElement = helper.getDocument().getDocumentElement(); 194 195 for (int i = 0; i < credentials.length; i++) { 196 Accreditable accreditable = credentials[i].getAccreditable(); 197 Element accreditableElement = save(accreditable, helper); 198 199 Role[] roles = credentials[i].getRoles(); 200 for (int j = 0; j < roles.length; j++) { 201 Element roleElement = helper.createElement(ROLE_ELEMENT); 202 roleElement.setAttribute(ID_ATTRIBUTE, roles[j].getId()); 203 accreditableElement.appendChild(roleElement); 204 } 205 206 policyElement.appendChild(accreditableElement); 207 } 208 209 policyElement.setAttribute(SSL_ATTRIBUTE, Boolean.toString(policy.isSSLProtected())); 210 211 return helper.getDocument(); 212 } 213 214 221 protected static Element save(Accreditable accreditable, NamespaceHelper helper) 222 throws AccessControlException { 223 String localName = null; 224 String id = null; 225 226 if (accreditable instanceof User) { 227 localName = USER_ELEMENT; 228 id = ((User) accreditable).getId(); 229 } else if (accreditable instanceof AbstractGroup) { 230 localName = GROUP_ELEMENT; 231 id = ((AbstractGroup) accreditable).getId(); 232 } else if (accreditable instanceof World) { 233 localName = WORLD_ELEMENT; 234 } else if (accreditable instanceof AbstractIPRange) { 235 localName = IP_RANGE_ELEMENT; 236 id = ((AbstractIPRange) accreditable).getId(); 237 } 238 239 if (localName == null) { 240 throw new AccessControlException("Could not save accreditable [" + accreditable + "]"); 241 } 242 243 Element element = helper.createElement(localName); 244 245 if (id != null) { 246 element.setAttribute(ID_ATTRIBUTE, id); 247 } 248 249 return element; 250 } 251 252 255 public Object build(InputStream stream) throws BuildException { 256 Object value = null; 257 try { 258 value = buildPolicy(stream); 259 } catch (AccessControlException e) { 260 throw new BuildException(e); 261 } 262 return value; 263 } 264 265 } 266 | Popular Tags |