1 22 package org.jboss.test.security.test.xacml.modules; 23 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.jboss.logging.Logger; 34 35 import com.sun.xacml.AbstractPolicy; 36 import com.sun.xacml.EvaluationCtx; 37 import com.sun.xacml.ParsingException; 38 import com.sun.xacml.UnknownIdentifierException; 39 import com.sun.xacml.combine.CombiningAlgFactory; 40 import com.sun.xacml.combine.PolicyCombiningAlgorithm; 41 import com.sun.xacml.ctx.Status; 42 import com.sun.xacml.finder.PolicyFinder; 43 import com.sun.xacml.finder.PolicyFinderModule; 44 import com.sun.xacml.finder.PolicyFinderResult; 45 import com.sun.xacml.support.finder.PolicyCollection; 46 import com.sun.xacml.support.finder.PolicyReader; 47 import com.sun.xacml.support.finder.StaticPolicyFinderModule; 48 import com.sun.xacml.support.finder.TopLevelPolicyException; 49 50 52 60 public class JBossStaticPolicyFinderModule extends PolicyFinderModule 61 { 62 private List policyList; 64 65 private PolicyCollection policies; 67 68 private File schemaFile = null; 70 71 private static final String POLICY_ID = 73 "urn:com:sun:xacml:support:finder:dynamic-policy-set"; 74 75 private static URI policyId = null; 76 77 private static final Logger log = 79 Logger.getLogger(StaticPolicyFinderModule.class.getName()); 80 81 private boolean encounteredParsingException = false; 83 84 static 85 { 86 try 87 { 88 policyId = new URI (POLICY_ID); 89 } catch (Exception e) 90 { 91 log.error("couldn't assign default policy id:",e); 92 } 93 }; 94 95 107 public JBossStaticPolicyFinderModule(List policyList) 108 { 109 this.policyList = policyList; 110 this.policies = new PolicyCollection(); 111 112 String schemaName = 113 System.getProperty(PolicyReader.POLICY_SCHEMA_PROPERTY); 114 if (schemaName != null) 115 schemaFile = new File (schemaName); 116 } 117 118 129 public JBossStaticPolicyFinderModule(List policyList, String schemaFile) 130 { 131 this.policyList = policyList; 132 this.policies = new PolicyCollection(); 133 134 if (schemaFile != null) 135 this.schemaFile = new File (schemaFile); 136 } 137 138 158 public JBossStaticPolicyFinderModule(String combiningAlg, List policyList) 159 throws URISyntaxException , UnknownIdentifierException 160 { 161 PolicyCombiningAlgorithm alg = (PolicyCombiningAlgorithm) 162 (CombiningAlgFactory.getInstance(). 163 createAlgorithm(new URI (combiningAlg))); 164 165 this.policyList = policyList; 166 this.policies = new PolicyCollection(alg, policyId); 167 168 String schemaName = 169 System.getProperty(PolicyReader.POLICY_SCHEMA_PROPERTY); 170 if (schemaName != null) 171 schemaFile = new File (schemaName); 172 } 173 174 193 public JBossStaticPolicyFinderModule(String combiningAlg, List policyList, 194 String schemaFile) 195 throws URISyntaxException , UnknownIdentifierException 196 { 197 PolicyCombiningAlgorithm alg = (PolicyCombiningAlgorithm) 198 (CombiningAlgFactory.getInstance(). 199 createAlgorithm(new URI (combiningAlg))); 200 201 this.policyList = policyList; 202 this.policies = new PolicyCollection(alg, policyId); 203 204 if (schemaFile != null) 205 this.schemaFile = new File (schemaFile); 206 } 207 208 214 public boolean isRequestSupported() 215 { 216 return true; 217 } 218 219 226 public void init(PolicyFinder finder) 227 { 228 String clazzName = JBossStaticPolicyFinderModule.class.getName(); 229 PolicyReader reader = new PolicyReader(finder, 231 java.util.logging.Logger.getLogger(clazzName), schemaFile); 232 233 Iterator it = policyList.iterator(); 234 while (it.hasNext()) 235 { 236 String str = (String )(it.next()); 237 AbstractPolicy policy = null; 238 try 239 { 240 try 241 { 242 URL url = new URL (str); 244 policy = reader.readPolicy(url); 245 } 246 catch (MalformedURLException murle) 247 { 248 policy = reader.readPolicy(new File (str)); 250 } 251 } 252 catch (ParsingException e) 253 { 254 this.encounteredParsingException = true; 255 log.error("Parsing Exception in policy:",e); 256 continue; 257 } 258 259 if (! policies.addPolicy(policy)) 261 log.warn("tried to load the same " + 262 "policy multiple times: " + str); 263 } 264 } 265 266 276 public PolicyFinderResult findPolicy(EvaluationCtx context) 277 { 278 List aList = new ArrayList (); 279 aList.add(Status.STATUS_SYNTAX_ERROR); 280 281 try 282 { 283 if(this.encounteredParsingException) 284 return new PolicyFinderResult(new Status(aList)); 285 AbstractPolicy policy = policies.getPolicy(context); 286 287 if (policy == null) 288 return new PolicyFinderResult(); 289 else 290 return new PolicyFinderResult(policy); 291 } 292 catch (TopLevelPolicyException tlpe) 293 { 294 return new PolicyFinderResult(tlpe.getStatus()); 295 } 296 } 297 } 298 | Popular Tags |