necouchman commented on code in PR #830:
URL: https://github.com/apache/guacamole-client/pull/830#discussion_r1164590791


##########
extensions/guacamole-auth-restrict/src/main/java/org/apache/guacamole/calendar/TimeRestrictionParser.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.guacamole.calendar;
+
+import java.time.DayOfWeek;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A class for parsing time-based restrictions stored in a String into other
+ * formats that can be used by Guacamole.
+ */
+public class TimeRestrictionParser {
+    
+    /**
+     * The compiled regular expression that matches one or more instances of
+     * a restriction string, which specifies at least one day and time range
+     * that the restriction applies to.
+     */
+    private static final Pattern RESTRICTION_REGEX = 
+            
Pattern.compile("(([1-7](?:\\-(?:[1-7]))?)(?::((?:[01]?[0-9]|2[0-3])[0-5][0-9])\\-((?:[01]?[0-9]|2[0-3])[0-5][0-9]))*)+(?:;|$)?");
+    
+    /**
+     * The RegEx group that contains the start day-of-week of the restriction.
+     */
+    private static final int RESTRICTION_DAY_GROUP = 2;
+    
+    /**
+     * The RegEx group that contains the start time of the restriction.
+     */
+    private static final int RESTRICTION_TIME_START_GROUP = 3;
+    
+    /**
+     * The RegEx group that contains the end time of the restriction.
+     */
+    private static final int RESTRICTION_TIME_END_GROUP = 4;
+    
+    /**
+     * Parse the provided string containing one or more restrictions into
+     * a list of objects.
+     * 
+     * @param restrictionString
+     *     The string that should contain one or more semicolon-separated
+     *     restriction periods.
+     * 
+     * @return 
+     *     A list of objects parsed from the string.
+     */
+    public static List<DailyRestriction> parseString(String restrictionString) 
{
+        
+        List<DailyRestriction> restrictions = new ArrayList<>();
+        Matcher restrictionMatcher = 
RESTRICTION_REGEX.matcher(restrictionString);
+        
+        // Loop through RegEx matches
+        while (restrictionMatcher.find()) {
+            
+            // Pull the day string, start time, and end time
+            String dayString = restrictionMatcher.group(RESTRICTION_DAY_GROUP);
+            String startTimeString = 
restrictionMatcher.group(RESTRICTION_TIME_START_GROUP);
+            String endTimeString = 
restrictionMatcher.group(RESTRICTION_TIME_END_GROUP);
+            int dayStart, dayEnd;
+            
+            // We must always have a start day.
+            if (dayString == null || dayString.isEmpty())
+                continue;
+            
+            // Check to see if the day string contains a range or not.
+            if (dayString.contains("-")) {
+                String dayStrings[] = dayString.split("-");
+                dayStart = Integer.parseInt(dayStrings[0]);
+                dayEnd = Integer.parseInt(dayStrings[1]);
+            }
+            else {
+                dayStart = Integer.parseInt(dayString);
+                dayEnd = Integer.parseInt(dayString);
+            }
+            
+            // Convert the start and end time strings to LocalTime values.
+            DateTimeFormatter hourFormat = DateTimeFormatter.ofPattern("HHmm");
+            LocalTime startTime = LocalTime.parse(startTimeString, hourFormat);
+            LocalTime endTime = LocalTime.parse(endTimeString, hourFormat);
+            
+            // Loop through days and add entries for each day
+            for (int i = dayStart; i <= dayEnd; i++) {
+                DayOfWeek weekDay = DayOfWeek.of(i);
+                
+                // We must always have both start time and end time.
+                if (startTimeString == null || startTimeString.isEmpty() 
+                        || endTimeString == null || endTimeString.isEmpty())
+                    restrictions.add(new DailyRestriction(weekDay));
+
+                else
+                    restrictions.add(new DailyRestriction(weekDay, 
+                            startTime, endTime));
+            }
+            
+        }
+        
+        // Return the list of restrictions
+        return restrictions;
+        
+    }
+    
+    public static String toString(List<DailyRestriction> restrictions) {

Review Comment:
   Nope, not used - removed it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@guacamole.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to