Fix property names for stream binder headers
Spring Cloud Stream changed the names of the headers config that we need to propagate headers in kafka (and other binders that do not support headers natively). Fixes gh-284
This commit is contained in:
@@ -63,7 +63,8 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor
|
||||
for (Resource resource : resolver
|
||||
.getResources("classpath*:META-INF/spring.binders")) {
|
||||
for (String binderType : parseBinderConfigurations(resource)) {
|
||||
addHeaders(map, binderType);
|
||||
int startIndex = findStartIndex(environment, binderType);
|
||||
addHeaders(map, binderType, startIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,6 +81,15 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor
|
||||
addOrReplace(environment.getPropertySources(), map);
|
||||
}
|
||||
|
||||
private int findStartIndex(ConfigurableEnvironment environment, String binder) {
|
||||
String prefix = "spring.cloud.stream." + binder + ".binder.headers";
|
||||
int i = 0;
|
||||
while (environment.getProperty(prefix + "[" + i + "]")!=null) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
private Collection<String> parseBinderConfigurations(Resource resource) {
|
||||
Collection<String> keys = new HashSet<>();
|
||||
try {
|
||||
@@ -115,10 +125,10 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor
|
||||
}
|
||||
}
|
||||
|
||||
private void addHeaders(Map<String, Object> map, String binder) {
|
||||
String stem = "spring.cloud.stream.binder." + binder + ".headers";
|
||||
private void addHeaders(Map<String, Object> map, String binder, int startIndex) {
|
||||
String stem = "spring.cloud.stream." + binder + ".binder.headers";
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
map.put(stem + "[" + i + "]", headers[i]);
|
||||
map.put(stem + "[" + (i + startIndex) + "]", headers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.sleuth.stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class StreamEnvironmentPostProcessorTests {
|
||||
|
||||
private StreamEnvironmentPostProcessor processor = new StreamEnvironmentPostProcessor();
|
||||
private ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
|
||||
@Test
|
||||
public void testHeadersAdded() {
|
||||
this.processor.postProcessEnvironment(this.environment,
|
||||
new SpringApplication(StreamEnvironmentPostProcessorTests.class));
|
||||
assertThat(this.environment.getProperty("spring.cloud.stream.test.binder.headers[0]"))
|
||||
.isEqualTo(Span.SPAN_ID_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headersAppendedIfNecessary() {
|
||||
EnvironmentTestUtils.addEnvironment(this.environment, "spring.cloud.stream.test.binder.headers[0]=X-Custom",
|
||||
"spring.cloud.stream.test.binder.headers[1]=X-Mine");
|
||||
this.processor.postProcessEnvironment(this.environment,
|
||||
new SpringApplication(StreamEnvironmentPostProcessorTests.class));
|
||||
assertThat(this.environment.getProperty("spring.cloud.stream.test.binder.headers[2]"))
|
||||
.isEqualTo(Span.SPAN_ID_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user