2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.utils;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
26 import lombok.NoArgsConstructor;
27 import org.apache.logging.log4j.util.Strings;
28 import org.springframework.web.util.UriComponentsBuilder;
31 public class DmiServiceUrlBuilder {
33 private static final String FIXED_PATH_SEGMENT = null;
35 final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();
36 final Map<String, Object> pathSegments = new LinkedHashMap<>();
38 public static DmiServiceUrlBuilder newInstance() {
39 return new DmiServiceUrlBuilder();
43 * Add a fixed pathSegment to the URI.
45 * @param pathSegment the path segment
46 * @return this builder
48 public DmiServiceUrlBuilder pathSegment(final String pathSegment) {
49 pathSegments.put(pathSegment, FIXED_PATH_SEGMENT);
54 * Add a variable pathSegment to the URI.
55 * Do NOT add { } braces. the builder will take care of that
57 * @param pathSegment the name of the variable path segment (with { and }
58 * @param value the value to be insert in teh URI for the given variable path segment
59 * @return this builder
61 public DmiServiceUrlBuilder variablePathSegment(final String pathSegment, final Object value) {
62 pathSegments.put(pathSegment, value);
67 * Add a query parameter to the URI.
68 * Do NOT encode as the builder wil take care of encoding
70 * @param name the name of the variable
71 * @param value the value of the variable (only Strings are supported).
73 * @return this builder
75 public DmiServiceUrlBuilder queryParameter(final String name, final String value) {
76 if (Strings.isNotBlank(value)) {
77 uriComponentsBuilder.queryParam(name, value);
83 * Build the URI as a correctly percentage-encoded String.
85 * @param dmiServiceName the name of the dmi service
86 * @param dmiBasePath the base path of the dmi service
88 * @return URI as a string
90 public String build(final String dmiServiceName, final String dmiBasePath) {
92 .path("{dmiServiceName}")
93 .pathSegment("{dmiBasePath}")
96 final Map<String, Object> uriVariables = new HashMap<>();
97 uriVariables.put("dmiServiceName", dmiServiceName);
98 uriVariables.put("dmiBasePath", dmiBasePath);
100 pathSegments.forEach((pathSegment, variablePathValue) -> {
101 if (variablePathValue == FIXED_PATH_SEGMENT) {
102 uriComponentsBuilder.pathSegment(pathSegment);
104 uriComponentsBuilder.pathSegment("{" + pathSegment + "}");
105 uriVariables.put(pathSegment, variablePathValue);
108 return uriComponentsBuilder.buildAndExpand(uriVariables).encode().toUriString();