/** * $Id: BusinessHoursAcr_java.txt,v 1.9 2009/08/14 23:51:32 norb Exp $ * * Copyright (c) 1996-2009 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package examples.acrs; import com.cafesoft.cams.access.AbstractAccessControlRule; import com.cafesoft.cams.access.InternalAccessControlRequest; import com.cafesoft.cams.access.InternalAccessControlResponse; import com.cafesoft.cams.access.EvaluationException; import com.cafesoft.cams.access.SyntaxException; import java.util.Calendar; import java.util.GregorianCalendar; /** * BusinessHoursAcr demonstrates creation of a * custom Cams AccessControlRule. This class implements logic that requires * that protected resource be accessed only on weekdays during * "normal business hours". *

* This rule evaluates to true if: *

*

* else this rule evaluates to false. * * @version $Revision: 1.9 $ $Date: 2009/08/14 23:51:32 $ * @author Cafesoft LLC * * @see com.cafesoft.cams.access.AccessControlRule * @see com.cafesoft.cams.access.AbstractAccessControlRule * @see com.cafesoft.cams.access.EvaluationException * @see com.cafesoft.cams.access.SyntaxException */ public class BusinessHoursAcr extends AbstractAccessControlRule { // The configured normal business start hour private int startHour; // The configured normal business end hour private int endHour; /** * Create a new BusinessHoursAcr. */ public BusinessHoursAcr() { super(); this.startHour = -1; this.endHour = -1; } /** * Create a new BusinessHoursAcr with the specified id. * * @param id the AccessControlRule id. */ public BusinessHoursAcr(String id) { this(); setId(id); } // // extending AbstractAccessControlRule // /** * Set the "normal" business start hour. * * @param startHour the normal business start hour within a * 24-hour clock (0-23). * @exception SyntaxException if the start hour is not in the range 0-23 * or if the start hour is after the end hour. */ public void setStartHour(int startHour) throws SyntaxException { if ((startHour < 0) || (startHour > 23)) throw new SyntaxException( "Invalid business start hour=" + startHour); if (endHour > 0) { if (startHour > endHour) throw new SyntaxException( "Invalid business start hour=" + startHour + ": is before end hour=" + endHour); } this.startHour = startHour; } /** * Get the "normal" business start hour. * * @return the normal business start hour. The value is for a * 24-hour clock (0-23). */ public int getStartHour() { return startHour; } /** * Set the "normal" business end hour. * * @param endHour the normal business end hour within a * 24-hour clock (0-23). * @exception SyntaxException if the end hour is not in the range 0-23 * or if the end hour is before the start hour. */ public void setEndHour(int endHour) throws SyntaxException { if ((endHour < 0) || (endHour > 23)) throw new SyntaxException( "Invalid business end hour=" + endHour); if (startHour > 0) { if (endHour < startHour) throw new SyntaxException( "Invalid business end hour=" + endHour + ": it's before start hour=" + startHour); } this.endHour = endHour; } /** * Get the "normal" business end hour. * * @return the normal business end hour. The value is for a * 24-hour clock (0-23). */ public int getEndHour() { return endHour; } /** * Evaluate the AccessControlRule. * * @param request the access request. * @param response the access response. * @return true in all cases. * @exception EvaluationException if an error when attempting to * evaluate the rule. * @see AccessControlRequest * @see AccessControlResponse */ public boolean evaluate(InternalAccessControlRequest request, InternalAccessControlResponse response) throws EvaluationException { if (request == null) throw new EvaluationException("null InternalAccessControlRequest"); if (response == null) throw new EvaluationException("null InternalAccessControlResponse"); // Make sure the business hours were set if ((startHour < 0) || (endHour < 0)) throw new EvaluationException( "normal business hours have not been set"); // Get the current day/time Calendar cal = new GregorianCalendar(); // Enforce the day of week to a business day int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if ((dayOfWeek >= Calendar.MONDAY) && (dayOfWeek <= Calendar.FRIDAY)) { // Enforce business hours int hourOfDay = cal.get(Calendar.HOUR_OF_DAY); if ((hourOfDay >= startHour) && (hourOfDay <= endHour)) return true; // Not within normal business hours response.setReason(AccessControlResponse.RC_ACCESS_DENIED_UNCONDITIONALLY); response.setMessage("not within business hours: " + startHour + ":00 - " + endHour + ":00"); return false; } // Not a business day response.setReason(AccessControlResponse.RC_ACCESS_DENIED_UNCONDITIONALLY); response.setMessage("not a business day: M-F"); return false; } } // End of class: BusinessHoursAcr