001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.authorization.dseecompat;
029    import org.opends.messages.Message;
030    
031    import static org.opends.messages.AccessControlMessages.*;
032    import org.opends.server.util.TimeThread;
033    import java.util.regex.Pattern;
034    
035    /**
036     * This class represents the timeofday keyword in a bind rule.
037     */
038    public class TimeOfDay implements KeywordBindRule {
039    
040        /*
041         * Regular expression matching a valid timeofday rule value (0-2359).
042         */
043        private static final String timeofdayRegex = "[0-2]\\d[0-5]\\d";
044    
045        /*
046         *  Enumeration representing the bind rule operation type.
047         */
048        private EnumBindRuleType type=null;
049    
050        /*
051         * Holds the time value parsed from the ACI.
052         */
053        private int timeRef;
054    
055        /**
056         * Constructor to create a timeofday keyword class.
057         * @param timeVal The time value to check for (0-2359).
058         * @param type An enumeration of the type of the expression.
059         */
060        private TimeOfDay(int timeVal, EnumBindRuleType type) {
061            this.timeRef=timeVal;
062            this.type=type;
063        }
064    
065        /**
066         * Decodes a string representation of a timeofday bind rule expression.
067         * @param expr A string representation of the expression.
068         * @param type An enumeration of the type of the expression.
069         * @return  A TimeOfDay class representing the expression.
070         * @throws AciException If the expression is invalid.
071         */
072        public static TimeOfDay decode(String expr,  EnumBindRuleType type)
073        throws AciException  {
074            if (!Pattern.matches(timeofdayRegex, expr))
075            {
076                Message message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY.get(expr);
077                throw new AciException(message);
078             }
079            int valueAsInt = Integer.parseInt(expr);
080            if ((valueAsInt < 0) || (valueAsInt > 2359))
081            {
082                Message message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY_RANGE.get(expr);
083                throw new AciException(message);
084            }
085    
086            return new TimeOfDay(valueAsInt, type);
087        }
088    
089        /**
090         * Evaluates the timeofday bind rule using the evaluation context
091         * passed into the method.
092         * @param evalCtx  The evaluation context to use for the evaluation.
093         * @return  An enumeration result representing the result of the
094         * evaluation.
095         */
096        public EnumEvalResult evaluate(AciEvalContext evalCtx) {
097            EnumEvalResult matched=EnumEvalResult.FALSE;
098    
099            int currentTime=TimeThread.getHourAndMinute();
100            //check the type
101            switch (type) {
102            case LESS_OR_EQUAL_BINDRULE_TYPE:
103                if (currentTime <= timeRef)
104                {
105                    matched=EnumEvalResult.TRUE;
106                }
107                break;
108    
109            case LESS_BINDRULE_TYPE:
110                if (currentTime < timeRef)
111                {
112                    matched=EnumEvalResult.TRUE;
113                }
114                break;
115    
116            case GREATER_OR_EQUAL_BINDRULE_TYPE:
117                if (currentTime >= timeRef)
118                {
119                    matched=EnumEvalResult.TRUE;
120                }
121                break;
122    
123            case GREATER_BINDRULE_TYPE:
124                if (currentTime > timeRef)
125                {
126                    matched=EnumEvalResult.TRUE;
127                }
128            }
129            return matched.getRet(type, false);
130        }
131    }