Ejemplo n.º 1
0
  /*
   * Helper method that parses a set of Attribute types. The Subject,
   * Action and Environment sections all look like this.
   */
  private static List parseAttributes(Node root) throws ParsingException {
    List set = new ArrayList();

    // the Environment section is just a list of Attributes
    NodeList nodes = root.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      Node node = nodes.item(i);
      if (SunxacmlUtil.getNodeName(node).equals("Attribute"))
        set.add(BasicAttribute.getInstance(node));
    }

    return set;
  }
Ejemplo n.º 2
0
  /**
   * Create a new <code>RequestCtx</code> by parsing a node. This node should be created by
   * schema-verified parsing of an <code>XML</code> document.
   *
   * @param root the node to parse for the <code>RequestCtx</code>
   * @return a new <code>RequestCtx</code> constructed by parsing
   * @throws ParsingException if the DOM node is invalid
   */
  public static RequestCtx getInstance(Node root) throws ParsingException {
    List newSubjects = new ArrayList();
    List newResource = null;
    List newAction = null;
    List newEnvironment = null;

    // First check to be sure the node passed is indeed a Request node.
    String tagName = SunxacmlUtil.getNodeName(root);
    if (!tagName.equals("Request")) {
      throw new ParsingException(
          "Request cannot be constructed using " + "type: " + SunxacmlUtil.getNodeName(root));
    }

    // Now go through its child nodes, finding Subject,
    // Resource, Action, and Environment data
    NodeList children = root.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
      Node node = children.item(i);
      String tag = SunxacmlUtil.getNodeName(node);

      if (tag.equals("Subject")) {
        // see if there is a category
        Node catNode = node.getAttributes().getNamedItem("SubjectCategory");
        URI category = null;

        if (catNode != null) {
          try {
            category = new URI(catNode.getNodeValue());
          } catch (Exception e) {
            throw new ParsingException("Invalid Category URI", e);
          }
        }

        // now we get the attributes
        List attributes = parseAttributes(node);

        // finally, add the list to the set of subject attributes
        newSubjects.add(new Subject(category, attributes));
      } else if (tag.equals("Resource")) {
        // For now, this code doesn't parse the content, since it's
        // a set of anys with a set of anyAttributes, and therefore
        // no useful data can be gleaned from it anyway. The theory
        // here is that it's only useful in the instance doc, so
        // we won't bother parse it, but we may still want to go
        // back and provide some support at some point...
        newResource = parseAttributes(node);
      } else if (tag.equals("Action")) {
        newAction = parseAttributes(node);
      } else if (tag.equals("Environment")) {
        newEnvironment = parseAttributes(node);
      }
    }

    // if we didn't have an environment section, the only optional section
    // of the four, then create a new empty set for it
    if (newEnvironment == null) newEnvironment = new ArrayList();

    // Now create and return the RequestCtx from the information
    // gathered
    return new BasicRequestCtx(newSubjects, newResource, newAction, newEnvironment, root);
  }