Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / util / URITools.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.restcore.util;
22
23 import java.net.URI;
24 import java.util.HashMap;
25 import java.util.LinkedHashSet;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Optional;
29 import java.util.Set;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import javax.ws.rs.core.MultivaluedHashMap;
34 import javax.ws.rs.core.MultivaluedMap;
35
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
38 import org.onap.aai.schema.enums.PropertyMetadata;
39 import org.springframework.web.util.UriUtils;
40
41 public class URITools {
42
43     protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
44
45     public static MultivaluedMap<String, String> getQueryMap(URI uri) {
46         MultivaluedMap<String, String> result = new MultivaluedHashMap<>();
47         String queryParams = uri.getRawQuery();
48         if (queryParams != null) {
49             String[] sections = queryParams.split("&");
50             String[] query = null;
51             String key, value = "";
52             for (String section : sections) {
53                 query = section.split("=");
54                 key = UriUtils.decode(query[0], "UTF-8");
55                 if (query[1] != null) {
56                     query[1] = query[1].replaceAll("\\+", "%20");
57                 }
58                 value = UriUtils.decode(query[1], "UTF-8");
59                 if (result.containsKey(key)) {
60                     result.add(key, value);
61                 } else {
62                     result.putSingle(key, value);
63                 }
64             }
65         }
66         return result;
67
68     }
69
70     public static Optional<String> replaceTemplates(Introspector obj, String uriString, PropertyMetadata metadata,
71             boolean replaceWithWildcard) throws AAIMissingRequiredPropertyException {
72         String result = uriString;
73         final Map<String, String> propMap = URITools.findProperties(obj, uriString, metadata, replaceWithWildcard);
74         if (propMap.isEmpty()) {
75             return Optional.empty();
76         }
77         for (Entry<String, String> entry : propMap.entrySet()) {
78             result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
79         }
80         // drop out wildcards if they exist
81         result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
82         return Optional.of(result);
83     }
84
85     private static Map<String, String> findProperties(Introspector obj, String uriString, PropertyMetadata metadata,
86             boolean replaceWithWildcard) throws AAIMissingRequiredPropertyException {
87
88         final Map<String, String> result = new HashMap<>();
89         final Set<String> missing = new LinkedHashSet<>();
90         Matcher m = template.matcher(uriString);
91         int properties = 0;
92         while (m.find()) {
93             String propName = m.group(1);
94             String value = obj.getValue(propName);
95             properties++;
96             if (value != null) {
97                 result.put(propName, value);
98             } else {
99                 if (replaceWithWildcard) {
100                     result.put(propName, "*");
101                 }
102                 missing.add(propName);
103             }
104         }
105
106         if (!missing.isEmpty() && (properties != missing.size())) {
107             throw new AAIMissingRequiredPropertyException(
108                     "Cannot complete " + metadata.toString() + " uri. Missing properties " + missing);
109         }
110         return result;
111     }
112
113 }