2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.nbi.commons;
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;
24 import org.springframework.util.LinkedMultiValueMap;
25 import org.springframework.util.MultiValueMap;
30 public class QueryParserUtils {
32 private QueryParserUtils() {}
36 * @param queryParameters
39 public static Set<String> getFields(MultiValueMap<String, String> queryParameters) {
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);
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);
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));
65 public static MultiValueMap<String, String> popCriteria(MultiValueMap<String, String> queryParameters) {
67 Set<Entry<String, List<String>>> entrySet = queryParameters.entrySet();
69 MultiValueMap<String, String> criterias = new LinkedMultiValueMap<>();
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);