Skip to content

Commit 3c1a2cd

Browse files
Added passing login credentials from EI Frontend to EI Backend (#20)
1 parent a59de9f commit 3c1a2cd

File tree

7 files changed

+295
-291
lines changed

7 files changed

+295
-291
lines changed

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

Lines changed: 59 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
import javax.servlet.http.HttpServletRequest;
2525

2626
import org.apache.http.HttpEntity;
27-
import org.apache.http.HttpResponse;
28-
import org.apache.http.client.HttpClient;
29-
import org.apache.http.client.methods.HttpDelete;
30-
import org.apache.http.client.methods.HttpGet;
31-
import org.apache.http.client.methods.HttpPost;
32-
import org.apache.http.client.methods.HttpPut;
27+
import org.apache.http.client.methods.*;
3328
import org.apache.http.entity.ByteArrayEntity;
34-
import org.apache.http.impl.client.HttpClients;
29+
import org.apache.http.impl.client.CloseableHttpClient;
30+
import org.apache.http.impl.client.HttpClientBuilder;
3531
import org.slf4j.Logger;
3632
import org.slf4j.LoggerFactory;
3733
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -47,18 +43,17 @@
4743

4844
@RestController
4945
@ConfigurationProperties(prefix = "ei")
50-
// @RequestMapping(value = "")
5146
public class EIRequestsController {
5247

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

50+
private CloseableHttpClient client = HttpClientBuilder.create().build();
51+
5552
private String backendServerHost;
5653
private int backendServerPort;
5754
private String backendContextPath;
5855
private boolean useSecureHttp;
5956

60-
private static final String APPLICATION_JSON = "application/json";
61-
6257
// Backend host and port (Getter & Setters), application.properties ->
6358
// greeting.xxx
6459
public String getBackendServerHost() {
@@ -93,17 +88,33 @@ public void setUseSecureHttp(boolean useSecureHttp) {
9388
this.useSecureHttp = useSecureHttp;
9489
}
9590

96-
public String getEIBackendSubscriptionAddress() {
97-
String httpMethod = "http";
98-
if (useSecureHttp) {
99-
httpMethod = "https";
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");
100108
}
101109

102-
if (backendContextPath != null && !backendContextPath.isEmpty()) {
103-
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort() + "/"
104-
+ backendContextPath;
110+
HttpGet eiRequest = new HttpGet(newRequestUrl);
111+
112+
String header = request.getHeader("Authorization");
113+
if (header != null) {
114+
eiRequest.addHeader("Authorization", header);
105115
}
106-
return httpMethod + "://" + this.getBackendServerHost() + ":" + this.getBackendServerPort();
116+
117+
return getResponse(eiRequest);
107118
}
108119

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

123-
HttpClient client = HttpClients.createDefault();
124134
HttpGet eiRequest = new HttpGet(newRequestUrl);
125135

126-
String jsonContent = "";
127-
HttpResponse eiResponse = null;
128-
try {
129-
eiResponse = client.execute(eiRequest);
130-
131-
InputStream inStream = eiResponse.getEntity().getContent();
132-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
133-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
134-
jsonContent += line;
135-
}
136-
bufReader.close();
137-
inStream.close();
138-
} catch (IOException e) {
139-
LOG.error("Forward Request Errors: " + e);
140-
}
141-
142-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
143-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
144-
145-
if (jsonContent.isEmpty()) {
146-
jsonContent = "[]";
147-
}
148-
149-
HttpHeaders headers = new HttpHeaders();
150-
headers.setContentType(MediaType.APPLICATION_JSON);
151-
152-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
153-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
154-
return responseEntity;
136+
return getResponse(eiRequest);
155137
}
156138

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

183-
HttpClient client = HttpClients.createDefault();
184165
HttpPost eiRequest = new HttpPost(newRequestUrl);
185166
eiRequest.setEntity(inputReqJsonEntity);
186167
eiRequest.setHeader("Content-type", "application/json");
187168

188-
String jsonContent = "";
189-
HttpResponse eiResponse = null;
190-
try {
191-
eiResponse = client.execute(eiRequest);
192-
193-
InputStream inStream = eiResponse.getEntity().getContent();
194-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
195-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
196-
jsonContent += line;
197-
}
198-
bufReader.close();
199-
inStream.close();
200-
} catch (IOException e) {
201-
LOG.error("Forward Request Errors: " + e);
202-
}
203-
204-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
205-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
206-
207-
if (jsonContent.isEmpty()) {
208-
jsonContent = "[]";
209-
}
210-
211-
HttpHeaders headers = new HttpHeaders();
212-
headers.setContentType(MediaType.APPLICATION_JSON);
213-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
214-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
215-
return responseEntity;
169+
return getResponse(eiRequest);
216170
}
217171

218172
/**
@@ -223,7 +177,6 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
223177
@CrossOrigin
224178
@RequestMapping(value = "/subscriptions", method = RequestMethod.PUT)
225179
public ResponseEntity<String> putRequests(Model model, HttpServletRequest request) {
226-
227180
String eiBackendAddressSuffix = request.getServletPath();
228181
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
229182
LOG.info("Got HTTP Request with method PUT.\nUrlSuffix: " + eiBackendAddressSuffix
@@ -243,41 +196,11 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest reques
243196
LOG.debug("Input Request JSON Content to be forwarded:\n" + inputReqJsonContent);
244197
HttpEntity inputReqJsonEntity = new ByteArrayEntity(inputReqJsonContent.getBytes());
245198

246-
HttpClient client = HttpClients.createDefault();
247199
HttpPut eiRequest = new HttpPut(newRequestUrl);
248200
eiRequest.setEntity(inputReqJsonEntity);
249201
eiRequest.setHeader("Content-type", "application/json");
250202

251-
String jsonContent = "";
252-
HttpResponse eiResponse = null;
253-
try {
254-
eiResponse = client.execute(eiRequest);
255-
256-
InputStream inStream = eiResponse.getEntity().getContent();
257-
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
258-
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
259-
jsonContent += line;
260-
}
261-
bufReader.close();
262-
inStream.close();
263-
} catch (IOException e) {
264-
LOG.error("Forward Request Errors: " + e);
265-
}
266-
267-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
268-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
269-
270-
if (jsonContent.isEmpty()) {
271-
jsonContent = "[]";
272-
}
273-
274-
HttpHeaders headers = new HttpHeaders();
275-
headers.setContentType(MediaType.APPLICATION_JSON);
276-
277-
ResponseEntity<String> responseEntity = new ResponseEntity<String>(jsonContent, headers,
278-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
279-
280-
return responseEntity;
203+
return getResponse(eiRequest);
281204
}
282205

283206
/**
@@ -293,38 +216,50 @@ public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest req
293216
LOG.info("Got HTTP Request with method DELETE.\nUrlSuffix: " + eiBackendAddressSuffix
294217
+ "\nForwarding Request to EI Backend with url: " + newRequestUrl);
295218

296-
HttpClient client = HttpClients.createDefault();
297219
HttpDelete eiRequest = new HttpDelete(newRequestUrl);
298220

299-
String jsonContent = "";
300-
HttpResponse eiResponse = null;
301-
try {
302-
eiResponse = client.execute(eiRequest);
221+
return getResponse(eiRequest);
222+
}
223+
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+
}
303236

237+
private ResponseEntity<String> getResponse(HttpRequestBase request) {
238+
String jsonContent = "";
239+
int statusCode = 0;
240+
try (CloseableHttpResponse eiResponse = client.execute(request)) {
304241
InputStream inStream = eiResponse.getEntity().getContent();
305242
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
306243
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
307244
jsonContent += line;
308245
}
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.");
309253
bufReader.close();
310254
inStream.close();
311255
} catch (IOException e) {
312256
LOG.error("Forward Request Errors: " + e);
313257
}
314258

315-
LOG.info("EI Http Reponse Status Code: " + eiResponse.getStatusLine().getStatusCode()
316-
+ "\nEI Recevied jsonContent:\n" + jsonContent + "\nForwarding response back to EI Frontend WebUI.");
317-
318-
if (jsonContent.isEmpty()) {
319-
jsonContent = "[]";
320-
}
321-
322259
HttpHeaders headers = new HttpHeaders();
323260
headers.setContentType(MediaType.APPLICATION_JSON);
324261

325-
ResponseEntity<String> responseEntity = new ResponseEntity<>(jsonContent, headers,
326-
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
327-
return responseEntity;
262+
return new ResponseEntity<>(jsonContent, headers, HttpStatus.valueOf(statusCode));
328263
}
329264

330265
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,6 @@ public String login(Model model) {
8585
return "login";
8686
}
8787

88-
@RequestMapping("/register.html")
89-
public String register(Model model) {
90-
91-
return "register";
92-
}
93-
94-
@RequestMapping("/forgot-password.html")
95-
public String forgotPassword(Model model) {
96-
97-
return "forgot-password";
98-
}
99-
10088
// Added documentation for JMESPath rules usage
10189
@RequestMapping("/jmesPathRulesSetUp.html")
10290
public String jmesPathRulesSetUp(Model model) {

0 commit comments

Comments
 (0)