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