From 69c16f3821354c9b0732687825cb902375649c5a Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 30 Jan 2017 14:18:46 +0100 Subject: [PATCH] Add headers in InterceptingClientHttpRequest This commit *adds* the "intercepted" headers to the ClientHttpRequest, as opposed to replacing them, which is what happened before this commit. Issue: SPR-15166 --- .../springframework/util/CollectionUtils.java | 14 ++++++++------ .../util/LinkedMultiValueMap.java | 14 ++++++++------ .../springframework/util/MultiValueMap.java | 9 ++++++++- .../util/LinkedMultiValueMapTests.java | 18 ++++++++++++++++-- .../messaging/simp/stomp/StompHeaders.java | 14 ++++++++------ .../org/springframework/http/HttpHeaders.java | 15 +++++++++------ .../client/InterceptingClientHttpRequest.java | 7 +++++-- 7 files changed, 62 insertions(+), 29 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index c4256df2bf..7a81a368c6 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -406,14 +406,16 @@ public abstract class CollectionUtils { @Override public void add(K key, V value) { - List values = this.map.get(key); - if (values == null) { - values = new LinkedList<>(); - this.map.put(key, values); - } + List values = this.map.computeIfAbsent(key, k -> new LinkedList<>()); values.add(value); } + @Override + public void addAll(K key, List values) { + List currentValues = this.map.computeIfAbsent(key, k -> new LinkedList<>()); + currentValues.addAll(values); + } + @Override public V getFirst(K key) { List values = this.map.get(key); diff --git a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java index acbbcafceb..6df23bcf13 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -75,14 +75,16 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa @Override public void add(K key, V value) { - List values = this.targetMap.get(key); - if (values == null) { - values = new LinkedList<>(); - this.targetMap.put(key, values); - } + List values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>()); values.add(value); } + @Override + public void addAll(K key, List values) { + List currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>()); + currentValues.addAll(values); + } + @Override public V getFirst(K key) { List values = this.targetMap.get(key); diff --git a/spring-core/src/main/java/org/springframework/util/MultiValueMap.java b/spring-core/src/main/java/org/springframework/util/MultiValueMap.java index a526af78ab..e6527a1e70 100644 --- a/spring-core/src/main/java/org/springframework/util/MultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/MultiValueMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -41,6 +41,13 @@ public interface MultiValueMap extends Map> { */ void add(K key, V value); + /** + * Add all the values of the given list to the current list of values for the given key. + * @param key they key + * @param values the values to be added + */ + void addAll(K key, List values); + /** * Set the given single value under the given key. * @param key the key diff --git a/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java b/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java index 8758a38c2c..70a756174c 100644 --- a/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java +++ b/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -17,6 +17,7 @@ package org.springframework.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -25,7 +26,8 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; /** * @author Arjen Poutsma @@ -50,6 +52,18 @@ public class LinkedMultiValueMapTests { assertEquals(expected, map.get("key")); } + @Test + public void addAll() throws Exception { + map.add("key", "value1"); + map.addAll("key", Arrays.asList("value2", "value3")); + assertEquals(1, map.size()); + List expected = new ArrayList<>(2); + expected.add("value1"); + expected.add("value2"); + expected.add("value3"); + assertEquals(expected, map.get("key")); + } + @Test public void getFirst() { List values = new ArrayList<>(2); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java index 5530aae6c0..8f91741e73 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -397,14 +397,16 @@ public class StompHeaders implements MultiValueMap, Serializable */ @Override public void add(String headerName, String headerValue) { - List headerValues = headers.get(headerName); - if (headerValues == null) { - headerValues = new LinkedList<>(); - this.headers.put(headerName, headerValues); - } + List headerValues = headers.computeIfAbsent(headerName, k -> new LinkedList<>()); headerValues.add(headerValue); } + @Override + public void addAll(String headerName, List headerValues) { + List currentValues = headers.computeIfAbsent(headerName, k -> new LinkedList<>()); + currentValues.addAll(headerValues); + } + /** * Set the given, single header value under the given name. * @param headerName the header name diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 0cdf0e0d72..bf036eb4a5 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -1333,14 +1333,17 @@ public class HttpHeaders implements MultiValueMap, Serializable */ @Override public void add(String headerName, String headerValue) { - List headerValues = this.headers.get(headerName); - if (headerValues == null) { - headerValues = new LinkedList<>(); - this.headers.put(headerName, headerValues); - } + List headerValues = + this.headers.computeIfAbsent(headerName, k -> new LinkedList<>()); headerValues.add(headerValue); } + @Override + public void addAll(String key, List values) { + List currentValues = this.headers.computeIfAbsent(key, k -> new LinkedList<>()); + currentValues.addAll(values); + } + /** * Set the given, single header value under the given name. * @param headerName the header name diff --git a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java index 9a33b31a0c..abf78a6b9b 100644 --- a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.URI; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -86,7 +87,9 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest { } else { ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod()); - delegate.getHeaders().putAll(request.getHeaders()); + for (Map.Entry> entry : request.getHeaders().entrySet()) { + delegate.getHeaders().addAll(entry.getKey(), entry.getValue()); + } if (body.length > 0) { StreamUtils.copy(body, delegate.getBody()); } -- GitLab