001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2015 ForgeRock AS.
016 */
017package org.opends.server.authorization.dseecompat;
018import org.forgerock.i18n.LocalizableMessage;
019
020import static org.opends.messages.AccessControlMessages.*;
021import org.opends.server.util.TimeThread;
022import java.util.regex.Pattern;
023
024/**
025 * This class represents the timeofday keyword in a bind rule.
026 */
027public class TimeOfDay implements KeywordBindRule {
028
029    /**
030     * Regular expression matching a valid timeofday rule value (0-2359).
031     */
032    private static final String timeofdayRegex = "[0-2]\\d[0-5]\\d";
033
034    /** Enumeration representing the bind rule operation type. */
035    private EnumBindRuleType type;
036
037    /** Holds the time value parsed from the ACI. */
038    private int timeRef;
039
040    /**
041     * Constructor to create a timeofday keyword class.
042     * @param timeVal The time value to check for (0-2359).
043     * @param type An enumeration of the type of the expression.
044     */
045    private TimeOfDay(int timeVal, EnumBindRuleType type) {
046        this.timeRef=timeVal;
047        this.type=type;
048    }
049
050    /**
051     * Decodes a string representation of a timeofday bind rule expression.
052     * @param expr A string representation of the expression.
053     * @param type An enumeration of the type of the expression.
054     * @return  A TimeOfDay class representing the expression.
055     * @throws AciException If the expression is invalid.
056     */
057    public static TimeOfDay decode(String expr,  EnumBindRuleType type)
058    throws AciException  {
059        int valueAsInt = 0;
060        if (!Pattern.matches(timeofdayRegex, expr))
061        {
062            LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY.get(expr);
063            throw new AciException(message);
064         }
065        try {
066            valueAsInt = Integer.parseInt(expr);
067        } catch (NumberFormatException nfe) {
068          LocalizableMessage message =
069           WARN_ACI_SYNTAX_INVALID_TIMEOFDAY_FORMAT.get(expr, nfe.getMessage());
070            throw new AciException(message);
071        }
072        if (valueAsInt < 0 || valueAsInt > 2359)
073        {
074            LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_TIMEOFDAY_RANGE.get(expr);
075            throw new AciException(message);
076        }
077
078        return new TimeOfDay(valueAsInt, type);
079    }
080
081    /**
082     * Evaluates the timeofday bind rule using the evaluation context
083     * passed into the method.
084     * @param evalCtx  The evaluation context to use for the evaluation.
085     * @return  An enumeration result representing the result of the
086     * evaluation.
087     */
088    public EnumEvalResult evaluate(AciEvalContext evalCtx) {
089        EnumEvalResult matched=EnumEvalResult.FALSE;
090
091        int currentTime=TimeThread.getHourAndMinute();
092        //check the type
093        switch (type) {
094        case EQUAL_BINDRULE_TYPE:
095        case NOT_EQUAL_BINDRULE_TYPE:
096            if (currentTime != timeRef)
097            {
098                matched=EnumEvalResult.TRUE;
099            }
100            break;
101
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
132    /** {@inheritDoc} */
133    @Override
134    public String toString()
135    {
136        final StringBuilder sb = new StringBuilder();
137        toString(sb);
138        return sb.toString();
139    }
140
141    /** {@inheritDoc} */
142    @Override
143    public final void toString(StringBuilder buffer)
144    {
145        buffer.append(super.toString());
146    }
147
148}