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.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.json.JSONObject;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
37 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
38 import org.openecomp.portalsdk.core.web.support.UserUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.node.ArrayNode;
50 @RequestMapping({"/"})
51 public class PolicyNotificationController extends RestrictedBaseController {
54 CommonClassDao commonClassDao;
56 @RequestMapping(value={"/watchPolicy"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
57 public ModelAndView watchPolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{
59 String responseValue = "";
61 String userId = UserUtils.getUserSession(request).getOrgUserId();
62 System.out.println(userId);
63 ObjectMapper mapper = new ObjectMapper();
64 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
65 JsonNode root = mapper.readTree(request.getReader());
66 String name = root.get("watchData").get("name").toString();
67 JsonNode pathList = root.get("watchData").get("path");
68 String finalName = "";
69 if(pathList.isArray()){
70 ArrayNode arrayNode = (ArrayNode) pathList;
71 for (int i = 0; i < arrayNode.size(); i++) {
72 JsonNode individualElement = arrayNode.get(i);
74 path = path + individualElement.toString().replace("\"", "").trim();
76 path = path + File.separator + individualElement.toString().replace("\"", "").trim();
81 if(pathList.size() > 0){
82 finalName = path + File.separator + name.replace("\"", "").trim();
84 finalName = name.replace("\"", "").trim();
86 if(finalName.contains("\\")){
87 finalName = finalName.replace("\\", "\\\\");
89 String query = "from WatchPolicyNotificationTable where POLICYNAME = '"+finalName+"' and LOGINIDS = '"+userId+"'";
90 List<Object> watchList = commonClassDao.getDataByQuery(query);
91 if(watchList.isEmpty()){
92 if(finalName.contains("\\\\")){
93 finalName = finalName.replace("\\\\", File.separator);
95 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
96 watch.setPolicyName(finalName);
97 watch.setLoginIds(userId);
98 commonClassDao.save(watch);
99 responseValue = "You have Subscribed Successfully";
101 commonClassDao.delete(watchList.get(0));
102 responseValue = "You have UnSubscribed Successfully";
105 response.setCharacterEncoding("UTF-8");
106 response.setContentType("application / json");
107 request.setCharacterEncoding("UTF-8");
109 PrintWriter out = response.getWriter();
110 String responseString = mapper.writeValueAsString(responseValue);
111 JSONObject j = new JSONObject("{watchData: " + responseString + "}");
112 out.write(j.toString());
115 response.setCharacterEncoding("UTF-8");
116 request.setCharacterEncoding("UTF-8");
117 PrintWriter out = response.getWriter();
118 out.write(e.getMessage());