Fixes for sonar critical issues
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyNotificationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.controller;
22
23
24 /*
25  * 
26  * */
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.util.List;
31
32 import javax.script.SimpleBindings;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.json.JSONObject;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
41 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
42 import org.openecomp.portalsdk.core.web.support.UserUtils;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.servlet.ModelAndView;
47
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.databind.node.ArrayNode;
52
53 @Controller
54 @RequestMapping({"/"})
55 public class PolicyNotificationController extends RestrictedBaseController {
56     private static Logger logger = FlexLogger.getLogger(PolicyNotificationController.class);
57
58         @Autowired
59         CommonClassDao commonClassDao;
60         
61         @RequestMapping(value={"/watchPolicy"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
62         public ModelAndView watchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException{
63                 String path = "";
64                 String responseValue = "";
65                 try {
66                         String userId = UserUtils.getUserSession(request).getOrgUserId();
67                         System.out.println(userId);
68                         ObjectMapper mapper = new ObjectMapper();
69                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
70                         JsonNode root = mapper.readTree(request.getReader());
71                         String name = root.get("watchData").get("name").toString();
72                         JsonNode pathList = root.get("watchData").get("path");
73                         String finalName = "";
74                         if(pathList.isArray()){
75                                 ArrayNode arrayNode = (ArrayNode) pathList;
76                                 for (int i = 0; i < arrayNode.size(); i++) {
77                                         JsonNode individualElement = arrayNode.get(i);
78                                         if(i == 0){
79                                                 path = path + individualElement.toString().replace("\"", "").trim();
80                                         }else{
81                                                 path = path + File.separator + individualElement.toString().replace("\"", "").trim();
82                                         }
83                                 }
84                         }
85                         
86                         if(pathList.size() > 0){
87                                 finalName = path + File.separator + name.replace("\"", "").trim();
88                         }else{
89                                 finalName = name.replace("\"", "").trim();
90                         }
91                         if(finalName.contains("\\")){
92                                 finalName = finalName.replace("\\", "\\\\");
93                         }
94                         String query = "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
95                         SimpleBindings params = new SimpleBindings();
96                         params.put("finalName", finalName);
97                         params.put("userId", userId);
98                         List<Object> watchList = commonClassDao.getDataByQuery(query, params);
99                         if(watchList.isEmpty()){
100                                 if(finalName.contains("\\\\")){
101                                         finalName = finalName.replace("\\\\", File.separator);
102                                 }
103                                 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
104                                 watch.setPolicyName(finalName);
105                                 watch.setLoginIds(userId);
106                                 commonClassDao.save(watch);
107                                 responseValue = "You have Subscribed Successfully";
108                         }else{
109                                 commonClassDao.delete(watchList.get(0));
110                                 responseValue = "You have UnSubscribed Successfully";
111                         }
112                         
113                         response.setCharacterEncoding("UTF-8");
114                         response.setContentType("application / json");
115                         request.setCharacterEncoding("UTF-8");
116
117                         PrintWriter out = response.getWriter();
118                         String responseString = mapper.writeValueAsString(responseValue);
119                         JSONObject j = new JSONObject("{watchData: " + responseString + "}");
120                         out.write(j.toString());
121                         return null;
122                 }catch(Exception e){
123                         response.setCharacterEncoding("UTF-8");
124                         request.setCharacterEncoding("UTF-8");
125                         logger.error("Error druing watchPolicy function " + e);
126                         PrintWriter out = response.getWriter();
127                         out.write(e.getMessage());
128                 }
129                 return null;
130         }
131 }