Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor eTag formatting logic into a static utility method #33412

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spring-web/src/main/java/org/springframework/http/ETag.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ public static List<ETag> parse(String source) {
return result;
}

public static String format(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
return etag;
}

private enum State {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,7 @@ public BodyBuilder contentType(MediaType contentType) {
@Override
public BodyBuilder eTag(@Nullable String etag) {
if (etag != null) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
etag = ETag.format(etag);
}
this.headers.setETag(etag);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@
import java.util.Set;
import java.util.function.Consumer;

import org.springframework.http.*;
import reactor.core.publisher.Mono;

import org.springframework.core.codec.Hints;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
Expand Down Expand Up @@ -148,12 +142,7 @@ public EntityResponse.Builder<T> contentType(MediaType contentType) {

@Override
public EntityResponse.Builder<T> eTag(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
etag = ETag.format(etag);
this.headers.setETag(etag);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@
import java.util.function.Function;

import org.reactivestreams.Publisher;
import org.springframework.http.*;
import reactor.core.publisher.Mono;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.Hints;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.ResponseCookie;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
Expand Down Expand Up @@ -148,12 +142,7 @@ public ServerResponse.BodyBuilder contentType(MediaType contentType) {
@Override
public ServerResponse.BodyBuilder eTag(String etag) {
Assert.notNull(etag, "etag must not be null");
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
etag = ETag.format(etag);
this.headers.setETag(etag);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.SmartHttpMessageConverter;
Expand Down Expand Up @@ -166,12 +159,7 @@ public EntityResponse.Builder<T> contentType(MediaType contentType) {

@Override
public EntityResponse.Builder<T> eTag(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
etag = ETag.format(etag);
this.headers.setETag(etag);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
Expand Down Expand Up @@ -128,12 +124,7 @@ public ServerResponse.BodyBuilder contentType(MediaType contentType) {
@Override
public ServerResponse.BodyBuilder eTag(String etag) {
Assert.notNull(etag, "etag must not be null");
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
}
etag = ETag.format(etag);
this.headers.setETag(etag);
return this;
}
Expand Down