001package com.randomnoun.common.security;
002
003/* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
004 * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
005 */
006
007import java.io.*;
008import java.util.*;
009
010/**
011 * A criteria than can be used to evaluate fine-grained resources. (e.g.
012 * an 'account' resource where account.balance > 1000). This class should be
013 * subclassed by security implementations that wish to perform access control
014 * over fine-grained resources.
015 *
016 * <p>It is the responsibility of this class to translate its criteriaString into
017 * a form that can return a true or false value when supplied a
018 * criteria context (in this case, an instance of the account). 
019 * This class provides a default implementation of the
020 * {@link #evaluate(Map)} method, which currently always returns true.
021 * 
022 * @author knoxg
023 */
024public abstract class ResourceCriteria
025    implements Serializable
026{
027    
028    /** generated serialVersionUID */
029        private static final long serialVersionUID = 7387179933750418124L;
030        
031        /** The string used to construct this ResourceCriteria object */
032    private String criteriaString;
033
034    /** Create a ResourceCriteria object */
035    public ResourceCriteria(String criteriaString)
036    {
037        this.criteriaString = criteriaString;
038    }
039
040    /** Returns the string used to construct this ResourceCriteria object.
041     *
042     * @return the string used to construct this ResourceCriteria object.
043     */
044    public String getCriteriaString()
045    {
046        return criteriaString;
047    }
048
049    /** Returns a string representing this ResourceCriteria object.
050     *  This method Should be overridden by subclasses.
051     *
052     *  @return  a string representing this ResourceCriteria object
053     */
054    public String toString()
055    {
056        return criteriaString;
057    }
058
059    /**
060     * Returns true if this resourceCriteria identifies a resource with the
061     * supplied criteriaContext. The context used is resource-specific, but
062     * is always expressed as a name/value Map. e.g. for a message resource,
063     * the context may include a the headers of that message, and the values
064     * for those headers. For a system property resource, the context may
065     * just be the name of that system property.
066     *
067     * @param criteriaContext The context used to identify the resource
068     * @return true if this ResourceCriteria matches the supplied context,
069     *   false if it does not.
070     */
071    public boolean evaluate(Map<String, Object> criteriaContext)
072    {
073        return true;
074        
075    }
076}