Skip to content

Commit 8eef97d

Browse files
committed
HTTP header adapters print header values
Issue: SPR-17546
1 parent 2700993 commit 8eef97d

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,26 @@ public int hashCode() {
16401640

16411641
@Override
16421642
public String toString() {
1643-
return this.headers.toString();
1643+
return formatHeaders(this.headers);
1644+
}
1645+
1646+
/**
1647+
* Helps to format HTTP header values, as HTTP header values themselves can
1648+
* contain comma-separated values, can become confusing with regular
1649+
* {@link Map} formatting that also uses commas between entries.
1650+
* @param headers the headers to format
1651+
* @return the headers to a String
1652+
* @since 5.1.4
1653+
*/
1654+
public static String formatHeaders(MultiValueMap<String, String> headers) {
1655+
return headers.entrySet().stream()
1656+
.map(entry -> {
1657+
List<String> values = entry.getValue();
1658+
return entry.getKey() + ":" + (values.size() == 1 ?
1659+
"\"" + values.get(0) + "\"" :
1660+
values.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")));
1661+
})
1662+
.collect(Collectors.joining(", ", "[", "]"));
16441663
}
16451664

16461665

spring-web/src/main/java/org/springframework/http/server/reactive/JettyHeadersAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.eclipse.jetty.http.HttpField;
3030
import org.eclipse.jetty.http.HttpFields;
3131

32+
import org.springframework.http.HttpHeaders;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.MultiValueMap;
3435

@@ -176,6 +177,12 @@ public int size() {
176177
}
177178

178179

180+
@Override
181+
public String toString() {
182+
return HttpHeaders.formatHeaders(this);
183+
}
184+
185+
179186
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
180187

181188
private Enumeration<String> names = headers.getFieldNames();

spring-web/src/main/java/org/springframework/http/server/reactive/NettyHeadersAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ public int size() {
176176
}
177177

178178

179+
@Override
180+
public String toString() {
181+
return org.springframework.http.HttpHeaders.formatHeaders(this);
182+
}
183+
184+
179185
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
180186

181187
private Iterator<String> names = headers.names().iterator();

spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHeadersAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.tomcat.util.buf.MessageBytes;
3232
import org.apache.tomcat.util.http.MimeHeaders;
3333

34+
import org.springframework.http.HttpHeaders;
3435
import org.springframework.lang.Nullable;
3536
import org.springframework.util.MultiValueMap;
3637

@@ -195,6 +196,12 @@ public int size() {
195196
}
196197

197198

199+
@Override
200+
public String toString() {
201+
return HttpHeaders.formatHeaders(this);
202+
}
203+
204+
198205
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
199206

200207
private Enumeration<String> names = headers.names();

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHeadersAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ public int size() {
177177
}
178178

179179

180+
@Override
181+
public String toString() {
182+
return org.springframework.http.HttpHeaders.formatHeaders(this);
183+
}
184+
185+
180186
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
181187

182188
private Iterator<HttpString> names = headers.getHeaderNames().iterator();

spring-webflux/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Configuration status="WARN">
33
<Appenders>
44
<Console name="Console" target="SYSTEM_OUT">
5-
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.} - %msg%n" />
66
</Console>
77
</Appenders>
88
<Loggers>

0 commit comments

Comments
 (0)