mirror of
https://gitee.com/dcren/initializr.git
synced 2025-11-28 09:22:41 +08:00
Polish contribution
Closes gh-349
This commit is contained in:
@@ -35,6 +35,12 @@
|
||||
<artifactId>spring-retry</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@@ -82,11 +88,6 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -23,7 +23,6 @@ import io.spring.initializr.actuate.stat.ProjectRequestDocumentFactory;
|
||||
import io.spring.initializr.actuate.stat.StatsProperties;
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -44,8 +43,11 @@ import org.springframework.retry.support.RetryTemplate;
|
||||
@ConditionalOnProperty("initializr.stats.elastic.uri")
|
||||
class InitializrStatsAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private StatsProperties statsProperties;
|
||||
private final StatsProperties statsProperties;
|
||||
|
||||
public InitializrStatsAutoConfiguration(StatsProperties statsProperties) {
|
||||
this.statsProperties = statsProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ProjectGenerationStatPublisher projectRequestStatHandler(
|
||||
|
||||
@@ -61,7 +61,9 @@ public class MetricsProperties {
|
||||
}
|
||||
|
||||
public String getId(String defaultValue) {
|
||||
if (StringUtils.hasText(id)) return id;
|
||||
if (StringUtils.hasText(id)) {
|
||||
return id;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.springframework.context.event.EventListener;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.retry.RetryCallback;
|
||||
import org.springframework.retry.RetryContext;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@@ -68,7 +67,7 @@ public class ProjectGenerationStatPublisher {
|
||||
try {
|
||||
ProjectRequestDocument document = documentFactory.createDocument(event);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Publishing $document");
|
||||
log.debug("Publishing " + document);
|
||||
}
|
||||
json = toJson(document);
|
||||
|
||||
@@ -76,12 +75,9 @@ public class ProjectGenerationStatPublisher {
|
||||
.post(this.statsProperties.getElastic().getEntityUrl())
|
||||
.contentType(MediaType.APPLICATION_JSON).body(json);
|
||||
|
||||
this.retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
|
||||
@Override
|
||||
public Void doWithRetry(RetryContext context) {
|
||||
restTemplate.exchange(request, String.class);
|
||||
return null;
|
||||
}
|
||||
this.retryTemplate.execute((RetryCallback<Void, RuntimeException>) context -> {
|
||||
restTemplate.exchange(request, String.class);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -95,8 +91,8 @@ public class ProjectGenerationStatPublisher {
|
||||
try {
|
||||
return this.objectMapper.writeValueAsString(stats);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Cannot convert to JSON", e);
|
||||
catch (JsonProcessingException ex) {
|
||||
throw new IllegalStateException("Cannot convert to JSON", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ProjectRequestDocumentFactory {
|
||||
|
||||
private static final Pattern IP_PATTERN = Pattern.compile("[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*");
|
||||
private static final Pattern IP_PATTERN =
|
||||
Pattern.compile("[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*");
|
||||
|
||||
private final InitializrMetadataProvider metadataProvider;
|
||||
|
||||
@@ -71,25 +72,29 @@ public class ProjectRequestDocumentFactory {
|
||||
document.setBootVersion(request.getBootVersion());
|
||||
|
||||
document.setJavaVersion(request.getJavaVersion());
|
||||
if (StringUtils.hasText(request.getJavaVersion()) && metadata.getJavaVersions().get(request.getJavaVersion()) == null) {
|
||||
if (StringUtils.hasText(request.getJavaVersion())
|
||||
&& metadata.getJavaVersions().get(request.getJavaVersion()) == null) {
|
||||
document.setInvalid(true);
|
||||
document.setInvalidJavaVersion(true);
|
||||
}
|
||||
|
||||
document.setLanguage(request.getLanguage());
|
||||
if (StringUtils.hasText(request.getLanguage()) && metadata.getLanguages().get(request.getLanguage()) == null) {
|
||||
if (StringUtils.hasText(request.getLanguage())
|
||||
&& metadata.getLanguages().get(request.getLanguage()) == null) {
|
||||
document.setInvalid(true);
|
||||
document.setInvalidLanguage(true);
|
||||
}
|
||||
|
||||
document.setPackaging(request.getPackaging());
|
||||
if (StringUtils.hasText(request.getPackaging()) && metadata.getPackagings().get(request.getPackaging()) == null) {
|
||||
if (StringUtils.hasText(request.getPackaging())
|
||||
&& metadata.getPackagings().get(request.getPackaging()) == null) {
|
||||
document.setInvalid(true);
|
||||
document.setInvalidPackaging(true);
|
||||
}
|
||||
|
||||
document.setType(request.getType());
|
||||
if (StringUtils.hasText(request.getType()) && metadata.getTypes().get(request.getType()) == null) {
|
||||
if (StringUtils.hasText(request.getType())
|
||||
&& metadata.getTypes().get(request.getType()) == null) {
|
||||
document.setInvalid(true);
|
||||
document.setInvalidType(true);
|
||||
}
|
||||
@@ -120,14 +125,15 @@ public class ProjectRequestDocumentFactory {
|
||||
return document;
|
||||
}
|
||||
|
||||
private static void handleCloudFlareHeaders(ProjectRequest request, ProjectRequestDocument document) {
|
||||
private static void handleCloudFlareHeaders(ProjectRequest request,
|
||||
ProjectRequestDocument document) {
|
||||
String candidate = (String) request.getParameters().get("cf-connecting-ip");
|
||||
if (StringUtils.hasText(candidate)) {
|
||||
document.setRequestIp(candidate);
|
||||
document.setRequestIpv4(extractIpv4(candidate));
|
||||
}
|
||||
String country = (String) request.getParameters().get("cf-ipcountry");
|
||||
if (StringUtils.hasText(country) && !country.toLowerCase().equals("xx")) {
|
||||
if (StringUtils.hasText(country) && !"xx".equalsIgnoreCase(country)) {
|
||||
document.setRequestCountry(country);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -30,6 +31,7 @@ import org.springframework.util.StringUtils;
|
||||
@ConfigurationProperties("initializr.stats")
|
||||
public class StatsProperties {
|
||||
|
||||
@NestedConfigurationProperty
|
||||
private final Elastic elastic = new Elastic();
|
||||
|
||||
public Elastic getElastic() {
|
||||
@@ -121,8 +123,8 @@ public class StatsProperties {
|
||||
try {
|
||||
return new URI(string);
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
throw new IllegalStateException("Cannot create entity URL: " + string, e);
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Cannot create entity URL: " + string, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "initializr.metrics",
|
||||
"type": "io.spring.initializr.actuate.autoconfigure.MetricsProperties",
|
||||
"sourceType": "io.spring.initializr.actuate.autoconfigure.MetricsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats",
|
||||
"type": "io.spring.initializr.actuate.stat.StatsProperties",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic",
|
||||
"type": "io.spring.initializr.actuate.stat.StatsProperties$Elastic",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties",
|
||||
"sourceMethod": "getElastic()"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "initializr.metrics.id",
|
||||
"type": "java.lang.String",
|
||||
"description": "Identifier for application in metrics keys. Keys will be exported in the form '[id].[hex].[name]' (where '[id]' is this value, '[hex]' is unique per application context, and '[name]' is the \"natural\" name for the metric.",
|
||||
"sourceType": "io.spring.initializr.actuate.autoconfigure.MetricsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.metrics.key",
|
||||
"type": "java.lang.String",
|
||||
"description": "Redis key holding index to metrics keys in data store.",
|
||||
"defaultValue": "keys.spring.metrics.collector",
|
||||
"sourceType": "io.spring.initializr.actuate.autoconfigure.MetricsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.metrics.prefix",
|
||||
"type": "java.lang.String",
|
||||
"description": "Prefix for redis keys holding metrics in data store.",
|
||||
"defaultValue": "spring.metrics.collector.",
|
||||
"sourceType": "io.spring.initializr.actuate.autoconfigure.MetricsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.metrics.rate-millis",
|
||||
"type": "java.lang.Long",
|
||||
"description": "The rate (in milliseconds) at which metrics are exported to Redis. If the value is <=0 then the export is disabled.",
|
||||
"defaultValue": 5000,
|
||||
"sourceType": "io.spring.initializr.actuate.autoconfigure.MetricsProperties"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.uri",
|
||||
"type": "java.lang.String",
|
||||
"description": "Elastic service uri.",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.username",
|
||||
"type": "java.lang.String",
|
||||
"description": "Elastic service username.",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.password",
|
||||
"type": "java.lang.String",
|
||||
"description": "Elastic service password.",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.index-name",
|
||||
"type": "java.lang.String",
|
||||
"description": "Name of the index.",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.entity-name",
|
||||
"type": "java.lang.String",
|
||||
"description": "Name of the entity to use to publish stats.",
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
},
|
||||
{
|
||||
"name": "initializr.stats.elastic.max-attempts",
|
||||
"type": "java.lang.Integer",
|
||||
"description": "Number of attempts before giving up.",
|
||||
"defaultValue": 3,
|
||||
"sourceType": "io.spring.initializr.actuate.stat.StatsProperties$Elastic"
|
||||
}
|
||||
],
|
||||
"hints": []
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class ActuatorIntegrationTests
|
||||
updatedResult.getInt("counter.initializr.java_version.1_8"));
|
||||
assertEquals("web dependency metric should have increased", webDependency + 1,
|
||||
updatedResult.getInt("counter.initializr.dependency.web"));
|
||||
assertEquals("jpa dependency metric should not have increased", jpaDependency + 0,
|
||||
assertEquals("jpa dependency metric should not have increased", jpaDependency,
|
||||
updatedResult.getInt("counter.initializr.dependency.data-jpa"));
|
||||
}
|
||||
|
||||
|
||||
@@ -67,9 +67,7 @@ public class MetricsExportTests {
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
repository = (RedisMetricRepository) writer;
|
||||
repository.findAll().forEach(it -> {
|
||||
repository.reset(it.getName());
|
||||
});
|
||||
repository.findAll().forEach(it -> repository.reset(it.getName()));
|
||||
assertTrue("Metrics not empty", repository.count() == 0);
|
||||
}
|
||||
|
||||
@@ -93,4 +91,5 @@ public class MetricsExportTests {
|
||||
return new SimpleInitializrMetadataProvider(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ public class ProjectGenerationMetricsListenerTests {
|
||||
private ProjectGenerationMetricsListener listener;
|
||||
private MetricsAssert metricsAssert;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
TestCounterService counterService = new TestCounterService();
|
||||
|
||||
@@ -90,11 +90,12 @@ public class MainControllerStatsIntegrationTests
|
||||
|
||||
String authorization = content.authorization;
|
||||
assertNotNull("Authorization header must be set", authorization);
|
||||
assertTrue("Wrong value for authorization header", authorization.startsWith("Basic "));
|
||||
assertTrue("Wrong value for authorization header",
|
||||
authorization.startsWith("Basic "));
|
||||
String token = authorization.substring("Basic ".length(), authorization.length());
|
||||
String[] data = new String(Base64Utils.decodeFromString(token)).split(":");
|
||||
assertEquals("Wrong user from $token", "test-user", data[0]);
|
||||
assertEquals("Wrong password $token", "test-password", data[1]);
|
||||
assertEquals("Wrong user from " + token, "test-user", data[0]);
|
||||
assertEquals("Wrong password " + token, "test-password", data[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -193,15 +194,14 @@ public class MainControllerStatsIntegrationTests
|
||||
|
||||
public static class Content {
|
||||
|
||||
public Content(String authorization, String body) {
|
||||
private final String authorization;
|
||||
private final String json;
|
||||
|
||||
Content(String authorization, String body) {
|
||||
this.authorization = authorization;
|
||||
json = body;
|
||||
}
|
||||
|
||||
private String authorization;
|
||||
|
||||
private String json;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||
*/
|
||||
public class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
|
||||
|
||||
private StatsProperties properties;
|
||||
private RetryTemplate retryTemplate;
|
||||
private ProjectGenerationStatPublisher statPublisher;
|
||||
private MockRestServiceServer mockServer;
|
||||
@@ -50,12 +49,14 @@ public class ProjectGenerationStatPublisherTests extends AbstractInitializrStatT
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.properties = createProperties();
|
||||
StatsProperties properties = createProperties();
|
||||
ProjectRequestDocumentFactory documentFactory =
|
||||
new ProjectRequestDocumentFactory(createProvider(getMetadata()));
|
||||
this.retryTemplate = new RetryTemplate();
|
||||
this.statPublisher = new ProjectGenerationStatPublisher(documentFactory, properties, retryTemplate);
|
||||
mockServer = MockRestServiceServer.createServer(this.statPublisher.getRestTemplate());
|
||||
this.statPublisher = new ProjectGenerationStatPublisher(documentFactory,
|
||||
properties, retryTemplate);
|
||||
mockServer = MockRestServiceServer.createServer(
|
||||
this.statPublisher.getRestTemplate());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -204,7 +204,8 @@ public class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTe
|
||||
@Test
|
||||
public void createDocumentWithProjectFailedEvent() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
ProjectFailedEvent event = new ProjectFailedEvent(request, new IllegalStateException("my test message"));
|
||||
ProjectFailedEvent event = new ProjectFailedEvent(request,
|
||||
new IllegalStateException("my test message"));
|
||||
ProjectRequestDocument document = factory.createDocument(event);
|
||||
assertTrue(document.isInvalid());
|
||||
assertEquals("my test message", document.getErrorMessage());
|
||||
|
||||
@@ -47,10 +47,9 @@ public class MetricsAssert {
|
||||
}
|
||||
|
||||
public MetricsAssert hasNoValue(String... metrics) {
|
||||
Arrays.asList(metrics).forEach(it -> {
|
||||
assertEquals("Metric '" + it + "' should not be registered", null,
|
||||
counterService.getValues().get(it));
|
||||
});
|
||||
Arrays.asList(metrics).forEach(it ->
|
||||
assertEquals("Metric '" + it + "' should not be registered", null,
|
||||
counterService.getValues().get(it)));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,8 +41,8 @@ public class RedisRunning extends TestWatcher {
|
||||
try {
|
||||
connectionFactory.getConnection();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assume.assumeNoException("Cannot connect to Redis (so skipping tests)", e);
|
||||
catch (Exception ex) {
|
||||
Assume.assumeNoException("Cannot connect to Redis (so skipping tests)", ex);
|
||||
}
|
||||
return super.apply(base, description);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user