Skip to content

Commit 133337f

Browse files
authored
Merge pull request #25 from ned29/instances
ADD: handling several EI instances
2 parents 46c1fb3 + 598e9cd commit 133337f

16 files changed

+750
-144
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
<artifactId>spring-boot-starter-tomcat</artifactId>
5555
<scope>compile</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.projectlombok</groupId>
59+
<artifactId>lombok</artifactId>
60+
<version>1.16.20</version>
61+
<scope>provided</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.code.gson</groupId>
65+
<artifactId>gson</artifactId>
66+
<version>2.8.4</version>
67+
</dependency>
68+
5769
</dependencies>
5870
<build>
5971
<plugins>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2018 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.frontend;
18+
19+
import org.springframework.http.ResponseEntity;
20+
import org.springframework.ui.Model;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestMethod;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
import javax.servlet.http.HttpServletRequest;
26+
27+
@RestController
28+
public interface BackEndInformationController {
29+
30+
@RequestMapping(value = "/get-instances", method = RequestMethod.GET)
31+
ResponseEntity<String> getInstances(Model model);
32+
33+
@RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
34+
ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request);
35+
36+
@RequestMapping(value = "/switch-backendByMainPage", method = RequestMethod.POST)
37+
ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request);
38+
39+
@RequestMapping(value = "/switch-backend", method = RequestMethod.DELETE)
40+
ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request);
41+
42+
@RequestMapping(value = "/add-instances", method = RequestMethod.POST)
43+
ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request);
44+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright 2018 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.frontend;
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.HttpStatus;
28+
import org.springframework.http.ResponseEntity;
29+
import org.springframework.stereotype.Component;
30+
import org.springframework.ui.Model;
31+
32+
import javax.servlet.http.HttpServletRequest;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
import java.util.stream.Collectors;
36+
37+
@Component
38+
public class BackEndInformationControllerImpl implements BackEndInformationController {
39+
40+
@Autowired
41+
private BackEndInformation backEndInformation;
42+
43+
@Autowired
44+
private BackEndInstancesUtils utils;
45+
46+
public ResponseEntity<String> getInstances(Model model) {
47+
return new ResponseEntity<>(utils.getInstances().toString(), HttpStatus.OK);
48+
}
49+
50+
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
51+
try {
52+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
53+
utils.setInstances(new JsonParser().parse(body).getAsJsonArray());
54+
utils.writeIntoFile();
55+
utils.parseBackEndInstancesFile();
56+
for (BackEndInformation backEndInformation : utils.getInformation()) {
57+
if (backEndInformation.isActive()) {
58+
utils.setBackEndProperties(backEndInformation);
59+
}
60+
}
61+
return new ResponseEntity<>(HttpStatus.OK);
62+
} catch (Exception e) {
63+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
64+
}
65+
}
66+
67+
public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) {
68+
try {
69+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
70+
utils.setInstances(new JsonParser().parse(body).getAsJsonArray());
71+
utils.writeIntoFile();
72+
return new ResponseEntity<>(HttpStatus.OK);
73+
} catch (Exception e) {
74+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
75+
}
76+
}
77+
78+
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
79+
try {
80+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
81+
JsonObject instance = new JsonParser().parse(body).getAsJsonObject();
82+
if (!utils.checkIfInstanceAlreadyExist(instance)) {
83+
instance.addProperty("default", false);
84+
utils.getInstances().add(instance);
85+
utils.writeIntoFile();
86+
return new ResponseEntity<>(HttpStatus.OK);
87+
} else {
88+
return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST);
89+
}
90+
} catch (Exception e) {
91+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
92+
}
93+
}
94+
95+
public ResponseEntity<String> switchBackEndInstanceByMainPage(Model model, HttpServletRequest request) {
96+
try {
97+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
98+
List<BackEndInformation> info = new ArrayList<>();
99+
for (BackEndInformation backEndInformation : utils.getInformation()) {
100+
backEndInformation.setActive(false);
101+
if (backEndInformation.getName().equals(body)) {
102+
utils.setBackEndProperties(backEndInformation);
103+
backEndInformation.setActive(true);
104+
}
105+
info.add(backEndInformation);
106+
}
107+
utils.setInformation(info);
108+
JsonArray result = (JsonArray) new Gson().toJsonTree(utils.getInformation(), new TypeToken<List<BackEndInformation>>() {
109+
}.getType());
110+
utils.setInstances(result);
111+
utils.writeIntoFile();
112+
utils.parseBackEndInstancesFile();
113+
return new ResponseEntity<>(HttpStatus.OK);
114+
} catch (Exception e) {
115+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
116+
}
117+
}
118+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
16-
*/
16+
*/
1717
package com.ericsson.ei.frontend;
1818

