2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.sample;
40 import java.io.PrintWriter;
41 import java.util.HashMap;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.json.JSONObject;
48 import org.onap.portalapp.util.SecurityXssValidator;
49 import org.onap.portalsdk.core.controller.RestrictedBaseController;
50 import org.onap.portalsdk.core.domain.BroadcastMessage;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.service.BroadcastService;
53 import org.onap.portalsdk.core.util.SystemProperties;
54 import org.onap.portalsdk.core.web.support.AppUtils;
55 import org.onap.portalsdk.core.web.support.JsonMessage;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.servlet.ModelAndView;
62 import com.fasterxml.jackson.databind.DeserializationFeature;
63 import com.fasterxml.jackson.databind.JsonNode;
64 import com.fasterxml.jackson.databind.ObjectMapper;
68 public class BroadcastController extends RestrictedBaseController {
70 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastController.class);
73 private BroadcastService broadcastService;
75 @RequestMapping(value = { "/broadcast" }, method = RequestMethod.GET)
76 public ModelAndView broadcast(HttpServletRequest request) {
77 Map<String, Object> model = new HashMap<String, Object>();
78 ObjectMapper mapper = new ObjectMapper();
81 model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
82 model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
83 } catch (Exception e) {
84 logger.error(EELFLoggerDelegate.errorLogger, "broadcast() failed", e);
86 return new ModelAndView(getViewName(), model);
89 @RequestMapping(value = { "/get_broadcast" }, method = RequestMethod.GET)
90 public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
91 Map<String, Object> model = new HashMap<String, Object>();
92 ObjectMapper mapper = new ObjectMapper();
96 model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
97 model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
98 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
99 JSONObject j = new JSONObject(msg);
100 response.getWriter().write(j.toString());
102 } catch (Exception e) {
103 logger.error(EELFLoggerDelegate.errorLogger, "getBroadcast() failed", e);
108 @SuppressWarnings({ "unchecked", "rawtypes" })
109 protected Map referenceData(HttpServletRequest request) {
110 Map lookupData = new HashMap();
112 if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) {
113 lookupData.put("broadcastSites", AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd",
114 "broadcast_site_descr", "", "broadcast_site_descr"));
120 @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST)
121 public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
125 ObjectMapper mapper = new ObjectMapper();
126 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
127 JsonNode root = mapper.readTree(request.getReader());
128 BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
129 BroadcastMessage.class);
131 broadcastService.saveBroadcastMessage(broadcastMessage);
133 response.setCharacterEncoding("UTF-8");
134 response.setContentType("application / json");
135 request.setCharacterEncoding("UTF-8");
137 PrintWriter out = response.getWriter();
138 String responseString = mapper.writeValueAsString(broadcastMessage);
139 JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
141 out.write(j.toString());
144 } catch (Exception e) {
145 response.setCharacterEncoding("UTF-8");
146 request.setCharacterEncoding("UTF-8");
147 PrintWriter out = response.getWriter();
148 out.write("An error occurred while saving the BroadcastMessage in the save () mapping-/broadcast/save ");
149 logger.error(EELFLoggerDelegate.errorLogger, "save() failed", e);