eric-maynard commented on code in PR #1037:
URL: https://github.com/apache/polaris/pull/1037#discussion_r1978266463


##########
service/common/src/main/java/org/apache/polaris/service/http/IfNoneMatch.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.polaris.service.http;
+
+import jakarta.annotation.Nonnull;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+/**
+ * Logical representation of an HTTP compliant If-None-Match header.
+ */
+public record IfNoneMatch(boolean isWildcard, @Nonnull List<String> eTags) {
+
+    protected static Pattern ETAG_PATTERN = 
Pattern.compile("(W/)?\"([^\"]*)\"");
+
+    public IfNoneMatch(List<String> etags) {
+        this(false, etags);
+    }
+
+    public IfNoneMatch {
+        if (isWildcard && !eTags.isEmpty()) {
+            // if the header is a wildcard, it must not be constructed with
+            // any etags, if it is not a wildcard, it can still contain no 
ETags, so
+            // the converse is not true
+            throw new IllegalArgumentException(
+                    "Invalid representation for If-None-Match header. 
If-None-Match cannot contain ETags if it takes " +
+                            "the wildcard value '*'");
+        }
+
+        for (String etag : eTags) {
+            Matcher matcher = ETAG_PATTERN.matcher(etag);
+
+            if (!matcher.matches()) {
+                throw new IllegalArgumentException("Invalid ETag 
representation: " + etag);
+            }
+        }
+    }
+
+    /**
+     * Parses the raw content of an If-None-Match header into the logical 
representation
+     * @param rawValue The raw value of the If-None-Match header
+     * @return A logically equivalent representation of the raw header content
+     */
+    public static IfNoneMatch fromHeader(String rawValue) {
+        // parse null header as an empty header
+        if (rawValue == null) {
+            return new IfNoneMatch(List.of());

Review Comment:
   Can we make `IfNoneMatch(List.of())` a constant? 



##########
service/common/src/main/java/org/apache/polaris/service/http/IfNoneMatch.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.polaris.service.http;
+
+import jakarta.annotation.Nonnull;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+/**
+ * Logical representation of an HTTP compliant If-None-Match header.
+ */
+public record IfNoneMatch(boolean isWildcard, @Nonnull List<String> eTags) {
+
+    protected static Pattern ETAG_PATTERN = 
Pattern.compile("(W/)?\"([^\"]*)\"");
+
+    public IfNoneMatch(List<String> etags) {
+        this(false, etags);
+    }
+
+    public IfNoneMatch {
+        if (isWildcard && !eTags.isEmpty()) {
+            // if the header is a wildcard, it must not be constructed with
+            // any etags, if it is not a wildcard, it can still contain no 
ETags, so
+            // the converse is not true
+            throw new IllegalArgumentException(
+                    "Invalid representation for If-None-Match header. 
If-None-Match cannot contain ETags if it takes " +
+                            "the wildcard value '*'");
+        }
+
+        for (String etag : eTags) {
+            Matcher matcher = ETAG_PATTERN.matcher(etag);
+
+            if (!matcher.matches()) {
+                throw new IllegalArgumentException("Invalid ETag 
representation: " + etag);
+            }
+        }
+    }
+
+    /**
+     * Parses the raw content of an If-None-Match header into the logical 
representation
+     * @param rawValue The raw value of the If-None-Match header
+     * @return A logically equivalent representation of the raw header content
+     */
+    public static IfNoneMatch fromHeader(String rawValue) {
+        // parse null header as an empty header
+        if (rawValue == null) {
+            return new IfNoneMatch(List.of());
+        }
+
+        rawValue = rawValue.trim();
+        if (rawValue.equals("*")) {

Review Comment:
   Let's use a constant here, too



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to