cd107d0baacfd53ed510b6dac313c9a99f4c19db
[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 boolean isNumeric(String str) {
58         for (char c : str.toCharArray()) {
59             if (!Character.isDigit(c))
60                 return false;
61         }
62         return true;
63     }
64
65     public static JsonObject stringToJsonObject(String value)
66             throws JsonException, JsonParsingException, IllegalStateException {
67         JsonReader jsonReader = Json.createReader(new StringReader(value));
68         JsonObject object = jsonReader.readObject();
69         jsonReader.close();
70         return object;
71     }
72 }