2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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=========================================================
21 package org.onap.policy.pap.xacml.rest.util;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.json.JSONObject;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.Category;
39 import org.onap.policy.rest.jpa.Datatype;
40 import org.onap.policy.rest.jpa.UserInfo;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44 import org.springframework.web.servlet.ModelAndView;
47 public class DictionaryUtils {
49 private static final Log LOGGER = LogFactory.getLog(DictionaryUtils.class);
51 private static String apiflag = "apiflag";
52 private static String operation = "operation";
53 private static String duplicateResponseString = "Duplicate";
54 private static String utf8 = "UTF-8";
55 private static String applicationJsonContentType = "application / json";
57 private static CommonClassDao commonClassDao;
59 private static DictionaryUtils dictionaryUtils;
61 public static synchronized DictionaryUtils getDictionaryUtils() {
62 return dictionaryUtils != null ? dictionaryUtils : new DictionaryUtils();
65 public static synchronized void setDictionaryUtils(DictionaryUtils dictionaryUtils) {
66 DictionaryUtils.dictionaryUtils = dictionaryUtils;
70 public DictionaryUtils(CommonClassDao commonClassDao) {
71 DictionaryUtils.commonClassDao = commonClassDao;
74 public DictionaryUtils() {
78 public UserInfo getUserInfo(String loginId) {
79 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
82 public boolean isRequestFromAPI(HttpServletRequest request) {
83 return request.getParameter(apiflag) != null
84 && "api".equalsIgnoreCase(request.getParameter(apiflag));
87 public String appendKey(List<Object> objects, String key1, String appendValue) {
88 StringBuilder userValue = new StringBuilder();
90 for (Object attribute : objects) {
91 if (attribute instanceof LinkedHashMap<?, ?>) {
92 String key = ((LinkedHashMap<?, ?>) attribute).get(key1).toString();
94 userValue.append(appendValue);
96 userValue.append(key);
100 return userValue.toString();
103 public String appendKeyValue(List<Object> objects, String append1, String append2) {
104 StringBuilder header = new StringBuilder();
106 for (Object attribute : objects) {
107 if (attribute instanceof LinkedHashMap<?, ?>) {
108 String key = ((LinkedHashMap<?, ?>) attribute).get("option").toString();
109 String value = ((LinkedHashMap<?, ?>) attribute).get("number").toString();
111 header.append(append1);
113 header.append(key).append(append2).append(value);
117 return header.toString();
120 public Datatype getDataType(String datatype) {
121 Datatype a = new Datatype();
122 if ("string".equalsIgnoreCase(datatype)) {
124 } else if ("integer".equalsIgnoreCase(datatype)) {
126 } else if ("boolean".equalsIgnoreCase(datatype)) {
128 } else if ("double".equalsIgnoreCase(datatype)) {
130 } else if ("user".equalsIgnoreCase(datatype)) {
136 public Category getCategory() {
137 return (Category) commonClassDao.getDataById(Category.class, "shortName", "resource")
141 public ModelAndView getResultForApi(String inResponseString) {
142 String responseString = inResponseString;
143 if (responseString != null && !duplicateResponseString.equals(responseString)) {
144 responseString = "Success";
146 ModelAndView result = new ModelAndView();
147 result.setViewName(responseString);
151 public void setResponseData(HttpServletResponse response, String key, String responseString)
153 response.setCharacterEncoding(utf8);
154 response.setContentType(applicationJsonContentType);
156 PrintWriter out = response.getWriter();
157 JSONObject j = new JSONObject("{" + key + ":" + responseString + "}");
158 out.write(j.toString());
161 public void setErrorResponseData(HttpServletResponse response, Exception e) throws IOException {
162 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
163 response.setCharacterEncoding(utf8);
164 PrintWriter out = response.getWriter();
165 out.write(e.getMessage());
168 @SuppressWarnings("rawtypes")
169 public void getDataByEntity(HttpServletResponse response, String key, String value,
172 Map<String, Object> model = new HashMap<>();
173 ObjectMapper mapper = new ObjectMapper();
175 mapper.writeValueAsString(commonClassDao.getDataByColumn(className, value)));
176 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
177 JSONObject j = new JSONObject(msg);
178 response.getWriter().write(j.toString());
179 } catch (Exception e) {
180 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
184 @SuppressWarnings("rawtypes")
185 public void getData(HttpServletResponse response, String key, Class className) {
187 Map<String, Object> model = new HashMap<>();
188 ObjectMapper mapper = new ObjectMapper();
189 model.put(key, mapper.writeValueAsString(commonClassDao.getData(className)));
190 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
191 JSONObject j = new JSONObject(msg);
192 response.addHeader("successMapKey", "success");
193 response.addHeader(operation, "getDictionary");
194 response.getWriter().write(j.toString());
195 } catch (Exception e) {
196 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
197 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
198 response.addHeader("error", "dictionaryDBQuery");
202 @SuppressWarnings("unchecked")
203 public void removeData(HttpServletRequest request, HttpServletResponse response, String key,
204 @SuppressWarnings("rawtypes") Class className) throws IOException {
206 ObjectMapper mapper = new ObjectMapper();
207 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
208 JsonNode root = mapper.readTree(request.getReader());
210 .delete(mapper.readValue(root.get("data").toString(), className));
211 String responseString = mapper.writeValueAsString(commonClassDao.getData(className));
212 setResponseData(response, key, responseString);
213 } catch (Exception e) {
214 setErrorResponseData(response, e);