Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / GraphInventoryPatchConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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
21 package org.onap.so.client.graphinventory;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26 import org.onap.so.client.aai.AAICommonObjectMapperPatchProvider;
27 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
28 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException;
29 import org.onap.so.jsonpath.JsonPathUtil;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31
32 public class GraphInventoryPatchConverter {
33
34     private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider();
35     private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider();
36     private static final Pattern LOCATE_COMPLEX_OBJECT =
37             Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$");
38
39
40     public String convertPatchFormat(Object obj) {
41         return validatePatchObject(marshallObjectToPatchFormat(obj));
42     }
43
44     public String validatePatchObject(String payload) {
45         if (hasComplexObject(payload)) {
46             throw new GraphInventoryPatchDepthExceededException(payload);
47         }
48
49         return payload;
50     }
51
52     /**
53      * validates client side that json does not include any complex objects relationship-list is omitted from this
54      * validation
55      */
56     protected boolean hasComplexObject(String json) {
57         if (json.isEmpty()) {
58             return false;
59         }
60         String complex = "$.*.*";
61         String array = "$.*.*.*";
62         List<String> result = JsonPathUtil.getInstance().getPathList(json, complex);
63         List<String> result2 = JsonPathUtil.getInstance().getPathList(json, array);
64
65         result.addAll(result2);
66         return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find());
67     }
68
69     protected String marshallObjectToPatchFormat(Object obj) {
70         Object value = obj;
71         try {
72             if (!(obj instanceof Map || obj instanceof String)) {
73                 value = patchProvider.getMapper().writeValueAsString(obj);
74             } else if (obj instanceof Map) {
75                 value = standardProvider.getMapper().writeValueAsString(obj);
76             }
77         } catch (JsonProcessingException e) {
78             value = "{}";
79         }
80
81         return (String) value;
82     }
83 }