|
16 | 16 | */
|
17 | 17 | package com.ericsson.ei.frontend;
|
18 | 18 |
|
| 19 | +import com.ericsson.ei.frontend.model.BackEndInformation; |
| 20 | +import com.ericsson.ei.frontend.utils.BackEndInstancesUtils; |
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.JsonArray; |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import com.google.gson.JsonParser; |
| 25 | +import com.google.gson.reflect.TypeToken; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.http.HttpHeaders; |
| 28 | +import org.springframework.http.HttpStatus; |
| 29 | +import org.springframework.http.MediaType; |
19 | 30 | import org.springframework.http.ResponseEntity;
|
| 31 | +import org.springframework.stereotype.Controller; |
20 | 32 | import org.springframework.ui.Model;
|
21 |
| -import org.springframework.web.bind.annotation.CrossOrigin; |
22 | 33 | import org.springframework.web.bind.annotation.RequestMapping;
|
23 | 34 | import org.springframework.web.bind.annotation.RequestMethod;
|
24 |
| -import org.springframework.web.bind.annotation.RestController; |
25 | 35 |
|
26 | 36 | import javax.servlet.http.HttpServletRequest;
|
| 37 | +import java.net.URI; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.stream.Collectors; |
27 | 41 |
|
28 |
| -@RestController |
29 |
| -public interface BackEndInformationController { |
| 42 | +@Controller |
| 43 | +public class BackEndInformationController { |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private BackEndInstancesUtils backEndInstancesUtils; |
30 | 47 |
|
31 |
| - @CrossOrigin |
32 | 48 | @RequestMapping(value = "/get-instances", method = RequestMethod.GET)
|
33 |
| - ResponseEntity<String> getInstances(Model model); |
| 49 | + public ResponseEntity<String> getInstances(Model model) { |
| 50 | + return new ResponseEntity<>(backEndInstancesUtils.getInstances().toString(), getHeaders(), HttpStatus.OK); |
| 51 | + } |
34 | 52 |
|
35 |
| - @CrossOrigin |
36 | 53 | @RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
|
37 |
| - ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request); |
38 |
| - |
39 |
| - @CrossOrigin |
40 |
| - @RequestMapping(value = "/switch-backendByMainPage", method = RequestMethod.POST) |
41 |
| - ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request); |
| 54 | + public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) { |
| 55 | + try { |
| 56 | + String listOfInstances = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 57 | + backEndInstancesUtils.setInstances(new JsonParser().parse(listOfInstances).getAsJsonArray()); |
| 58 | + backEndInstancesUtils.writeIntoFile(); |
| 59 | + for (BackEndInformation backEndInformation : backEndInstancesUtils.getInformation()) { |
| 60 | + if (backEndInformation.isActive()) { |
| 61 | + backEndInstancesUtils.setBackEndProperties(backEndInformation); |
| 62 | + } |
| 63 | + } |
| 64 | + return new ResponseEntity<>(getHeaders(), HttpStatus.MOVED_PERMANENTLY); |
| 65 | + } catch (Exception e) { |
| 66 | + return new ResponseEntity<>("Internal error" + e.getMessage(), getHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); |
| 67 | + } |
| 68 | + } |
42 | 69 |
|
43 |
| - @CrossOrigin |
44 | 70 | @RequestMapping(value = "/switch-backend", method = RequestMethod.DELETE)
|
45 |
| - ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request); |
| 71 | + public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) { |
| 72 | + try { |
| 73 | + String nameOfDeletedInstance = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 74 | + backEndInstancesUtils.setInstances(new JsonParser().parse(nameOfDeletedInstance).getAsJsonArray()); |
| 75 | + backEndInstancesUtils.writeIntoFile(); |
| 76 | + return new ResponseEntity<>("Backend instance was deleted", getHeaders(), HttpStatus.OK); |
| 77 | + } catch (Exception e) { |
| 78 | + return new ResponseEntity<>("Internal error" + e.getMessage(), getHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); |
| 79 | + } |
| 80 | + } |
46 | 81 |
|
47 |
| - @CrossOrigin |
48 | 82 | @RequestMapping(value = "/add-instances", method = RequestMethod.POST)
|
49 |
| - ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request); |
| 83 | + public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) { |
| 84 | + try { |
| 85 | + String nameOfNewInstance = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 86 | + JsonObject instance = new JsonParser().parse(nameOfNewInstance).getAsJsonObject(); |
| 87 | + if (!backEndInstancesUtils.checkIfInstanceAlreadyExist(instance)) { |
| 88 | + backEndInstancesUtils.getInstances().add(instance); |
| 89 | + backEndInstancesUtils.writeIntoFile(); |
| 90 | + return new ResponseEntity<>(getHeaders(), HttpStatus.MOVED_PERMANENTLY); |
| 91 | + } else { |
| 92 | + return new ResponseEntity<>("Instance already exist", getHeaders(), HttpStatus.BAD_REQUEST); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + return new ResponseEntity<>("Internal error" + e.getMessage(), getHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @RequestMapping(value = "/switchBackend", method = RequestMethod.POST) |
| 100 | + public ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request) { |
| 101 | + try { |
| 102 | + String backEndName = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); |
| 103 | + List<BackEndInformation> info = new ArrayList<>(); |
| 104 | + for (BackEndInformation backEndInformation : backEndInstancesUtils.getInformation()) { |
| 105 | + backEndInformation.setActive(false); |
| 106 | + if (backEndInformation.getName().equals(backEndName)) { |
| 107 | + backEndInstancesUtils.setBackEndProperties(backEndInformation); |
| 108 | + backEndInformation.setActive(true); |
| 109 | + } |
| 110 | + info.add(backEndInformation); |
| 111 | + } |
| 112 | + backEndInstancesUtils.setInformation(info); |
| 113 | + JsonArray result = (JsonArray) new Gson().toJsonTree(backEndInstancesUtils.getInformation(), new TypeToken<List<BackEndInformation>>() { |
| 114 | + }.getType()); |
| 115 | + backEndInstancesUtils.setInstances(result); |
| 116 | + backEndInstancesUtils.writeIntoFile(); |
| 117 | + return new ResponseEntity<>("Backend instance was switched", getHeaders(), HttpStatus.OK); |
| 118 | + } catch (Exception e) { |
| 119 | + return new ResponseEntity<>("Internal error" + e.getMessage(), getHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private HttpHeaders getHeaders() { |
| 124 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 125 | + httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 126 | + httpHeaders.setLocation(URI.create("/")); |
| 127 | + return httpHeaders; |
| 128 | + } |
50 | 129 | }
|
0 commit comments