policyreference.java

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

JAVA
553 行
字号
    public int getReferenceType() {
        return policyType;
    }

    /**
     * Returns the id of this policy. If the policy is invalid or can't be
     * retrieved, then a runtime exception is thrown.
     *
     * @return the policy id
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public URI getId() {
        return resolvePolicy().getId();
    }

    /**
     * Returns the version of this policy. If the policy is invalid or can't
     * be retrieved, then a runtime exception is thrown.
     *
     * @return the policy version
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public String getVersion() {
        return resolvePolicy().getVersion();
    }

    /**
     * Returns the combining algorithm used by this policy. If the policy is
     * invalid or can't be retrieved, then a runtime exception is thrown.
     *
     * @return the combining algorithm
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public CombiningAlgorithm getCombiningAlg() {
        return resolvePolicy().getCombiningAlg();
    }

    /**
     * Returns the given description of this policy or null if there is no
     * description. If the policy is invalid or can't be retrieved, then a
     * runtime exception is thrown.
     *
     * @return the description or null
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public String getDescription() {
        return resolvePolicy().getDescription();
    }

    /**
     * Returns the target for this policy. If the policy is invalid or can't be
     * retrieved, then a runtime exception is thrown.
     *
     * @return the policy's target
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public Target getTarget() {
        return resolvePolicy().getTarget();
    }

    /**
     * Returns the default version for this policy. If the policy is 
     * invalid or can't be retrieved, then a runtime exception is thrown.
     *
     * @return the policy's default version
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public String getDefaultVersion() {
        return resolvePolicy().getDefaultVersion();
    }

    /**
     * Returns the child policy nodes under this node in the policy tree. If
     * the policy is invalid or can't be retrieved, then a runtime exception
     * is thrown.
     *
     * @return the <code>List</code> of child policy nodes
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public List getChildren() {
        return resolvePolicy().getChildren();
    }

    /**
     * Returns the child policy nodes and their associated parameters. If
     * the policy is invalid or can't be retrieved, then a runtime exception
     * is thrown.
     *
     * @return a <code>List</code> of <code>CombinerElement</code>s
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public List getChildElements() {
        return resolvePolicy().getChildElements();
    }

    /**
     * Returns the Set of obligations for this policy, which may be empty if 
     * there are no obligations. If the policy is invalid or can't be 
     * retrieved, then a runtime exception is thrown.
     *
     * @return the policy's obligations
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public Set getObligations() {
        return resolvePolicy().getObligations();
    }

    /**
     * Returns the meta-data associated with this policy. If the policy is
     * invalid or can't be retrieved, then a runtime exception is thrown.
     * Note that this is the meta-data for the referenced policy, not the
     * meta-data for the parent policy (which is what gets provided to the
     * constructors of this class).
     *
     * @return the policy's meta-data
     *
     * @throws ProcessingException if the referenced policy can't be retrieved
     */
    public PolicyMetaData getMetaData() {
        return resolvePolicy().getMetaData();
    }

    /**
     * Given the input context sees whether or not the request matches this
     * policy. This must be called by combining algorithms before they
     * evaluate a policy. This is also used in the initial policy finding
     * operation to determine which top-level policies might apply to the
     * request. If the policy is invalid or can't be retrieved, then a
     * runtime exception is thrown.
     *
     * @param context the representation of the request
     *
     * @return the result of trying to match the policy and the request
     */
    public MatchResult match(EvaluationCtx context) {
        try {
            return getTarget().match(context);
        } catch (ProcessingException pe) {
            // this means that we couldn't resolve the policy
            ArrayList code = new ArrayList();
            code.add(Status.STATUS_PROCESSING_ERROR);
            Status status = new Status(code, "couldn't resolve policy ref");
            return new MatchResult(MatchResult.INDETERMINATE, status);
        }
    }

    /**
     * Private helper method that tried to resolve the policy
     */
    private AbstractPolicy resolvePolicy() {
        // see if this reference was setup with a finder
        if (finder == null) {
            if (logger.isLoggable(Level.WARNING))
                logger.warning("PolicyReference with id " +
                               reference.toString() + " was queried but was " +
                               "not configured with a PolicyFinder");

            throw new ProcessingException("couldn't find the policy with " +
                                          "a null finder");
        }

        PolicyFinderResult pfr =
            finder.findPolicy(reference, policyType, constraints,
                              parentMetaData);
        
        if (pfr.notApplicable())
            throw new ProcessingException("couldn't resolve the policy");
        
        if (pfr.indeterminate())
            throw new ProcessingException("error resolving the policy");

        return pfr.getPolicy();
    }

    /**
     * Tries to evaluate the policy by calling the combining algorithm on
     * the given policies or rules. The <code>match</code> method must always
     * be called first, and must always return MATCH, before this method
     * is called.
     *
     * @param context the representation of the request
     *
     * @return the result of evaluation
     */
    public Result evaluate(EvaluationCtx context) {
        // if there is no finder, then we return NotApplicable
        if (finder == null)
            return new Result(Result.DECISION_NOT_APPLICABLE,
                              context.getResourceId().encode());

        PolicyFinderResult pfr = finder.findPolicy(reference, policyType,
                                                   constraints,
                                                   parentMetaData);

        // if we found nothing, then we return NotApplicable
        if (pfr.notApplicable())
            return new Result(Result.DECISION_NOT_APPLICABLE,
                              context.getResourceId().encode());

        // if there was an error, we return that status data
        if (pfr.indeterminate())
            return new Result(Result.DECISION_INDETERMINATE, pfr.getStatus(),
                              context.getResourceId().encode());

        // we must have found a policy
        return pfr.getPolicy().evaluate(context);
    }

    /**
     * Encodes this <code>PolicyReference</code> into its XML representation
     * and writes this encoding to the given <code>OutputStream</code> with
     * no indentation.
     *
     * @param output a stream into which the XML-encoded data is written
     */
    public void encode(OutputStream output) {
        encode(output, new Indenter(0));
    }

    /**
     * Encodes this <code>PolicyReference</code> into its XML representation
     * and writes this encoding to the given <code>OutputStream</code> with
     * indentation.
     *
     * @param output a stream into which the XML-encoded data is written
     * @param indenter an object that creates indentation strings
     */
    public void encode(OutputStream output, Indenter indenter) {
        PrintStream out = new PrintStream(output);
        String encoded = indenter.makeString();

        if (policyType == POLICY_REFERENCE) {
            out.println(encoded + "<PolicyIdReference" + encodeConstraints() +
                        ">" + reference.toString() + "</PolicyIdReference>");
        } else {
            out.println(encoded + "<PolicySetIdReference" +
                        encodeConstraints() + ">" + reference.toString() +
                        "</PolicySetIdReference>");
        }
    }

    /**
     * Private helper method that encodes the variable constraints info. Note
     * that if this is a pre-2.0 policy the constraints are always null, so
     * nothing will be added here.
     */
    private String encodeConstraints() {
        String str = "";
        VersionConstraints version = getConstraints();

        String v = version.getVersionConstraint();
        if (v != null)
            str += " Version=\"" + v + "\"";

        String e = version.getEarliestConstraint();
        if (e != null)
            str += " EarliestVersion=\"" + e + "\"";

        String l = version.getLatestConstraint();
        if (l != null)
            str += " LatestVersion=\"" + l + "\"";

        return str;
    }

}

⌨️ 快捷键说明

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