|
| 1 | +/* |
| 2 | + * This program and the accompanying materials are made available under the terms of the |
| 3 | + * Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 4 | + * https://www.eclipse.org/legal/epl-v20.html |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: EPL-2.0 |
| 7 | + * |
| 8 | + * Copyright Contributors to the Zowe Project. |
| 9 | + */ |
| 10 | + |
| 11 | +package org.zowe.apiml.apicatalog.staticapi; |
| 12 | + |
| 13 | +import lombok.extern.slf4j.Slf4j; |
| 14 | +import org.springframework.beans.factory.annotation.Value; |
| 15 | +import org.springframework.stereotype.Service; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.FileAlreadyExistsException; |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +/** |
| 26 | + * Service to handle the creation of the static definition file. |
| 27 | + * Allows the generation and the override of the .yml. |
| 28 | + * Retrieves the static definition location and stores the file there. |
| 29 | + */ |
| 30 | +@Service |
| 31 | +@Slf4j |
| 32 | +public class StaticDefinitionGenerator { |
| 33 | + |
| 34 | + private AtomicReference<String> fileName = new AtomicReference<>(""); |
| 35 | + |
| 36 | + @Value("${apiml.discovery.userid:eureka}") |
| 37 | + private String eurekaUserid; |
| 38 | + |
| 39 | + @Value("${apiml.discovery.password:password}") |
| 40 | + private String eurekaPassword; |
| 41 | + |
| 42 | + @Value("${server.attls.enabled:false}") |
| 43 | + private boolean isAttlsEnabled; |
| 44 | + |
| 45 | + @Value("${apiml.discovery.staticApiDefinitionsDirectories:config/local/api-defs}") |
| 46 | + private String staticApiDefinitionsDirectories; |
| 47 | + |
| 48 | + public StaticAPIResponse generateFile(String file, String serviceId) throws IOException { |
| 49 | + if (!serviceIdIsValid(serviceId)) { |
| 50 | + log.debug("The service ID {} has not valid format", serviceId); |
| 51 | + return new StaticAPIResponse(400, "The service ID format is not valid."); |
| 52 | + } |
| 53 | + String location = retrieveStaticDefLocation(); |
| 54 | + file = formatFile(file); |
| 55 | + String absoluteFilePath = String.format("./%s/%s.yml", location, serviceId); |
| 56 | + fileName.set(absoluteFilePath); |
| 57 | + |
| 58 | + checkIfFileExists(absoluteFilePath); |
| 59 | + String message = "The static definition file has been created by the user! Its location is: %s"; |
| 60 | + return writeFileAndSendResponse(file, fileName, String.format(message, fileName)); |
| 61 | + } |
| 62 | + |
| 63 | + public StaticAPIResponse overrideFile(String file, String serviceId) throws IOException { |
| 64 | + if (!serviceIdIsValid(serviceId)) { |
| 65 | + log.debug("The service ID {} has not valid format", serviceId); |
| 66 | + return new StaticAPIResponse(400, "The service ID format is not valid."); |
| 67 | + } |
| 68 | + String location = retrieveStaticDefLocation(); |
| 69 | + file = formatFile(file); |
| 70 | + String absoluteFilePath = String.format("./%s/%s.yml", location, serviceId); |
| 71 | + fileName.set(absoluteFilePath); |
| 72 | + String message = "The static definition file %s has been overwritten by the user!"; |
| 73 | + return writeFileAndSendResponse(file, fileName, String.format(message, fileName)); |
| 74 | + } |
| 75 | + |
| 76 | + private String formatFile(String file) { |
| 77 | + file = file.replace("\\n", System.lineSeparator()); |
| 78 | + file = file.substring(1, file.length() - 1); |
| 79 | + return file; |
| 80 | + } |
| 81 | + |
| 82 | + private StaticAPIResponse writeFileAndSendResponse(String file, AtomicReference<String> fileName, String message) throws IOException { |
| 83 | + try (FileOutputStream fos = new FileOutputStream(fileName.get())) { |
| 84 | + fos.write(file.getBytes(StandardCharsets.UTF_8)); |
| 85 | + |
| 86 | + log.debug(message); |
| 87 | + return new StaticAPIResponse(201, message); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void checkIfFileExists(String location) throws FileAlreadyExistsException { |
| 92 | + File outFile = new File(location); |
| 93 | + if (outFile.exists()) { |
| 94 | + throw new FileAlreadyExistsException(String.format("The static definition file %s already exists!", location)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Retrieve the static definition location either from the System environments or configuration. If no property is set, |
| 100 | + * the default value is used (local environment). The static definition is stored inside the first defined directory. |
| 101 | + * @return the static definition location |
| 102 | + */ |
| 103 | + private String retrieveStaticDefLocation() { |
| 104 | + log.debug(String.format("The value of apiml.discovery.staticApiDefinitionsDirectories is: %s", staticApiDefinitionsDirectories)); |
| 105 | + String[] directories = staticApiDefinitionsDirectories.split(";"); |
| 106 | + return directories[0]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Validate the service ID |
| 111 | + * @param serviceId the service ID |
| 112 | + * @return true if valid |
| 113 | + */ |
| 114 | + private boolean serviceIdIsValid(String serviceId) { |
| 115 | + if (serviceId != null && !serviceId.isEmpty()) { |
| 116 | + Pattern p = Pattern.compile("^[A-Za-z][A-Za-z0-9-]*$"); |
| 117 | + if (p.matcher(serviceId).find() && serviceId.length() < 16) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | +} |
| 124 | + |
0 commit comments