Skip to content

Commit 80b264b

Browse files
hyunmin0317rstoyanchev
authored andcommitted
Refactor eTag formatting into utility method
See gh-33412
1 parent cc3f4b8 commit 80b264b

File tree

6 files changed

+18
-57
lines changed

6 files changed

+18
-57
lines changed

spring-web/src/main/java/org/springframework/http/ETag.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ public static List<ETag> parse(String source) {
134134
return result;
135135
}
136136

137+
public static String format(String etag) {
138+
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
139+
etag = "\"" + etag;
140+
}
141+
if (!etag.endsWith("\"")) {
142+
etag = etag + "\"";
143+
}
144+
return etag;
145+
}
137146

138147
private enum State {
139148

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,7 @@ public BodyBuilder contentType(MediaType contentType) {
570570
@Override
571571
public BodyBuilder eTag(@Nullable String etag) {
572572
if (etag != null) {
573-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
574-
etag = "\"" + etag;
575-
}
576-
if (!etag.endsWith("\"")) {
577-
etag = etag + "\"";
578-
}
573+
etag = ETag.format(etag);
579574
}
580575
this.headers.setETag(etag);
581576
return this;

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@
2828
import java.util.Set;
2929
import java.util.function.Consumer;
3030

31+
import org.springframework.http.*;
3132
import reactor.core.publisher.Mono;
3233

3334
import org.springframework.core.codec.Hints;
34-
import org.springframework.http.CacheControl;
35-
import org.springframework.http.HttpHeaders;
36-
import org.springframework.http.HttpMethod;
37-
import org.springframework.http.HttpStatus;
38-
import org.springframework.http.HttpStatusCode;
39-
import org.springframework.http.MediaType;
40-
import org.springframework.http.ResponseCookie;
4135
import org.springframework.http.codec.HttpMessageWriter;
4236
import org.springframework.http.server.reactive.ServerHttpRequest;
4337
import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -148,12 +142,7 @@ public EntityResponse.Builder<T> contentType(MediaType contentType) {
148142

149143
@Override
150144
public EntityResponse.Builder<T> eTag(String etag) {
151-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
152-
etag = "\"" + etag;
153-
}
154-
if (!etag.endsWith("\"")) {
155-
etag = etag + "\"";
156-
}
145+
etag = ETag.format(etag);
157146
this.headers.setETag(etag);
158147
return this;
159148
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@
3232
import java.util.function.Function;
3333

3434
import org.reactivestreams.Publisher;
35+
import org.springframework.http.*;
3536
import reactor.core.publisher.Mono;
3637

3738
import org.springframework.core.ParameterizedTypeReference;
3839
import org.springframework.core.codec.Hints;
39-
import org.springframework.http.CacheControl;
40-
import org.springframework.http.HttpHeaders;
41-
import org.springframework.http.HttpMethod;
42-
import org.springframework.http.HttpStatusCode;
43-
import org.springframework.http.MediaType;
44-
import org.springframework.http.ReactiveHttpOutputMessage;
45-
import org.springframework.http.ResponseCookie;
4640
import org.springframework.http.codec.HttpMessageWriter;
4741
import org.springframework.http.server.reactive.ServerHttpRequest;
4842
import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -148,12 +142,7 @@ public ServerResponse.BodyBuilder contentType(MediaType contentType) {
148142
@Override
149143
public ServerResponse.BodyBuilder eTag(String etag) {
150144
Assert.notNull(etag, "etag must not be null");
151-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
152-
etag = "\"" + etag;
153-
}
154-
if (!etag.endsWith("\"")) {
155-
etag = etag + "\"";
156-
}
145+
etag = ETag.format(etag);
157146
this.headers.setETag(etag);
158147
return this;
159148
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilder.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@
4545
import org.springframework.core.io.InputStreamResource;
4646
import org.springframework.core.io.Resource;
4747
import org.springframework.core.io.support.ResourceRegion;
48-
import org.springframework.http.CacheControl;
49-
import org.springframework.http.HttpHeaders;
50-
import org.springframework.http.HttpMethod;
51-
import org.springframework.http.HttpRange;
52-
import org.springframework.http.HttpStatus;
53-
import org.springframework.http.HttpStatusCode;
54-
import org.springframework.http.InvalidMediaTypeException;
55-
import org.springframework.http.MediaType;
48+
import org.springframework.http.*;
5649
import org.springframework.http.converter.GenericHttpMessageConverter;
5750
import org.springframework.http.converter.HttpMessageConverter;
5851
import org.springframework.http.converter.SmartHttpMessageConverter;
@@ -166,12 +159,7 @@ public EntityResponse.Builder<T> contentType(MediaType contentType) {
166159

167160
@Override
168161
public EntityResponse.Builder<T> eTag(String etag) {
169-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
170-
etag = "\"" + etag;
171-
}
172-
if (!etag.endsWith("\"")) {
173-
etag = etag + "\"";
174-
}
162+
etag = ETag.format(etag);
175163
this.headers.setETag(etag);
176164
return this;
177165
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerResponseBuilder.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
import jakarta.servlet.http.HttpServletResponse;
3131

3232
import org.springframework.core.ParameterizedTypeReference;
33-
import org.springframework.http.CacheControl;
34-
import org.springframework.http.HttpHeaders;
35-
import org.springframework.http.HttpMethod;
36-
import org.springframework.http.HttpStatusCode;
37-
import org.springframework.http.MediaType;
33+
import org.springframework.http.*;
3834
import org.springframework.lang.Nullable;
3935
import org.springframework.util.Assert;
4036
import org.springframework.util.LinkedMultiValueMap;
@@ -128,12 +124,7 @@ public ServerResponse.BodyBuilder contentType(MediaType contentType) {
128124
@Override
129125
public ServerResponse.BodyBuilder eTag(String etag) {
130126
Assert.notNull(etag, "etag must not be null");
131-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
132-
etag = "\"" + etag;
133-
}
134-
if (!etag.endsWith("\"")) {
135-
etag = etag + "\"";
136-
}
127+
etag = ETag.format(etag);
137128
this.headers.setETag(etag);
138129
return this;
139130
}

0 commit comments

Comments
 (0)