1919
import java.util.ArrayList;

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

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/*
22
Copyright 2017 Ericsson AB.
33
For a full list of individual contributors, please see the commit history.
4-
54
Licensed under the Apache License, Version 2.0 (the "License");
65
you may not use this file except in compliance with the License.
76
You may obtain a copy of the License at
8-
97
http://www.apache.org/licenses/LICENSE-2.0
10-
118
Unless required by applicable law or agreed to in writing, software
129
distributed under the License is distributed on an "AS IS" BASIS,
1310
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,21 +13,15 @@
1613
*/
1714
package com.ericsson.ei.frontend;
1815

19-
import java.io.BufferedReader;
20-
import java.io.IOException;
21-
import java.io.InputStream;
22-
import java.io.InputStreamReader;
23-
24-
import javax.servlet.http.HttpServletRequest;
25-
16+
import com.ericsson.ei.frontend.model.BackEndInformation;
2617
import org.apache.http.HttpEntity;
2718
import org.apache.http.client.methods.*;
2819
import org.apache.http.entity.ByteArrayEntity;
2920
import org.apache.http.impl.client.CloseableHttpClient;
3021
import org.apache.http.impl.client.HttpClientBuilder;
3122
import org.slf4j.Logger;
3223
import org.slf4j.LoggerFactory;
33-
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.beans.factory.annotation.Autowired;
3425
import org.springframework.http.HttpHeaders;
3526
import org.springframework.http.HttpStatus;
3627
import org.springframework.http.MediaType;
@@ -41,64 +32,32 @@
4132
import org.springframework.web.bind.annotation.RequestMethod;
4233
import org.springframework.web.bind.annotation.RestController;
4334

