|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.docker.compose.service.connection.postgres; |
| 18 | + |
| 19 | +import java.util.StringTokenizer; |
| 20 | + |
| 21 | +import org.springframework.util.LinkedMultiValueMap; |
| 22 | +import org.springframework.util.MultiValueMap; |
| 23 | + |
| 24 | +/** |
| 25 | + * Utility class to customize Postgres JDBC URL. |
| 26 | + * |
| 27 | + * @author Dmytro Nosan |
| 28 | + */ |
| 29 | +class PostgresJdbcUrl { |
| 30 | + |
| 31 | + private final String url; |
| 32 | + |
| 33 | + private final MultiValueMap<String, String> parameters; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates new {@link PostgresJdbcUrl} instance. |
| 37 | + * @param jdbcUrl the JDBC URL |
| 38 | + */ |
| 39 | + PostgresJdbcUrl(String jdbcUrl) { |
| 40 | + this.url = getUrl(jdbcUrl); |
| 41 | + this.parameters = getParameters(jdbcUrl); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Adds value to the JDBC URL if a given name does not exist. |
| 46 | + * @param name the JDBC parameter name |
| 47 | + * @param value the JDBC parameter value |
| 48 | + */ |
| 49 | + void addParameterIfDoesNotExist(String name, String value) { |
| 50 | + if (this.parameters.containsKey(name)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + this.parameters.add(name, value); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Build a JDBC URL. |
| 58 | + * @return a new JDBC URL |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + StringBuilder jdbcUrlBuilder = new StringBuilder(this.url); |
| 63 | + if (this.parameters.isEmpty()) { |
| 64 | + return jdbcUrlBuilder.toString(); |
| 65 | + } |
| 66 | + jdbcUrlBuilder.append('?'); |
| 67 | + this.parameters.forEach((name, values) -> values.forEach((value) -> { |
| 68 | + jdbcUrlBuilder.append(name); |
| 69 | + if (value != null) { |
| 70 | + jdbcUrlBuilder.append('=').append(value); |
| 71 | + } |
| 72 | + jdbcUrlBuilder.append('&'); |
| 73 | + })); |
| 74 | + jdbcUrlBuilder.deleteCharAt(jdbcUrlBuilder.length() - 1); |
| 75 | + return jdbcUrlBuilder.toString(); |
| 76 | + } |
| 77 | + |
| 78 | + private static String getUrl(String jdbcUrl) { |
| 79 | + int index = jdbcUrl.indexOf('?'); |
| 80 | + return (index != -1) ? jdbcUrl.substring(0, index) : jdbcUrl; |
| 81 | + } |
| 82 | + |
| 83 | + private static MultiValueMap<String, String> getParameters(String jdbcUrl) { |
| 84 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
| 85 | + int index = jdbcUrl.indexOf('?'); |
| 86 | + if (index == -1) { |
| 87 | + return parameters; |
| 88 | + } |
| 89 | + StringTokenizer tokenizer = new StringTokenizer(jdbcUrl.substring(index + 1), "&"); |
| 90 | + while (tokenizer.hasMoreTokens()) { |
| 91 | + String token = tokenizer.nextToken(); |
| 92 | + int pos = token.indexOf('='); |
| 93 | + if (pos == -1) { |
| 94 | + parameters.add(token, null); |
| 95 | + } |
| 96 | + else { |
| 97 | + parameters.add(token.substring(0, pos), token.substring(pos + 1)); |
| 98 | + } |
| 99 | + } |
| 100 | + return parameters; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments