Commit d665d083 by Johannes Edmeier

Extract servlet specific parts from DefaultApplicationFactory to ServletApplicationFactory

parent 98629238
......@@ -36,6 +36,10 @@
</dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
......
......@@ -34,15 +34,16 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
......
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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,
......@@ -21,10 +21,13 @@ import de.codecentric.boot.admin.client.registration.DefaultApplicationFactory;
import de.codecentric.boot.admin.client.registration.RegistrationApplicationListener;
import javax.servlet.ServletContext;
import de.codecentric.boot.admin.client.registration.ServletApplicationFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
......@@ -36,11 +39,27 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
@Configuration
@EnableConfigurationProperties({ClientProperties.class, InstanceProperties.class})
@Conditional(SpringBootAdminClientEnabledCondition.class)
public class SpringBootAdminClientAutoConfiguration {
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
public static class ServletConfiguration {
@Bean
@ConditionalOnMissingBean
public ApplicationFactory applicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
@Value("${endpoints.health.path:/${endpoints.health.id:health}}") String healthEndpointPath,
ServletContext servletContext) {
return new ServletApplicationFactory(instance, management, server, servletContext, healthEndpointPath);
}
}
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator(ClientProperties client,
......@@ -59,9 +78,8 @@ public class SpringBootAdminClientAutoConfiguration {
public ApplicationFactory applicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
@Value("${endpoints.health.path:/${endpoints.health.id:health}}") String healthEndpointPath,
ServletContext servletContext) {
return new DefaultApplicationFactory(instance, management, server, servletContext, healthEndpointPath);
@Value("${endpoints.health.path:/${endpoints.health.id:health}}") String healthEndpointPath) {
return new DefaultApplicationFactory(instance, management, server, healthEndpointPath);
}
@Bean
......
......@@ -21,8 +21,6 @@ import de.codecentric.boot.admin.client.config.InstanceProperties;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
......@@ -45,18 +43,15 @@ public class DefaultApplicationFactory implements ApplicationFactory {
private ManagementServerProperties management;
private Integer localServerPort;
private Integer localManagementPort;
private ServletContext servletContext;
private String healthEndpointPath;
public DefaultApplicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
ServletContext servletContext,
String healthEndpointPath) {
this.instance = instance;
this.management = management;
this.server = server;
this.servletContext = servletContext;
this.healthEndpointPath = healthEndpointPath;
}
......@@ -94,7 +89,11 @@ public class DefaultApplicationFactory implements ApplicationFactory {
.port(getLocalServerPort());
}
return builder.path("/").path(servletContext.getContextPath()).path("/").toUriString();
return builder.path("/").path(getServerContextPath()).path("/").toUriString();
}
protected String getServerContextPath() {
return "";
}
protected String getManagementUrl() {
......@@ -108,9 +107,7 @@ public class DefaultApplicationFactory implements ApplicationFactory {
if (!StringUtils.isEmpty(baseUrl)) {
builder = UriComponentsBuilder.fromUriString(baseUrl);
} else if (isManagementPortEqual()) {
builder = UriComponentsBuilder.fromHttpUrl(getServiceUrl())
.path("/")
.path(server.getServlet().getServletPrefix());
builder = UriComponentsBuilder.fromHttpUrl(getServiceUrl()).path("/").path(getDispatcherServletPrefix());
} else {
Ssl ssl = management.getSsl() != null ? management.getSsl() : server.getSsl();
builder = UriComponentsBuilder.newInstance()
......@@ -119,13 +116,21 @@ public class DefaultApplicationFactory implements ApplicationFactory {
.port(getLocalManagementPort());
}
return builder.path("/").path(management.getContextPath()).path("/").toUriString();
return builder.path("/").path(getManagementContextPath()).path("/").toUriString();
}
protected String getDispatcherServletPrefix() {
return "";
}
protected boolean isManagementPortEqual() {
return getLocalManagementPort() == null || getLocalManagementPort().equals(getLocalServerPort());
}
protected String getManagementContextPath() {
return management.getContextPath();
}
protected String getHealthUrl() {
if (instance.getHealthUrl() != null) {
return instance.getHealthUrl();
......@@ -196,5 +201,4 @@ public class DefaultApplicationFactory implements ApplicationFactory {
.getProperty("local.management.port", Integer.class, localServerPort);
}
}
}
/*
* Copyright 2014-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.
* 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.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import javax.servlet.ServletContext;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
public class ServletApplicationFactory extends DefaultApplicationFactory {
private final ServletContext servletContext;
private final ServerProperties.Servlet servlet;
public ServletApplicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
ServletContext servletContext,
String healthEndpointPath) {
super(instance, management, server, healthEndpointPath);
this.servletContext = servletContext;
this.servlet = server.getServlet();
}
@Override
protected String getServerContextPath() {
return servletContext.getContextPath();
}
@Override
protected String getDispatcherServletPrefix() {
return servlet.getServletPrefix();
}
}
......@@ -13,12 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.registration;
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.ClientProperties;
import de.codecentric.boot.admin.client.registration.Application;
import de.codecentric.boot.admin.client.registration.ApplicationFactory;
import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;
import java.util.Collections;
import java.util.Map;
......
......@@ -14,9 +14,7 @@
* limitations under the License.
*/
package de.codecentric.boot.admin.registration;
import de.codecentric.boot.admin.client.registration.Application;
package de.codecentric.boot.admin.client.registration;
import java.io.IOException;
import org.junit.Test;
......
package de.codecentric.boot.admin.registration;
/*
* Copyright 2014-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.
* 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.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import de.codecentric.boot.admin.client.registration.Application;
import de.codecentric.boot.admin.client.registration.DefaultApplicationFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
......@@ -14,7 +28,6 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.server.Ssl;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -26,9 +39,8 @@ public class DefaultApplicationFactoryTest {
private InstanceProperties instanceProperties = new InstanceProperties();
private ServerProperties server = new ServerProperties();
private ManagementServerProperties management = new ManagementServerProperties();
private MockServletContext servletContext = new MockServletContext();
private DefaultApplicationFactory factory = new DefaultApplicationFactory(instanceProperties, management, server,
servletContext, "/health");
"/health");
@Before
public void setup() {
......@@ -39,7 +51,7 @@ public class DefaultApplicationFactoryTest {
public void test_mgmtPortPath() {
management.setContextPath("/admin");
DefaultApplicationFactory factory = new DefaultApplicationFactory(instanceProperties, management, server,
servletContext, "/alive");
"/alive");
publishApplicationReadyEvent(factory, 8080, 8081);
......@@ -50,53 +62,6 @@ public class DefaultApplicationFactoryTest {
}
@Test
public void test_contextPath_mgmtPath() {
servletContext.setContextPath("app");
management.setContextPath("/admin");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app/");
}
@Test
public void test_contextPath_mgmtPortPath() {
servletContext.setContextPath("app");
management.setContextPath("/admin");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app/");
}
@Test
public void test_contextPath() {
servletContext.setContextPath("app");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/app/application/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/app/application/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/app/");
}
@Test
public void test_servletPath() {
server.getServlet().setPath("app");
servletContext.setContextPath("srv");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/application/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/application/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/srv/");
}
@Test
public void test_default() {
publishApplicationReadyEvent(factory, 8080, null);
......@@ -178,11 +143,10 @@ public class DefaultApplicationFactoryTest {
public void test_all_baseUrls() {
instanceProperties.setManagementBaseUrl("http://management:8090");
instanceProperties.setServiceBaseUrl("http://service:80");
servletContext.setContextPath("/srv");
management.setContextPath("/admin");
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/srv/");
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
assertThat(app.getManagementUrl()).isEqualTo("http://management:8090/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://management:8090/admin/health/");
}
......@@ -190,14 +154,12 @@ public class DefaultApplicationFactoryTest {
@Test
public void test_service_baseUrl() {
instanceProperties.setServiceBaseUrl("http://service:80");
servletContext.setContextPath("/srv");
server.getServlet().setPath("/app");
management.setContextPath("/admin");
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/srv/");
assertThat(app.getManagementUrl()).isEqualTo("http://service:80/srv/app/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://service:80/srv/app/admin/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
assertThat(app.getManagementUrl()).isEqualTo("http://service:80/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://service:80/admin/health/");
}
@Test
......
package de.codecentric.boot.admin.registration;
import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;
import de.codecentric.boot.admin.client.registration.RegistrationApplicationListener;
/*
* Copyright 2014-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.
* 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.client.registration;
import java.util.concurrent.ScheduledFuture;
import org.junit.Test;
......
/*
* Copyright 2014-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.
* 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.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ServletApplicationFactoryTest {
private InstanceProperties instance = new InstanceProperties();
private ServerProperties server = new ServerProperties();
private ManagementServerProperties management = new ManagementServerProperties();
private MockServletContext servletContext = new MockServletContext();
private ServletApplicationFactory factory = new ServletApplicationFactory(instance, management, server,
servletContext, "/health");
@Before
public void setup() {
instance.setName("test");
}
@Test
public void test_contextPath_mgmtPath() {
servletContext.setContextPath("app");
management.setContextPath("/admin");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app/");
}
@Test
public void test_contextPath_mgmtPortPath() {
servletContext.setContextPath("app");
management.setContextPath("/admin");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app/");
}
@Test
public void test_contextPath() {
servletContext.setContextPath("app");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/app/application/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/app/application/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/app/");
}
@Test
public void test_servletPath() {
server.getServlet().setPath("app");
servletContext.setContextPath("srv");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/application/");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/application/health/");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/srv/");
}
private String getHostname() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
throw new IllegalStateException(e);
}
}
private void publishApplicationReadyEvent(DefaultApplicationFactory factory,
Integer serverport,
Integer managementport) {
MockEnvironment env = new MockEnvironment();
if (serverport != null) {
env.setProperty("local.server.port", serverport.toString());
}
if (managementport != null) {
env.setProperty("local.management.port", managementport.toString());
}
ConfigurableWebApplicationContext context = mock(ConfigurableWebApplicationContext.class);
when(context.getEnvironment()).thenReturn(env);
factory.onApplicationReady(new ApplicationReadyEvent(mock(SpringApplication.class), new String[]{}, context));
}
}
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