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