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