a43b5bbc0bbe5a13a84e889f4885ad57c587f384
[externalapi/nbi.git] / src / main / java / org / onap / nbi / commons / QueryParserUtils.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.commons;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import org.springframework.util.LinkedMultiValueMap;
25 import org.springframework.util.MultiValueMap;
26
27 /**
28  *
29  */
30 public class QueryParserUtils {
31
32     private QueryParserUtils() {}
33
34     /**
35      *
36      * @param queryParameters
37      * @return
38      */
39     public static Set<String> getFields(MultiValueMap<String, String> queryParameters) {
40
41         Set<String> fieldSet = new HashSet<>();
42         if (queryParameters != null) {
43             // search for "all" parameter
44             if (queryParameters.containsKey(ReservedKeys.ALL_FIELDS)) {
45                 queryParameters.remove(ReservedKeys.ALL_FIELDS);
46                 fieldSet.add(ReservedKeys.ALL_FIELDS);
47             }
48             // search for "fields" parameters
49             List<String> queryParameterField =
50                     queryParameters.remove(ReservedKeys.QUERY_KEY_FIELD_ESCAPE + ReservedKeys.QUERY_KEY_FIELD);
51             if (queryParameterField == null) {
52                 queryParameterField = queryParameters.remove(ReservedKeys.QUERY_KEY_FIELD);
53             }
54             if (queryParameterField != null && !queryParameterField.isEmpty()) {
55                 String queryParameterValue = queryParameterField.get(0);
56                 if (queryParameterValue != null) {
57                     String[] tokenArray = queryParameterValue.split(",");
58                     fieldSet.addAll(Arrays.asList(tokenArray));
59                 }
60             }
61         }
62         return fieldSet;
63     }
64
65     public static MultiValueMap<String, String> popCriteria(MultiValueMap<String, String> queryParameters) {
66
67         Set<Entry<String, List<String>>> entrySet = queryParameters.entrySet();
68
69         MultiValueMap<String, String> criterias = new LinkedMultiValueMap<>();
70
71         entrySet.stream().forEach(entry -> {
72             final List<String> tempValues = new ArrayList<>();
73             entry.getValue().stream().forEach(value -> tempValues.addAll(Arrays.asList(value.split(","))));
74             criterias.put(entry.getKey(), tempValues);
75         });
76
77         return criterias;
78     }
79
80 }