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.IOException;
41 import java.io.PrintWriter;
42 import java.io.UnsupportedEncodingException;
43 import java.util.HashMap;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.json.JSONObject;
50 import org.onap.portalsdk.core.controller.RestrictedBaseController;
51 import org.onap.portalsdk.core.domain.BroadcastMessage;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.core.service.BroadcastService;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.onap.portalsdk.core.web.support.AppUtils;
56 import org.onap.portalsdk.core.web.support.JsonMessage;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Controller;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.servlet.ModelAndView;
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.JsonNode;
65 import com.fasterxml.jackson.databind.ObjectMapper;
69 public class BroadcastController extends RestrictedBaseController {
71 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastController.class);
73 private static final String BROADCAST_MESSAGE = "broadcastMessage";
74 private static final String BROADCAST_SITIES = "broadcastSites";
75 private static final String UTF8 = "UTF-8";
78 private BroadcastService broadcastService;
80 @RequestMapping(value = { "/broadcast" }, method = RequestMethod.GET)
81 public ModelAndView broadcast(HttpServletRequest request) {
82 Map<String, Object> model = new HashMap<>();
83 ObjectMapper mapper = new ObjectMapper();
86 model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
87 model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES)));
88 } catch (Exception e) {
89 logger.error(EELFLoggerDelegate.errorLogger, "broadcast() failed", e);
91 return new ModelAndView(getViewName(), model);
94 @RequestMapping(value = { "/get_broadcast" }, method = RequestMethod.GET)
95 public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
96 Map<String, Object> model = new HashMap<>();
97 ObjectMapper mapper = new ObjectMapper();
101 model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
102 model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES)));
103 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
104 JSONObject j = new JSONObject(msg);
105 response.getWriter().write(j.toString());
107 } catch (Exception e) {
108 logger.error(EELFLoggerDelegate.errorLogger, "getBroadcast() failed", e);
113 @SuppressWarnings({ "unchecked", "rawtypes" })
114 protected Map referenceData(HttpServletRequest request) {
115 Map lookupData = new HashMap();
117 if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) {
118 lookupData.put(BROADCAST_SITIES, AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd",
119 "broadcast_site_descr", "", "broadcast_site_descr"));
125 @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST)
126 public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws IOException {
130 ObjectMapper mapper = new ObjectMapper();
131 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
132 JsonNode root = mapper.readTree(request.getReader());
133 BroadcastMessage broadcastMessage = mapper.readValue(root.get(BROADCAST_MESSAGE).toString(),
134 BroadcastMessage.class);
136 broadcastService.saveBroadcastMessage(broadcastMessage);
138 response.setCharacterEncoding(UTF8);
139 response.setContentType("application / json");
140 request.setCharacterEncoding(UTF8);
142 PrintWriter out = response.getWriter();
143 String responseString = mapper.writeValueAsString(broadcastMessage);
144 JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
146 out.write(j.toString());
149 } catch (Exception e) {
150 response.setCharacterEncoding(UTF8);
151 request.setCharacterEncoding(UTF8);
152 PrintWriter out = response.getWriter();
153 out.write("An error occurred while saving the BroadcastMessage in the save () mapping-/broadcast/save ");
154 logger.error(EELFLoggerDelegate.errorLogger, "save() failed", e);