2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.restcore.util;
23 import java.io.UnsupportedEncodingException;
25 import java.util.HashMap;
26 import java.util.LinkedHashSet;
28 import java.util.Map.Entry;
29 import java.util.Optional;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.MultivaluedMap;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
39 import org.onap.aai.schema.enums.PropertyMetadata;
40 import org.springframework.web.util.UriUtils;
42 public class URITools {
44 protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
46 public static MultivaluedMap<String, String> getQueryMap(URI uri) {
47 MultivaluedMap<String, String> result = new MultivaluedHashMap<>();
48 String queryParams = uri.getRawQuery();
49 if (queryParams != null) {
50 String[] sections = queryParams.split("&");
51 String[] query = null;
52 String key, value = "";
53 for (String section : sections) {
54 query = section.split("=");
55 key = UriUtils.decode(query[0], "UTF-8");
56 if (query[1] != null) {
57 query[1] = query[1].replaceAll("\\+", "%20");
59 value = UriUtils.decode(query[1], "UTF-8");
60 if (result.containsKey(key)) {
61 result.add(key, value);
63 result.putSingle(key, value);
71 public static Optional<String> replaceTemplates(Introspector obj, String uriString, PropertyMetadata metadata,
72 boolean replaceWithWildcard) throws AAIMissingRequiredPropertyException {
73 String result = uriString;
74 final Map<String, String> propMap = URITools.findProperties(obj, uriString, metadata, replaceWithWildcard);
75 if (propMap.isEmpty()) {
76 return Optional.empty();
78 for (Entry<String, String> entry : propMap.entrySet()) {
79 result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
81 // drop out wildcards if they exist
82 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
83 return Optional.of(result);
86 private static Map<String, String> findProperties(Introspector obj, String uriString, PropertyMetadata metadata,
87 boolean replaceWithWildcard) throws AAIMissingRequiredPropertyException {
89 final Map<String, String> result = new HashMap<>();
90 final Set<String> missing = new LinkedHashSet<>();
91 Matcher m = template.matcher(uriString);
94 String propName = m.group(1);
95 String value = obj.getValue(propName);
98 result.put(propName, value);
100 if (replaceWithWildcard) {
101 result.put(propName, "*");
103 missing.add(propName);
107 if (!missing.isEmpty() && (properties != missing.size())) {
108 throw new AAIMissingRequiredPropertyException(
109 "Cannot complete " + metadata.toString() + " uri. Missing properties " + missing);