35+
import javax.servlet.http.HttpServletRequest;
36+
import java.io.BufferedReader;
37+
import java.io.IOException;
38+
import java.io.InputStream;
39+
import java.io.InputStreamReader;
40+
4441
@RestController
45-
@ConfigurationProperties(prefix = "ei")
4642
public class EIRequestsController {
4743

4844
private static final Logger LOG = LoggerFactory.getLogger(EIRequestsController.class);
4945

5046
private CloseableHttpClient client = HttpClientBuilder.create().build();
5147

52-
private String backendServerHost;
53-
private int backendServerPort;
54-
private String backendContextPath;
55-
private boolean useSecureHttp;
56-
57-
// Backend host and port (Getter & Setters), application.properties ->
58-
// greeting.xxx
59-
public String getBackendServerHost() {
60-
return backendServerHost;
61-
}
62-
63-
public void setBackendServerHost(String backendServerHost) {
64-
this.backendServerHost = backendServerHost;
65-
}
66-
67-
public int getBackendServerPort() {
68-
return backendServerPort;
69-
}
70-
71-
public void setBackendServerPort(int backendServerPort) {
72-
this.backendServerPort = backendServerPort;
73-
}
74-
75-
public String getBackendContextPath() {
76-
return backendContextPath;
77-
}
78-
79-
public void setBackendContextPath(String backendContextPath) {
80-
this.backendContextPath = backendContextPath;
81-
}
82-
83-
public boolean getUseSecureHttp() {
84-
return useSecureHttp;
85-
}
86-
87-
public void setUseSecureHttp(boolean useSecureHttp) {
88-
this.useSecureHttp = useSecureHttp;
89-
}
48+
@Autowired
49+
private BackEndInformation backEndInformation;
9050

9151
/**
9252
* Bridge authorized EI Http Requests with GET method. Used for login and logout
93-
*
9453
*/
9554
@CrossOrigin
9655
@RequestMapping(value = "/auth/login", method = RequestMethod.GET)
9756
public ResponseEntity<String> getAuthRequests(Model model, HttpServletRequest request) {
9857
String eiBackendAddressSuffix = request.getServletPath();
9958
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
10059
LOG.info("Got HTTP Request with method GET.\nUrlSuffix: " + eiBackendAddressSuffix +
101-
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
60+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
10261

10362
try {
10463
client.close();
@@ -120,7 +79,6 @@ public ResponseEntity<String> getAuthRequests(Model model, HttpServletRequest re
12079
/**
12180
* Bridge all EI Http Requests with GET method. Used for fetching
12281
* Subscription by id or all subscriptions and EI Env Info.
123-
*
12482
*/
12583
@CrossOrigin
12684
@RequestMapping(value = { "/subscriptions", "/subscriptions/*", "/information", "/auth",
@@ -138,10 +96,9 @@ public ResponseEntity<String> getRequests(Model model, HttpServletRequest reques
13896

13997
/**
14098
* Bridge all EI Http Requests with POST method.
141-
*
14299
*/
143100
@CrossOrigin
144-
@RequestMapping(value = { "/subscriptions", "/rules/rule-check/aggregation"}, method = RequestMethod.POST)
101+
@RequestMapping(value = {"/subscriptions", "/rules/rule-check/aggregation"}, method = RequestMethod.POST)
145102
public ResponseEntity<String> postRequests(Model model, HttpServletRequest request) {
146103
String eiBackendAddressSuffix = request.getServletPath();
147104
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
@@ -172,7 +129,6 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
172129
/**
173130
* Bridge all EI Http Requests with PUT method. E.g. Making Update
174131
* Subscription Request.
175-
*
176132
*/
177133
@CrossOrigin
178134
@RequestMapping(value = "/subscriptions", method = RequestMethod.PUT)
@@ -206,7 +162,6 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest reques
206162
/**
207163
* Bridge all EI Http Requests with DELETE method. Used for DELETE
208164
* subscriptions.
209-
*
210165
*/
211166
@CrossOrigin
212167
@RequestMapping(value = "/subscriptions/*", method = RequestMethod.DELETE)
@@ -223,15 +178,15 @@ public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest req
223178

224179
private String getEIBackendSubscriptionAddress() {
225180
String httpMethod = "http";
226-
if (useSecureHttp) {
181+
if (backEndInformation.isHttps()) {
227182
httpMethod = "https";
228183
}
229184

230-
if (backendContextPath != null && !backendContextPath.isEmpty()) {
231-
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort() + "/"
232-
+ backendContextPath;
185+
if (backEndInformation.getPath() != null && !backEndInformation.getPath().isEmpty()) {
186+
return httpMethod + "://" + backEndInformation.getHost() + ":" + backEndInformation.getPort() + "/"
187+
+ backEndInformation.getPath();
233188
}
234-
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort();
189+
return httpMethod + "://" + backEndInformation.getHost() + ":" + backEndInformation.getPort();
235190
}
236191

237192
private ResponseEntity<String> getResponse(HttpRequestBase request) {
@@ -248,8 +203,8 @@ private ResponseEntity<String> getResponse(HttpRequestBase request) {
248203
}
249204
statusCode = eiResponse.getStatusLine().getStatusCode();
250205
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
251-
+ "\nEI Recevied jsonContent:\n" + jsonContent
252-
+ "\nForwarding response back to EI Frontend WebUI.");
206+
+ "\nEI Recevied jsonContent:\n" + jsonContent
207+
+ "\nForwarding response back to EI Frontend WebUI.");
253208
bufReader.close();
254209
inStream.close();
255210
} catch (IOException e) {

0 commit comments

Comments
 (0)