Commit 918201c2 by Johannes Edmeier

Remove deprecations and use RestTemplateBuilder

parent 97335237
......@@ -3,7 +3,7 @@ package de.codecentric.boot.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import de.codecentric.boot.admin.config.EnableAdminServer;
......
......@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
......@@ -36,7 +37,6 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.util.StringUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
......@@ -81,6 +81,9 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
@Autowired
private ResourcePatternResolver resourcePatternResolver;
@Autowired
private RestTemplateBuilder builder;
@Bean
@ConditionalOnMissingBean
public AdminServerProperties adminServerProperties() {
......@@ -178,15 +181,15 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
@Bean
@ConditionalOnMissingBean
public StatusUpdater statusUpdater() {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
template.setErrorHandler(new DefaultResponseErrorHandler() {
builder.messageConverters(new MappingJackson2HttpMessageConverter())
.errorHandler(new DefaultResponseErrorHandler() {
@Override
protected boolean hasError(HttpStatus statusCode) {
return false;
}
});
StatusUpdater statusUpdater = new StatusUpdater(template, applicationStore);
StatusUpdater statusUpdater = new StatusUpdater(builder.build(), applicationStore);
statusUpdater.setStatusLifetime(adminServerProperties().getMonitor().getStatusLifetime());
return statusUpdater;
}
......
......@@ -30,12 +30,11 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
......@@ -51,7 +50,7 @@ import de.codecentric.boot.admin.model.Application;
* @author Dennis Schulte
*/
public class AdminApplicationHazelcastTest {
private RestTemplate template = new TestRestTemplate();
private TestRestTemplate template = new TestRestTemplate();
private EmbeddedWebApplicationContext instance1;
private EmbeddedWebApplicationContext instance2;
......
......@@ -24,9 +24,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -42,8 +42,8 @@ import de.codecentric.boot.admin.model.Application;
* @author Dennis Schulte
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestAdminApplication.class)
@WebIntegrationTest({ "server.port=0", "spring.cloud.config.enabled=false" })
@SpringBootTest(classes = TestAdminApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.cloud.config.enabled=false" })
public class AdminApplicationTest {
@Value("${local.server.port}")
......
......@@ -30,7 +30,8 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration.RestTemplateConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -131,6 +132,7 @@ public class AdminServerWebConfigurationTest {
applicationContext.register(config);
}
applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
applicationContext.register(RestTemplateConfiguration.class);
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(HazelcastAutoConfiguration.class);
applicationContext.register(HazelcastStoreConfiguration.class);
......
......@@ -8,6 +8,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration.RestTemplateConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
......@@ -77,6 +78,7 @@ public class DiscoveryClientConfigurationTest {
}
applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
applicationContext.register(RestTemplateConfiguration.class);
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(AdminServerWebConfiguration.class);
applicationContext.register(DiscoveryClientConfiguration.class);
......
......@@ -28,7 +28,7 @@ import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
......@@ -158,7 +158,7 @@ public class NotifierConfigurationTest {
}
private static class TestNotifier implements Notifier {
private List<ClientApplicationEvent> events = new ArrayList<ClientApplicationEvent>();
private List<ClientApplicationEvent> events = new ArrayList<>();
@Override
public void notify(ClientApplicationEvent event) {
......
......@@ -56,6 +56,7 @@ public class StatusUpdaterTest {
}
@Test
@SuppressWarnings("rawtypes")
public void test_update_statusChanged() {
when(template.getForEntity("health", Map.class)).thenReturn(
ResponseEntity.ok().body((Map) Collections.singletonMap("status", "UP")));
......@@ -70,6 +71,7 @@ public class StatusUpdaterTest {
}
@Test
@SuppressWarnings("rawtypes")
public void test_update_statusUnchanged() {
when(template.getForEntity("health", Map.class))
.thenReturn(ResponseEntity.ok((Map) Collections.singletonMap("status", "UNKNOWN")));
......@@ -82,6 +84,7 @@ public class StatusUpdaterTest {
}
@Test
@SuppressWarnings("rawtypes")
public void test_update_noBody() {
// HTTP 200 - UP
when(template.getForEntity("health", Map.class)).thenReturn(ResponseEntity.ok((Map) null));
......@@ -113,6 +116,7 @@ public class StatusUpdaterTest {
}
@Test
@SuppressWarnings("rawtypes")
public void test_updateStatusForApplications() {
Application app1 = Application.create("foo").withId("id-1").withHealthUrl("health-1")
.build();
......
......@@ -15,21 +15,17 @@
*/
package de.codecentric.boot.admin.config;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.services.ApplicationRegistrator;
import de.codecentric.boot.admin.services.RegistrationApplicationListener;
import de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor;
@Configuration
@EnableConfigurationProperties({ AdminProperties.class, AdminClientProperties.class })
......@@ -42,25 +38,20 @@ public class SpringBootAdminClientAutoConfiguration {
@Autowired
private AdminProperties admin;
@Autowired
private RestTemplateBuilder builder;
/**
* Task that registers the application at the spring-boot-admin application.
*/
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator() {
return new ApplicationRegistrator(createRestTemplate(admin), admin, client);
}
protected RestTemplate createRestTemplate(AdminProperties admin) {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
builder.messageConverters(new MappingJackson2HttpMessageConverter());
if (admin.getUsername() != null) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(
new BasicAuthHttpRequestInterceptor(admin.getUsername(), admin.getPassword())));
builder.basicAuthorization(admin.getUsername(), admin.getPassword());
}
return template;
return new ApplicationRegistrator(builder.build(), admin, client);
}
/**
......
package de.codecentric.boot.admin.web;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import com.fasterxml.jackson.core.Base64Variants;
public class BasicAuthHttpRequestInterceptor implements ClientHttpRequestInterceptor {
private final String encodedAuth;
public BasicAuthHttpRequestInterceptor(String username, String password) {
String auth = username + ":" + password;
encodedAuth = "Basic "
+ Base64Variants.MIME_NO_LINEFEEDS.encode(auth.getBytes(StandardCharsets.US_ASCII));
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().add("Authorization", encodedAuth);
return execution.execute(request, body);
}
}
......@@ -15,7 +15,7 @@ import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class AdminClientPropertiesTest {
......
......@@ -6,7 +6,8 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration.RestTemplateConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import de.codecentric.boot.admin.services.ApplicationRegistrator;
......@@ -44,6 +45,7 @@ public class SpringBootAdminClientAutoConfigurationTest {
private void load(String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(RestTemplateConfiguration.class);
applicationContext.register(ManagementServerPropertiesAutoConfiguration.class);
applicationContext.register(SpringBootAdminClientAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
......
package de.codecentric.boot.admin.web;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.mock.http.client.MockClientHttpRequest;
public class BasicAuthHttpRequestInterceptorTest {
@Test
public void test() throws IOException {
BasicAuthHttpRequestInterceptor interceptor = new BasicAuthHttpRequestInterceptor("admin",
"secret");
HttpRequest request = new MockClientHttpRequest();
interceptor.intercept(request, (byte[]) null, new ClientHttpRequestExecution() {
@Override
public ClientHttpResponse execute(HttpRequest paramHttpRequest, byte[] paramArrayOfByte)
throws IOException {
return null;
}
});
assertEquals(Collections.singletonList("Basic YWRtaW46c2VjcmV0"),
request.getHeaders().get("Authorization"));
}
}
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