/** * Tries to read an XACML policy or policy set from the given file. * * @param file * the file containing the policy to read * @return a (potentially schema-validated) policy loaded from the given * file * @throws ParsingException * if an error occurs while reading or parsing the policy */ public synchronized Document readPolicy(File file) throws ParsingException { try { return builder.parse(file); } catch (IOException ioe) { throw new ParsingException("Failed to read the file", ioe); } catch (SAXException saxe) { throw new ParsingException("Failed to parse the file", saxe); } }
/** * Tries to read an XACML policy or policy set from the given stream. * * @param input * the stream containing the policy to read * @return a (potentially schema-validated) policy loaded from the given * file * @throws ParsingException * if an error occurs while reading or parsing the policy */ public synchronized Document readPolicy(InputStream input) throws ParsingException { try { return builder.parse(input); } catch (IOException ioe) { throw new ParsingException("Failed to read the stream", ioe); } catch (SAXException saxe) { throw new ParsingException("Failed to parse the stream", saxe); } }
/** * Returns a <code>BooleanAttribute</code> that represents * the xs:boolean value indicated by the string provided. * * @param value a string representing the desired value * @return a <code>BooleanAttribute</code> representing the * appropriate value (null if there is a parsing error) */ public static BooleanAttribute getInstance(String value) throws ParsingException { if (value.equals("true")) return trueInstance; if (value.equals("false")) return falseInstance; throw new ParsingException("Boolean string must be true or false"); }
/** * Private helper routine that converts text into a node */ private Node textToNode(String encoded) throws ParsingException { try { String text = "<?xml version=\"1.0\"?>\n"; byte [] bytes = (text + encoded).getBytes(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(bytes)); return doc.getDocumentElement(); } catch (Exception e) { throw new ParsingException("invalid XML for status detail"); } }
/** * Tries to read an XACML policy or policy set from the given file. * * @param file the file containing the policy to read * * @return a (potentially schema-validated) policy loaded from the * given file * * @throws ParsingException if an error occurs while reading or * parsing the policy */ public synchronized AbstractPolicy readPolicy(File file) throws ParsingException { try { return handleDocument(builder.parse(file)); } catch (IOException ioe) { throw new ParsingException("Failed to read the file", ioe); } catch (SAXException saxe) { throw new ParsingException("Failed to parse the file", saxe); } }
/** * Tries to read an XACML policy or policy set from the given stream. * * @param input the stream containing the policy to read * * @return a (potentially schema-validated) policy loaded from the * given file * * @throws ParsingException if an error occurs while reading or * parsing the policy */ public synchronized AbstractPolicy readPolicy(InputStream input) throws ParsingException { try { return handleDocument(builder.parse(input)); } catch (IOException ioe) { throw new ParsingException("Failed to read the stream", ioe); } catch (SAXException saxe) { throw new ParsingException("Failed to parse the stream", saxe); } }
/** * Helper method that handles the obligations */ private static Set parseObligations(Node root) throws ParsingException { Set set = new HashSet(); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (SunxacmlUtil.getNodeName(node).equals("Obligation")) set.add(Obligation.getInstance(node)); } if (set.size() == 0) throw new ParsingException("ObligationsType must not be empty"); return set; }
/** * Tries to read an XACML policy or policy set based on the given URL. This * may be any resolvable URL, like a file or http pointer. * * @param url * a URL pointing to the policy to read * @return a (potentially schema-validated) policy loaded from the given * file * @throws ParsingException * if an error occurs while reading or parsing the policy, or if the * URL can't be resolved */ public synchronized Document readPolicy(URL url) throws ParsingException { try { return readPolicy(url.openStream()); } catch (IOException ioe) { throw new ParsingException("Failed to resolve the URL: " + url.toString(), ioe); } }
/** * Tries to read an XACML policy or policy set based on the given URL. * This may be any resolvable URL, like a file or http pointer. * * @param url a URL pointing to the policy to read * * @return a (potentially schema-validated) policy loaded from the * given file * * @throws ParsingException if an error occurs while reading or * parsing the policy, or if the URL can't * be resolved */ public synchronized AbstractPolicy readPolicy(URL url) throws ParsingException { try { return readPolicy(url.openStream()); } catch (IOException ioe) { throw new ParsingException("Failed to resolve the URL: " + url.toString(), ioe); } }
/** * Default constructor. This constructor uses the * <code>PDP_CONFIG_PROPERTY</code> property to load the configuration. * If the property isn't set, if it names a file that can't be accessed, * or if the file is invalid, then an exception is thrown. * * @throws ParsingException if anything goes wrong during the parsing * of the configuration file, the class loading, * or the factory and pdp setup */ public ConfigurationStore() throws ParsingException { String configFile = System.getProperty(PDP_CONFIG_PROPERTY); // make sure that the right property was set if (configFile == null) { logger.severe("A property defining a config file was expected, " + "but none was provided"); throw new ParsingException("Config property " + PDP_CONFIG_PROPERTY + " needs to be set"); } try { setupConfig(new File(configFile)); } catch (ParsingException pe) { logger.log(Level.SEVERE, "Runtime config file couldn't be loaded" + " so no configurations will be available", pe); throw pe; } }
/** * A private method that handles reading the policy and creates the correct * kind of AbstractPolicy. * Because this makes use of the policyFinder, it cannot be reused between finders. * Consider moving to policyManager, which is not intended to be reused outside * of a policyFinderModule, which is not intended to be reused amongst PolicyFinder instances. */ protected AbstractPolicy handleDocument(Document doc, PolicyFinder policyFinder) throws ParsingException { // handle the policy, if it's a known type Element root = doc.getDocumentElement(); String name = root.getTagName(); // see what type of policy this is if (name.equals("Policy")) { return Policy.getInstance(root); } else if (name.equals("PolicySet")) { return PolicySet.getInstance(root, policyFinder); } else { // this isn't a root type that we know how to handle throw new ParsingException("Unknown root document type: " + name); } }
/** * Creates an instance of a <code>StatusDetail</code> object based on * the given DOM root node. The node must be a valid StatusDetailType * root, or else a <code>ParsingException</code> is thrown. * * @param root the DOM root of the StatusDetailType XML type * * @return a new <code>StatusDetail</code> object * * @throws ParsingException if the root node is invalid */ public static StatusDetail getInstance(Node root) throws ParsingException { // check that it's really a StatusDetailType root if (! SunxacmlUtil.getNodeName(root).equals("StatusDetail")) throw new ParsingException("not a StatusDetail node"); return new StatusDetail(root); }
/** * A private method that handles reading the policy and creates the * correct kind of AbstractPolicy. */ private AbstractPolicy handleDocument(Document doc) throws ParsingException { // handle the policy, if it's a known type Element root = doc.getDocumentElement(); String name = root.getTagName(); // see what type of policy this is if (name.equals("Policy")) { return Policy.getInstance(root); } else if (name.equals("PolicySet")) { return PolicySet.getInstance(root, finder); } else { // this isn't a root type that we know how to handle throw new ParsingException("Unknown root document type: " + name); } }
/** * Creates a new instance of <code>ResponseCtx</code> based on the given * DOM root node. A <code>ParsingException</code> is thrown if the DOM * root doesn't represent a valid ResponseType. * * @param root the DOM root of a ResponseType * * @return a new <code>ResponseCtx</code> * * @throws ParsingException if the node is invalid */ public static ResponseCtx getInstance(Node root) throws ParsingException { Set results = new HashSet(); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (SunxacmlUtil.getNodeName(node).equals("Result")) { results.add(Result.getInstance(node)); } } if (results.size() == 0) throw new ParsingException("must have at least one Result"); return new ResponseCtx(results); }
/** * Returns a new <code>IPAddressAttribute</code> that represents * the name indicated by the <code>String</code> provided. * * @param value a string representing the address * * @return a new <code>IPAddressAttribute</code> * * @throws ParsingException if any of the address components is invalid */ public static IPAddressAttribute getInstance(String value) throws ParsingException { try { // an IPv6 address starts with a '[' if (value.indexOf('[') == 0) return IPv6AddressAttribute.getV6Instance(value); else return IPv4AddressAttribute.getV4Instance(value); } catch (UnknownHostException uhe) { throw new ParsingException("Failed to parse an IPAddress", uhe); } }
/** * Returns a new <code>HexBinaryAttribute</code> that represents * the xsi:hexBinary value indicated by the string provided. * * @param value a string representing the desired value * @return a new <code>HexBinaryAttribute</code> representing the * desired value * @exception ParsingException if a parsing error occurs */ public static HexBinaryAttribute getInstance(String value) throws ParsingException { byte [] bytes = hexToBin(value); if (bytes == null) throw new ParsingException("Couldn't parse purported " + "hex string: " + value); return new HexBinaryAttribute(bytes); }
/** * Returns a new instance of the <code>CombinerParameter</code> class * based on a DOM node. The node must be the root of an XML * CombinerParameterType. * * @param root the DOM root of a CombinerParameterType XML type * * @throws ParsingException if the CombinerParameterType is invalid */ public static CombinerParameter getInstance(Node root) throws ParsingException { // get the name, which is a required attribute String name = root.getAttributes().getNamedItem("ParameterName"). getNodeValue(); // get the attribute value, the only child of this element AttributeFactory attrFactory = AttributeFactory.getInstance(); AttributeValue value = null; try { value = attrFactory.createValue(root.getFirstChild()); } catch (UnknownIdentifierException uie) { throw new ParsingException("Unknown AttributeId", uie); } return new CombinerParameter(name, value); }
/** * Helper method that tries to get a function instance */ public static Function getFunction(Node root, PolicyMetaData metaData, FunctionFactory factory) throws ParsingException { Node functionNode = root.getAttributes().getNamedItem("FunctionId"); String functionName = functionNode.getNodeValue(); try { // try to get an instance of the given function return factory.createFunction(functionName); } catch (UnknownIdentifierException uie) { throw new ParsingException("Unknown FunctionId", uie); } catch (FunctionTypeException fte) { // try creating as an abstract function try { FunctionFactory ff = FunctionFactory.getGeneralInstance(); return ff.createAbstractFunction(functionName, root, metaData. getXPathIdentifier()); } catch (Exception e) { // any exception at this point is a failure throw new ParsingException("failed to create abstract function" + " " + functionName, e); } } }
/** * Creates an instance of a <code>Policy</code> object based on a * DOM node. The node must be the root of PolicyType XML object, * otherwise an exception is thrown. * * @param root the DOM root of a PolicyType XML type * * @throws ParsingException if the PolicyType is invalid */ public static Policy getInstance(Node root) throws ParsingException { // first off, check that it's the right kind of node if (! SunxacmlUtil.getNodeName(root).equals("Policy")) { throw new ParsingException("Cannot create Policy from root of " + "type " + SunxacmlUtil.getNodeName(root)); } return new Policy(root); }
/** * Creates an instance of a <code>PolicySet</code> object based on a * DOM node. The node must be the root of PolicySetType XML object, * otherwise an exception is thrown. The finder is used to handle * policy references. * * @param root the DOM root of a PolicySetType XML type * @param finder the <code>PolicyFinder</code> used to handle references * * @throws ParsingException if the PolicySetType is invalid */ public static PolicySet getInstance(Node root, PolicyFinder finder) throws ParsingException { // first off, check that it's the right kind of node if (! SunxacmlUtil.getNodeName(root).equals("PolicySet")) { throw new ParsingException("Cannot create PolicySet from root of" + " type " + SunxacmlUtil.getNodeName(root)); } return new PolicySet(root, finder); }