c9c666fb3ad1601cc0023993f436370ef6005afd
[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.web.support.JsonMessage;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.stereotype.Controller;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RequestMethod;
58 import org.springframework.web.servlet.ModelAndView;
59
60 import com.fasterxml.jackson.databind.DeserializationFeature;
61 import com.fasterxml.jackson.databind.JsonNode;
62 import com.fasterxml.jackson.databind.ObjectMapper;
63
64 @Controller
65 @RequestMapping("/")
66 public class BroadcastListController extends RestrictedBaseController {
67         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastListController.class);
68
69         private static final String UTF8 = "UTF-8";
70
71         @Autowired
72         private BroadcastService broadcastService;
73
74         @RequestMapping(value = { "/broadcast_list" }, method = RequestMethod.GET)
75         public ModelAndView broadcastList(HttpServletRequest request) {
76                 Map<String, Object> model = new HashMap<>();
77
78                 model.put("model", broadcastService.getBcModel(request));
79                 return new ModelAndView(getViewName(), model);
80         }
81
82         @RequestMapping(value = { "/get_broadcast_list" }, method = RequestMethod.GET)
83         public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
84                 Map<String, Object> model = new HashMap<>();
85                 ObjectMapper mapper = new ObjectMapper();
86                 try {
87                         model.put("model", broadcastService.getBcModel(request));
88                         model.put("messagesList", broadcastService.getBcModel(request).get("messagesList"));
89                         model.put("messageLocations", broadcastService.getBcModel(request).get("messageLocations"));
90                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
91                         JSONObject j = new JSONObject(msg);
92                         response.getWriter().write(j.toString());
93                 } catch (Exception e) {
94                         logger.error(EELFLoggerDelegate.errorLogger, "getBroadcast() failed", e);
95                 }
96
97         }
98
99         @RequestMapping(value = { "/broadcast_list/remove" }, method = RequestMethod.POST)
100         public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
101
102                 try {
103
104                         ObjectMapper mapper = new ObjectMapper();
105                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106                         JsonNode root = mapper.readTree(request.getReader());
107                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
108                                         BroadcastMessage.class);
109
110                         broadcastService.removeBroadcastMessage(broadcastMessage);
111
112                         response.setCharacterEncoding(UTF8);
113                         response.setContentType("application / json");
114                         request.setCharacterEncoding(UTF8);
115
116                         PrintWriter out = response.getWriter();
117                         String responseString = mapper.writeValueAsString(broadcastMessage);
118                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
119
120                         out.write(j.toString());
121
122                         return null;
123                 } catch (Exception e) {
124                         response.setCharacterEncoding(UTF8);
125                         request.setCharacterEncoding(UTF8);
126                         PrintWriter out = response.getWriter();
127                         out.write("An error occurred while removing the BroadcastMessage in the remove ()");
128                         logger.error(EELFLoggerDelegate.errorLogger, "remove() failed", e);
129                         return null;
130                 }
131
132         }
133
134         @RequestMapping(value = { "/broadcast_list/toggleActive" }, method = RequestMethod.POST)
135         public ModelAndView toggleActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
136
137                 try {
138
139                         ObjectMapper mapper = new ObjectMapper();
140                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
141                         JsonNode root = mapper.readTree(request.getReader());
142                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
143                                         BroadcastMessage.class);
144
145                         broadcastService.saveBroadcastMessage(broadcastMessage);
146
147                         response.setCharacterEncoding(UTF8);
148                         response.setContentType("application / json");
149                         request.setCharacterEncoding(UTF8);
150
151                         PrintWriter out = response.getWriter();
152                         String responseString = mapper.writeValueAsString(broadcastMessage);
153                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
154
155                         out.write(j.toString());
156
157                         return null;
158                 } catch (Exception e) {
159                         response.setCharacterEncoding(UTF8);
160                         request.setCharacterEncoding(UTF8);
161                         PrintWriter out = response.getWriter();
162                         out.write("An error occurred while saving the BroadcastMessage in the toggleActive () ");
163                         logger.error(EELFLoggerDelegate.errorLogger, "toggleActive() failed", e);
164                         return null;
165                 }
166
167         }
168 }