2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ArrayNode;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.List;
36 import javax.script.SimpleBindings;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 import org.json.JSONObject;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.rest.dao.CommonClassDao;
44 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
45 import org.onap.policy.utils.PolicyUtils;
46 import org.onap.portalsdk.core.controller.RestrictedBaseController;
47 import org.onap.portalsdk.core.web.support.UserUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.stereotype.Controller;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.servlet.ModelAndView;
54 @RequestMapping({"/"})
55 public class PolicyNotificationController extends RestrictedBaseController {
56 private static Logger logger = FlexLogger.getLogger(PolicyNotificationController.class);
59 CommonClassDao commonClassDao;
64 * @param request HttpServletRequest
65 * @param response HttpServletResponse
66 * @return ModelAndView
67 * @throws IOException IOException
69 @RequestMapping(value = {"/watchPolicy"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST})
70 public ModelAndView watchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException {
71 StringBuilder path = new StringBuilder();
72 String responseValue = "";
74 String userId = UserUtils.getUserSession(request).getOrgUserId();
75 logger.info("userid info: " + userId);
76 ObjectMapper mapper = new ObjectMapper();
77 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
78 JsonNode root = mapper.readTree(request.getReader());
79 String name = root.get("watchData").get("name").toString();
80 JsonNode pathList = root.get("watchData").get("path");
82 if (pathList.isArray()) {
83 ArrayNode arrayNode = (ArrayNode) pathList;
84 for (int i = 0; i < arrayNode.size(); i++) {
85 JsonNode individualElement = arrayNode.get(i);
87 path.append(individualElement.toString().replace("\"", "").trim());
89 path.append(File.separator + individualElement.toString().replace("\"", "").trim());
94 if (pathList.size() > 0) {
95 finalName = path + File.separator + name.replace("\"", "").trim();
97 finalName = name.replace("\"", "").trim();
99 if (finalName.contains("\\")) {
100 finalName = finalName.replace("\\", "\\\\");
102 String query = "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
103 SimpleBindings params = new SimpleBindings();
104 params.put("finalName", finalName);
105 params.put("userId", userId);
106 List<Object> watchList = commonClassDao.getDataByQuery(query, params);
107 if (watchList.isEmpty()) {
108 if (finalName.contains("\\\\")) {
109 finalName = finalName.replace("\\\\", File.separator);
111 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
112 watch.setPolicyName(finalName);
113 watch.setLoginIds(userId);
114 commonClassDao.save(watch);
115 responseValue = "You have Subscribed Successfully";
117 commonClassDao.delete(watchList.get(0));
118 responseValue = "You have UnSubscribed Successfully";
121 response.setCharacterEncoding("UTF-8");
122 response.setContentType("application / json");
123 request.setCharacterEncoding("UTF-8");
125 response.getWriter().write(new JSONObject("{watchData: "
126 + mapper.writeValueAsString(responseValue) + "}").toString());
127 } catch (Exception e) {
128 response.setCharacterEncoding("UTF-8");
129 request.setCharacterEncoding("UTF-8");
130 logger.error("Error druing watchPolicy function " + e);
131 PrintWriter out = response.getWriter();
132 out.write(PolicyUtils.CATCH_EXCEPTION);