24
24
import java .util .Arrays ;
25
25
import java .util .Map ;
26
26
import java .util .function .Consumer ;
27
- import java .util .function .Function ;
28
27
29
28
import org .springframework .lang .Nullable ;
30
29
import org .springframework .util .MultiValueMap ;
31
30
import org .springframework .util .ObjectUtils ;
32
- import org .springframework .web .util .DefaultUriBuilderFactory ;
33
- import org .springframework .web .util .UriTemplateHandler ;
34
31
35
32
/**
36
33
* Extension of {@link HttpEntity} that also exposes the HTTP method and the
69
66
*/
70
67
public class RequestEntity <T > extends HttpEntity <T > {
71
68
72
- private final static UriTemplateHandler DEFAULT_TEMPLATE_HANDLER = new DefaultUriBuilderFactory ();
73
-
74
69
@ Nullable
75
70
private final HttpMethod method ;
76
71
77
- private final Function < UriTemplateHandler , URI > uriFunction ;
72
+ private final URI url ;
78
73
79
74
@ Nullable
80
75
private final Type type ;
81
76
82
-
83
77
/**
84
78
* Constructor with method and URL but without body nor headers.
85
79
* @param method the method
@@ -146,19 +140,9 @@ public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> h
146
140
public RequestEntity (@ Nullable T body , @ Nullable MultiValueMap <String , String > headers ,
147
141
@ Nullable HttpMethod method , URI url , @ Nullable Type type ) {
148
142
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
-
159
143
super (body , headers );
160
144
this .method = method ;
161
- this .uriFunction = uriFunction ;
145
+ this .url = url ;
162
146
this .type = type ;
163
147
}
164
148
@@ -174,24 +158,9 @@ public HttpMethod getMethod() {
174
158
175
159
/**
176
160
* 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}
180
161
*/
181
162
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 ;
195
164
}
196
165
197
166
@@ -235,13 +204,15 @@ public int hashCode() {
235
204
236
205
@ Override
237
206
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 ) {
238
211
StringBuilder builder = new StringBuilder ("<" );
239
- builder .append (getMethod () );
212
+ builder .append (httpMethod );
240
213
builder .append (' ' );
241
- builder .append (getUrl () );
214
+ builder .append (url );
242
215
builder .append (',' );
243
- T body = getBody ();
244
- HttpHeaders headers = getHeaders ();
245
216
if (body != null ) {
246
217
builder .append (body );
247
218
builder .append (',' );
@@ -563,24 +534,42 @@ private static class DefaultBodyBuilder implements BodyBuilder {
563
534
564
535
private final HttpMethod method ;
565
536
566
- private final Function <UriTemplateHandler , URI > uriFunction ;
567
-
568
537
private final HttpHeaders headers = new HttpHeaders ();
569
538
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 ;
570
550
571
- public DefaultBodyBuilder (HttpMethod method , URI url ) {
551
+ DefaultBodyBuilder (HttpMethod method , URI url ) {
572
552
this .method = method ;
573
- this .uriFunction = handler -> url ;
553
+ this .uri = url ;
554
+ this .uriTemplate = null ;
555
+ this .uriVarsArray = null ;
556
+ this .uriVarsMap = null ;
574
557
}
575
558
576
- public DefaultBodyBuilder (HttpMethod method , String uriTemplate , Object ... uriVars ) {
559
+ DefaultBodyBuilder (HttpMethod method , String uriTemplate , Object ... uriVars ) {
577
560
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 ;
579
565
}
580
566
581
- public DefaultBodyBuilder (HttpMethod method , String uriTemplate , Map <String , ?> uriVars ) {
567
+ DefaultBodyBuilder (HttpMethod method , String uriTemplate , Map <String , ?> uriVars ) {
582
568
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 ;
584
573
}
585
574
586
575
@ Override
@@ -655,17 +644,85 @@ public BodyBuilder ifNoneMatch(String... ifNoneMatches) {
655
644
656
645
@ Override
657
646
public RequestEntity <Void > build () {
658
- return new RequestEntity <> (null , this . headers , this . method , this . uriFunction , null );
647
+ return buildInternal (null , null );
659
648
}
660
649
661
650
@ Override
662
651
public <T > RequestEntity <T > body (T body ) {
663
- return new RequestEntity <> (body , this . headers , this . method , this . uriFunction , null );
652
+ return buildInternal (body , null );
664
653
}
665
654
666
655
@ Override
667
656
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 ());
669
725
}
670
726
}
727
+
671
728
}
0 commit comments