Skip to content

Commit 5ac85dc

Browse files
author
eznedan
committed
Fixes
1 parent b7d85a5 commit 5ac85dc

File tree

9 files changed

+234
-208
lines changed

9 files changed

+234
-208
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
@RequestMapping
29+
public interface BackEndInformationController {
30+
31+
@RequestMapping(value = "/get-instances", method = RequestMethod.GET)
32+
ResponseEntity<String> getInstances(Model model);
33+
34+
@RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
35+
ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request);
36+
37+
@RequestMapping(value = "/switch-backend", method = RequestMethod.DELETE)
38+
ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request);
39+
40+
@RequestMapping(value = "/add-instances", method = RequestMethod.POST)
41+
ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request);
42+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 org.json.JSONArray;
22+
import org.json.JSONObject;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.ResponseEntity;
26+
import org.springframework.stereotype.Component;
27+
import org.springframework.ui.Model;
28+
29+
import javax.servlet.http.HttpServletRequest;
30+
import java.util.stream.Collectors;
31+
32+
@Component
33+
public class BackEndInformationControllerImpl implements BackEndInformationController {
34+
35+
@Autowired
36+
private BackEndInformation backEndInformation;
37+
38+
@Autowired
39+
private BackEndInstancesUtils utils;
40+
41+
public ResponseEntity<String> getInstances(Model model) {
42+
return new ResponseEntity<>(utils.getInstances().toString(), HttpStatus.OK);
43+
}
44+
45+
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
46+
try {
47+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
48+
utils.setInstances(new JSONArray(body));
49+
utils.writeIntoFile();
50+
utils.parseBackEndInstancesFile();
51+
for (BackEndInformation backEndInformation : utils.getInformation()) {
52+
if (backEndInformation.isChecked()) {
53+
utils.setBackEndProperties(backEndInformation);
54+
}
55+
}
56+
return new ResponseEntity<>(HttpStatus.OK);
57+
} catch (Exception e) {
58+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
59+
}
60+
}
61+
62+
public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) {
63+
try {
64+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
65+
utils.setInstances(new JSONArray(body));
66+
utils.writeIntoFile();
67+
utils.parseBackEndInstancesFile();
68+
return new ResponseEntity<>(HttpStatus.OK);
69+
} catch (Exception e) {
70+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
71+
}
72+
}
73+
74+
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
75+
try {
76+
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
77+
JSONObject instance = new JSONObject(body);
78+
if (!utils.checkIfInstanceAlreadyExist(instance)) {
79+
instance.put("checked", false);
80+
utils.getInstances().put(instance);
81+
utils.writeIntoFile();
82+
return new ResponseEntity<>(HttpStatus.OK);
83+
} else {
84+
return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST);
85+
}
86+
} catch (Exception e) {
87+
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
88+
}
89+
}
90+
}

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

Lines changed: 21 additions & 66 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/*", 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,11 +79,10 @@ 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
126-
@RequestMapping(value = { "/subscriptions", "/subscriptions/*", "/information",
127-
"/download/subscriptiontemplate" }, method = RequestMethod.GET)
84+
@RequestMapping(value = {"/subscriptions", "/subscriptions/*", "/information",
85+
"/download/subscriptiontemplate"}, method = RequestMethod.GET)
12886
public ResponseEntity<String> getRequests(Model model, HttpServletRequest request) {
12987
String eiBackendAddressSuffix = request.getServletPath();
13088
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
@@ -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) {

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
@Controller
3838
public class WebController {
3939

40-
private static final Logger LOG = LoggerFactory.getLogger(WebController.class);
41-
4240
@Value("${ei.frontendServiceHost}")
4341
private String frontendServiceHost;
4442

@@ -54,9 +52,6 @@ public class WebController {
5452
@Autowired
5553
private BackEndInformation backEndInformation;
5654

57-
@Autowired
58-
private BackEndInstancesUtils utils;
59-
6055
@RequestMapping("/")
6156
public String greeting(Model model) {
6257
String eiffelDocumentationUrlLinks = String.format("%s", eiffelDocumentationUrls);
@@ -124,58 +119,4 @@ public String addInstance(Model model) {
124119
public String switchBackEnd(Model model) {
125120
return "switch-backend";
126121
}
127-
128-
@RequestMapping(value = "/get-instances", method = RequestMethod.GET)
129-
public ResponseEntity<String> getInstances(Model model) {
130-
return new ResponseEntity<>(utils.getInstances().toString(), HttpStatus.OK);
131-
}
132-
133-
@RequestMapping(value = "/switch-backend", method = RequestMethod.POST)
134-
public ResponseEntity<String> switchBackEndInstance(Model model, HttpServletRequest request) {
135-
try {
136-
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
137-
utils.setInstances(new JSONArray(body));
138-
utils.writeIntoFile();
139-
utils.parseBackEndInstancesFile();
140-
for (BackEndInformation backEndInformation : utils.getInformation()) {
141-
if (backEndInformation.isChecked()) {
142-
utils.setBackEndProperties(backEndInformation);
143-
}
144-
}
145-
return new ResponseEntity<>(HttpStatus.OK);
146-
} catch (Exception e) {
147-
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
148-
}
149-
}
150-
151-
@RequestMapping(value = "/switch-backend", method = RequestMethod.DELETE)
152-
public ResponseEntity<String> deleteBackEndInstance(Model model, HttpServletRequest request) {
153-
try {
154-
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
155-
utils.setInstances(new JSONArray(body));
156-
utils.writeIntoFile();
157-
utils.parseBackEndInstancesFile();
158-
return new ResponseEntity<>(HttpStatus.OK);
159-
} catch (Exception e) {
160-
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
161-
}
162-
}
163-
164-
@RequestMapping(value = "/add-instances", method = RequestMethod.POST)
165-
public ResponseEntity<String> addInstanceInformation(Model model, HttpServletRequest request) {
166-
try {
167-
String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
168-
JSONObject instance = new JSONObject(body);
169-
if (!utils.checkIfInstanceAlreadyExist(instance)) {
170-
instance.put("checked", false);
171-
utils.getInstances().put(instance);
172-
utils.writeIntoFile();
173-
return new ResponseEntity<>(HttpStatus.OK);
174-
} else {
175-
return new ResponseEntity<>("Instance already exist", HttpStatus.BAD_REQUEST);
176-
}
177-
} catch (Exception e) {
178-
return new ResponseEntity<>("Internal error", HttpStatus.INTERNAL_SERVER_ERROR);
179-
}
180-
}
181122
}

0 commit comments

Comments
 (0)