Refactor to provide Common Policy Validation
[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.JsonException;
26 import javax.json.JsonObject;
27 import javax.json.JsonReader;
28 import javax.json.stream.JsonParsingException;
29
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32
33 import com.google.common.base.CharMatcher;
34
35 public class PolicyApiUtils {
36     private static Logger LOGGER = FlexLogger.getLogger(PolicyApiUtils.class
37             .getName());
38
39     public static Boolean validateNONASCIICharactersAndAllowSpaces(
40             String jsonString) {
41         Boolean isValidForm = false;
42         if (jsonString.isEmpty()) {
43             LOGGER.error("The Value is empty.");
44             return false;
45         } else {
46             if (CharMatcher.ASCII.matchesAllOf((CharSequence) jsonString)) {
47                 LOGGER.info("The Value does not contain ASCII Characters");
48                 isValidForm = true;
49             } else {
50                 LOGGER.error("The Value Contains Non ASCII Characters");
51                 isValidForm = false;
52             }
53         }
54         return isValidForm;
55     }
56     
57     public static String formatResponse(StringBuilder responseString){
58         
59         LOGGER.info("Formatting response message from Policy Validator");
60                 String response = null;
61         response = responseString.toString().replace("<br>", " | ");            
62                 response = response.replaceAll("(<b>|<\\/b>|<br>|<i>|<\\/i>|@#)", "");
63                                 
64         return response;
65     }
66     
67     public static boolean isNumeric(String str) {
68         for (char c : str.toCharArray()) {
69             if (!Character.isDigit(c))
70                 return false;
71         }
72         return true;
73     }
74
75     public static JsonObject stringToJsonObject(String value)
76             throws JsonException, JsonParsingException, IllegalStateException {
77         JsonReader jsonReader = Json.createReader(new StringReader(value));
78         JsonObject object = jsonReader.readObject();
79         jsonReader.close();
80         return object;
81     }
82 }