Skip to content

Commit 8d10f3c

Browse files
Added frontendServiceUrl to each ajax request, where it was missed (#30)
1 parent 2f7e329 commit 8d10f3c

File tree

10 files changed

+62
-39
lines changed

10 files changed

+62
-39
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@
3737
import java.io.IOException;
3838
import java.io.InputStream;
3939
import java.io.InputStreamReader;
40+
import java.util.ArrayList;
41+
import java.util.Arrays;
42+
import java.util.List;
4043

4144
@RestController
4245
public class EIRequestsController {
4346

4447
private static final Logger LOG = LoggerFactory.getLogger(EIRequestsController.class);
4548

49+
private static final List<String> REQUSETS_WITH_QUERY_PARAM = new ArrayList<>(Arrays.asList("/queryAggregatedObject", "/queryMissedNotifications", "/query"));
50+
4651
private CloseableHttpClient client = HttpClientBuilder.create().build();
4752

4853
@Autowired
@@ -176,9 +181,14 @@ private String getEIBackendSubscriptionAddress() {
176181

177182
private String getEIRequestURL(HttpServletRequest request) {
178183
String eiBackendAddressSuffix = request.getServletPath();
179-
String requestQuery = request.getQueryString();
180-
String query = (requestQuery != null && !requestQuery.isEmpty()) ? "?" + requestQuery : "";
181-
String requestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix + query;
184+
String requestUrl;
185+
if(REQUSETS_WITH_QUERY_PARAM.contains(eiBackendAddressSuffix)) {
186+
String requestQuery = request.getQueryString();
187+
String query = (requestQuery != null && !requestQuery.isEmpty()) ? "?" + requestQuery : "";
188+
requestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix + query;
189+
} else {
190+
requestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
191+
}
182192
LOG.info("Got HTTP Request with method " + request.getMethod()
183193
+ "\nUrlSuffix: " + eiBackendAddressSuffix
184194
+ "\nForwarding Request to EI Backend with url: " + requestUrl);
@@ -187,7 +197,7 @@ private String getEIRequestURL(HttpServletRequest request) {
187197

188198
private ResponseEntity<String> getResponse(HttpRequestBase request) {
189199
String jsonContent = "";
190-
int statusCode = 102;
200+
int statusCode = HttpStatus.PROCESSING.value();
191201
try (CloseableHttpResponse eiResponse = client.execute(request)) {
192202
InputStream inStream = eiResponse.getEntity().getContent();
193203
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
@@ -204,6 +214,7 @@ private ResponseEntity<String> getResponse(HttpRequestBase request) {
204214
bufReader.close();
205215
inStream.close();
206216
} catch (IOException e) {
217+
statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
207218
LOG.error("Forward Request Errors: " + e);
208219
}
209220

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,35 @@ public class WebController {
4343

4444
@RequestMapping("/")
4545
public String greeting(Model model) {
46+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
4647
String eiffelDocumentationUrlLinks = String.format("%s", eiffelDocumentationUrls);
4748
model.addAttribute("eiffelDocumentationUrlLinks", eiffelDocumentationUrlLinks); // inject in DOM for AJAX etc
4849
return "index";
4950
}
5051

5152
@RequestMapping("/subscriptionpage.html")
5253
public String subscription(Model model) {
53-
String httpMethod = "http";
54-
if (backEndInformation.isHttps()) {
55-
httpMethod = "https";
56-
}
57-
String frontendServiceUrl;
58-
if (frontendContextPath != null && !frontendContextPath.isEmpty()) {
59-
frontendServiceUrl = String.format("%s://%s:%d/%s", httpMethod, frontendServiceHost, frontendServicePort, frontendContextPath);
60-
} else {
61-
frontendServiceUrl = String.format("%s://%s:%d", httpMethod, frontendServiceHost, frontendServicePort);
62-
}
63-
model.addAttribute("frontendServiceUrl", frontendServiceUrl); // inject in DOM for AJAX etc
54+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
6455
return "subscription";
6556
}
6657

6758
@RequestMapping("/testRules.html")
6859
public String testRules(Model model) {
60+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
6961
return "testRules";
7062
}
7163

7264
@RequestMapping("/eiInfo.html")
7365
public String eiInfo(Model model) {
74-
String frontendServiceUrl = String.format("http://%s:%d", frontendServiceHost, frontendServicePort);
75-
model.addAttribute("frontendServiceUrl", frontendServiceUrl); // inject in DOM for AJAX etc
66+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
7667
String backendServerUrl = String.format("http://%s:%d", backEndInformation.getHost(), backEndInformation.getPort());
7768
model.addAttribute("backendServerUrl", backendServerUrl);
7869
return "eiInfo";
7970
}
8071

8172
@RequestMapping("/login.html")
8273
public String login(Model model) {
74+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
8375
return "login";
8476
}
8577

@@ -91,11 +83,28 @@ public String jmesPathRulesSetUp(Model model) {
9183

9284
@RequestMapping("/add-instances.html")
9385
public String addInstance(Model model) {
86+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
9487
return "add-instances";
9588
}
9689

9790
@RequestMapping("/switch-backend.html")
9891
public String switchBackEnd(Model model) {
92+
model.addAttribute("frontendServiceUrl", getFrontendServiceUrl()); // inject in DOM for AJAX etc
9993
return "switch-backend";
10094
}
95+
96+
private String getFrontendServiceUrl() {
97+
String httpMethod = "http";
98+
if (backEndInformation.isHttps()) {
99+
httpMethod = "https";
100+
}
101+
String frontendServiceUrl;
102+
if (frontendContextPath != null && !frontendContextPath.isEmpty()) {
103+
frontendServiceUrl = String.format("%s://%s:%d/%s", httpMethod, frontendServiceHost, frontendServicePort, frontendContextPath);
104+
} else {
105+
frontendServiceUrl = String.format("%s://%s:%d", httpMethod, frontendServiceHost, frontendServicePort);
106+
}
107+
return frontendServiceUrl;
108+
}
109+
101110
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
jQuery(document).ready(function() {
2+
var frontendServiceUrl = $('#frontendServiceUrl').text();
23
function instanceModel() {
34
var self = this;
45
self.instance = {
@@ -15,7 +16,7 @@ function instanceModel() {
1516
$.jGrowl("Host and port fields cannot be empty", {sticky: false, theme: 'Error'});
1617
} else {
1718
$.ajax({
18-
url: "/add-instances",
19+
url: frontendServiceUrl + "/add-instances",
1920
type: "POST",
2021
data: data,
2122
contentType: 'application/json; charset=utf-8',

src/main/resources/static/js/login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
jQuery(document).ready(function() {
3-
3+
var frontendServiceUrl = $('#frontendServiceUrl').text();
44
// /Start ## Knockout ####################################################
55
function loginModel() {
66
this.userState = {
@@ -18,7 +18,7 @@ jQuery(document).ready(function() {
1818
});
1919
} else {
2020
var token = window.btoa(JSON.parse(dataJSON).username + ":" + JSON.parse(dataJSON).password);
21-
sendLoginRequest("/auth/login", "GET", token);
21+
sendLoginRequest(frontendServiceUrl + "/auth/login", "GET", token);
2222
}
2323
}
2424
}

src/main/resources/static/js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jQuery(document).ready(function() {
4949
$("#logoutBtn").click(function() {
5050
$("#selectInstances").visible();
5151
$.ajax({
52-
url : "/auth/logout",
52+
url : frontendServiceUrl + "/auth/logout",
5353
type : "GET",
5454
contentType : 'application/json; charset=utf-8',
5555
cache: false,

src/main/resources/static/js/subscription.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ jQuery(document).ready(function() {
4242

4343
// Check EI Backend Server Status ########################################
4444
var backendStatus = false;
45-
function checkBackendStatus() {
46-
var EIConnBtn = document.getElementById("btnEIConnection");
47-
if (EIConnBtn == null) {
48-
return;
49-
}
50-
var red="#ff0000";
51-
var green="#00ff00";
45+
function checkBackendStatus() {
46+
var EIConnBtn = document.getElementById("btnEIConnection");
47+
if (EIConnBtn == null) {
48+
return;
49+
}
50+
var red="#ff0000";
51+
var green="#00ff00";
5252
$.ajax({
53-
url: "/auth/checkStatus",
53+
url: frontendServiceUrl + "/auth/checkStatus",
5454
contentType: 'application/json; charset=utf-8',
5555
type: 'GET',
5656
error: function (XMLHttpRequest) {
@@ -68,7 +68,7 @@ jQuery(document).ready(function() {
6868
backendStatus = true;
6969
}
7070
});
71-
}
71+
}
7272

7373
function doIfUserLoggedIn() {
7474
var currentUser = localStorage.getItem("currentUser");
@@ -115,14 +115,12 @@ jQuery(document).ready(function() {
115115
this.aggregationtype = ko.observable(data.aggregationtype);
116116

117117
this.notificationType.subscribe(function (new_value) {
118-
vm.delete_BulkNotificationMsgKeyValuePair();
119118
vm.subscription()[0].restPostBodyMediaType(null);
120119
vm.formpostkeyvaluepairs(false);
121120

122121
});
123122

124123
this.restPostBodyMediaType.subscribe(function (new_value) {
125-
vm.delete_BulkNotificationMsgKeyValuePair();
126124
if(new_value=="application/x-www-form-urlencoded"){
127125
vm.formpostkeyvaluepairs(true);
128126
}else{
@@ -256,7 +254,7 @@ jQuery(document).ready(function() {
256254
// Start to check is backend secured
257255
var isSecured = false;
258256
$.ajax({
259-
url: "/auth",
257+
url: frontendServiceUrl + "/auth",
260258
contentType : 'application/json; charset=utf-8',
261259
type: 'GET',
262260
error: function () {},
@@ -592,7 +590,7 @@ jQuery(document).ready(function() {
592590
}
593591
// /Stop ## Reload Datatables ############################################
594592

595-
function get_subscription_data(object, mode) {
593+
function get_subscription_data(object, mode, event) {
596594
event.stopPropagation();
597595
event.preventDefault();
598596
// Fetch datatable row -> subscriptionName
@@ -619,13 +617,13 @@ jQuery(document).ready(function() {
619617

620618
// /Start ## Edit Subscription ###########################################
621619
$('#table').on( 'click', 'tbody tr td button.edit_record', function (event) {
622-
get_subscription_data(this, "edit");
620+
get_subscription_data(this, "edit", event);
623621
});
624622
// /Stop ## Edit Subscription ###########################################
625623

626624
// /Start ## View Subscription ###########################################
627625
$('#table').on( 'click', 'tbody tr td button.view_record', function (event) {
628-
get_subscription_data(this, "view");
626+
get_subscription_data(this, "view", event);
629627
});
630628
// /Stop ## View Subscription ###########################################
631629

src/main/resources/templates/add-instances.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
33
<head>
44
<meta charset="utf-8"/>
55
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
@@ -8,6 +8,7 @@
88
<meta name="author" content=""/>
99
</head>
1010
<body class="bg-dark">
11+
<div class="hidden" style="display: none" id="frontendServiceUrl" th:text="${frontendServiceUrl}"></div>
1112
<div class="container pull-left">
1213
<div class="card card-login mx-auto mt-5">
1314
<div class="card-header">Add backend instance</div>

src/main/resources/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131

3232
<div class="hidden" style="display: none" id="eiffelDocumentationUrlLinks" th:text="${eiffelDocumentationUrlLinks}"></div>
33+
<div class="hidden" style="display: none" id="frontendServiceUrl" th:text="${frontendServiceUrl}"></div>
3334
<!-- Navigation-->
3435
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" id="mainNav">
3536
<a class="navbar-brand" href="/"><img width="32" alt="Information" src="assets/images/favicon-32x32.png"/>Eiffel Intelligence</a>

src/main/resources/templates/login.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
33

44
<head>
55
<meta charset="utf-8"/>
@@ -10,6 +10,7 @@
1010
</head>
1111

1212
<body class="bg-dark">
13+
<div class="hidden" style="display: none" id="frontendServiceUrl" th:text="${frontendServiceUrl}"></div>
1314
<div class="container pull-left">
1415
<div class="card card-login mx-auto mt-5">
1516
<div class="card-header">Login</div>

src/main/resources/templates/switch-backend.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
33
<head>
44
<meta charset="utf-8"/>
55
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
@@ -8,6 +8,7 @@
88
<meta name="author" content=""/>
99
</head>
1010
<body class="bg-dark">
11+
<div class="hidden" style="display: none" id="frontendServiceUrl" th:text="${frontendServiceUrl}"></div>
1112
<div class="container pull-left">
1213
<div class="card card-login mx-auto mt-5">
1314
<div class="card-header">Switch backend</div>

0 commit comments

Comments
 (0)