Skip to content

Commit 5a2f0e3

Browse files
author
eznedan
committed
ADD: write to file
1 parent 815ed8a commit 5a2f0e3

File tree

9 files changed

+145
-169
lines changed

9 files changed

+145
-169
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<dependency>
5858
<groupId>org.projectlombok</groupId>
5959
<artifactId>lombok</artifactId>
60-
<version>1.16.16</version>
60+
<version>1.16.20</version>
6161
<scope>provided</scope>
6262
</dependency>
6363
<dependency>

src/main/java/com/ericsson/ei/frontend/WebController.java

Lines changed: 14 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package com.ericsson.ei.frontend;
1818

1919
import com.ericsson.ei.frontend.model.BackEndInformation;
20-
import com.ericsson.ei.frontend.model.Index;
21-
import com.ericsson.ei.frontend.model.ListWrapper;
22-
import com.fasterxml.jackson.core.JsonGenerator;
23-
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
2421
import org.json.JSONArray;
2522
import org.json.JSONObject;
2623
import org.slf4j.Logger;
@@ -34,14 +31,7 @@
3431
import org.springframework.web.bind.annotation.RequestMapping;
3532
import org.springframework.web.bind.annotation.RequestMethod;
3633

37-
import javax.annotation.PostConstruct;
3834
import javax.servlet.http.HttpServletRequest;
39-
import java.io.FileOutputStream;
40-
import java.io.IOException;
41-
import java.nio.file.Files;
42-
import java.nio.file.Paths;
43-
import java.util.ArrayList;
44-
import java.util.List;
4535
import java.util.stream.Collectors;
4636

