requestctx.java

来自「sunxacml源码」· Java 代码 · 共 475 行 · 第 1/2 页

JAVA
475 行
字号

/*
 * @(#)RequestCtx.java
 *
 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistribution of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 * 
 *   2. Redistribution in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed or intended for use in
 * the design, construction, operation or maintenance of any nuclear facility.
 */

package com.sun.xacml.ctx;

import com.sun.xacml.Indenter;
import com.sun.xacml.ParsingException;

import com.sun.xacml.attr.AttributeDesignator;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;


/**
 * Represents a request made to the PDP. This is the class that contains all
 * the data used to start a policy evaluation.
 *
 * @since 1.0
 * @author Seth Proctor
 * @author Marco Barreno
 */
public class RequestCtx
{

    // There must be at least one subject
    private Set subjects = null;

    // There must be exactly one resource
    private Set resource = null;

    // There must be exactly one action
    private Set action = null;

    // There may be any number of environment attributes
    private Set environment = null;

    // Hold onto the root of the document for XPath searches
    private Node documentRoot = null;

    // The optional, generic resource content
    private String resourceContent;

    /**
     * Constructor that creates a <code>RequestCtx</code> from components.
     *
     * @param subjects a <code>Set</code> of <code>Subject</code>s
     * @param resource a <code>Set</code> of <code>Attribute</code>s
     * @param action a <code>Set</code> of <code>Attribute</code>s
     * @param environment a <code>Set</code> of environment attributes
     */
    public RequestCtx(Set subjects, Set resource, Set action,
                      Set environment) {
        this(subjects, resource, action, environment, null, null);
    }

    /**
     * Constructor that creates a <code>RequestCtx</code> from components.
     *
     * @param subjects a <code>Set</code> of <code>Subject</code>s
     * @param resource a <code>Set</code> of <code>Attribute</code>s
     * @param action a <code>Set</code> of <code>Attribute</code>s
     * @param environment a <code>Set</code> of environment attributes
     * @param documentRoot the root node of the DOM tree for this request
     */
    public RequestCtx(Set subjects, Set resource, Set action, 
                      Set environment, Node documentRoot) {
        this(subjects, resource, action, environment, documentRoot, null);
    }

    /**
     * Constructor that creates a <code>RequestCtx</code> from components.
     *
     * @param subjects a <code>Set</code> of <code>Subject</code>s
     * @param resource a <code>Set</code> of <code>Attribute</code>s
     * @param action a <code>Set</code> of <code>Attribute</code>s
     * @param environment a <code>Set</code> of environment attributes
     * @param resourceContent a text-encoded version of the content, suitable
     *                        for including in the RequestType, including the
     *                        root <code>RequestContent</code> node
     */
    public RequestCtx(Set subjects, Set resource, Set action, 
                      Set environment, String resourceContent) {
        this(subjects, resource, action, environment, null, resourceContent);
    }

    /**
     * Constructor that creates a <code>RequestCtx</code> from components.
     *
     * @param subjects a <code>Set</code> of <code>Subject</code>s
     * @param resource a <code>Set</code> of <code>Attribute</code>s
     * @param action a <code>Set</code> of <code>Attribute</code>s
     * @param environment a <code>Set</code> of environment attributes
     * @param documentRoot the root node of the DOM tree for this request
     * @param resourceContent a text-encoded version of the content, suitable
     *                        for including in the RequestType, including the
     *                        root <code>RequestContent</code> node
     *
     * @throws IllegalArgumentException if the inputs are not well formed
     */
    public RequestCtx(Set subjects, Set resource, Set action, 
                      Set environment, Node documentRoot,
                      String resourceContent) throws IllegalArgumentException {
      
        // make sure subjects is well formed
        Iterator sIter = subjects.iterator();
        while (sIter.hasNext()){
            if (!(sIter.next() instanceof Subject))
                throw new IllegalArgumentException("Subjects input is not " +
                                                   "well formed");
        }
        this.subjects = Collections.unmodifiableSet(new HashSet(subjects));

        // make sure resource is well formed
        Iterator rIter = resource.iterator();
        while (rIter.hasNext()){
            if (!(rIter.next() instanceof Attribute))
                throw new IllegalArgumentException("Resource input is not " +
                                                   "well formed");
        }
        this.resource = Collections.unmodifiableSet(new HashSet(resource));

        // make sure action is well formed
        Iterator aIter = action.iterator();
        while (aIter.hasNext()){
            if (!(aIter.next() instanceof Attribute))
                throw new IllegalArgumentException("Action input is not " +
                                                   "well formed");
        }
        this.action = Collections.unmodifiableSet(new HashSet(action));
        
        // make sure environment is well formed
        Iterator eIter = environment.iterator();
        while (eIter.hasNext()){
            if (!(eIter.next() instanceof Attribute))
                throw new IllegalArgumentException("Environment input is not" +
                                                   " well formed");
        }
        this.environment =
            Collections.unmodifiableSet(new HashSet(environment));

        this.documentRoot = documentRoot;
        this.resourceContent = resourceContent;
    }

    /**
     * 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 URISyntaxException if there is a badly formed URI
     * @throws ParsingException if the DOM node is invalid
     */
    public static RequestCtx getInstance(Node root) throws ParsingException {
        Set newSubjects = new HashSet();
        Set newResource = null;
        Set newAction = null;
        Set newEnvironment = null;
        String resourceContent;

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

            if (tag.equals("Subject")) {
                // see if there is a category
                Node catNode =

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?