@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2019 the original author or authors .
* Copyright 2002 - 2020 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -22,39 +22,32 @@ import java.nio.charset.Charset;
@@ -22,39 +22,32 @@ import java.nio.charset.Charset;
import java.time.Instant ;
import java.time.ZonedDateTime ;
import java.util.Arrays ;
import java.util.Map ;
import java.util.function.Consumer ;
import java.util.function.Function ;
import org.springframework.lang.Nullable ;
import org.springframework.util.MultiValueMap ;
import org.springframework.util.ObjectUtils ;
import org.springframework.web.util.DefaultUriBuilderFactory ;
import org.springframework.web.util.UriTemplateHandler ;
/ * *
* Extension of { @link HttpEntity } that adds a { @linkplain HttpMethod method } and
* { @linkplain URI uri } . Used in { @code RestTemplate } and { @code @Controller } methods .
* Extension of { @link HttpEntity } that also exposes the HTTP method and the
* target URL . For use in the { @code RestTemplate } to prepare requests with
* and in { @code @Controller } methods to represent request input .
*
* < p > In { @code RestTemplate } , this class is used as parameter in
* { @link org . springframework . web . client . RestTemplate # exchange ( RequestEntity , Class ) exchange ( ) } :
* < p > Example use with the { @code RestTemplate } :
* < pre class = "code" >
* MyRequest body = . . .
* RequestEntity & lt ; MyRequest & gt ; request = RequestEntity
* . post ( new URI ( & quot ; https : //example.com/bar") )
* . post ( & quot ; https : //example.com/{foo}", " bar")
* . accept ( MediaType . APPLICATION_JSON )
* . body ( body ) ;
* ResponseEntity & lt ; MyResponse & gt ; response = template . exchange ( request , MyResponse . class ) ;
* < / pre >
*
* < p > If you would like to provide a URI template with variables , consider using
* { @link org . springframework . web . util . DefaultUriBuilderFactory DefaultUriBuilderFactory } :
* < pre class = "code" >
* // Create shared factory
* UriBuilderFactory factory = new DefaultUriBuilderFactory ( ) ;
*
* // Use factory to create URL from template
* URI uri = factory . uriString ( & quot ; https : //example.com/{foo}").build("bar");
* RequestEntity & lt ; MyRequest & gt ; request = RequestEntity . post ( uri ) . accept ( MediaType . APPLICATION_JSON ) . body ( body ) ;
* < / pre >
*
* < p > Can also be used in Spring MVC , as a parameter in a @Controller method :
* < p > Example use in an { @code @Controller } :
* < pre class = "code" >
* & # 64 ; RequestMapping ( "/handle" )
* public void handle ( RequestEntity & lt ; String & gt ; request ) {
@ -66,6 +59,7 @@ import org.springframework.util.ObjectUtils;
@@ -66,6 +59,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Parviz Rozikov
* @since 4 . 1
* @param < T > the body type
* @see # getMethod ( )
@ -73,10 +67,12 @@ import org.springframework.util.ObjectUtils;
@@ -73,10 +67,12 @@ import org.springframework.util.ObjectUtils;
* /
public class RequestEntity < T > extends HttpEntity < T > {
private final static UriTemplateHandler DEFAULT_TEMPLATE_HANDLER = new DefaultUriBuilderFactory ( ) ;
@Nullable
private final HttpMethod method ;
private final URI url ;
private final Function < UriTemplateHandler , URI > uriFunction ;
@Nullable
private final Type type ;
@ -148,9 +144,19 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -148,9 +144,19 @@ public class RequestEntity<T> extends HttpEntity<T> {
public RequestEntity ( @Nullable T body , @Nullable MultiValueMap < String , String > headers ,
@Nullable HttpMethod method , URI url , @Nullable Type type ) {
this ( body , headers , method , handler - > url , type ) ;
}
/ * *
* Private constructor with URI function .
* @since 5 . 3
* /
private RequestEntity ( @Nullable T body , @Nullable MultiValueMap < String , String > headers ,
@Nullable HttpMethod method , Function < UriTemplateHandler , URI > uriFunction , @Nullable Type type ) {
super ( body , headers ) ;
this . method = method ;
this . url = url ;
this . uriFunction = uriFunction ;
this . type = type ;
}
@ -166,12 +172,27 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -166,12 +172,27 @@ public class RequestEntity<T> extends HttpEntity<T> {
/ * *
* Return the URL of the request .
* < p > If the URL was provided as a URI template , the returned URI is expanded
* and encoded with { @link DefaultUriBuilderFactory } .
* @return the URL as a { @code URI }
* /
public URI getUrl ( ) {
return this . url ;
return this . uriFunction . apply ( DEFAULT_TEMPLATE_HANDLER ) ;
}
/ * *
* Return the URL of the request .
* < p > If the URL was provided as a URI template , the returned URI is expanded
* with the given { @link DefaultUriBuilderFactory } .
* @param templateHandler the handler to use to expand the URI template with
* @return the URL as a { @code URI }
* @since 5 . 3
* /
public URI getUrl ( UriTemplateHandler templateHandler ) {
return this . uriFunction . apply ( templateHandler ) ;
}
/ * *
* Return the type of the request ' s body .
* @return the request ' s body type , or { @code null } if not known
@ -206,7 +227,7 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -206,7 +227,7 @@ public class RequestEntity<T> extends HttpEntity<T> {
public int hashCode ( ) {
int hashCode = super . hashCode ( ) ;
hashCode = 29 * hashCode + ObjectUtils . nullSafeHashCode ( this . method ) ;
hashCode = 29 * hashCode + ObjectUtils . nullSafeHashCode ( this . url ) ;
hashCode = 29 * hashCode + ObjectUtils . nullSafeHashCode ( getUrl ( ) ) ;
return hashCode ;
}
@ -241,6 +262,30 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -241,6 +262,30 @@ public class RequestEntity<T> extends HttpEntity<T> {
return new DefaultBodyBuilder ( method , url ) ;
}
/ * *
* Create a builder with the given HTTP method , URI template , and variables .
* @param method the HTTP method ( GET , POST , etc )
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static BodyBuilder method ( HttpMethod method , String uriTemplate , Object . . . uriVariables ) {
return new DefaultBodyBuilder ( method , uriTemplate , uriVariables ) ;
}
/ * *
* Create a builder with the given HTTP method , URI template , and variables .
* @param method the HTTP method ( GET , POST , etc )
* @param uriTemplate the uri template to use
* @return the created builder
* @since 5 . 3
* /
public static BodyBuilder method ( HttpMethod method , String uriTemplate , Map < String , ? > uriVariables ) {
return new DefaultBodyBuilder ( method , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP GET builder with the given url .
* @param url the URL
@ -250,6 +295,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -250,6 +295,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . GET , url ) ;
}
/ * *
* Create an HTTP GET builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static HeadersBuilder < ? > get ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . GET , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP HEAD builder with the given url .
* @param url the URL
@ -259,6 +315,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -259,6 +315,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . HEAD , url ) ;
}
/ * *
* Create an HTTP HEAD builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static HeadersBuilder < ? > head ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . HEAD , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP POST builder with the given url .
* @param url the URL
@ -268,6 +335,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -268,6 +335,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . POST , url ) ;
}
/ * *
* Create an HTTP POST builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static BodyBuilder post ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . POST , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP PUT builder with the given url .
* @param url the URL
@ -277,6 +355,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -277,6 +355,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . PUT , url ) ;
}
/ * *
* Create an HTTP PUT builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static BodyBuilder put ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . PUT , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP PATCH builder with the given url .
* @param url the URL
@ -286,6 +375,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -286,6 +375,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . PATCH , url ) ;
}
/ * *
* Create an HTTP PATCH builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static BodyBuilder patch ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . PATCH , uriTemplate , uriVariables ) ;
}
/ * *
* Create an HTTP DELETE builder with the given url .
* @param url the URL
@ -295,6 +395,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -295,6 +395,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . DELETE , url ) ;
}
/ * *
* Create an HTTP DELETE builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static HeadersBuilder < ? > delete ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . DELETE , uriTemplate , uriVariables ) ;
}
/ * *
* Creates an HTTP OPTIONS builder with the given url .
* @param url the URL
@ -304,6 +415,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -304,6 +415,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method ( HttpMethod . OPTIONS , url ) ;
}
/ * *
* Creates an HTTP OPTIONS builder with the given string base uri template .
* @param uriTemplate the uri template to use
* @param uriVariables variables to expand the URI template with
* @return the created builder
* @since 5 . 3
* /
public static HeadersBuilder < ? > options ( String uriTemplate , Object . . . uriVariables ) {
return method ( HttpMethod . OPTIONS , uriTemplate , uriVariables ) ;
}
/ * *
* Defines a builder that adds headers to the request entity .
@ -439,13 +561,24 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -439,13 +561,24 @@ public class RequestEntity<T> extends HttpEntity<T> {
private final HttpMethod method ;
private final URI url ;
private final Function < UriTemplateHandler , URI > uriFunction ;
private final HttpHeaders headers = new HttpHeaders ( ) ;
public DefaultBodyBuilder ( HttpMethod method , URI url ) {
this . method = method ;
this . url = url ;
this . uriFunction = handler - > url ;
}
public DefaultBodyBuilder ( HttpMethod method , String uriTemplate , Object . . . uriVars ) {
this . method = method ;
this . uriFunction = handler - > handler . expand ( uriTemplate , uriVars ) ;
}
public DefaultBodyBuilder ( HttpMethod method , String uriTemplate , Map < String , ? > uriVars ) {
this . method = method ;
this . uriFunction = handler - > handler . expand ( uriTemplate , uriVars ) ;
}
@Override
@ -520,18 +653,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@@ -520,18 +653,17 @@ public class RequestEntity<T> extends HttpEntity<T> {
@Override
public RequestEntity < Void > build ( ) {
return new RequestEntity < > ( this . headers , this . method , this . url ) ;
return new RequestEntity < > ( null , this . headers , this . method , this . uriFunction , nul l ) ;
}
@Override
public < T > RequestEntity < T > body ( T body ) {
return new RequestEntity < > ( body , this . headers , this . method , this . url ) ;
return new RequestEntity < > ( body , this . headers , this . method , this . uriFunction , nul l ) ;
}
@Override
public < T > RequestEntity < T > body ( T body , Type type ) {
return new RequestEntity < > ( body , this . headers , this . method , this . url , type ) ;
return new RequestEntity < > ( body , this . headers , this . method , this . uriFunction , type ) ;
}
}
}