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