Skip to content

Commit dc37a29

Browse files
tobiasakeemichaf
authored andcommitted
Bridge EI Backend request via EI Frontend Spring mvc controller (#5)
* Added first version of bridging EI Backend request via Spring mvc cotroller. GET Method Requests is working. * Added bridging POST method request functionality. Creating subscription functionality working. * Added PUT and DELETE Method request functionality. Updating and Removing Subscription works. * Cleaned up code. Added logging framework and changed printouts to use logging instead.
1 parent 62caa80 commit dc37a29

File tree

3 files changed

+362
-2
lines changed

3 files changed

+362
-2
lines changed
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
/*
2+
Copyright 2017 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 java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.io.OutputStreamWriter;
24+
import java.net.HttpURLConnection;
25+
import java.net.URL;
26+
import java.net.URLConnection;
27+
import java.net.UnknownServiceException;
28+
import java.util.List;
29+
30+
import javax.servlet.http.HttpServletRequest;
31+
import javax.servlet.http.HttpServletResponse;
32+
import javax.servlet.http.HttpSession;
33+
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
import org.apache.http.HttpEntity;
38+
import org.apache.http.HttpResponse;
39+
import org.apache.http.client.HttpClient;
40+
import org.apache.http.client.methods.HttpDelete;
41+
import org.apache.http.client.methods.HttpGet;
42+
import org.apache.http.client.methods.HttpPost;
43+
import org.apache.http.client.methods.HttpPut;
44+
import org.apache.http.entity.ByteArrayEntity;
45+
import org.apache.http.protocol.HTTP;
46+
import org.springframework.beans.factory.annotation.Value;
47+
import org.springframework.boot.context.properties.ConfigurationProperties;
48+
import org.springframework.http.HttpHeaders;
49+
import org.springframework.http.HttpMethod;
50+
import org.springframework.http.HttpStatus;
51+
import org.springframework.http.MediaType;
52+
import org.springframework.http.ResponseEntity;
53+
import org.springframework.stereotype.Controller;
54+
import org.springframework.ui.Model;
55+
import org.springframework.web.bind.annotation.CrossOrigin;
56+
import org.springframework.web.bind.annotation.GetMapping;
57+
import org.springframework.web.bind.annotation.PathVariable;
58+
import org.springframework.web.bind.annotation.RequestBody;
59+
import org.springframework.web.bind.annotation.RequestMapping;
60+
import org.springframework.web.bind.annotation.RequestMethod;
61+
import org.springframework.web.bind.annotation.RestController;
62+
import org.springframework.web.servlet.ModelAndView;
63+
import org.apache.http.impl.client.HttpClients;
64+
65+
66+
@RestController
67+
@ConfigurationProperties(prefix="ei")
68+
//@RequestMapping(value = "")
69+
public class EIRequestsController {
70+
71+
private static final Logger LOG = LoggerFactory.getLogger(EIRequestsController.class);
72+
73+
private String backendServerHost;
74+
private int backendServerPort;
75+
76+
private static final String APPLICATION_JSON = "application/json";
77+
78+
// Backend host and port (Getter & Setters), application.properties -> greeting.xxx
79+
public String getBackendServerHost() {
80+
return backendServerHost;
81+
}
82+
83+
public void setBackendServerHost(String backendServerHost) {
84+
this.backendServerHost = backendServerHost;
85+
}
86+
87+
public int getBackendServerPort() {
88+
return backendServerPort;
89+
}
90+
91+
public void setBackendServerPort(int backendServerPort) {
92+
this.backendServerPort = backendServerPort;
93+
}
94+
95+
public String getEIBackendSubscriptionAddress() {
96+
return "http://" + getBackendServerHost() + ":" + getBackendServerPort();
97+
}
98+
99+
100+
/**
101+
* Bridge all EI Http Requests with GET method. Used for fetching Subscription by id or all subscriptions and EI Env Info.
102+
*
103+
*/
104+
@CrossOrigin
105+
@RequestMapping(value = {"/subscriptions", "/subscriptions/*", "/information" }, method = RequestMethod.GET)
106+
public ResponseEntity<String> getRequests(Model model, HttpServletRequest request) {
107+
String eiBackendAddressSuffix = request.getServletPath();
108+
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
109+
LOG.info("Got HTTP Request with method GET.\nUrlSuffix: " + eiBackendAddressSuffix +
110+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
111+
112+
113+
HttpClient client = HttpClients.createDefault();
114+
HttpGet eiRequest = new HttpGet(newRequestUrl);
115+
116+
String JsonContent = "";
117+
HttpResponse eiResponse = null;
118+
try {
119+
eiResponse = client.execute(eiRequest);
120+
121+
InputStream inStream = eiResponse.getEntity().getContent();
122+
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
123+
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
124+
JsonContent += line;
125+
}
126+
bufReader.close();
127+
inStream.close();
128+
}
129+
catch (IOException e) {
130+
LOG.error("Forward Request Errors: " + e);
131+
}
132+
133+
LOG.debug("Http Status Code: " + eiResponse.getStatusLine().getStatusCode());
134+
LOG.debug("Recevied JsonContent:\n" + JsonContent);
135+
136+
if (JsonContent.isEmpty()) {
137+
JsonContent="[]";
138+
}
139+
140+
HttpHeaders headers = new HttpHeaders();
141+
headers.setContentType(MediaType.APPLICATION_JSON);
142+
143+
ResponseEntity<String> responseEntity = new ResponseEntity<>(JsonContent,
144+
headers,
145+
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
146+
return responseEntity;
147+
}
148+
149+
150+
151+
/**
152+
* Bridge all EI Http Requests with POST method. E.g. Making Create Subscription Request.
153+
*
154+
*/
155+
@CrossOrigin
156+
@RequestMapping(value = "/subscriptions", method = RequestMethod.POST)
157+
public ResponseEntity<String> postRequests(Model model, HttpServletRequest request) {
158+
159+
String eiBackendAddressSuffix = request.getServletPath();
160+
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
161+
LOG.info("Got HTTP Request with method POST.\nUrlSuffix: " + eiBackendAddressSuffix +
162+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
163+
164+
String inputReqJsonContent = "";
165+
try {
166+
BufferedReader inputBufReader = new BufferedReader(request.getReader());
167+
for (String line = inputBufReader.readLine(); line != null; line = inputBufReader.readLine()) {
168+
inputReqJsonContent += line;
169+
}
170+
inputBufReader.close();
171+
}
172+
catch (IOException e) {
173+
LOG.error("Forward Request Errors: " + e);
174+
}
175+
176+
LOG.debug("Input Request JSON Content to be forwarded:\n" + inputReqJsonContent);
177+
HttpEntity inputReqJsonEntity = new ByteArrayEntity(inputReqJsonContent.getBytes());
178+
179+
180+
HttpClient client = HttpClients.createDefault();
181+
HttpPost eiRequest = new HttpPost(newRequestUrl);
182+
eiRequest.setEntity(inputReqJsonEntity);
183+
eiRequest.setHeader("Content-type", "application/json");
184+
185+
186+
String JsonContent = "";
187+
HttpResponse eiResponse = null;
188+
try {
189+
eiResponse = client.execute(eiRequest);
190+
191+
InputStream inStream = eiResponse.getEntity().getContent();
192+
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
193+
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
194+
JsonContent += line;
195+
}
196+
bufReader.close();
197+
inStream.close();
198+
}
199+
catch (IOException e) {
200+
LOG.error("Forward Request Errors: " + e);
201+
}
202+
203+
LOG.debug("Http Status Code: " + eiResponse.getStatusLine().getStatusCode());
204+
LOG.debug("Recevied JsonContent: \n" + JsonContent);
205+
206+
if (JsonContent.isEmpty()) {
207+
JsonContent="[]";
208+
}
209+
210+
211+
HttpHeaders headers = new HttpHeaders();
212+
headers.setContentType(MediaType.APPLICATION_JSON);
213+
ResponseEntity<String> responseEntity = new ResponseEntity<>(JsonContent,
214+
headers,
215+
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
216+
return responseEntity;
217+
}
218+
219+
/**
220+
* Bridge all EI Http Requests with PUT method. E.g. Making Update Subscription Request.
221+
*
222+
*/
223+
@CrossOrigin
224+
@RequestMapping(value = "/subscriptions", method = RequestMethod.PUT)
225+
public ResponseEntity<String> putRequests(Model model, HttpServletRequest request) {
226+
227+
String eiBackendAddressSuffix = request.getServletPath();
228+
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
229+
LOG.info("Got HTTP Request with method PUT.\nUrlSuffix: " + eiBackendAddressSuffix +
230+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
231+
232+
String inputReqJsonContent = "";
233+
try {
234+
BufferedReader inputBufReader = new BufferedReader(request.getReader());
235+
for (String line = inputBufReader.readLine(); line != null; line = inputBufReader.readLine()) {
236+
inputReqJsonContent += line;
237+
}
238+
inputBufReader.close();
239+
}
240+
catch (IOException e) {
241+
LOG.error("Forward Request Errors: " + e);
242+
}
243+
244+
LOG.debug("Input Request JSON Content to be forwarded:\n" + inputReqJsonContent);
245+
HttpEntity inputReqJsonEntity = new ByteArrayEntity(inputReqJsonContent.getBytes());
246+
247+
248+
HttpClient client = HttpClients.createDefault();
249+
HttpPut eiRequest = new HttpPut(newRequestUrl);
250+
eiRequest.setEntity(inputReqJsonEntity);
251+
eiRequest.setHeader("Content-type", "application/json");
252+
253+
254+
String JsonContent = "";
255+
HttpResponse eiResponse = null;
256+
try {
257+
eiResponse = client.execute(eiRequest);
258+
259+
InputStream inStream = eiResponse.getEntity().getContent();
260+
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
261+
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
262+
JsonContent += line;
263+
}
264+
bufReader.close();
265+
inStream.close();
266+
}
267+
catch (IOException e) {
268+
LOG.error("Forward Request Errors: " + e);
269+
}
270+
271+
LOG.debug("Http Status Code: " + eiResponse.getStatusLine().getStatusCode());
272+
LOG.debug("Recevied JsonContent:\n" + JsonContent);
273+
274+
if (JsonContent.isEmpty()) {
275+
JsonContent="[]";
276+
}
277+
278+
HttpHeaders headers = new HttpHeaders();
279+
headers.setContentType(MediaType.APPLICATION_JSON);
280+
281+
ResponseEntity<String> responseEntity = new ResponseEntity<String>(JsonContent,
282+
headers,
283+
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
284+
285+
return responseEntity;
286+
}
287+
288+
289+
/**
290+
* Bridge all EI Http Requests with DELETE method. Used for DELETE subscriptions.
291+
*
292+
*/
293+
@CrossOrigin
294+
@RequestMapping(value = "/subscriptions/*", method = RequestMethod.DELETE)
295+
public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest request) {
296+
String eiBackendAddressSuffix = request.getServletPath();
297+
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
298+
LOG.info("Got HTTP Request with method DELETE.\nUrlSuffix: " + eiBackendAddressSuffix +
299+
"\nForwarding Request to EI Backend with url: " + newRequestUrl);
300+
301+
HttpClient client = HttpClients.createDefault();
302+
HttpDelete eiRequest = new HttpDelete(newRequestUrl);
303+
304+
String JsonContent = "";
305+
HttpResponse eiResponse = null;
306+
try {
307+
eiResponse = client.execute(eiRequest);
308+
309+
InputStream inStream = eiResponse.getEntity().getContent();
310+
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
311+
for (String line = bufReader.readLine(); line != null; line = bufReader.readLine()) {
312+
JsonContent += line;
313+
}
314+
bufReader.close();
315+
inStream.close();
316+
}
317+
catch (IOException e) {
318+
LOG.error("Forward Request Errors: " + e);
319+
}
320+
321+
LOG.debug("Http Status Code: " + eiResponse.getStatusLine().getStatusCode());
322+
LOG.debug("Recevied JsonContent:\n" + JsonContent);
323+
324+
if (JsonContent.isEmpty()) {
325+
JsonContent="[]";
326+
}
327+
328+
HttpHeaders headers = new HttpHeaders();
329+
headers.setContentType(MediaType.APPLICATION_JSON);
330+
331+
ResponseEntity<String> responseEntity = new ResponseEntity<>(JsonContent,
332+
headers,
333+
HttpStatus.valueOf(eiResponse.getStatusLine().getStatusCode()));
334+
return responseEntity;
335+
}
336+
337+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
package com.ericsson.ei.frontend;
1818

19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
1923
import org.slf4j.Logger;
2024
import org.slf4j.LoggerFactory;
2125
import org.springframework.boot.SpringApplication;
@@ -27,6 +31,20 @@ public class EI_FrontEnd_Application {
2731
public static final Logger log = LoggerFactory.getLogger(EI_FrontEnd_Application.class);
2832

2933
public static void main(String[] args) {
34+
35+
List<String> logLevels = new ArrayList<>();
36+
Collections.addAll(logLevels, "ALL", "DEBUG", "ERROR", "FATAL", "INFO", "TRACE", "WARN");
37+
38+
if(args != null && args.length > 0 && logLevels.contains(args[0])) {
39+
System.setProperty("logging.level.root", args[0]);
40+
System.setProperty("logging.level.org.springframework.web", args[0]);
41+
System.setProperty("logging.level.com.ericsson.ei", args[0]);
42+
} else {
43+
System.setProperty("logging.level.root", "INFO");
44+
System.setProperty("logging.level.org.springframework.web", "INFO");
45+
System.setProperty("logging.level.com.ericsson.ei", "INFO");
46+
}
47+
3048
SpringApplication.run(EI_FrontEnd_Application.class, args);
3149
}
3250
}

src/main/resources/application.properties

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ server.port=8080
88

99
######## EI Backend
1010
ei.backendServiceHost=localhost
11-
ei.backendServicePort=8090
11+
ei.backendServicePort=8080
12+
ei.backendServerHost=localhost
13+
ei.backendServerPort=8090
1214

1315
###### Subscription Template file ########
1416
ei.subscriptionFilePath=subscriptions/subscriptionsTemplate.json
1517

16-
18+
#### LOGGING #########
19+
logging.level.root: INFO
20+
logging.level.org.springframework.web: DEBUG
21+
logging.level.com.ericsson.ei: DEBUG
1722

0 commit comments

Comments
 (0)