Adding SONAR fixes for
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / utils / PolicyApiUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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 package org.onap.policy.pdp.rest.api.utils;
21
22 import java.io.StringReader;
23
24 import javax.json.Json;
25 import javax.json.JsonArray;
26 import javax.json.JsonException;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
29 import javax.json.stream.JsonParsingException;
30
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.utils.PolicyUtils;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35
36 import com.google.common.base.CharMatcher;
37
38 public class PolicyApiUtils {
39     private static Logger LOGGER = FlexLogger.getLogger(PolicyApiUtils.class
40             .getName());
41     private static final String SUCCESS = "success";
42
43                 
44     public static Boolean validateNONASCIICharactersAndAllowSpaces(
45             String jsonString) {
46         Boolean isValidForm = false;
47         if (jsonString.isEmpty()) {
48             LOGGER.error("The Value is empty.");
49             return false;
50         } else {
51             if (CharMatcher.ASCII.matchesAllOf((CharSequence) jsonString)) {
52                 LOGGER.info("The Value does not contain ASCII Characters");
53                 isValidForm = true;
54             } else {
55                 LOGGER.error("The Value Contains Non ASCII Characters");
56                 isValidForm = false;
57             }
58         }
59         return isValidForm;
60     }
61     
62     public static String formatResponse(StringBuilder responseString){
63         
64         LOGGER.info("Formatting response message from Policy Validator");
65         String response = responseString.toString().replace("<br>", " | ");             
66                 response = response.replaceAll("(<b>|<\\/b>|<br>|<i>|<\\/i>|@#)", "");
67                                 
68         return response;
69     }
70     
71     public static boolean isNumeric(String str) {
72         for (char c : str.toCharArray()) {
73             if (!Character.isDigit(c))
74                 return false;
75         }
76         return true;
77     }
78
79     public static JsonObject stringToJsonObject(String value)
80             throws JsonException, JsonParsingException, IllegalStateException {
81         JsonReader jsonReader = Json.createReader(new StringReader(value));
82         JsonObject object = jsonReader.readObject();
83         jsonReader.close();
84         return object;
85     }
86     
87     public static String validateDictionaryJsonFields(JsonObject json, String dictionary) {
88         
89         LOGGER.info("Validating DictionaryJsonField values");
90         String message;
91         
92         if("Action".equals(dictionary.trim())){
93                 if(json.containsKey("attributeName")){
94                         if(json.getString("attributeName")==null || json.getString("attributeName").trim().isEmpty()){
95                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Attribute Name provided.";
96                                 return message;
97                         }
98                         if(!SUCCESS.equals(PolicyUtils.policySpecialCharValidator(json.getString("attributeName").trim()))){
99                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Attribute Name value.";
100                                 return message;
101                         }
102                 }else{
103                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing attributeName key in the dictionaryJson parameter.";
104                         return message;
105                 }
106                 if(json.containsKey("type")){
107                         if(json.getString("type")==null || json.getString("type").trim().isEmpty()){
108                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Type provided.";
109                                 return message;
110                         }
111                         if(!"REST".equals(json.getString("type").trim())){
112                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Type value.";
113                                 return message;
114                         }
115                 }else{
116                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing type key in the dictionaryJson parameter.";
117                         return message;
118                 }
119                 if(json.containsKey("method")){
120                         if(json.getString("method")==null || json.getString("method").trim().isEmpty()){
121                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Method provided.";
122                                 return message;
123                         }
124                         if("GET".equals(json.getString("method").trim()) 
125                                         || "PUT".equals(json.getString("method").trim()) 
126                                         || "POST".equals(json.getString("method").trim())){
127                                 
128                                 //Successful Validation
129                                 
130                         }else{
131                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Method value.";
132                                 return message; 
133                         }
134                 }else{
135                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing method key in the dictionaryJson parameter.";
136                         return message;
137                 }
138                 if(json.containsKey("url")){
139                         if(json.getString("url")==null || json.getString("url").trim().isEmpty()){
140                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No URL provided.";
141                                 return message;
142                         }
143                 }else{
144                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing url key in the dictionaryJson parameter.";
145                         return message;
146                 }
147                 if(json.containsKey("body")){
148                         if(json.getString("body")==null || json.getString("body").trim().isEmpty()){
149                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Body provided.";
150                                 return message;
151                         }
152                 }else{
153                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing body key in the dictionaryJson parameter.";
154                         return message;
155                 }
156                 if(json.containsKey("headers")){
157                         JsonArray array = json.getJsonArray("headers");
158                         
159                         for (int i = 0;i<array.size(); i++) { 
160                                 JsonObject jsonObj = array.getJsonObject(i);
161                                 if(jsonObj.containsKey("option")){
162                                         if(jsonObj.getString("option")==null || jsonObj.getString("option").trim().isEmpty()){
163                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Option value";
164                                         return message;
165                                         }
166                                 }else{
167                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing option key in the headers list of the dictionaryJson parameter.";
168                                 return message;
169                                 }
170
171                                 if(jsonObj.containsKey("number")){
172                                         if(jsonObj.getString("number")==null || jsonObj.getString("number").trim().isEmpty()){
173                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Number value";
174                                         return message;
175                                         }
176                                 }else{
177                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing number key in the headers list of the dictionaryJson parameter.";
178                                 return message;
179                                 }
180                         }
181                 }
182
183         }
184
185         return SUCCESS;
186     }
187 }