2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.pap.xacml.rest.util;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.json.JSONObject;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.Category;
37 import org.onap.policy.rest.jpa.Datatype;
38 import org.onap.policy.rest.jpa.UserInfo;
39 import org.onap.policy.xacml.api.XACMLErrorConstants;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
42 import org.springframework.web.servlet.ModelAndView;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
49 public class DictionaryUtils {
51 private static final Log LOGGER = LogFactory.getLog(DictionaryUtils.class);
53 private static String apiflag = "apiflag";
54 private static String operation = "operation";
55 private static String duplicateResponseString = "Duplicate";
56 private static String utf8 = "UTF-8";
57 private static String applicationJsonContentType = "application / json";
59 private static CommonClassDao commonClassDao;
61 private static DictionaryUtils dictionaryUtils;
63 public static synchronized DictionaryUtils getDictionaryUtils() {
64 return dictionaryUtils != null ? dictionaryUtils : new DictionaryUtils();
67 public static synchronized void setDictionaryUtils(DictionaryUtils dictionaryUtils) {
68 DictionaryUtils.dictionaryUtils = dictionaryUtils;
72 public DictionaryUtils(CommonClassDao commonClassDao){
73 DictionaryUtils.commonClassDao = commonClassDao;
76 public DictionaryUtils(){
80 public UserInfo getUserInfo(String loginId){
81 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
84 public boolean isRequestFromAPI(HttpServletRequest request){
85 return request.getParameter(apiflag)!=null && "api".equalsIgnoreCase(request.getParameter(apiflag));
88 public String appendKey(List<Object> objects, String key1, String appendValue){
89 StringBuilder userValue = new StringBuilder();
91 for(Object attribute : objects){
92 if(attribute instanceof LinkedHashMap<?, ?>){
93 String key = ((LinkedHashMap<?, ?>) attribute).get(key1).toString();
95 userValue.append(appendValue);
97 userValue.append(key);
101 return userValue.toString();
104 public String appendKeyValue(List<Object> objects, String append1, String append2){
105 StringBuilder header = new StringBuilder();
107 for(Object attribute : objects){
108 if(attribute instanceof LinkedHashMap<?, ?>){
109 String key = ((LinkedHashMap<?, ?>) attribute).get("option").toString();
110 String value = ((LinkedHashMap<?, ?>) attribute).get("number").toString();
112 header.append(append1);
114 header.append(key).append(append2).append(value);
118 return header.toString();
121 public Datatype getDataType(String datatype){
122 Datatype a = new Datatype();
123 if("string".equalsIgnoreCase(datatype)){
125 }else if("integer".equalsIgnoreCase(datatype)){
127 }else if("boolean".equalsIgnoreCase(datatype)){
129 }else if("double".equalsIgnoreCase(datatype)){
131 }else if("user".equalsIgnoreCase(datatype)){
137 public Category getCategory(){
138 return (Category) commonClassDao.getDataById(Category.class, "shortName", "resource").get(0);
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) throws IOException{
152 response.setCharacterEncoding(utf8);
153 response.setContentType(applicationJsonContentType);
155 PrintWriter out = response.getWriter();
156 JSONObject j = new JSONObject("{"+key+":" + responseString + "}");
157 out.write(j.toString());
160 public void setErrorResponseData(HttpServletResponse response, Exception e) throws IOException{
161 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
162 response.setCharacterEncoding(utf8);
163 PrintWriter out = response.getWriter();
164 out.write(e.getMessage());
167 @SuppressWarnings("rawtypes")
168 public void getDataByEntity(HttpServletResponse response, String key, String value, Class className){
170 Map<String, Object> model = new HashMap<>();
171 ObjectMapper mapper = new ObjectMapper();
172 model.put(key, mapper.writeValueAsString(commonClassDao.getDataByColumn(className, value)));
173 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
174 JSONObject j = new JSONObject(msg);
175 response.getWriter().write(j.toString());
177 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
181 @SuppressWarnings("rawtypes")
182 public void getData(HttpServletResponse response, String key, Class className){
184 Map<String, Object> model = new HashMap<>();
185 ObjectMapper mapper = new ObjectMapper();
186 model.put(key, mapper.writeValueAsString(commonClassDao.getData(className)));
187 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
188 JSONObject j = new JSONObject(msg);
189 response.addHeader("successMapKey", "success");
190 response.addHeader(operation, "getDictionary");
191 response.getWriter().write(j.toString());
193 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
194 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
195 response.addHeader("error", "dictionaryDBQuery");
199 @SuppressWarnings("unchecked")
200 public void removeData(HttpServletRequest request, HttpServletResponse response, String key, @SuppressWarnings("rawtypes") Class className) throws IOException{
202 ObjectMapper mapper = new ObjectMapper();
203 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
204 JsonNode root = mapper.readTree(request.getReader());
205 commonClassDao.delete((Object)mapper.readValue(root.get("data").toString(), className));
206 String responseString = mapper.writeValueAsString(commonClassDao.getData(className));
207 setResponseData(response, key, responseString);
209 setErrorResponseData(response, e);