4392aeb5459596e6ec240c9758875ff42fe0a662
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.controller.sample;
39
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.json.JSONObject;
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;
61
62 import com.fasterxml.jackson.databind.DeserializationFeature;
63 import com.fasterxml.jackson.databind.JsonNode;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65
66 @Controller
67 @RequestMapping("/")
68 public class BroadcastController extends RestrictedBaseController {
69
70         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastController.class);
71
72         private static final String BROADCAST_MESSAGE = "broadcastMessage";
73   private static final String BROADCAST_SITIES = "broadcastSites";
74   private static final String UTF8 = "UTF-8";
75         
76         @Autowired
77         private BroadcastService broadcastService;
78
79         @RequestMapping(value = { "/broadcast" }, method = RequestMethod.GET)
80         public ModelAndView broadcast(HttpServletRequest request) {
81                 Map<String, Object> model = new HashMap<>();
82                 ObjectMapper mapper = new ObjectMapper();
83
84                 try {
85                         model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
86                         model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES)));
87                 } catch (Exception e) {
88                         logger.error(EELFLoggerDelegate.errorLogger, "broadcast() failed", e);
89                 }
90                 return new ModelAndView(getViewName(), model);
91         }
92
93         @RequestMapping(value = { "/get_broadcast" }, method = RequestMethod.GET)
94         public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
95                 Map<String, Object> model = new HashMap<>();
96                 ObjectMapper mapper = new ObjectMapper();
97
98                 try {
99
100                         model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
101                         model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES)));
102                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
103                         JSONObject j = new JSONObject(msg);
104                         response.getWriter().write(j.toString());
105
106                 } catch (Exception e) {
107                         logger.error(EELFLoggerDelegate.errorLogger, "getBroadcast() failed", e);
108                 }
109
110         }
111
112         @SuppressWarnings({ "unchecked", "rawtypes" })
113         protected Map referenceData(HttpServletRequest request) {
114                 Map lookupData = new HashMap();
115
116                 if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) {
117                         lookupData.put(BROADCAST_SITIES, AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd",
118                                         "broadcast_site_descr", "", "broadcast_site_descr"));
119                 }
120
121                 return lookupData;
122         }
123
124         @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST)
125         public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws IOException {
126
127                 try {
128
129                         ObjectMapper mapper = new ObjectMapper();
130                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
131                         JsonNode root = mapper.readTree(request.getReader());
132                         BroadcastMessage broadcastMessage = mapper.readValue(root.get(BROADCAST_MESSAGE).toString(),
133                                         BroadcastMessage.class);
134
135                         broadcastService.saveBroadcastMessage(broadcastMessage);
136
137                         response.setCharacterEncoding(UTF8);
138                         response.setContentType("application / json");
139                         request.setCharacterEncoding(UTF8);
140
141                         PrintWriter out = response.getWriter();
142                         String responseString = mapper.writeValueAsString(broadcastMessage);
143                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
144
145                         out.write(j.toString());
146
147                         return null;
148                 } catch (Exception e) {
149                         response.setCharacterEncoding(UTF8);
150                         request.setCharacterEncoding(UTF8);
151                         PrintWriter out = response.getWriter();
152                         out.write("An error occurred while saving the BroadcastMessage in the save () mapping-/broadcast/save   ");
153                         logger.error(EELFLoggerDelegate.errorLogger, "save() failed", e);
154                         return null;
155                 }
156
157         }
158 }