cd4c1a4353d7a0cbecf9fa71affb0ee0175ea288
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / EnforcerDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-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
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.json.JSONObject;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.EnforcingType;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.JsonNode;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49
50 @Controller
51 public class EnforcerDictionaryController {
52
53         private static final Logger LOGGER      = FlexLogger.getLogger(EnforcerDictionaryController.class);
54         
55         @Autowired
56         CommonClassDao commonClassDao;
57
58         @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
59         public void getEnforcerDictionaryEntityData(HttpServletResponse response){
60                 try{
61                         Map<String, Object> model = new HashMap<>();
62                         ObjectMapper mapper = new ObjectMapper();
63                         List<Object> list = commonClassDao.getData(EnforcingType.class);
64                         List<String> dictList = new ArrayList<>();
65                         for(int i = 0; i < list.size(); i++){
66                                 EnforcingType dict = (EnforcingType) list.get(i);
67                                 dictList.add(dict.getEnforcingType());
68                         }
69                         model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
70                         org.onap.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
71                         JSONObject j = new JSONObject(msg);
72                         response.getWriter().write(j.toString());
73                 }
74                 catch (Exception e){
75                         LOGGER.error("Exception Occured"+e);
76                 }
77         }
78         
79         @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
80         public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
81                 try {
82                         ObjectMapper mapper = new ObjectMapper();
83                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
84                         JsonNode root = mapper.readTree(request.getReader());
85                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
86                         if(enforcingType.getId() == 0){
87                                 commonClassDao.save(enforcingType);
88                         }else{
89                                 commonClassDao.update(enforcingType); 
90                         } 
91                         response.setCharacterEncoding("UTF-8");
92                         response.setContentType("application / json");
93                         request.setCharacterEncoding("UTF-8");
94
95                         PrintWriter out = response.getWriter();
96                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
97                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
98
99                         out.write(j.toString());
100
101                         return null;
102                 }
103                 catch (Exception e){
104                         response.setCharacterEncoding("UTF-8");
105                         request.setCharacterEncoding("UTF-8");
106                         PrintWriter out = response.getWriter();
107                         out.write(e.getMessage());
108                         LOGGER.error(e);
109                 }
110                 return null;
111         }
112
113         @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
114         public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
115                 try{
116                         ObjectMapper mapper = new ObjectMapper();
117                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118                         JsonNode root = mapper.readTree(request.getReader());
119                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
120                         commonClassDao.delete(enforcingType);
121                         response.setCharacterEncoding("UTF-8");
122                         response.setContentType("application / json");
123                         request.setCharacterEncoding("UTF-8");
124
125                         PrintWriter out = response.getWriter();
126
127                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
128                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
129                         out.write(j.toString());
130
131                         return null;
132                 }
133                 catch (Exception e){
134                         System.out.println(e);
135                         response.setCharacterEncoding("UTF-8");
136                         request.setCharacterEncoding("UTF-8");
137                         PrintWriter out = response.getWriter();
138                         out.write(e.getMessage());
139                 }
140                 return null;
141         }
142 }