2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalapp.controller.sample;
22 import java.io.PrintWriter;
23 import java.util.HashMap;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.json.JSONObject;
30 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
31 import org.openecomp.portalsdk.core.domain.BroadcastMessage;
32 import org.openecomp.portalsdk.core.service.BroadcastService;
33 import org.openecomp.portalsdk.core.web.support.JsonMessage;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.servlet.ModelAndView;
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.JsonNode;
42 import com.fasterxml.jackson.databind.ObjectMapper;
46 public class BroadcastListController extends RestrictedBaseController {
49 private BroadcastService broadcastService;
51 @RequestMapping(value = { "/broadcast_list" }, method = RequestMethod.GET)
52 public ModelAndView broadcastList(HttpServletRequest request) {
53 Map<String, Object> model = new HashMap<String, Object>();
55 model.put("model", broadcastService.getBcModel(request));
56 return new ModelAndView(getViewName(), model);
59 @RequestMapping(value = { "/get_broadcast_list" }, method = RequestMethod.GET)
60 public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
61 Map<String, Object> model = new HashMap<String, Object>();
62 ObjectMapper mapper = new ObjectMapper();
64 model.put("model", broadcastService.getBcModel(request));
65 model.put("messagesList", broadcastService.getBcModel(request).get("messagesList"));
66 model.put("messageLocations", broadcastService.getBcModel(request).get("messageLocations"));
67 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
68 JSONObject j = new JSONObject(msg);
69 response.getWriter().write(j.toString());
70 } catch (Exception e) {
76 @RequestMapping(value = { "/broadcast_list/remove" }, method = RequestMethod.POST)
77 public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
81 ObjectMapper mapper = new ObjectMapper();
82 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
83 JsonNode root = mapper.readTree(request.getReader());
84 BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
85 BroadcastMessage.class);
87 broadcastService.removeBroadcastMessage(broadcastMessage);
89 response.setCharacterEncoding("UTF-8");
90 response.setContentType("application / json");
91 request.setCharacterEncoding("UTF-8");
93 PrintWriter out = response.getWriter();
94 String responseString = mapper.writeValueAsString(broadcastMessage);
95 JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
97 out.write(j.toString());
100 } catch (Exception e) {
101 response.setCharacterEncoding("UTF-8");
102 request.setCharacterEncoding("UTF-8");
103 PrintWriter out = response.getWriter();
104 out.write(e.getMessage());
110 @RequestMapping(value = { "/broadcast_list/toggleActive" }, method = RequestMethod.POST)
111 public ModelAndView toggleActive(HttpServletRequest request, HttpServletResponse response) throws Exception {
115 ObjectMapper mapper = new ObjectMapper();
116 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
117 JsonNode root = mapper.readTree(request.getReader());
118 BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
119 BroadcastMessage.class);
121 broadcastService.saveBroadcastMessage(broadcastMessage);
123 response.setCharacterEncoding("UTF-8");
124 response.setContentType("application / json");
125 request.setCharacterEncoding("UTF-8");
127 PrintWriter out = response.getWriter();
128 String responseString = mapper.writeValueAsString(broadcastMessage);
129 JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
131 out.write(j.toString());
134 } catch (Exception e) {
135 response.setCharacterEncoding("UTF-8");
136 request.setCharacterEncoding("UTF-8");
137 PrintWriter out = response.getWriter();
138 out.write(e.getMessage());