Commit 81168636 by Johannes Edmeier

Add CompositeHttpHeadersProvider as primary header

... to support multiple HttpHeadersProviders
parent 4a936c45
...@@ -34,9 +34,11 @@ import de.codecentric.boot.admin.server.services.endpoints.ChainingStrategy; ...@@ -34,9 +34,11 @@ import de.codecentric.boot.admin.server.services.endpoints.ChainingStrategy;
import de.codecentric.boot.admin.server.services.endpoints.ProbeEndpointsStrategy; import de.codecentric.boot.admin.server.services.endpoints.ProbeEndpointsStrategy;
import de.codecentric.boot.admin.server.services.endpoints.QueryIndexEndpointStrategy; import de.codecentric.boot.admin.server.services.endpoints.QueryIndexEndpointStrategy;
import de.codecentric.boot.admin.server.web.client.BasicAuthHttpHeaderProvider; import de.codecentric.boot.admin.server.web.client.BasicAuthHttpHeaderProvider;
import de.codecentric.boot.admin.server.web.client.CompositeHttpHeadersProvider;
import de.codecentric.boot.admin.server.web.client.HttpHeadersProvider; import de.codecentric.boot.admin.server.web.client.HttpHeadersProvider;
import de.codecentric.boot.admin.server.web.client.InstanceWebClient; import de.codecentric.boot.admin.server.web.client.InstanceWebClient;
import java.util.Collection;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
...@@ -44,6 +46,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties ...@@ -44,6 +46,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
@Configuration @Configuration
@ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class) @ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)
...@@ -70,8 +74,16 @@ public class AdminServerAutoConfiguration { ...@@ -70,8 +74,16 @@ public class AdminServerAutoConfiguration {
} }
@Bean @Bean
@Primary
@ConditionalOnMissingBean @ConditionalOnMissingBean
public HttpHeadersProvider httpHeadersProvider() { public CompositeHttpHeadersProvider httpHeadersProvider(Collection<HttpHeadersProvider> delegates) {
return new CompositeHttpHeadersProvider(delegates);
}
@Bean
@Order(0)
@ConditionalOnMissingBean
public BasicAuthHttpHeaderProvider basicAuthHttpHeadersProvider() {
return new BasicAuthHttpHeaderProvider(); return new BasicAuthHttpHeaderProvider();
} }
......
/*
* Copyright 2014-2018 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 de.codecentric.boot.admin.server.web.client;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import java.util.Collection;
import org.springframework.http.HttpHeaders;
public class CompositeHttpHeadersProvider implements HttpHeadersProvider {
private final Collection<HttpHeadersProvider> delegates;
public CompositeHttpHeadersProvider(Collection<HttpHeadersProvider> delegates) {
this.delegates = delegates;
}
@Override
public HttpHeaders getHeaders(Instance instance) {
HttpHeaders headers = new HttpHeaders();
delegates.forEach(delegate -> headers.addAll(delegate.getHeaders(instance)));
return headers;
}
}
/*
* Copyright 2014-2018 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 de.codecentric.boot.admin.server.web.client;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
public class CompositeHttpHeadersProviderTest {
@Test
public void should_return_all_headers() {
HttpHeadersProvider provider = new CompositeHttpHeadersProvider(asList(i -> {
HttpHeaders headers = new HttpHeaders();
headers.set("a", "1");
headers.set("b", "2-a");
return headers;
}, i -> {
HttpHeaders headers = new HttpHeaders();
headers.set("b", "2-b");
headers.set("c", "3");
return headers;
}));
HttpHeaders headers = provider.getHeaders(null);
assertThat(headers.get("a")).isEqualTo(singletonList("1"));
assertThat(headers.get("b")).isEqualTo(asList("2-a", "2-b"));
assertThat(headers.get("c")).isEqualTo(singletonList("3"));
}
@Test
public void should_return_empty_headers() {
HttpHeadersProvider provider = new CompositeHttpHeadersProvider(emptyList());
HttpHeaders headers = provider.getHeaders(null);
assertThat(headers.size()).isEqualTo(0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment