23
23
24
24
import javax .servlet .http .HttpServletRequest ;
25
25
26
- import com .ericsson .ei .frontend .model .BackEndInformation ;
27
26
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 .*;
34
28
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 ;
36
31
import org .slf4j .Logger ;
37
32
import org .slf4j .LoggerFactory ;
38
- import org .springframework .beans .factory .annotation .Autowired ;
39
33
import org .springframework .boot .context .properties .ConfigurationProperties ;
40
34
import org .springframework .http .HttpHeaders ;
41
35
import org .springframework .http .HttpStatus ;
48
42
import org .springframework .web .bind .annotation .RestController ;
49
43
50
44
@ RestController
45
+ @ ConfigurationProperties (prefix = "ei" )
51
46
public class EIRequestsController {
52
47
53
48
private static final Logger LOG = LoggerFactory .getLogger (EIRequestsController .class );
54
49
55
- @ Autowired
56
- private BackEndInformation backEndInformation ;
50
+ private CloseableHttpClient client = HttpClientBuilder .create ().build ();
57
51
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.\n UrlSuffix: " + eiBackendAddressSuffix +
101
+ "\n Forwarding 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" );
62
108
}
63
109
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 );
67
115
}
68
- return httpMethod + "://" + backEndInformation .getHost () + ":" + backEndInformation .getPort ();
116
+
117
+ return getResponse (eiRequest );
69
118
}
70
119
71
120
/**
@@ -82,38 +131,9 @@ public ResponseEntity<String> getRequests(Model model, HttpServletRequest reques
82
131
LOG .info ("Got HTTP Request with method GET.\n UrlSuffix: " + eiBackendAddressSuffix
83
132
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
84
133
85
- HttpClient client = HttpClients .createDefault ();
86
134
HttpGet eiRequest = new HttpGet (newRequestUrl );
87
135
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
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding 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 );
117
137
}
118
138
119
139
/**
@@ -142,39 +162,11 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
142
162
LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
143
163
HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
144
164
145
- HttpClient client = HttpClients .createDefault ();
146
165
HttpPost eiRequest = new HttpPost (newRequestUrl );
147
166
eiRequest .setEntity (inputReqJsonEntity );
148
167
eiRequest .setHeader ("Content-type" , "application/json" );
149
168
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
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding 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 );
178
170
}
179
171
180
172
/**
@@ -185,7 +177,6 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
185
177
@ CrossOrigin
186
178
@ RequestMapping (value = "/subscriptions" , method = RequestMethod .PUT )
187
179
public ResponseEntity <String > putRequests (Model model , HttpServletRequest request ) {
188
-
189
180
String eiBackendAddressSuffix = request .getServletPath ();
190
181
String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
191
182
LOG .info ("Got HTTP Request with method PUT.\n UrlSuffix: " + eiBackendAddressSuffix
@@ -205,41 +196,11 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest reques
205
196
LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
206
197
HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
207
198
208
- HttpClient client = HttpClients .createDefault ();
209
199
HttpPut eiRequest = new HttpPut (newRequestUrl );
210
200
eiRequest .setEntity (inputReqJsonEntity );
211
201
eiRequest .setHeader ("Content-type" , "application/json" );
212
202
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
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding 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 );
243
204
}
244
205
245
206
/**
@@ -255,37 +216,50 @@ public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest req
255
216
LOG .info ("Got HTTP Request with method DELETE.\n UrlSuffix: " + eiBackendAddressSuffix
256
217
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
257
218
258
- HttpClient client = HttpClients .createDefault ();
259
219
HttpDelete eiRequest = new HttpDelete (newRequestUrl );
260
220
261
- String jsonContent = "" ;
262
- HttpResponse eiResponse = null ;
263
- try {
264
- eiResponse = client .execute (eiRequest );
221
+ return getResponse (eiRequest );
222
+ }
265
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
+ }
236
+
237
+ private ResponseEntity <String > getResponse (HttpRequestBase request ) {
238
+ String jsonContent = "" ;
239
+ int statusCode = 0 ;
240
+ try (CloseableHttpResponse eiResponse = client .execute (request )) {
266
241
InputStream inStream = eiResponse .getEntity ().getContent ();
267
242
BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
268
243
for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
269
244
jsonContent += line ;
270
245
}
246
+ if (jsonContent .isEmpty ()) {
247
+ jsonContent = "[]" ;
248
+ }
249
+ statusCode = eiResponse .getStatusLine ().getStatusCode ();
250
+ LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
251
+ + "\n EI Recevied jsonContent:\n " + jsonContent
252
+ + "\n Forwarding response back to EI Frontend WebUI." );
271
253
bufReader .close ();
272
254
inStream .close ();
273
255
} catch (IOException e ) {
274
256
LOG .error ("Forward Request Errors: " + e );
275
257
}
276
258
277
- LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
278
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding response back to EI Frontend WebUI." );
279
-
280
- if (jsonContent .isEmpty ()) {
281
- jsonContent = "[]" ;
282
- }
283
-
284
259
HttpHeaders headers = new HttpHeaders ();
285
260
headers .setContentType (MediaType .APPLICATION_JSON );
286
261
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 ));
290
263
}
264
+
291
265
}
0 commit comments