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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.authorization.dseecompat;
028
029import static org.opends.messages.AccessControlMessages.*;
030
031import java.util.Calendar;
032import java.util.GregorianCalendar;
033import java.util.LinkedList;
034import java.util.List;
035
036import org.forgerock.i18n.LocalizableMessage;
037
038/**
039 * This class implements the dayofweek bind rule keyword.
040 */
041public class DayOfWeek  implements KeywordBindRule {
042
043    /** List containing the enumeration of the day of the week. */
044    private List<EnumDayOfWeek> days;
045
046    /** Enumeration representing the bind rule operation type. */
047    private EnumBindRuleType type;
048
049    /**
050     * Create a class representing a dayofweek bind rule keyword.
051     * @param days  A list of day of the week enumerations.
052     * @param type An enumeration representing the bind rule type.
053     */
054    private DayOfWeek(List<EnumDayOfWeek> days, EnumBindRuleType type) {
055        this.days=days;
056        this.type=type;
057    }
058
059    /**
060     * Decode an string representing a dayofweek bind rule.
061     * @param expr A string representation of the bind rule.
062     * @param type  An enumeration representing the bind rule type.
063     * @return  A keyword bind rule class that can be used to evaluate
064     * this bind rule.
065     * @throws AciException  If the expression string is invalid.
066     */
067    public static KeywordBindRule decode(String expr, EnumBindRuleType type)
068    throws AciException
069    {
070        List<EnumDayOfWeek> days = new LinkedList<>();
071        String[] dayArray=expr.split(",", -1);
072        for (String element : dayArray)
073        {
074          EnumDayOfWeek day=EnumDayOfWeek.createDayOfWeek(element);
075          if (day == null)
076          {
077              LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_DAYOFWEEK.get(expr);
078              throw new AciException(message);
079          }
080          days.add(day);
081        }
082        return new DayOfWeek(days, type);
083    }
084
085    /**
086     * Performs evaluation of a dayofweek bind rule using the provided
087     * evaluation context.
088     * @param evalCtx  An evaluation context to use in the evaluation.
089     * @return An enumeration evaluation result.
090     */
091    @Override
092    public EnumEvalResult evaluate(AciEvalContext evalCtx) {
093        EnumEvalResult matched=EnumEvalResult.FALSE;
094        GregorianCalendar calendar = new GregorianCalendar();
095        EnumDayOfWeek dayofweek
096            = EnumDayOfWeek.getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
097        if(days.contains(dayofweek))
098        {
099          matched=EnumEvalResult.TRUE;
100        }
101        return matched.getRet(type, false);
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public String toString()
107    {
108      final StringBuilder sb = new StringBuilder();
109      toString(sb);
110      return sb.toString();
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public final void toString(StringBuilder buffer)
116    {
117      buffer.append(super.toString());
118    }
119
120}