Skip to content

Commit 6079e92

Browse files
authored
fixes #16 (#17)
use correct URL to access project importer service simplify exception handling format code
1 parent cb45f89 commit 6079e92

1 file changed

Lines changed: 90 additions & 97 deletions

File tree

Lines changed: 90 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package org.eclipse.opensmartclide.architecturalpatterns.application;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import org.eclipse.opensmartclide.architecturalpatterns.service.ArchitecturalPatternsJsonHandler;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
6-
7-
import java.lang.IllegalArgumentException;
87
import org.springframework.beans.factory.annotation.Value;
98
import org.springframework.http.HttpEntity;
109
import org.springframework.http.HttpHeaders;
@@ -16,105 +15,99 @@
1615
import org.springframework.web.bind.annotation.RequestHeader;
1716
import org.springframework.web.bind.annotation.RequestParam;
1817
import org.springframework.web.bind.annotation.RestController;
19-
import org.springframework.web.client.RestClientException;
2018
import org.springframework.web.client.RestTemplate;
2119
import org.springframework.web.server.ResponseStatusException;
2220
import org.springframework.web.util.UriComponentsBuilder;
23-
import com.fasterxml.jackson.databind.JsonNode;
2421

2522
@RestController
2623
public class PatternApplicationController {
27-
28-
@Value("${IMPORT_PROJECT_URL}")
29-
private String importProjectURL;
30-
private static final Logger logger = LoggerFactory.getLogger(PatternApplicationController.class);
31-
32-
private final ArchitecturalPatternsJsonHandler projectJsonHandler;
33-
34-
public PatternApplicationController(final ArchitecturalPatternsJsonHandler jsonHandler) {
35-
this.projectJsonHandler = jsonHandler;
36-
37-
}
38-
39-
@PostMapping(value = "/application")
40-
public String applyPattern(@RequestParam("framework") String framework, @RequestParam("pattern") String pattern,
41-
@Nullable @RequestParam("name") String projName, @Nullable @RequestParam("visibility") String visibility,
42-
@RequestHeader String gitLabServerURL, @RequestHeader String gitlabToken) {
43-
44-
try {
45-
46-
String repoUrl = getProjectURL(framework, pattern);
47-
48-
if (repoUrl == null) {
49-
throw new NullPointerException("Repository URL is not found.");
50-
}
51-
String response = createProject(repoUrl, projName, visibility, gitLabServerURL, gitlabToken);
52-
logger.info("Pattern application succeeded!");
53-
54-
return response;
55-
56-
} catch (IllegalArgumentException e) {
57-
logger.error("Exception during pattern application.", e);
58-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
59-
"Problem with either project template resource or input parameters: " + e.getMessage());
60-
}
61-
}
62-
63-
public String getProjectURL(String framework, String pattern) {
64-
final JsonNode projUrlsJsonNode = projectJsonHandler.getProjectUrlsNode();
65-
66-
JsonNode frameworkNode = projUrlsJsonNode.get(framework);
67-
68-
if (frameworkNode == null) {
69-
throw new IllegalArgumentException(
70-
"Invalid framework received: " + framework + " is not a valid framework.");
71-
}
72-
73-
JsonNode patternNode = frameworkNode.get(pattern);
74-
75-
if (frameworkNode.get(pattern) == null) {
76-
throw new IllegalArgumentException("Invalid pattern received: " + pattern + " is not a valid pattern.");
77-
}
78-
79-
return patternNode.asText();
80-
}
81-
82-
public String createProject(String repoUrl, String projName, String visibility, String gitLabServerURL,
83-
String gitlabToken) {
84-
85-
// Creating URL with parameters
86-
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
87-
parameters.add("repoUrl", repoUrl);
88-
89-
if (projName != null) {
90-
parameters.add("name", projName);
91-
}
92-
93-
if (visibility != null) {
94-
parameters.add("visibility", visibility);
95-
}
96-
97-
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(importProjectURL)
98-
.queryParams(parameters);
99-
String url = uriComponentsBuilder.build().encode().toUriString();
100-
101-
// Setting headers
102-
HttpHeaders headers = new HttpHeaders();
103-
headers.set("gitLabServerURL", gitLabServerURL);
104-
headers.set("gitlabToken", gitlabToken);
105-
106-
// Creating POST request query
107-
HttpEntity<String> request = new HttpEntity<>(url, headers);
108-
109-
try {
110-
111-
// Make POST request to external project importer
112-
RestTemplate restTemplate = new RestTemplate();
113-
return restTemplate.postForObject(importProjectURL, request, String.class);
114-
115-
} catch (RestClientException e) {
116-
logger.error("Problem encountered while sending POST request: " + url);
117-
throw new IllegalStateException(e.getMessage());
118-
}
119-
}
24+
private static final Logger logger = LoggerFactory.getLogger(PatternApplicationController.class);
25+
26+
@Value("${IMPORT_PROJECT_URL}")
27+
private String importProjectURL;
28+
29+
private final RestTemplate restTemplate = new RestTemplate();
30+
31+
private final ArchitecturalPatternsJsonHandler projectJsonHandler;
32+
33+
public PatternApplicationController(final ArchitecturalPatternsJsonHandler jsonHandler) {
34+
this.projectJsonHandler = jsonHandler;
35+
}
36+
37+
@PostMapping(value = "/application")
38+
public String applyPattern(@RequestParam("framework") String framework,
39+
@RequestParam("pattern") String pattern,
40+
@Nullable @RequestParam("name") String projName,
41+
@Nullable @RequestParam("visibility") String visibility,
42+
@RequestHeader String gitLabServerURL,
43+
@RequestHeader String gitlabToken) {
44+
try {
45+
String repoUrl = getProjectURL(framework, pattern);
46+
if (repoUrl == null) {
47+
throw new NullPointerException("Repository URL is not found.");
48+
}
49+
50+
String response = createProject(repoUrl, projName, visibility, gitLabServerURL, gitlabToken);
51+
logger.info("Pattern application succeeded!");
52+
return response;
53+
} catch (Exception e) {
54+
logger.error("Exception during pattern application.", e);
55+
throw new ResponseStatusException(
56+
HttpStatus.BAD_REQUEST,
57+
"Problem with either project template resource or input parameters: " + e.getMessage()
58+
);
59+
}
60+
}
61+
62+
public String getProjectURL(String framework, String pattern) {
63+
final JsonNode projUrlsJsonNode = projectJsonHandler.getProjectUrlsNode();
64+
65+
JsonNode frameworkNode = projUrlsJsonNode.get(framework);
66+
if (frameworkNode == null) {
67+
throw new IllegalArgumentException("Invalid framework received: " + framework + " is not a valid framework.");
68+
}
69+
70+
JsonNode patternNode = frameworkNode.get(pattern);
71+
if (frameworkNode.get(pattern) == null) {
72+
throw new IllegalArgumentException("Invalid pattern received: " + pattern + " is not a valid pattern.");
73+
}
74+
75+
return patternNode.asText();
76+
}
77+
78+
public String createProject(String repoUrl,
79+
String projName,
80+
String visibility,
81+
String gitLabServerURL,
82+
String gitlabToken) {
83+
// Creating URL with parameters
84+
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
85+
parameters.add("repoUrl", repoUrl);
86+
87+
if (projName != null) {
88+
parameters.add("name", projName);
89+
}
90+
91+
if (visibility != null) {
92+
parameters.add("visibility", visibility);
93+
}
94+
95+
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(importProjectURL).queryParams(parameters);
96+
String url = uriComponentsBuilder.build().encode().toUriString();
97+
98+
if (!url.startsWith(importProjectURL)) {
99+
throw new IllegalStateException("Not a valid url: " + url);
100+
}
101+
102+
// Setting headers
103+
HttpHeaders headers = new HttpHeaders();
104+
headers.set("gitLabServerURL", gitLabServerURL);
105+
headers.set("gitlabToken", gitlabToken);
106+
107+
// Creating POST request query
108+
HttpEntity<String> request = new HttpEntity<>(url, headers);
109+
110+
// Make POST request to external project importer
111+
return restTemplate.postForObject(url, request, String.class);
112+
}
120113
}

0 commit comments

Comments
 (0)