Skip to content

Commit a0f4d81

Browse files
committed
Remove http -> web package dependency
See gh-24406
1 parent adc76a1 commit a0f4d81

File tree

3 files changed

+130
-62
lines changed

3 files changed

+130
-62
lines changed

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

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@
2424
import java.util.Arrays;
2525
import java.util.Map;
2626
import java.util.function.Consumer;
27-
import java.util.function.Function;
2827

2928
import org.springframework.lang.Nullable;
3029
import org.springframework.util.MultiValueMap;
3130
import org.springframework.util.ObjectUtils;
32-
import org.springframework.web.util.DefaultUriBuilderFactory;
33-
import org.springframework.web.util.UriTemplateHandler;
3431

3532
/**
3633
* Extension of {@link HttpEntity} that also exposes the HTTP method and the
@@ -69,17 +66,14 @@
6966
*/
7067
public class RequestEntity<T> extends HttpEntity<T> {
7168

72-
private final static UriTemplateHandler DEFAULT_TEMPLATE_HANDLER = new DefaultUriBuilderFactory();
73-
7469
@Nullable
7570
private final HttpMethod method;
7671

77-
private final Function<UriTemplateHandler, URI> uriFunction;
72+
private final URI url;
7873

7974
@Nullable
8075
private final Type type;
8176

82-
8377
/**
8478
* Constructor with method and URL but without body nor headers.
8579
* @param method the method
@@ -146,19 +140,9 @@ public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> h
146140
public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers,
147141
@Nullable HttpMethod method, URI url, @Nullable Type type) {
148142

149-
this(body, headers, method, handler -> url, type);
150-
}
151-
152-
/**
153-
* Private constructor with URI function.
154-
* @since 5.3
155-
*/
156-
private RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers,
157-
@Nullable HttpMethod method, Function<UriTemplateHandler, URI> uriFunction, @Nullable Type type) {
158-
159143
super(body, headers);
160144
this.method = method;
161-
this.uriFunction = uriFunction;
145+
this.url = url;
162146
this.type = type;
163147
}
164148

