Skip to content

Commit e281480

Browse files
mbhavesnicoll
andcommitted
Switch project structure to use the new generator
This commit removes the former `ProjectGenerator` api based on mustache template in favour of a new DSL infrastructure to be detailed in further commits. Event handling is now web-specific with a `ProjectRequest` and a `WebProjectRequest` that gathers the base input from the request and some additional web-specific metadata, respectively. As a consequence the `initializr-actuator` module has now a dependency on the `initializr-web` module. See gh-340 Co-authored-by: Stephane Nicoll <[email protected]>
1 parent 0628829 commit e281480

File tree

129 files changed

+1153
-8245
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1153
-8245
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
.factorypath
1414
*.iml
1515
bin
16-
build
1716
target
1817
.springBeans
1918
.vscode/

initializr-actuator/pom.xml

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
<groupId>io.spring.initializr</groupId>
2020
<artifactId>initializr-generator</artifactId>
2121
</dependency>
22-
22+
<dependency>
23+
<groupId>io.spring.initializr</groupId>
24+
<artifactId>initializr-web</artifactId>
25+
</dependency>
2326
<dependency>
2427
<groupId>org.springframework.boot</groupId>
2528
<artifactId>spring-boot-autoconfigure</artifactId>
@@ -75,11 +78,6 @@
7578
<type>test-jar</type>
7679
<scope>test</scope>
7780
</dependency>
78-
<dependency>
79-
<groupId>io.spring.initializr</groupId>
80-
<artifactId>initializr-web</artifactId>
81-
<scope>test</scope>
82-
</dependency>
8381
<dependency>
8482
<groupId>io.spring.initializr</groupId>
8583
<artifactId>initializr-web</artifactId>

initializr-actuator/src/main/java/io/spring/initializr/actuate/stat/ProjectGenerationStatPublisher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.fasterxml.jackson.core.JsonProcessingException;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import io.spring.initializr.actuate.stat.StatsProperties.Elastic;
26-
import io.spring.initializr.generator.ProjectRequestEvent;
26+
import io.spring.initializr.web.project.ProjectRequestEvent;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

initializr-actuator/src/main/java/io/spring/initializr/actuate/stat/ProjectRequestDocumentFactory.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,12 +24,13 @@
2424
import io.spring.initializr.actuate.stat.ProjectRequestDocument.DependencyInformation;
2525
import io.spring.initializr.actuate.stat.ProjectRequestDocument.ErrorStateInformation;
2626
import io.spring.initializr.actuate.stat.ProjectRequestDocument.VersionInformation;
27-
import io.spring.initializr.generator.ProjectFailedEvent;
28-
import io.spring.initializr.generator.ProjectRequest;
29-
import io.spring.initializr.generator.ProjectRequestEvent;
3027
import io.spring.initializr.metadata.InitializrMetadata;
3128
import io.spring.initializr.util.Agent;
3229
import io.spring.initializr.util.Version;
30+
import io.spring.initializr.web.project.ProjectFailedEvent;
31+
import io.spring.initializr.web.project.ProjectRequest;
32+
import io.spring.initializr.web.project.ProjectRequestEvent;
33+
import io.spring.initializr.web.project.WebProjectRequest;
3334

3435
import org.springframework.util.StringUtils;
3536

