|
16 | 16 | */
|
17 | 17 | package com.ericsson.ei.frontend;
|
18 | 18 |
|
19 |
| - |
20 | 19 | import com.ericsson.ei.frontend.model.BackEndInformation;
|
21 | 20 | import com.ericsson.ei.frontend.model.Index;
|
22 | 21 | import com.ericsson.ei.frontend.model.ListWrapper;
|
|
28 | 27 | import org.slf4j.LoggerFactory;
|
29 | 28 | import org.springframework.beans.factory.annotation.Autowired;
|
30 | 29 | import org.springframework.beans.factory.annotation.Value;
|
| 30 | +import org.springframework.http.HttpStatus; |
| 31 | +import org.springframework.http.ResponseEntity; |
31 | 32 | import org.springframework.stereotype.Controller;
|
32 | 33 | import org.springframework.ui.Model;
|
33 |
| -import org.springframework.validation.BindingResult; |
34 |
| -import org.springframework.web.bind.annotation.ModelAttribute; |
35 | 34 | import org.springframework.web.bind.annotation.RequestMapping;
|
36 | 35 | import org.springframework.web.bind.annotation.RequestMethod;
|
37 | 36 |
|
38 | 37 | import javax.annotation.PostConstruct;
|
39 |
| -import javax.validation.Valid; |
| 38 | +import javax.servlet.http.HttpServletRequest; |
40 | 39 | import java.io.FileOutputStream;
|
41 | 40 | import java.io.IOException;
|
42 | 41 | import java.nio.file.Files;
|
43 | 42 | import java.nio.file.Paths;
|
44 | 43 | import java.util.ArrayList;
|
45 | 44 | import java.util.List;
|
| 45 | +import java.util.stream.Collectors; |
46 | 46 |
|
47 | 47 | @Controller
|
48 | 48 | public class WebController {
|
@@ -87,25 +87,28 @@ public class WebController {
|
87 | 87 |
|
88 | 88 | private List<BackEndInformation> information = new ArrayList<>();
|
89 | 89 |
|
| 90 | + private JSONArray instances = new JSONArray(); |
| 91 | + |
90 | 92 | @PostConstruct
|
91 | 93 | public void init() {
|
92 |
| - index.setIndex(0); |
93 |
| - information.add(backEndInformation); |
94 |
| - if (eiInstancesPath != null) { |
95 |
| - try { |
96 |
| - JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))); |
97 |
| - for (Object o : inputBackEndInstances) { |
98 |
| - JSONObject instance = (JSONObject) o; |
99 |
| - BackEndInformation backEndInformations = new ObjectMapper().readValue(instance.toString(), BackEndInformation.class); |
100 |
| - if (!checkIfInstanceAlreadyExist(backEndInformations)) { |
101 |
| - information.add(backEndInformations); |
102 |
| - } |
103 |
| - } |
104 |
| - } catch (IOException e) { |
105 |
| - LOG.error("Failure when try to parse json file" + e.getMessage()); |
106 |
| - } |
107 |
| - } |
108 |
| - writeIntoFile(); |
| 94 | + instances.put(getCurrentInstance()); |
| 95 | +// index.setIndex(0); |
| 96 | +// information.add(backEndInformation); |
| 97 | +// if (eiInstancesPath != null) { |
| 98 | +// try { |
| 99 | +// JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))); |
| 100 | +// for (Object o : inputBackEndInstances) { |
| 101 | +// JSONObject instance = (JSONObject) o; |
| 102 | +// BackEndInformation backEndInformations = new ObjectMapper().readValue(instance.toString(), BackEndInformation.class); |
| 103 | +// if (!checkIfInstanceAlreadyExist(backEndInformations)) { |
| 104 | +// information.add(backEndInformations); |
| 105 | +// } |
| 106 | +// } |
| 107 | +// } catch (IOException e) { |
| 108 | +// LOG.error("Failure when try to parse json file" + e.getMessage()); |
| 109 | +// } |
| 110 | +// } |
| 111 | +// writeIntoFile(); |
109 | 112 | }
|
110 | 113 |
|
111 | 114 | @RequestMapping("/")
|
@@ -166,70 +169,84 @@ public String jmesPathRulesSetUp(Model model) {
|
166 | 169 | return "jmesPathRulesSetUp";
|
167 | 170 | }
|
168 | 171 |
|
169 |
| - @RequestMapping(value = "/switch-backend", method = RequestMethod.GET) |
| 172 | + @RequestMapping("/switch-backend.html") |
170 | 173 | public String switchBackEnd(Model model) {
|
171 |
| - wrapper.setBackEndInformation(information); |
172 |
| - model.addAttribute("listWrapper", wrapper); |
173 |
| - model.addAttribute("index", index); |
174 | 174 | return "switch-backend";
|
175 | 175 | }
|
176 | 176 |
|
177 |
| - @RequestMapping(params = "switch", value = "/switch-backend", method = RequestMethod.POST) |
178 |
| - public String switchBackEndInstance(@ModelAttribute("index") Index index, Model model) { |
179 |
| - this.index.setIndex(index.getIndex()); |
180 |
| - setBackEndProperties(index); |
181 |
| - information.set(0, getBackEndProperties()); |
182 |
| - return "redirect:/"; |
183 |
| - } |
184 |
| - |
185 |
| - @RequestMapping(params = "delete", value = "/switch-backend", method = RequestMethod.POST) |
186 |
| - public String deleteBackEndInstance(@ModelAttribute("index") Index index, Model model) { |
187 |
| - information.remove(index.getIndex()); |
188 |
| - writeIntoFile(); |
189 |
| - wrapper.setBackEndInformation(information); |
190 |
| - model.addAttribute("listWrapper", wrapper); |
191 |
| - model.addAttribute("index", index); |
192 |
| - return "switch-backend.html"; |
193 |
| - } |
194 |
| - |
195 |
| - @RequestMapping(value = "/add-instances", method = RequestMethod.GET) |
| 177 | + @RequestMapping("/add-instances.html") |
196 | 178 | public String addInstance(Model model) {
|
197 |
| - model.addAttribute("backendinformation", new BackEndInformation()); |
198 | 179 | return "add-instances";
|
199 | 180 | }
|
200 | 181 |
|
| 182 | + @RequestMapping(value = "/switch-backend", method = RequestMethod.POST) |
| 183 | + public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) { |
| 184 | + try { |
| 185 | + String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 186 | + instances = new JSONArray(body); |
| 187 | + LOG.info(instances.toString()); |
| 188 | +// for(int i = 0; i < instances.length(); i++) { |
| 189 | +// if(instances.getJSONObject(i).get("checked").equals(true) { |
| 190 | +// instances.getJSONObject(i); |
| 191 | +// break; |
| 192 | +// } |
| 193 | +// } |
| 194 | + return new ResponseEntity<>(HttpStatus.OK); |
| 195 | + } catch (Exception e) { |
| 196 | + return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR); |
| 197 | + } |
| 198 | + } |
| 199 | + |
201 | 200 | @RequestMapping(value = "/add-instances", method = RequestMethod.POST)
|
202 |
| - public String addInstanceInformation(@Valid @ModelAttribute(value = "backendinformation") BackEndInformation backEndInfo, BindingResult bindingResult) { |
203 |
| - if (bindingResult.hasErrors()) { |
204 |
| - return "/add-instances"; |
205 |
| - } else { |
206 |
| - information.add(backEndInfo); |
207 |
| - if (!checkIfInstanceAlreadyExist(backEndInfo)) { |
208 |
| - writeIntoFile(); |
| 201 | + public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) { |
| 202 | + try { |
| 203 | + String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 204 | + JSONObject instance = new JSONObject(body); |
| 205 | + if(!checkIfInstanceAlreadyExist(instance)) { |
| 206 | + instance.put("checked", false); |
| 207 | + instances.put(instance); |
| 208 | + LOG.info(instances.toString()); |
| 209 | + return new ResponseEntity<>(HttpStatus.OK); |
| 210 | + } else { |
| 211 | + return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST); |
209 | 212 | }
|
| 213 | + } catch (Exception e) { |
| 214 | + return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR); |
210 | 215 | }
|
211 |
| - return "/switch-backend"; |
212 | 216 | }
|
213 | 217 |
|
214 |
| - private BackEndInformation getBackEndProperties() { |
215 |
| - BackEndInformation backEndInformationFromProperties = new BackEndInformation(); |
216 |
| - backEndInformationFromProperties.setHost(host); |
217 |
| - backEndInformationFromProperties.setPort(port); |
218 |
| - backEndInformationFromProperties.setPath(path); |
219 |
| - backEndInformationFromProperties.setHttps(https); |
220 |
| - return backEndInformationFromProperties; |
| 218 | + @RequestMapping(value = "/get-instances", method = RequestMethod.GET) |
| 219 | + public ResponseEntity<String> getInstances(Model model) { |
| 220 | + return new ResponseEntity<>(instances.toString(), HttpStatus.OK); |
221 | 221 | }
|
222 | 222 |
|
223 |
| - private void setBackEndProperties(Index index) { |
| 223 | + private JSONObject getCurrentInstance() { |
| 224 | + JSONObject instance = new JSONObject(); |
| 225 | + instance.put("name", "core"); |
| 226 | + instance.put("host", host); |
| 227 | + instance.put("port", port); |
| 228 | + instance.put("path", path); |
| 229 | + instance.put("https", https); |
| 230 | + instance.put("checked", true); |
| 231 | + return instance; |
| 232 | + } |
| 233 | + |
| 234 | + private void setBackEndProperties(JSONObject instance) { |
224 | 235 | backEndInformation.setName(information.get(index.getIndex()).getName());
|
225 | 236 | backEndInformation.setHost(information.get(index.getIndex()).getHost());
|
226 | 237 | backEndInformation.setPort(information.get(index.getIndex()).getPort());
|
227 | 238 | backEndInformation.setPath(information.get(index.getIndex()).getPath());
|
228 | 239 | backEndInformation.setHttps(information.get(index.getIndex()).isHttps());
|
229 | 240 | }
|
230 | 241 |
|
231 |
| - private boolean checkIfInstanceAlreadyExist(BackEndInformation backEndInformation) { |
232 |
| - return backEndInformation.getHost().equals(host) && backEndInformation.getPort() == port; |
| 242 | + private boolean checkIfInstanceAlreadyExist(JSONObject instance) { |
| 243 | + for(int i = 0; i < instances.length(); i++) { |
| 244 | + if(instances.getJSONObject(i).get("host").equals(instance.get("host")) && |
| 245 | + instances.getJSONObject(i).get("port").equals(instance.get("port"))) { |
| 246 | + return true; |
| 247 | + } |
| 248 | + } |
| 249 | + return false; |
233 | 250 | }
|
234 | 251 |
|
235 | 252 | private void writeIntoFile() {
|
|
0 commit comments