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-2016 ForgeRock AS. 016 */ 017package org.opends.server.authorization.dseecompat; 018 019import static org.opends.messages.AccessControlMessages.*; 020 021import java.util.Calendar; 022import java.util.GregorianCalendar; 023import java.util.LinkedList; 024import java.util.List; 025 026import org.forgerock.i18n.LocalizableMessage; 027 028/** This class implements the dayofweek bind rule keyword. */ 029public class DayOfWeek implements KeywordBindRule { 030 /** List containing the enumeration of the day of the week. */ 031 private final List<EnumDayOfWeek> days; 032 /** Enumeration representing the bind rule operation type. */ 033 private final EnumBindRuleType type; 034 035 /** 036 * Create a class representing a dayofweek bind rule keyword. 037 * @param days A list of day of the week enumerations. 038 * @param type An enumeration representing the bind rule type. 039 */ 040 private DayOfWeek(List<EnumDayOfWeek> days, EnumBindRuleType type) { 041 this.days=days; 042 this.type=type; 043 } 044 045 /** 046 * Decode an string representing a dayofweek bind rule. 047 * @param expr A string representation of the bind rule. 048 * @param type An enumeration representing the bind rule type. 049 * @return A keyword bind rule class that can be used to evaluate 050 * this bind rule. 051 * @throws AciException If the expression string is invalid. 052 */ 053 public static KeywordBindRule decode(String expr, EnumBindRuleType type) 054 throws AciException 055 { 056 List<EnumDayOfWeek> days = new LinkedList<>(); 057 String[] dayArray=expr.split(",", -1); 058 for (String element : dayArray) 059 { 060 EnumDayOfWeek day=EnumDayOfWeek.createDayOfWeek(element); 061 if (day == null) 062 { 063 LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_DAYOFWEEK.get(expr); 064 throw new AciException(message); 065 } 066 days.add(day); 067 } 068 return new DayOfWeek(days, type); 069 } 070 071 /** 072 * Performs evaluation of a dayofweek bind rule using the provided 073 * evaluation context. 074 * @param evalCtx An evaluation context to use in the evaluation. 075 * @return An enumeration evaluation result. 076 */ 077 @Override 078 public EnumEvalResult evaluate(AciEvalContext evalCtx) { 079 EnumEvalResult matched=EnumEvalResult.FALSE; 080 GregorianCalendar calendar = new GregorianCalendar(); 081 EnumDayOfWeek dayofweek 082 = EnumDayOfWeek.getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK)); 083 if(days.contains(dayofweek)) 084 { 085 matched=EnumEvalResult.TRUE; 086 } 087 return matched.getRet(type, false); 088 } 089 090 @Override 091 public String toString() 092 { 093 final StringBuilder sb = new StringBuilder(); 094 toString(sb); 095 return sb.toString(); 096 } 097 098 @Override 099 public final void toString(StringBuilder buffer) 100 { 101 buffer.append(super.toString()); 102 } 103}