2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pdp.rest.api.utils;
22 import java.io.StringReader;
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;
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;
36 import com.google.common.base.CharMatcher;
38 public class PolicyApiUtils {
39 private static Logger LOGGER = FlexLogger.getLogger(PolicyApiUtils.class
41 private static final String SUCCESS = "success";
44 public static Boolean validateNONASCIICharactersAndAllowSpaces(
46 Boolean isValidForm = false;
47 if (jsonString.isEmpty()) {
48 LOGGER.error("The Value is empty.");
51 if (CharMatcher.ascii().matchesAllOf((CharSequence) jsonString)) {
52 LOGGER.info("The Value does not contain ASCII Characters");
55 LOGGER.error("The Value Contains Non ASCII Characters");
62 public static String formatResponse(StringBuilder responseString){
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>|@#)", "");
71 public static boolean isNumeric(String str) {
72 for (char c : str.toCharArray()) {
73 if (!Character.isDigit(c))
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();
87 public static String validateDictionaryJsonFields(JsonObject json, String dictionary) {
89 LOGGER.info("Validating DictionaryJsonField values");
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.";
98 if(!SUCCESS.equals(PolicyUtils.policySpecialCharValidator(json.getString("attributeName").trim()))){
99 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Attribute Name value.";
103 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing attributeName key in the dictionaryJson parameter.";
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.";
111 if(!"REST".equals(json.getString("type").trim())){
112 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Type value.";
116 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing type key in the dictionaryJson parameter.";
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.";
124 if("GET".equals(json.getString("method").trim())
125 || "PUT".equals(json.getString("method").trim())
126 || "POST".equals(json.getString("method").trim())){
128 //Successful Validation
131 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Method value.";
135 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing method key in the dictionaryJson parameter.";
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.";
144 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing url key in the dictionaryJson parameter.";
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.";
153 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing body key in the dictionaryJson parameter.";
156 if(json.containsKey("headers")){
157 JsonArray array = json.getJsonArray("headers");
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";
167 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing option key in the headers list of the dictionaryJson parameter.";
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";
177 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing number key in the headers list of the dictionaryJson parameter.";