github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/ServerConfiguration.mustache (about) 1 package {{invokerPackage}}; 2 3 import java.util.Map; 4 5 /** 6 * Representing a Server configuration. 7 */ 8 public class ServerConfiguration { 9 public String URL; 10 public String description; 11 public Map<String, ServerVariable> variables; 12 13 /** 14 * @param URL A URL to the target host. 15 * @param description A describtion of the host designated by the URL. 16 * @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template. 17 */ 18 public ServerConfiguration(String URL, String description, Map<String, ServerVariable> variables) { 19 this.URL = URL; 20 this.description = description; 21 this.variables = variables; 22 } 23 24 /** 25 * Format URL template using given variables. 26 * 27 * @param variables A map between a variable name and its value. 28 * @return Formatted URL. 29 */ 30 public String URL(Map<String, String> variables) { 31 String url = this.URL; 32 33 // go through variables and replace placeholders 34 for (Map.Entry<String, ServerVariable> variable: this.variables.entrySet()) { 35 String name = variable.getKey(); 36 ServerVariable serverVariable = variable.getValue(); 37 String value = serverVariable.defaultValue; 38 39 if (variables != null && variables.containsKey(name)) { 40 value = variables.get(name); 41 if (serverVariable.enumValues.size() > 0 && !serverVariable.enumValues.contains(value)) { 42 throw new RuntimeException("The variable " + name + " in the server URL has invalid value " + value + "."); 43 } 44 } 45 url = url.replaceAll("\\{" + name + "\\}", value); 46 } 47 return url; 48 } 49 50 /** 51 * Format URL template using default server variables. 52 * 53 * @return Formatted URL. 54 */ 55 public String URL() { 56 return URL(null); 57 } 58 }