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 java.util.Calendar;
033    import java.util.GregorianCalendar;
034    import java.util.LinkedList;
035    
036    /**
037     * This class implements the dayofweek bind rule keyword.
038     */
039    public class DayOfWeek  implements KeywordBindRule {
040    
041        /*
042         * List containing the enumeration of the day of the week.
043         */
044        LinkedList<EnumDayOfWeek> days=null;
045    
046        /*
047         * Enumeration representing the bind rule operation type.
048         */
049        private EnumBindRuleType type=null;
050    
051        /**
052         * Create a class representing a dayofweek bind rule keyword.
053         * @param days  A list of day of the week enumerations.
054         * @param type An enumeration representing the bind rule type.
055         */
056        private DayOfWeek(LinkedList<EnumDayOfWeek> days, EnumBindRuleType type) {
057            this.days=days;
058            this.type=type;
059        }
060    
061        /**
062         * Decode an string representing a dayofweek bind rule.
063         * @param expr A string representation of the bind rule.
064         * @param type  An enumeration representing the bind rule type.
065         * @return  A keyword bind rule class that can be used to evaluate
066         * this bind rule.
067         * @throws AciException  If the expression string is invalid.
068         */
069        public static KeywordBindRule decode(String expr, EnumBindRuleType type)
070        throws AciException
071        {
072            LinkedList<EnumDayOfWeek>days=new LinkedList<EnumDayOfWeek>();
073            String[] dayArray=expr.split(",", -1);
074            for(int i=0, m=dayArray.length; i < m; i++)
075            {
076              EnumDayOfWeek day=EnumDayOfWeek.createDayOfWeek(dayArray[i]);
077              if (day == null)
078              {
079                  Message message = WARN_ACI_SYNTAX_INVALID_DAYOFWEEK.get(expr);
080                  throw new AciException(message);
081              }
082              days.add(day);
083            }
084            return new DayOfWeek(days, type);
085        }
086    
087        /**
088         * Performs evaluation of a dayofweek bind rule using the provided
089         * evaluation context.
090         * @param evalCtx  An evaluation context to use in the evaluation.
091         * @return An enumeration evaluation result.
092         */
093        public EnumEvalResult evaluate(AciEvalContext evalCtx) {
094            EnumEvalResult matched=EnumEvalResult.FALSE;
095            GregorianCalendar calendar = new GregorianCalendar();
096            EnumDayOfWeek dayofweek
097                = EnumDayOfWeek.getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
098            if(days.contains(dayofweek))
099                matched=EnumEvalResult.TRUE;
100            return matched.getRet(type, false);
101        }
102    }