@@ -43,7 +44,6 @@ public class ProjectRequestDocumentFactory {
4344
public ProjectRequestDocument createDocument(ProjectRequestEvent event) {
4445
InitializrMetadata metadata = event.getMetadata();
4546
ProjectRequest request = event.getProjectRequest();
46-
4747
ProjectRequestDocument document = new ProjectRequestDocument();
4848
document.setGenerationTimestamp(event.getTimestamp());
4949
document.setGroupId(request.getGroupId());
@@ -118,30 +118,33 @@ private VersionInformation determineVersionInformation(ProjectRequest request) {
118118
}
119119

120120
private ClientInformation determineClientInformation(ProjectRequest request) {
121-
Agent agent = determineAgent(request);
122-
String ip = determineIp(request);
123-
String country = determineCountry(request);
124-
if (agent != null || ip != null || country != null) {
125-
return new ClientInformation(agent, ip, country);
121+
if (request instanceof WebProjectRequest) {
122+
WebProjectRequest webProjectRequest = (WebProjectRequest) request;
123+
Agent agent = determineAgent(webProjectRequest);
124+
String ip = determineIp(webProjectRequest);
125+
String country = determineCountry(webProjectRequest);
126+
if (agent != null || ip != null || country != null) {
127+
return new ClientInformation(agent, ip, country);
128+
}
126129
}
127130
return null;
128131
}
129132

130-
private Agent determineAgent(ProjectRequest request) {
133+
private Agent determineAgent(WebProjectRequest request) {
131134
String userAgent = (String) request.getParameters().get("user-agent");
132135
if (StringUtils.hasText(userAgent)) {
133136
return Agent.fromUserAgent(userAgent);
134137
}
135138
return null;
136139
}
137140

138-
private String determineIp(ProjectRequest request) {
141+
private String determineIp(WebProjectRequest request) {
139142
String candidate = (String) request.getParameters().get("cf-connecting-ip");
140143
return (StringUtils.hasText(candidate)) ? candidate
141144
: (String) request.getParameters().get("x-forwarded-for");
142145
}
143146

144-
private String determineCountry(ProjectRequest request) {
147+
private String determineCountry(WebProjectRequest request) {
145148
String candidate = (String) request.getParameters().get("cf-ipcountry");
146149
if (StringUtils.hasText(candidate) && !"xx".equalsIgnoreCase(candidate)) {
147150
return candidate;

initializr-actuator/src/test/java/io/spring/initializr/actuate/stat/AbstractInitializrStatTests.java

-50
This file was deleted.

initializr-actuator/src/test/java/io/spring/initializr/actuate/stat/ProjectGenerationStatPublisherTests.java

+30-15
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import java.util.UUID;
2727

2828
import io.spring.initializr.actuate.stat.StatsProperties.Elastic;
29-
import io.spring.initializr.generator.ProjectGeneratedEvent;
30-
import io.spring.initializr.generator.ProjectRequest;
29+
import io.spring.initializr.metadata.InitializrMetadata;
30+
import io.spring.initializr.test.metadata.InitializrMetadataTestBuilder;
31+
import io.spring.initializr.web.project.ProjectGeneratedEvent;
32+
import io.spring.initializr.web.project.ProjectRequest;
33+
import io.spring.initializr.web.project.WebProjectRequest;
3134
import org.json.JSONException;
3235
import org.junit.jupiter.api.BeforeEach;
3336
import org.junit.jupiter.api.Test;
@@ -59,7 +62,13 @@
5962
*
6063
* @author Stephane Nicoll
6164
*/
62-
class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
65+
class ProjectGenerationStatPublisherTests {
66+
67+
private final InitializrMetadata metadata = InitializrMetadataTestBuilder
68+
.withDefaults().addDependencyGroup("core", "security", "validation", "aop")
69+
.addDependencyGroup("web", "web", "data-rest", "jersey")
70+
.addDependencyGroup("data", "data-jpa", "jdbc")
71+
.addDependencyGroup("database", "h2", "mysql").build();
6372

6473
private RetryTemplate retryTemplate;
6574

@@ -132,13 +141,13 @@ private void testAuthorization(String expectedUri,
132141
.andRespond(withStatus(HttpStatus.CREATED)
133142
.body(mockResponse(UUID.randomUUID().toString(), true))
134143
.contentType(MediaType.APPLICATION_JSON));
135-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
144+
handleEvent(request);
136145
this.mockServer.verify();
137146
}
138147

139148
@Test
140149
void publishDocument() {
141-
ProjectRequest request = createProjectRequest();
150+
WebProjectRequest request = createProjectRequest();
142151
request.setGroupId("com.example.acme");
143152
request.setArtifactId("project");
144153
request.setType("maven-project");
@@ -156,7 +165,7 @@ void publishDocument() {
156165
.body(mockResponse(UUID.randomUUID().toString(), true))
157166
.contentType(MediaType.APPLICATION_JSON));
158167

159-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
168+
handleEvent(request);
160169
this.mockServer.verify();
161170
}
162171

@@ -177,7 +186,7 @@ void publishDocumentWithNoClientInformation() {
177186
.body(mockResponse(UUID.randomUUID().toString(), true))
178187
.contentType(MediaType.APPLICATION_JSON));
179188

180-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
189+
handleEvent(request);
181190
this.mockServer.verify();
182191
}
183192

@@ -198,7 +207,7 @@ void publishDocumentWithInvalidType() {
198207
.body(mockResponse(UUID.randomUUID().toString(), true))
199208
.contentType(MediaType.APPLICATION_JSON));
200209

201-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
210+
handleEvent(request);
202211
this.mockServer.verify();
203212
}
204213

@@ -219,7 +228,7 @@ void publishDocumentWithInvalidLanguage() {
219228
.body(mockResponse(UUID.randomUUID().toString(), true))
220229
.contentType(MediaType.APPLICATION_JSON));
221230

222-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
231+
handleEvent(request);
223232
this.mockServer.verify();
224233
}
225234

@@ -241,7 +250,7 @@ void publishDocumentWithInvalidJavaVersion() {
241250
.body(mockResponse(UUID.randomUUID().toString(), true))
242251
.contentType(MediaType.APPLICATION_JSON));
243252

244-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
253+
handleEvent(request);
245254
this.mockServer.verify();
246255
}
247256

@@ -262,7 +271,7 @@ void publishDocumentWithInvalidDependencies() {
262271
.body(mockResponse(UUID.randomUUID().toString(), true))
263272
.contentType(MediaType.APPLICATION_JSON));
264273

265-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
274+
handleEvent(request);
266275
this.mockServer.verify();
267276
}
268277

@@ -284,7 +293,7 @@ void recoverFromError() {
284293
.body(mockResponse(UUID.randomUUID().toString(), true))
285294
.contentType(MediaType.APPLICATION_JSON));
286295

287-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
296+
handleEvent(request);
288297
this.mockServer.verify();
289298
}
290299

@@ -302,12 +311,18 @@ void fatalErrorOnlyLogs() {
302311
.andExpect(method(HttpMethod.POST))
303312
.andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
304313

305-
this.statPublisher.handleEvent(createProjectGeneratedEvent(request));
314+
handleEvent(request);
306315
this.mockServer.verify();
307316
}
308317

309-
private ProjectGeneratedEvent createProjectGeneratedEvent(ProjectRequest request) {
310-
return new ProjectGeneratedEvent(request, getMetadata());
318+
private WebProjectRequest createProjectRequest() {
319+
WebProjectRequest request = new WebProjectRequest();
320+
request.initialize(this.metadata);
321+
return request;
322+
}
323+
324+
private void handleEvent(ProjectRequest request) {
325+
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request, this.metadata));
311326
}
312327

313328
private static String mockResponse(String id, boolean created) {

0 commit comments

Comments
 (0)