4737
@Controller
@@ -61,55 +51,11 @@ public class WebController {
6151
@Value("${ei.eiffelDocumentationUrls}")
6252
private String eiffelDocumentationUrls;
6353

64-
@Value("${ei.backendInstancesPath}")
65-
private String eiInstancesPath;
66-
67-
@Value("${ei.backendServerHost}")
68-
private String host;
69-
70-
@Value("${ei.backendServerPort}")
71-
private int port;
72-
73-
@Value("${ei.backendContextPath}")
74-
private String path;
75-
76-
@Value("${ei.useSecureHttp}")
77-
private boolean https;
78-
7954
@Autowired
8055
private BackEndInformation backEndInformation;
8156

8257
@Autowired
83-
private ListWrapper wrapper;
84-
85-
@Autowired
86-
private Index index;
87-
88-
private List<BackEndInformation> information = new ArrayList<>();
89-
90-
private JSONArray instances = new JSONArray();
91-
92-
@PostConstruct
93-
public void init() {
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();
112-
}
58+
private BackEndInstancesUtils utils;
11359

11460
@RequestMapping("/")
11561
public String greeting(Model model) {
@@ -183,14 +129,14 @@ public String addInstance(Model model) {
183129
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
184130
try {
185131
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-
// }
132+
utils.setInstances(new JSONArray(body));
133+
utils.writeIntoFile();
134+
utils.parseBackEndInstancesFile();
135+
for (BackEndInformation backEndInformation : utils.getInformation()) {
136+
if (backEndInformation.isChecked()) {
137+
utils.setBackEndProperties(backEndInformation);
138+
}
139+
}
194140
return new ResponseEntity<>(HttpStatus.OK);
195141
} catch (Exception e) {
196142
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
@@ -202,10 +148,10 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
202148
try {
203149
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
204150
JSONObject instance = new JSONObject(body);
205-
if(!checkIfInstanceAlreadyExist(instance)) {
151+
if (!utils.checkIfInstanceAlreadyExist(instance)) {
206152
instance.put("checked", false);
207-
instances.put(instance);
208-
LOG.info(instances.toString());
153+
utils.getInstances().put(instance);
154+
utils.writeIntoFile();
209155
return new ResponseEntity<>(HttpStatus.OK);
210156
} else {
211157
return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST);
@@ -217,46 +163,6 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
217163

218164
@RequestMapping(value = "/get-instances", method = RequestMethod.GET)
219165
public ResponseEntity<String> getInstances(Model model) {
220-
return new ResponseEntity<>(instances.toString(), HttpStatus.OK);
221-
}
222-
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) {
235-
backEndInformation.setName(information.get(index.getIndex()).getName());
236-
backEndInformation.setHost(information.get(index.getIndex()).getHost());
237-
backEndInformation.setPort(information.get(index.getIndex()).getPort());
238-
backEndInformation.setPath(information.get(index.getIndex()).getPath());
239-
backEndInformation.setHttps(information.get(index.getIndex()).isHttps());
240-
}
241-
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;
250-
}
251-
252-
private void writeIntoFile() {
253-
if (eiInstancesPath != null) {
254-
ObjectMapper mapper = new ObjectMapper();
255-
try (JsonGenerator add = mapper.getFactory().createGenerator(new FileOutputStream(eiInstancesPath))) {
256-
mapper.writeValue(add, information);
257-
} catch (IOException e) {
258-
LOG.error("Couldn't add instance to file " + e.getMessage());
259-
}
260-
}
166+
return new ResponseEntity<>(utils.getInstances().toString(), HttpStatus.OK);
261167
}
262168
}

src/main/java/com/ericsson/ei/frontend/model/BackEndInformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ericsson.ei.frontend.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import lombok.*;
56
import org.hibernate.validator.constraints.NotBlank;
@@ -36,4 +37,7 @@ public class BackEndInformation {
3637
@JsonProperty("https")
3738
@Value("${ei.useSecureHttp}")
3839
private boolean https;
40+
41+
@JsonIgnore
42+
private boolean checked;
3943
}

src/main/java/com/ericsson/ei/frontend/model/Index.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/main/java/com/ericsson/ei/frontend/model/ListWrapper.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.ericsson.ei.frontend.utils;
2+
3+
import com.ericsson.ei.frontend.model.BackEndInformation;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.stereotype.Component;
14+
15+
import javax.annotation.PostConstruct;
16+
import java.io.FileWriter;
17+
import java.io.IOException;
18+
import java.nio.file.Files;
19+
import java.nio.file.Paths;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
@Getter
24+
@Setter
25+
@Component
26+
public class BackEndInstancesUtils {
27+
28+
private static final Logger LOG = LoggerFactory.getLogger(BackEndInstancesUtils.class);
29+
30+
@Value("${ei.backendServerHost}")
31+
private String host;
32+
33+
@Value("${ei.backendServerPort}")
34+
private int port;
35+
36+
@Value("${ei.backendContextPath}")
37+
private String path;
38+
39+
@Value("${ei.useSecureHttp}")
40+
private boolean https;
41+
42+
@Value("${ei.backendInstancesPath}")
43+
private String eiInstancesPath;
44+
45+
@Autowired
46+
private BackEndInformation backEndInformation;
47+
48+
private List<BackEndInformation> information = new ArrayList<>();
49+
private JSONArray instances = new JSONArray();
50+
51+
@PostConstruct
52+
public void init() {
53+
parseBackEndInstancesFile();
54+
if (!checkIfInstanceAlreadyExist(getCurrentInstance())) {
55+
instances.put(getCurrentInstance());
56+
}
57+
writeIntoFile();
58+
}
59+
60+
private JSONObject getCurrentInstance() {
61+
JSONObject instance = new JSONObject();
62+
instance.put("name", "core");
63+
instance.put("host", host);
64+
instance.put("port", port);
65+
instance.put("path", path);
66+
instance.put("https", https);
67+
instance.put("checked", true);
68+
return instance;
69+
}
70+
71+
public void setBackEndProperties(BackEndInformation properties) {
72+
backEndInformation.setName(properties.getName());
73+
backEndInformation.setHost(properties.getHost());
74+
backEndInformation.setPort(properties.getPort());
75+
backEndInformation.setPath(properties.getPath());
76+
backEndInformation.setHttps(properties.isHttps());
77+
}
78+
79+
public boolean checkIfInstanceAlreadyExist(JSONObject instance) {
80+
for (int i = 0; i < instances.length(); i++) {
81+
if (instances.getJSONObject(i).get("host").equals(instance.get("host")) &&
82+
instances.getJSONObject(i).get("port").equals(instance.get("port")) &&
83+
instances.getJSONObject(i).get("path").equals(instance.get("path"))) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
89+
90+
public void writeIntoFile() {
91+
if (eiInstancesPath != null) {
92+
try {
93+
FileWriter fileWriter = new FileWriter(eiInstancesPath);
94+
fileWriter.append(instances.toString());
95+
fileWriter.flush();
96+
} catch (IOException e) {
97+
LOG.error("Couldn't add instance to file " + e.getMessage());
98+
}
99+
}
100+
}
101+
102+
public void parseBackEndInstancesFile() {
103+
if (eiInstancesPath != null) {
104+
try {
105+
JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath))));
106+
for (Object o : inputBackEndInstances) {
107+
JSONObject instance = (JSONObject) o;
108+
if (!checkIfInstanceAlreadyExist(instance)) {
109+
information.add(new ObjectMapper().readValue(instance.toString(), BackEndInformation.class));
110+
instances.put(instance);
111+
}
112+
}
113+
} catch (IOException e) {
114+
LOG.error("Failure when try to parse json file" + e.getMessage());
115+
}
116+
}
117+
}
118+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"name":null,"host":"localhost","port":8090,"path":"","https":false},{"name":"source","host":"host","port":3030,"path":"","https":true},{"name":"bnm","host":"bnm","port":0,"path":"","https":false},{"name":"yio","host":"yui","port":0,"path":"","https":false},{"name":"uio","host":"uiouio","port":0,"path":"","https":false},{"name":"yjgmhkh","host":"jhjmhgj","port":0,"path":"","https":false}]
1+
[{"path":"","port":8090,"name":"core","host":"localhost","checked":true,"https":false},{"path":"","port":8098,"name":"test","host":"localhost23","checked":false,"https":false},{"path":"","port":"567","name":"hgjghj","host":"ghjghj","checked":false,"https":false}]

src/main/resources/static/js/switch-instances.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,4 @@ jQuery(document).ready(function() {
5757

5858
}
5959
})
60-
61-
var selectedBox = null;
62-
$(".activeInstance").click(function() {
63-
selectedBox = this.id;
64-
65-
$(".activeInstance").each(function() {
66-
if ( this.id == selectedBox )
67-
{
68-
this.checked = true;
69-
}
70-
else
71-
{
72-
this.checked = false;
73-
};
74-
});
75-
});
76-
77-
// $("#activeInstance").change(function() {
78-
// $("#activeInstance").prop('checked', false);
79-
// $(this).prop('checked', true);
80-
// });
81-
8260
});

0 commit comments

Comments
 (0)