|
1 | 1 | package org.eclipse.opensmartclide.architecturalpatterns.application; |
2 | 2 |
|
3 | | -import org.springframework.http.MediaType; |
4 | | -import org.springframework.web.bind.annotation.GetMapping; |
| 3 | +import org.eclipse.opensmartclide.architecturalpatterns.service.ArchitecturalPatternsJsonHandler; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | + |
| 7 | +import java.lang.IllegalArgumentException; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.http.HttpEntity; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.lang.Nullable; |
| 13 | +import org.springframework.util.LinkedMultiValueMap; |
| 14 | +import org.springframework.util.MultiValueMap; |
| 15 | +import org.springframework.web.bind.annotation.PostMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 17 | +import org.springframework.web.bind.annotation.RequestParam; |
5 | 18 | import org.springframework.web.bind.annotation.RestController; |
| 19 | +import org.springframework.web.client.RestClientException; |
| 20 | +import org.springframework.web.client.RestTemplate; |
| 21 | +import org.springframework.web.server.ResponseStatusException; |
| 22 | +import org.springframework.web.util.UriComponentsBuilder; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
6 | 24 |
|
7 | 25 | @RestController |
8 | 26 | public class PatternApplicationController { |
9 | | - @GetMapping(value = "/application", produces = MediaType.APPLICATION_JSON_VALUE) |
10 | | - public String setupPattern() { |
11 | | - // TODO |
12 | | - return "TODO: Architectural Pattern is being set up."; |
| 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 | + } |
13 | 119 | } |
14 | 120 | } |
0 commit comments