提交 69c16f38 编写于 作者: A Arjen Poutsma

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
上级 b487ed67
/*
* 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<V> values = this.map.get(key);
if (values == null) {
values = new LinkedList<>();
this.map.put(key, values);
}
List<V> values = this.map.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(K key, List<V> values) {
List<V> currentValues = this.map.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}
@Override
public V getFirst(K key) {
List<V> values = this.map.get(key);
......
/*
* 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<K, V> implements MultiValueMap<K, V>, Serializa
@Override
public void add(K key, V value) {
List<V> values = this.targetMap.get(key);
if (values == null) {
values = new LinkedList<>();
this.targetMap.put(key, values);
}
List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
values.add(value);
}
@Override
public void addAll(K key, List<V> values) {
List<V> currentValues = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
currentValues.addAll(values);
}
@Override
public V getFirst(K key) {
List<V> values = this.targetMap.get(key);
......
/*
* 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<K, V> extends Map<K, List<V>> {
*/
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<V> values);
/**
* Set the given single value under the given key.
* @param key the key
......
/*
* 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<String> expected = new ArrayList<>(2);
expected.add("value1");
expected.add("value2");
expected.add("value3");
assertEquals(expected, map.get("key"));
}
@Test
public void getFirst() {
List<String> values = new ArrayList<>(2);
......
/*
* 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<String, String>, Serializable
*/
@Override
public void add(String headerName, String headerValue) {
List<String> headerValues = headers.get(headerName);
if (headerValues == null) {
headerValues = new LinkedList<>();
this.headers.put(headerName, headerValues);
}
List<String> headerValues = headers.computeIfAbsent(headerName, k -> new LinkedList<>());
headerValues.add(headerValue);
}
@Override
public void addAll(String headerName, List<String> headerValues) {
List<String> 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
......
/*
* 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<String, String>, Serializable
*/
@Override
public void add(String headerName, String headerValue) {
List<String> headerValues = this.headers.get(headerName);
if (headerValues == null) {
headerValues = new LinkedList<>();
this.headers.put(headerName, headerValues);
}
List<String> headerValues =
this.headers.computeIfAbsent(headerName, k -> new LinkedList<>());
headerValues.add(headerValue);
}
@Override
public void addAll(String key, List<String> values) {
List<String> 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
......
/*
* 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<String, List<String>> entry : request.getHeaders().entrySet()) {
delegate.getHeaders().addAll(entry.getKey(), entry.getValue());
}
if (body.length > 0) {
StreamUtils.copy(body, delegate.getBody());
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册