48
48
import org .springframework .util .MultiValueMap ;
49
49
import org .springframework .util .StringUtils ;
50
50
51
-
52
51
/**
53
52
* Represents HTTP request and response headers, mapping string header names to a list of string values.
54
53
*
@@ -472,8 +471,8 @@ public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
472
471
* {@link #getAcceptLanguageAsLocales()} or if you need to filter based on
473
472
* a list of supported locales you can pass the returned list to
474
473
* {@link Locale#filter(List, Collection)}.
475
- * @since 5.0
476
474
* @throws IllegalArgumentException if the value cannot be converted to a language range
475
+ * @since 5.0
477
476
*/
478
477
public List <Locale .LanguageRange > getAcceptLanguage () {
479
478
String value = getFirst (ACCEPT_LANGUAGE );
@@ -494,8 +493,8 @@ public void setAcceptLanguageAsLocales(List<Locale> locales) {
494
493
* A variant of {@link #getAcceptLanguage()} that converts each
495
494
* {@link java.util.Locale.LanguageRange} to a {@link Locale}.
496
495
* @return the locales or an empty list
497
- * @since 5.0
498
496
* @throws IllegalArgumentException if the value cannot be converted to a locale
497
+ * @since 5.0
499
498
*/
500
499
public List <Locale > getAcceptLanguageAsLocales () {
501
500
List <Locale .LanguageRange > ranges = getAcceptLanguage ();
@@ -711,6 +710,15 @@ public Set<HttpMethod> getAllow() {
711
710
}
712
711
}
713
712
713
+ /**
714
+ * Set a configured {@link CacheControl} instance as the
715
+ * new value of the {@code Cache-Control} header.
716
+ * @since 5.0.5
717
+ */
718
+ public void setCacheControl (CacheControl cacheControl ) {
719
+ set (CACHE_CONTROL , cacheControl .getHeaderValue ());
720
+ }
721
+
714
722
/**
715
723
* Set the (new) value of the {@code Cache-Control} header.
716
724
*/
@@ -907,6 +915,15 @@ public String getETag() {
907
915
return getFirst (ETAG );
908
916
}
909
917
918
+ /**
919
+ * Set the duration after which the message is no longer valid,
920
+ * as specified by the {@code Expires} header.
921
+ * @since 5.0.5
922
+ */
923
+ public void setExpires (ZonedDateTime expires ) {
924
+ setZonedDateTime (EXPIRES , expires );
925
+ }
926
+
910
927
/**
911
928
* Set the date and time at which the message is no longer valid,
912
929
* as specified by the {@code Expires} header.
@@ -932,13 +949,16 @@ public long getExpires() {
932
949
* Set the (new) value of the {@code Host} header.
933
950
* <p>If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0},
934
951
* the host header will only contain the
935
- * {@linkplain InetSocketAddress#getHostString() hostname }.
952
+ * {@linkplain InetSocketAddress#getHostString() host name }.
936
953
* @since 5.0
937
954
*/
938
955
public void setHost (@ Nullable InetSocketAddress host ) {
939
956
if (host != null ) {
940
- String value = (host .getPort () != 0 ?
941
- String .format ("%s:%d" , host .getHostString (), host .getPort ()) : host .getHostString ());
957
+ String value = host .getHostString ();
958
+ int port = host .getPort ();
959
+ if (port != 0 ) {
960
+ value = value + ":" + port ;
961
+ }
942
962
set (HOST , value );
943
963
}
944
964
else {
@@ -958,28 +978,25 @@ public InetSocketAddress getHost() {
958
978
if (value == null ) {
959
979
return null ;
960
980
}
961
- final int idx ;
962
- if (value .startsWith ("[" )) {
963
- idx = value .indexOf (':' , value .indexOf (']' ));
964
- } else {
965
- idx = value .lastIndexOf (':' );
966
- }
967
- String hostname = null ;
981
+
982
+ String host = null ;
968
983
int port = 0 ;
969
- if (idx != -1 && idx < value .length () - 1 ) {
970
- hostname = value .substring (0 , idx );
971
- String portString = value .substring (idx + 1 );
984
+ int separator = (value .startsWith ("[" ) ? value .indexOf (':' , value .indexOf (']' )) : value .lastIndexOf (':' ));
985
+ if (separator != -1 ) {
986
+ host = value .substring (0 , separator );
987
+ String portString = value .substring (separator + 1 );
972
988
try {
973
989
port = Integer .parseInt (portString );
974
990
}
975
991
catch (NumberFormatException ex ) {
976
- // ignored
992
+ // ignore
977
993
}
978
994
}
979
- if (hostname == null ) {
980
- hostname = value ;
995
+
996
+ if (host == null ) {
997
+ host = value ;
981
998
}
982
- return InetSocketAddress .createUnresolved (hostname , port );
999
+ return InetSocketAddress .createUnresolved (host , port );
983
1000
}
984
1001
985
1002
/**
@@ -1192,23 +1209,23 @@ public List<String> getVary() {
1192
1209
* Set the given date under the given header name after formatting it as a string
1193
1210
* using the RFC-1123 date-time formatter. The equivalent of
1194
1211
* {@link #set(String, String)} but for date headers.
1195
- * @since 3.2.4
1196
- * @see #setZonedDateTime(String, ZonedDateTime)
1212
+ * @since 5.0
1197
1213
*/
1198
- public void setDate (String headerName , long date ) {
1199
- Instant instant = Instant .ofEpochMilli (date );
1200
- ZonedDateTime zonedDateTime = ZonedDateTime .ofInstant (instant , GMT );
1201
- set (headerName , DATE_FORMATTERS [0 ].format (zonedDateTime ));
1214
+ public void setZonedDateTime (String headerName , ZonedDateTime date ) {
1215
+ set (headerName , DATE_FORMATTERS [0 ].format (date ));
1202
1216
}
1203
1217
1204
1218
/**
1205
1219
* Set the given date under the given header name after formatting it as a string
1206
1220
* using the RFC-1123 date-time formatter. The equivalent of
1207
1221
* {@link #set(String, String)} but for date headers.
1208
- * @since 5.0
1222
+ * @since 3.2.4
1223
+ * @see #setZonedDateTime(String, ZonedDateTime)
1209
1224
*/
1210
- public void setZonedDateTime (String headerName , ZonedDateTime date ) {
1211
- set (headerName , DATE_FORMATTERS [0 ].format (date ));
1225
+ public void setDate (String headerName , long date ) {
1226
+ Instant instant = Instant .ofEpochMilli (date );
1227
+ ZonedDateTime zonedDateTime = ZonedDateTime .ofInstant (instant , GMT );
1228
+ set (headerName , DATE_FORMATTERS [0 ].format (zonedDateTime ));
1212
1229
}
1213
1230
1214
1231
/**
@@ -1549,7 +1566,7 @@ public String toString() {
1549
1566
* Return a {@code HttpHeaders} object that can only be read, not written to.
1550
1567
*/
1551
1568
public static HttpHeaders readOnlyHttpHeaders (HttpHeaders headers ) {
1552
- return headers .readOnly ? headers : new HttpHeaders (headers , true );
1569
+ return ( headers .readOnly ? headers : new HttpHeaders (headers , true ) );
1553
1570
}
1554
1571
1555
1572
}
0 commit comments