rule.java

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

JAVA
413 行
字号
                condition = Condition.getInstance(child, metaData, manager);
            }
        }

        return new Rule(id, effect, description, target, condition);
    }

    /**
     * Returns the effect that this <code>Rule</code> will return from
     * the evaluate method (Permit or Deny) if the request applies.
     *
     * @return a decision effect, as defined in <code>Result</code>
     */
    public int getEffect() {
        return effectAttr;
    }

    /**
     * Returns the id of this <code>Rule</code>
     *
     * @return the rule id
     */
    public URI getId() {
        return idAttr;
    }

    /**
     * Returns the given description of this <code>Rule</code> or null if 
     * there is no description
     *
     * @return the description or null
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns the target for this <code>Rule</code> or null if there
     * is no target
     *
     * @return the rule's target
     */
    public Target getTarget() {
        return target;
    }

    /**
     * Since a rule is always a leaf in a policy tree because it can have
     * no children, this always returns an empty <code>List</code>.
     *
     * @return a <code>List</code> with no elements
     */
    public List getChildren() {
        return Collections.EMPTY_LIST;
    }

    /**
     * Returns the condition for this <code>Rule</code> or null if there
     * is no condition
     *
     * @return the rule's condition
     */
    public Condition getCondition() {
        return condition;
    }

    /**
     * Given the input context sees whether or not the request matches this
     * <code>Rule</code>'s <code>Target</code>. Note that unlike the matching
     * done by the <code>evaluate</code> method, if the <code>Target</code>
     * is missing than this will return Indeterminate. This lets you write
     * your own custom matching routines for rules but lets evaluation
     * proceed normally.
     *
     * @param context the representation of the request
     *
     * @return the result of trying to match this rule and the request
     */
    public MatchResult match(EvaluationCtx context) {
        if (target == null) {
            ArrayList code = new ArrayList();
            code.add(Status.STATUS_PROCESSING_ERROR);
            Status status = new Status(code, "no target available for " +
                                       "matching a rule");

            return new MatchResult(MatchResult.INDETERMINATE, status);
        }

        return target.match(context);
    }

    /**
     * Evaluates the rule against the supplied context. This will check that
     * the target matches, and then try to evaluate the condition. If the
     * target and condition apply, then the rule's effect is returned in
     * the result.
     * <p>
     * Note that rules are not required to have targets. If no target is
     * specified, then the rule inherits its parent's target. In the event
     * that this <code>Rule</code> has no <code>Target</code> then the
     * match is assumed to be true, since evaluating a policy tree to this
     * level required the parent's target to match.
     *
     * @param context the representation of the request we're evaluating
     *
     * @return the result of the evaluation
     */
    public Result evaluate(EvaluationCtx context) {
        // If the Target is null then it's supposed to inherit from the
        // parent policy, so we skip the matching step assuming we wouldn't
        // be here unless the parent matched
        if (target != null) {
            MatchResult match = target.match(context);
            int result = match.getResult();

            // if the target didn't match, then this Rule doesn't apply
            if (result == MatchResult.NO_MATCH)
                return new Result(Result.DECISION_NOT_APPLICABLE,
                                  context.getResourceId().encode());

            // if the target was indeterminate, we can't go on
            if (result == MatchResult.INDETERMINATE)
                return new Result(Result.DECISION_INDETERMINATE,
                                  match.getStatus(),
                                  context.getResourceId().encode());
        }

        // if there's no condition, then we just return the effect...
        if (condition == null)
            return new Result(effectAttr, context.getResourceId().encode());

        // ...otherwise we evaluate the condition
        EvaluationResult result = condition.evaluate(context);
        
        if (result.indeterminate()) {
            // if it was INDETERMINATE, then that's what we return
            return new Result(Result.DECISION_INDETERMINATE,
                              result.getStatus(),
                              context.getResourceId().encode());
        } else {
            // otherwise we return the effect on tue, and NA on false
            BooleanAttribute bool =
                (BooleanAttribute)(result.getAttributeValue());

            if (bool.getValue())
                return new Result(effectAttr,
                                  context.getResourceId().encode());
            else
                return new Result(Result.DECISION_NOT_APPLICABLE,
                                  context.getResourceId().encode());
        }
    }

    /**
     * Encodes this <code>Rule</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>Rule</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 indent = indenter.makeString();

        out.print(indent + "<Rule RuleId=\"" + idAttr.toString() +
                  "\" Effect=\"" + Result.DECISIONS[effectAttr] + "\"");

        if ((description != null) || (target != null) || (condition != null)) {
            // there is some content in the Rule
            out.println(">");

            indenter.in();
            String nextIndent = indenter.makeString();
            
            if (description != null)
                out.println(nextIndent + "<Description>" + description +
                            "</Description>");
            
            if (target != null)
                target.encode(output, indenter);
            
            if (condition != null)
                condition.encode(output, indenter);

            indenter.out();
            out.println(indent + "</Rule>");
        } else {
            // the Rule is empty, so close the tag and we're done
            out.println("/>");
        }
    }

}

⌨️ 快捷键说明

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