2 * ============LICENSE_START=======================================================
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
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.controller;
28 import java.io.PrintWriter;
29 import java.util.List;
31 import javax.script.SimpleBindings;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.json.JSONObject;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.openecomp.portalsdk.core.web.support.UserUtils;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.servlet.ModelAndView;
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.node.ArrayNode;
51 @RequestMapping({"/"})
52 public class PolicyNotificationController extends RestrictedBaseController {
55 CommonClassDao commonClassDao;
57 @RequestMapping(value={"/watchPolicy"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
58 public ModelAndView watchPolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{
60 String responseValue = "";
62 String userId = UserUtils.getUserSession(request).getOrgUserId();
63 System.out.println(userId);
64 ObjectMapper mapper = new ObjectMapper();
65 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66 JsonNode root = mapper.readTree(request.getReader());
67 String name = root.get("watchData").get("name").toString();
68 JsonNode pathList = root.get("watchData").get("path");
69 String finalName = "";
70 if(pathList.isArray()){
71 ArrayNode arrayNode = (ArrayNode) pathList;
72 for (int i = 0; i < arrayNode.size(); i++) {
73 JsonNode individualElement = arrayNode.get(i);
75 path = path + individualElement.toString().replace("\"", "").trim();
77 path = path + File.separator + individualElement.toString().replace("\"", "").trim();
82 if(pathList.size() > 0){
83 finalName = path + File.separator + name.replace("\"", "").trim();
85 finalName = name.replace("\"", "").trim();
87 if(finalName.contains("\\")){
88 finalName = finalName.replace("\\", "\\\\");
90 String query = "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
91 SimpleBindings params = new SimpleBindings();
92 params.put("finalName", finalName);
93 params.put("userId", userId);
94 List<Object> watchList = commonClassDao.getDataByQuery(query, params);
95 if(watchList.isEmpty()){
96 if(finalName.contains("\\\\")){
97 finalName = finalName.replace("\\\\", File.separator);
99 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
100 watch.setPolicyName(finalName);
101 watch.setLoginIds(userId);
102 commonClassDao.save(watch);
103 responseValue = "You have Subscribed Successfully";
105 commonClassDao.delete(watchList.get(0));
106 responseValue = "You have UnSubscribed Successfully";
109 response.setCharacterEncoding("UTF-8");
110 response.setContentType("application / json");
111 request.setCharacterEncoding("UTF-8");
113 PrintWriter out = response.getWriter();
114 String responseString = mapper.writeValueAsString(responseValue);
115 JSONObject j = new JSONObject("{watchData: " + responseString + "}");
116 out.write(j.toString());
119 response.setCharacterEncoding("UTF-8");
120 request.setCharacterEncoding("UTF-8");
121 PrintWriter out = response.getWriter();
122 out.write(e.getMessage());