mansehajsingh commented on code in PR #1037: URL: https://github.com/apache/polaris/pull/1037#discussion_r1975904659
########## service/common/src/main/java/org/apache/polaris/service/http/ETag.java: ########## @@ -0,0 +1,102 @@ +/* + * 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.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * HTTP Compliant ETag logical wrapper. + */ +public class ETag { + + protected static Pattern ETAG_PATTERN = Pattern.compile("(W/)?\"([^\"]*)\""); + + private final boolean weak; + + private final String value; + + public ETag(boolean weak, String value) { + this.weak = weak; + this.value = value; + } + + /** + * Consumes the raw value of an entity-tag and produces a logical representation + * @param rawValue + */ + public ETag(@Nonnull String rawValue) { + rawValue = rawValue.trim(); + Matcher matcher = ETag.ETAG_PATTERN.matcher(rawValue); + + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid ETag representation."); + } + + this.weak = rawValue.startsWith("W/"); + this.value = rawValue.substring(rawValue.indexOf('"') + 1, rawValue.length() - 1); + } + + /** + * Obtain the value of the etag. For example, if we had an etag W/"abc", this + * method would return abc + * @return The internal value of the etag + */ + public String value() { + return value; + } + + /** + * Determine if the etag is a weak etag + * @return true if the etag is prefixed by W/, false otherwise + */ + public boolean isWeak() { + return weak; + } + + @Override + public int hashCode() { + return Objects.hash(weak, value); + } + + @Override + public boolean equals(Object o) { + if (o instanceof ETag other) { + return weak == other.weak && value.equals(other.value); Review Comment: I agree. As such, if we receive a strong etag we should ensure that we don't match it. This requires us to define a way to tell strong etags from weak ones. While we don't generate strong etags, I don't believe the distinction being available is dead code. If I receive a strong etag in the header, the distinction that `isWeak()` provides me is the ability to check if the received etag was strong or weak, and then only return 304 if the received header was weak. -- 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]