@@ -174,24 +158,9 @@ public HttpMethod getMethod() {
174158

175159
/**
176160
* Return the URL of the request.
177-
* <p>If the URL was provided as a URI template, the returned URI is expanded
178-
* and encoded with {@link DefaultUriBuilderFactory}.
179-
* @return the URL as a {@code URI}
180161
*/
181162
public URI getUrl() {
182-
return this.uriFunction.apply(DEFAULT_TEMPLATE_HANDLER);
183-
}
184-
185-
/**
186-
* Return the URL of the request.
187-
* <p>If the URL was provided as a URI template, the returned URI is expanded
188-
* with the given {@link DefaultUriBuilderFactory}.
189-
* @param templateHandler the handler to use to expand the URI template with
190-
* @return the URL as a {@code URI}
191-
* @since 5.3
192-
*/
193-
public URI getUrl(UriTemplateHandler templateHandler) {
194-
return this.uriFunction.apply(templateHandler);
163+
return this.url;
195164
}
196165

197166

@@ -235,13 +204,15 @@ public int hashCode() {
235204

236205
@Override
237206
public String toString() {
207+
return format(getMethod(), getUrl().toString(), getBody(), getHeaders());
208+
}
209+
210+
static <T> String format(@Nullable HttpMethod httpMethod, String url, @Nullable T body, HttpHeaders headers) {
238211
StringBuilder builder = new StringBuilder("<");
239-
builder.append(getMethod());
212+
builder.append(httpMethod);
240213
builder.append(' ');
241-
builder.append(getUrl());
214+
builder.append(url);
242215
builder.append(',');
243-
T body = getBody();
244-
HttpHeaders headers = getHeaders();
245216
if (body != null) {
246217
builder.append(body);
247218
builder.append(',');
@@ -563,24 +534,42 @@ private static class DefaultBodyBuilder implements BodyBuilder {
563534

564535
private final HttpMethod method;
565536

566-
private final Function<UriTemplateHandler, URI> uriFunction;
567-
568537
private final HttpHeaders headers = new HttpHeaders();
569538

539+
@Nullable
540+
private final URI uri;
541+
542+
@Nullable
543+
String uriTemplate;
544+
545+
@Nullable
546+
private Object[] uriVarsArray;
547+
548+
@Nullable
549+
Map<String, ?> uriVarsMap;
570550

571-
public DefaultBodyBuilder(HttpMethod method, URI url) {
551+
DefaultBodyBuilder(HttpMethod method, URI url) {
572552
this.method = method;
573-
this.uriFunction = handler -> url;
553+
this.uri = url;
554+
this.uriTemplate = null;
555+
this.uriVarsArray = null;
556+
this.uriVarsMap = null;
574557
}
575558

576-
public DefaultBodyBuilder(HttpMethod method, String uriTemplate, Object... uriVars) {
559+
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Object... uriVars) {
577560
this.method = method;
578-
this.uriFunction = handler -> handler.expand(uriTemplate, uriVars);
561+
this.uri = null;
562+
this.uriTemplate = uriTemplate;
563+
this.uriVarsArray = uriVars;
564+
this.uriVarsMap = null;
579565
}
580566

581-
public DefaultBodyBuilder(HttpMethod method, String uriTemplate, Map<String, ?> uriVars) {
567+
DefaultBodyBuilder(HttpMethod method, String uriTemplate, Map<String, ?> uriVars) {
582568
this.method = method;
583-
this.uriFunction = handler -> handler.expand(uriTemplate, uriVars);
569+
this.uri = null;
570+
this.uriTemplate = uriTemplate;
571+
this.uriVarsArray = null;
572+
this.uriVarsMap = uriVars;
584573
}
585574

586575
@Override
@@ -655,17 +644,85 @@ public BodyBuilder ifNoneMatch(String... ifNoneMatches) {
655644

656645
@Override
657646
public RequestEntity<Void> build() {
658-
return new RequestEntity<>(null, this.headers, this.method, this.uriFunction, null);
647+
return buildInternal(null, null);
659648
}
660649

661650
@Override
662651
public <T> RequestEntity<T> body(T body) {
663-
return new RequestEntity<>(body, this.headers, this.method, this.uriFunction, null);
652+
return buildInternal(body, null);
664653
}
665654

666655
@Override
667656
public <T> RequestEntity<T> body(T body, Type type) {
668-
return new RequestEntity<>(body, this.headers, this.method, this.uriFunction, type);
657+
return buildInternal(body, type);
658+
}
659+
660+
private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
661+
if (this.uri != null) {
662+
return new RequestEntity<>(body, this.headers, this.method, this.uri, type);
663+
}
664+
else if (this.uriTemplate != null){
665+
return new UriTemplateRequestEntity<>(body, this.headers, this.method, type,
666+
this.uriTemplate, this.uriVarsArray, this.uriVarsMap);
667+
}
668+
else {
669+
throw new IllegalStateException("Neither URI nor URI template");
670+
}
671+
}
672+
}
673+
674+
675+
/**
676+
* RequestEntity initialized with a URI template and variables instead of a {@link URI}.
677+
* @since 5.3
678+
* @param <T> the body type
679+
*/
680+
public static class UriTemplateRequestEntity<T> extends RequestEntity<T> {
681+
682+
String uriTemplate;
683+
684+
@Nullable
685+
private Object[] uriVarsArray;
686+
687+
@Nullable
688+
Map<String, ?> uriVarsMap;
689+
690+
691+
UriTemplateRequestEntity(
692+
@Nullable T body, @Nullable MultiValueMap<String, String> headers,
693+
@Nullable HttpMethod method, @Nullable Type type, String uriTemplate,
694+
@Nullable Object[] uriVarsArray, @Nullable Map<String, ?> uriVarsMap) {
695+
696+
super(body, headers, method, null, type);
697+
this.uriTemplate = uriTemplate;
698+
this.uriVarsArray = uriVarsArray;
699+
this.uriVarsMap = uriVarsMap;
700+
}
701+
702+
703+
public String getUriTemplate() {
704+
return this.uriTemplate;
705+
}
706+
707+
@Nullable
708+
public Object[] getVars() {
709+
return this.uriVarsArray;
710+
}
711+
712+
@Nullable
713+
public Map<String, ?> getVarsMap() {
714+
return this.uriVarsMap;
715+
}
716+
717+
@Override
718+
public URI getUrl() {
719+
throw new UnsupportedOperationException();
720+
}
721+
722+
@Override
723+
public String toString() {
724+
return format(getMethod(), getUriTemplate(), getBody(), getHeaders());
669725
}
670726
}
727+
671728
}

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -633,24 +633,34 @@ public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable Http
633633
}
634634

635635
@Override
636-
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType)
636+
public <T> ResponseEntity<T> exchange(RequestEntity<?> entity, Class<T> responseType)
637637
throws RestClientException {
638638

639-
RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
639+
RequestCallback requestCallback = httpEntityCallback(entity, responseType);
640640
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
641-
URI url = requestEntity.getUrl(this.uriTemplateHandler);
642-
return nonNull(doExecute(url, requestEntity.getMethod(), requestCallback, responseExtractor));
641+
return nonNull(doExecute(resolveUrl(entity), entity.getMethod(), requestCallback, responseExtractor));
643642
}
644643

645644
@Override
646-
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
645+
public <T> ResponseEntity<T> exchange(RequestEntity<?> entity, ParameterizedTypeReference<T> responseType)
647646
throws RestClientException {
648647

649648
Type type = responseType.getType();
650-
RequestCallback requestCallback = httpEntityCallback(requestEntity, type);
649+
RequestCallback requestCallback = httpEntityCallback(entity, type);
651650
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type);
652-
URI url = requestEntity.getUrl(this.uriTemplateHandler);
653-
return nonNull(doExecute(url, requestEntity.getMethod(), requestCallback, responseExtractor));
651+
return nonNull(doExecute(resolveUrl(entity), entity.getMethod(), requestCallback, responseExtractor));
652+
}
653+
654+
private URI resolveUrl(RequestEntity<?> entity) {
655+
if (entity instanceof RequestEntity.UriTemplateRequestEntity) {
656+
RequestEntity.UriTemplateRequestEntity<?> ext = (RequestEntity.UriTemplateRequestEntity<?>) entity;
657+
return (ext.getVars() != null ?
658+
this.uriTemplateHandler.expand(ext.getUriTemplate(), ext.getVars()) :
659+
this.uriTemplateHandler.expand(ext.getUriTemplate(), ext.getVarsMap()));
660+
}
661+
else {
662+
return entity.getUrl();
663+
}
654664
}
655665

656666

spring-web/src/test/java/org/springframework/http/RequestEntityTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,17 @@ public void uriVariablesExpansion() throws URISyntaxException {
8282
}
8383

8484
@Test
85-
public void uriExpansion() throws URISyntaxException{
85+
public void uriExpansion() {
8686

8787
RequestEntity<Void> entity =
8888
RequestEntity.get("https://www.{host}.com/{path}", "example", "foo/bar").build();
8989

90-
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
91-
assertThat(entity.getUrl(factory)).isEqualTo(new URI("https://www.example.com/foo%2Fbar"));
90+
assertThat(entity).isInstanceOf(RequestEntity.UriTemplateRequestEntity.class);
91+
RequestEntity.UriTemplateRequestEntity<Void> ext = (RequestEntity.UriTemplateRequestEntity<Void>) entity;
9292

93-
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
94-
assertThat(entity.getUrl(factory)).isEqualTo(new URI("https://www.example.com/foo/bar"));
93+
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
94+
assertThat(ext.getUriTemplate()).isEqualTo("https://www.{host}.com/{path}");
95+
assertThat(ext.getVars()).containsExactly("example", "foo/bar");
9596
}
9697

9798

0 commit comments

Comments
 (0)