Skip to content

Commit b7d85a5

Browse files
author
eznedan
committed
Merge branch 'master' of https://github.com/Ericsson/eiffel-intelligence-frontend into instances
2 parents f49a61a + de36481 commit b7d85a5

File tree

7 files changed

+240
-297
lines changed

7 files changed

+240
-297
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>org.springframework.boot</groupId>
1111
<artifactId>spring-boot-starter-parent</artifactId>
12-
<version>1.5.5.RELEASE</version>
12+
<version>2.0.1.RELEASE</version>
1313
<relativePath/> <!-- .. lookup parent from repository -->
1414
</parent>
1515

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.boot.SpringApplication;
2626
import org.springframework.boot.autoconfigure.SpringBootApplication;
2727
import org.springframework.boot.builder.SpringApplicationBuilder;
28-
import org.springframework.boot.web.support.SpringBootServletInitializer;
28+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
2929

3030
@SpringBootApplication
3131
public class EIFrontendApplication extends SpringBootServletInitializer {

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

Lines changed: 99 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@
2323

2424
import javax.servlet.http.HttpServletRequest;
2525

26-
import com.ericsson.ei.frontend.model.BackEndInformation;
2726
import org.apache.http.HttpEntity;
28-
import org.apache.http.HttpResponse;
29-
import org.apache.http.client.HttpClient;
30-
import org.apache.http.client.methods.HttpDelete;
31-
import org.apache.http.client.methods.HttpGet;
32-
import org.apache.http.client.methods.HttpPost;
33-
import org.apache.http.client.methods.HttpPut;
27+
import org.apache.http.client.methods.*;
3428
import org.apache.http.entity.ByteArrayEntity;
35-
import org.apache.http.impl.client.HttpClients;
29+
import org.apache.http.impl.client.CloseableHttpClient;
30+
import org.apache.http.impl.client.HttpClientBuilder;
3631
import org.slf4j.Logger;
3732
import org.slf4j.LoggerFactory;
38-
import org.springframework.beans.factory.annotation.Autowired;
3933
import org.springframework.boot.context.properties.ConfigurationProperties;
4034
import org.springframework.http.HttpHeaders;
4135
import org.springframework.http.HttpStatus;
@@ -48,24 +42,79 @@
4842
import org.springframework.web.bind.annotation.RestController;
4943

5044
@RestController
45+
@ConfigurationProperties(prefix = "ei")
5146
public class EIRequestsController {
5247

5348
private static final Logger LOG = LoggerFactory.getLogger(EIRequestsController.class);
5449

55-
@Autowired
56-
private BackEndInformation backEndInformation;
50+
private CloseableHttpClient client = HttpClientBuilder.create().build();
5751

58-
private String getEIBackendSubscriptionAddress() {
59-
String httpMethod = "http";
60-
if (backEndInformation.isHttps()) {
61-
httpMethod = "https";
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+
}
90+
91+
/**
92+
* Bridge authorized EI Http Requests with GET method. Used for login and logout
93+
*
94+
*/
95+
@CrossOrigin
96+
@RequestMapping(value = "/auth/*", method = RequestMethod.GET)
97+
public ResponseEntity<String> getAuthRequests(Model model, HttpServletRequest request) {
98+
String eiBackendAddressSuffix = request.getServletPath();
99+
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
100+
LOG.info("Got HTTP Request with method GET.\nUrlSuffix: " + eiBackendAddressSuffix +
101+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
102+
103+
try {
104+
client.close();
105+
client = HttpClientBuilder.create().build();
106+
} catch (IOException e) {
107+
LOG.error("Failed to close HTTP Client");
62108
}
63109

64-
if (backEndInformation.getPath() != null && !backEndInformation.getPath().isEmpty()) {
65-
return httpMethod + "://" + backEndInformation.getHost() + ":" + backEndInformation.getPort() + "/"
66-
+ backEndInformation.getPath();
110+
HttpGet eiRequest = new HttpGet(newRequestUrl);
111+
112+
String header = request.getHeader("Authorization");
113+
if (header != null) {
114+
eiRequest.addHeader("Authorization", header);
67115
}
68-
return httpMethod + "://" + backEndInformation.getHost() + ":" + backEndInformation.getPort();
116+
117+
return getResponse(eiRequest);
69118
}
70119

71120
/**
@@ -82,38 +131,9 @@ public ResponseEntity<String> getRequests(Model model, HttpServletRequest reques
82131
LOG.info("Got HTTP Request with method GET.\nUrlSuffix: " + eiBackendAddressSuffix
83132
+ "\nForwarding Request to EI Backend with url: " + newRequestUrl);
84133

85-
HttpClient client = HttpClients.createDefault();
86134
HttpGet eiRequest = new HttpGet(newRequestUrl);
87135

88-
String jsonContent = "";
89-
HttpResponse eiResponse = null;
90-
try {
91-
eiResponse = client.execute(eiRequest);
92-
93-
InputStream inStream = eiResponse.getEntity().getContent();
94-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
95-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
96-
jsonContent += line;
97-
}
98-
bufReader.close();
99-
inStream.close();
100-
} catch (IOException e) {
101-
LOG.error("Forward Request Errors: " + e);
102-
}
103-
104-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
105-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
106-
107-
if (jsonContent.isEmpty()) {
108-
jsonContent = "[]";
109-
}
110-
111-
HttpHeaders headers = new HttpHeaders();
112-
headers.setContentType(MediaType.APPLICATION_JSON);
113-
114-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
115-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
116-
return responseEntity;
136+
return getResponse(eiRequest);
117137
}
118138

119139
/**
@@ -142,39 +162,11 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
142162
LOG.debug("Input Request JSON Content to be forwarded:\n" + inputReqJsonContent);
143163
HttpEntity inputReqJsonEntity = new ByteArrayEntity(inputReqJsonContent.getBytes());
144164

145-
HttpClient client = HttpClients.createDefault();
146165
HttpPost eiRequest = new HttpPost(newRequestUrl);
147166
eiRequest.setEntity(inputReqJsonEntity);
148167
eiRequest.setHeader("Content-type", "application/json");
149168

150-
String jsonContent = "";
151-
HttpResponse eiResponse = null;
152-
try {
153-
eiResponse = client.execute(eiRequest);
154-
155-
InputStream inStream = eiResponse.getEntity().getContent();
156-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
157-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
158-
jsonContent += line;
159-
}
160-
bufReader.close();
161-
inStream.close();
162-
} catch (IOException e) {
163-
LOG.error("Forward Request Errors: " + e);
164-
}
165-
166-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
167-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
168-
169-
if (jsonContent.isEmpty()) {
170-
jsonContent = "[]";
171-
}
172-
173-
HttpHeaders headers = new HttpHeaders();
174-
headers.setContentType(MediaType.APPLICATION_JSON);
175-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
176-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
177-
return responseEntity;
169+
return getResponse(eiRequest);
178170
}
179171

180172
/**
@@ -185,7 +177,6 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
185177
@CrossOrigin
186178
@RequestMapping(value = "/subscriptions", method = RequestMethod.PUT)
187179
public ResponseEntity<String> putRequests(Model model, HttpServletRequest request) {
188-
189180
String eiBackendAddressSuffix = request.getServletPath();
190181
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
191182
LOG.info("Got HTTP Request with method PUT.\nUrlSuffix: " + eiBackendAddressSuffix
@@ -205,41 +196,11 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest reques
205196
LOG.debug("Input Request JSON Content to be forwarded:\n" + inputReqJsonContent);
206197
HttpEntity inputReqJsonEntity = new ByteArrayEntity(inputReqJsonContent.getBytes());
207198

208-
HttpClient client = HttpClients.createDefault();
209199
HttpPut eiRequest = new HttpPut(newRequestUrl);
210200
eiRequest.setEntity(inputReqJsonEntity);
211201
eiRequest.setHeader("Content-type", "application/json");
212202

213-
String jsonContent = "";
214-
HttpResponse eiResponse = null;
215-
try {
216-
eiResponse = client.execute(eiRequest);
217-
218-
InputStream inStream = eiResponse.getEntity().getContent();
219-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
220-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
221-
jsonContent += line;
222-
}
223-
bufReader.close();
224-
inStream.close();
225-
} catch (IOException e) {
226-
LOG.error("Forward Request Errors: " + e);
227-
}
228-
229-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
230-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
231-
232-
if (jsonContent.isEmpty()) {
233-
jsonContent = "[]";
234-
}
235-
236-
HttpHeaders headers = new HttpHeaders();
237-
headers.setContentType(MediaType.APPLICATION_JSON);
238-
239-
ResponseEntity<String> responseEntity = new ResponseEntity<String>(jsonContent, headers,
240-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
241-
242-
return responseEntity;
203+
return getResponse(eiRequest);
243204
}
244205

245206
/**
@@ -255,37 +216,50 @@ public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest req
255216
LOG.info("Got HTTP Request with method DELETE.\nUrlSuffix: " + eiBackendAddressSuffix
256217
+ "\nForwarding Request to EI Backend with url: " + newRequestUrl);
257218

258-
HttpClient client = HttpClients.createDefault();
259219
HttpDelete eiRequest = new HttpDelete(newRequestUrl);
260220

261-
String jsonContent = "";
262-
HttpResponse eiResponse = null;
263-
try {
264-
eiResponse = client.execute(eiRequest);
221+
return getResponse(eiRequest);
222+
}
265223

224+
private String getEIBackendSubscriptionAddress() {
225+
String httpMethod = "http";
226+
if (useSecureHttp) {
227+
httpMethod = "https";
228+
}
229+
230+
if (backendContextPath != null && !backendContextPath.isEmpty()) {
231+
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort() + "/"
232+
+ backendContextPath;
233+
}
234+
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort();
235+
}
236+
237+
private ResponseEntity<String> getResponse(HttpRequestBase request) {
238+
String jsonContent = "";
239+
int statusCode = 0;
240+
try (CloseableHttpResponse eiResponse = client.execute(request)) {
266241
InputStream inStream = eiResponse.getEntity().getContent();
267242
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
268243
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
269244
jsonContent += line;
270245
}
246+
if (jsonContent.isEmpty()) {
247+
jsonContent = "[]";
248+
}
249+
statusCode = eiResponse.getStatusLine().getStatusCode();
250+
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
251+
+ "\nEI Recevied jsonContent:\n" + jsonContent
252+
+ "\nForwarding response back to EI Frontend WebUI.");
271253
bufReader.close();
272254
inStream.close();
273255
} catch (IOException e) {
274256
LOG.error("Forward Request Errors: " + e);
275257
}
276258

277-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
278-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
279-
280-
if (jsonContent.isEmpty()) {
281-
jsonContent = "[]";
282-
}
283-
284259
HttpHeaders headers = new HttpHeaders();
285260
headers.setContentType(MediaType.APPLICATION_JSON);
286261

287-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
288-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
289-
return responseEntity;
262+
return new ResponseEntity<>(jsonContent, headers, HttpStatus.valueOf(statusCode));
290263
}
264+
291265
}

0 commit comments

Comments
 (0)